Skip to content

Commit c36d327

Browse files
[3.15] gh-159739: Do not intern complex subclasses in code constants (GH-159980) (#160249)
gh-159739: Do not intern complex subclasses in code constants (GH-159980) (cherry picked from commit e67334a) Co-authored-by: Jelle Zijlstra <jelle@openai.com>
1 parent 90d0d66 commit c36d327

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎Lib/test/test_code.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,10 +1250,15 @@ def __hash__(self):
12501250
class MyInt(int):
12511251
pass
12521252

1253+
class MyComplex(complex):
1254+
pass
1255+
12531256
code = compile("a = 1", "<string>", "exec")
1254-
code = code.replace(co_consts=(1, Unhashable(), MyInt(1), MyInt(1)))
1257+
code = code.replace(co_consts=(1, Unhashable(), MyInt(1), MyInt(1),
1258+
MyComplex(1j)))
12551259
self.assertIsInstance(code.co_consts[1], Unhashable)
12561260
self.assertEqual(code.co_consts[2], code.co_consts[3])
1261+
self.assertFalse(sys._is_immortal(code.co_consts[4]))
12571262

12581263

12591264
class CodeWeakRefTest(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent free-threaded builds from interning and immortalizing complex
2+
subclasses in code object constants.

‎Objects/codeobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ should_immortalize_constant(PyObject *v)
174174
_Py_IsImmortal(slice->step));
175175
}
176176
return (PyLong_CheckExact(v) || PyFloat_CheckExact(v) ||
177-
PyComplex_Check(v) || PyBytes_CheckExact(v));
177+
PyComplex_CheckExact(v) || PyBytes_CheckExact(v));
178178
}
179179
#endif
180180

0 commit comments

Comments
 (0)