Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-5715: Python non-user defined fields mutable with slots #2816

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 36 additions & 6 deletions compiler/cpp/src/thrift/generate/t_py_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,42 @@ void t_py_generator::generate_py_struct_definition(ostream& out,

if (is_immutable(tstruct)) {
out << endl;
out << indent() << "def __setattr__(self, *args):" << endl
<< indent() << indent_str() << "raise TypeError(\"can't modify immutable instance\")" << endl
<< endl;
out << indent() << "def __delattr__(self, *args):" << endl
<< indent() << indent_str() << "raise TypeError(\"can't modify immutable instance\")" << endl
<< endl;
out << indent() << "def __setattr__(self, *args):" << endl;
indent_up();

// Not user-provided fields should be editable so that the Python Standard Library can edit
// internal fields of std library base classes. For example, in Python 3.11 ContextManager
// edits the `__traceback__` field on Exceptions. Allowing this to work with `__slots__` is
// trivial because we know which fields are user-provided, without slots we need to build a
// way to know which fields are user-provided.
if (gen_slots_ && !gen_dynamic_) {
out << indent() << "if args[0] not in self.__slots__:" << endl;
indent_up();
out << indent() << "super().__setattr__(*args)" << endl
<< indent() << "return" << endl;
indent_down();
}
out << indent() << "raise TypeError(\"can't modify immutable instance\")" << endl;
indent_down();
out << endl;
out << indent() << "def __delattr__(self, *args):" << endl;
indent_up();

// Not user-provided fields should be editable so that the Python Standard Library can edit
// internal fields of std library base classes. For example, in Python 3.11 ContextManager
// edits the `__traceback__` field on Exceptions. Allowing this to work with `__slots__` is
// trivial because we know which fields are user-provided, without slots we need to build a
// way to know which fields are user-provided.
if (gen_slots_ && !gen_dynamic_) {
out << indent() << "if args[0] not in self.__slots__:" << endl;
indent_up();
out << indent() << "super().__delattr__(*args)" << endl
<< indent() << "return" << endl;
indent_down();
}
out << indent() << "raise TypeError(\"can't modify immutable instance\")" << endl;
indent_down();
out << endl;

// Hash all of the members in order, and also hash in the class
// to avoid collisions for stuff like single-field structures.
Expand Down
29 changes: 29 additions & 0 deletions test/py/TestClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,35 @@ def testMultiException(self):
y = self.client.testMultiException('success', 'foobar')
self.assertEqual(y.string_thing, 'foobar')

def testException__traceback__(self):
print('testException__traceback__')
self.client.testException('Safe')
expect_slots = uses_slots = False
expect_dynamic = uses_dynamic = False
try:
self.client.testException('Xception')
self.fail("should have gotten exception")
except Xception as x:
uses_slots = hasattr(x, '__slots__')
uses_dynamic = (not isinstance(x, TException))
# We set expected values here so that we get clean tracebackes when
# the assertions fail.
try:
x.__traceback__ = x.__traceback__
# If `__traceback__` was set without errors than we expect that
# the slots option was used and that dynamic classes were not.
expect_slots = True
expect_dynamic = False
except Exception as e:
self.assertTrue(isinstance(e, TypeError))
# There are no other meaningful tests we can preform because we
# are unable to determine the desired state of either `__slots__`
# or `dynamic`.
return

self.assertEqual(expect_slots, uses_slots)
self.assertEqual(expect_dynamic, uses_dynamic)

def testOneway(self):
print('testOneway')
start = time.time()
Expand Down