https://github.com/python/cpython/commit/c36d3277dc32e5b8528e8665e76a30f88ce4b8d4 commit: c36d3277dc32e5b8528e8665e76a30f88ce4b8d4 branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: JelleZijlstra <[email protected]> date: 2026-10-22T15:18:54-07:00 summary:
[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 e67334ab071bbb2e57501312a661ee937610abdd) Co-authored-by: Jelle Zijlstra <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst M Lib/test/test_code.py M Objects/codeobject.c diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 657e70a92f09b9..c9ce1af10fd7aa 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -1250,10 +1250,15 @@ def __hash__(self): class MyInt(int): pass + class MyComplex(complex): + pass + code = compile("a = 1", "<string>", "exec") - code = code.replace(co_consts=(1, Unhashable(), MyInt(1), MyInt(1))) + code = code.replace(co_consts=(1, Unhashable(), MyInt(1), MyInt(1), + MyComplex(1j))) self.assertIsInstance(code.co_consts[1], Unhashable) self.assertEqual(code.co_consts[2], code.co_consts[3]) + self.assertFalse(sys._is_immortal(code.co_consts[4])) class CodeWeakRefTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst new file mode 100644 index 00000000000000..a027883426be19 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst @@ -0,0 +1,2 @@ +Prevent free-threaded builds from interning and immortalizing complex +subclasses in code object constants. diff --git a/Objects/codeobject.c b/Objects/codeobject.c index d4c96b36e3c8b5..878a8a35a04db0 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -174,7 +174,7 @@ should_immortalize_constant(PyObject *v) _Py_IsImmortal(slice->step)); } return (PyLong_CheckExact(v) || PyFloat_CheckExact(v) || - PyComplex_Check(v) || PyBytes_CheckExact(v)); + PyComplex_CheckExact(v) || PyBytes_CheckExact(v)); } #endif _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
