THRIFT-4002: Make generated exception classes immutable by default
Currently, the generated exception classes are not hashable under
Python 3 because of the generated `__eq__` method. Exception objects
are generally expected to be hashable by the Python standard library.
Post-construction mutation of an exception object seems like a very
unlikely case, so enable hashing for all exceptions by making them
immutable by default. This also adds a way to opt-out of immutability
by setting the `python.immutable` annotation to `"false"`.
diff --git a/lib/py/src/protocol/TProtocol.py b/lib/py/src/protocol/TProtocol.py
index 3456e8f..339a283 100644
--- a/lib/py/src/protocol/TProtocol.py
+++ b/lib/py/src/protocol/TProtocol.py
@@ -303,8 +303,14 @@
def readContainerStruct(self, spec):
(obj_class, obj_spec) = spec
- obj = obj_class()
- obj.read(self)
+
+ # If obj_class.read is a classmethod (e.g. in frozen structs),
+ # call it as such.
+ if getattr(obj_class.read, '__self__', None) is obj_class:
+ obj = obj_class.read(self)
+ else:
+ obj = obj_class()
+ obj.read(self)
return obj
def readContainerMap(self, spec):