Skip to content

Commit

Permalink
create_autospec for mutable types
Browse files Browse the repository at this point in the history
Summary: Add checks to prevent crash

Reviewed By: aristidisp

Differential Revision: D62579992

fbshipit-source-id: 99f37e27ae79b39df3ef64b99659bd89d56ce3d7
  • Loading branch information
yoney authored and facebook-github-bot committed Sep 12, 2024
1 parent 08c9b14 commit b48f21c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 18 additions & 0 deletions third-party/thrift/src/thrift/lib/python/mutable_types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,18 @@ class _MutableStructField:
self._field_index = field_id

def __get__(self, obj, objtype):
if obj is None:
return None

if isinstance(obj, MutableStruct):
return (<MutableStruct>obj)._fbthrift_get_field_value(self._field_index)
else:
return (<MutableGeneratedError>obj)._fbthrift_get_field_value(self._field_index)

def __set__(self, obj, value):
if obj is None:
return

if isinstance(obj, MutableStruct):
(<MutableStruct>obj)._fbthrift_set_field_value(self._field_index, value)
else:
Expand All @@ -136,12 +142,18 @@ class _MutableStructCachedField:
self._field_index = field_id

def __get__(self, obj, objtype):
if obj is None:
return None

if isinstance(obj, MutableStruct):
return (<MutableStruct>obj)._fbthrift_get_cached_field_value(self._field_index)
else:
return (<MutableGeneratedError>obj)._fbthrift_get_cached_field_value(self._field_index)

def __set__(self, obj, value):
if obj is None:
return

if isinstance(obj, MutableStruct):
(<MutableStruct>obj)._fbthrift_set_field_value(self._field_index, value)
else:
Expand Down Expand Up @@ -946,6 +958,9 @@ cdef class _MutableUnionFieldDescriptor:
Raises error if this field is not currently set on the given `union_instance`.
"""
if union_instance is None:
return None

field_info = self._field_info
cdef int field_id = self._field_info.id

Expand Down Expand Up @@ -978,6 +993,9 @@ cdef class _MutableUnionFieldDescriptor:
`TypeInfoBase`). If this field is adapted, this value should be in the
adapted type (as opposed to the underlying Thrift type).
"""
if union_instance is None:
return

field_info = self._field_info
cdef int field_id = self._field_info.id

Expand Down
12 changes: 6 additions & 6 deletions third-party/thrift/src/thrift/lib/python/test/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ def test_repr(self) -> None:
repr(self.easy(val=42)),
)

def test_autospec_iterable(self) -> None:
for _ in mock.create_autospec(self.easy):
pass
for _ in mock.create_autospec(self.easy()):
pass


class StructTestsImmutable(unittest.TestCase):
"""
Expand Down Expand Up @@ -357,12 +363,6 @@ def test_copy(self) -> None:
dif_int = copy.copy(x.an_int)
self.assertEqual(x.an_int, dif_int)

def test_autospec_iterable(self) -> None:
for _ in mock.create_autospec(easy):
pass
for _ in mock.create_autospec(easy()):
pass

def test_update_nested_fields(self) -> None:
n = Nested1(a=Nested2(b=Nested3(c=easy(val=42, name="foo"))))
n = update_nested_field(n, {"a.b.c": easy(val=128)})
Expand Down

0 comments on commit b48f21c

Please sign in to comment.