Skip to content

Commit

Permalink
Always append MutableStruct instance to internal state
Browse files Browse the repository at this point in the history
Summary: Move the append to the `__cinit__` so that an instance is appended for all valid `MutableStruct` objects.

Reviewed By: ahilger

Differential Revision: D65364413

fbshipit-source-id: 30f273910f2c1189057d427ee8677a8a471ec3b1
  • Loading branch information
yoney authored and facebook-github-bot committed Nov 2, 2024
1 parent 6dee7c9 commit b54056c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions thrift/lib/python/mutable_types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ cdef class MutableStruct(MutableStructOrUnion):
self._initStructListWithValues(kwargs)
cdef MutableStructInfo mutable_struct_info = type(self)._fbthrift_mutable_struct_info
self._fbthrift_field_cache = [None] * len(mutable_struct_info.fields)
# Append `MutableStruct` instance, see `_fbthrift_has_struct_instance()`
self._fbthrift_data.append(self)

def __init__(self, **kwargs):
pass
Expand All @@ -302,10 +304,10 @@ cdef class MutableStruct(MutableStructOrUnion):
return self_copy

def __deepcopy__(self, memo):
has_instance = self._fbthrift_has_struct_instance(self._fbthrift_data)
# we do not need to deep copy the instance and the field-cache (last element)
fbthrift_data = self._fbthrift_data[:-1] if has_instance else self._fbthrift_data
return self._fbthrift_create(copy.deepcopy(fbthrift_data))
# When `deepcopy` is called on an instance (`self`), the instance must
# already be populated in `_fbthrift_data`.
assert self._fbthrift_has_struct_instance(self._fbthrift_data)
return self._fbthrift_create(copy.deepcopy(self._fbthrift_data[:-1]))

cdef _initStructListWithValues(self, kwargs) except *:
cdef MutableStructInfo mutable_struct_info = self._fbthrift_mutable_struct_info
Expand Down Expand Up @@ -343,9 +345,6 @@ cdef class MutableStruct(MutableStructOrUnion):
mutable_struct_info.cpp_obj.get().getStructInfo()
)

# Append `MutableStruct` instance, see `_fbthrift_has_struct_instance()`
self._fbthrift_data.append(self)

cdef _fbthrift_set_field_value(self, int16_t index, object value):
cdef MutableStructInfo mutable_struct_info = self._fbthrift_mutable_struct_info
cdef FieldInfo field_info = mutable_struct_info.fields[index]
Expand Down

0 comments on commit b54056c

Please sign in to comment.