Jens-G commented on code in PR #3349:
URL: https://github.com/apache/thrift/pull/3349#discussion_r3030500288


##########
lib/py/test/test_immutable_exception.py:
##########
@@ -0,0 +1,243 @@
+#!/usr/bin/env python
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""
+Test cases for THRIFT-4002: Immutable exception deserialization.
+
+This test verifies that immutable structs (including exceptions, which are 
immutable
+by default since Thrift 0.14.0) can be properly deserialized without triggering
+the __setattr__ TypeError.
+
+The bug manifests when:
+1. A struct class is marked immutable (has __setattr__ that raises TypeError)
+2. Thrift's deserialization tries to set attributes via setattr instead of
+   using the kwargs constructor
+
+This test ensures that all deserialization paths (C extension, pure Python,
+all protocols) correctly handle immutable structs.
+"""
+
+import unittest
+from collections.abc import Hashable
+
+import glob
+import os
+import sys
+
+SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
+ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR)))
+
+for libpath in glob.glob(os.path.join(ROOT_DIR, 'lib', 'py', 'build', 
'lib.*')):
+    for pattern in ('-%d.%d', '-%d%d'):
+        postfix = pattern % (sys.version_info[0], sys.version_info[1])
+        if libpath.endswith(postfix):
+            sys.path.insert(0, libpath)
+            break
+else:
+    src_path = os.path.join(ROOT_DIR, 'lib', 'py', 'src')
+    if os.path.exists(src_path):
+        sys.path.insert(0, src_path)
+from thrift.Thrift import TException
+from thrift.transport import TTransport
+from thrift.protocol import TBinaryProtocol, TCompactProtocol
+
+
+class ImmutableException(TException):

Review Comment:
   +1 Agreed.
   
   I ran into the issue myself at some unrelated occasion, which gave me the 
idea to include a regression test. However, the current implementation has 
certainly room for improvement. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to