Skip to content

Commit 3f056a9

Browse files
committed
pythongh-129724: Limit constant cache changes to free-threaded builds
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ad09eb5a-b18e-4bb2-9225-d2d3d589452e
1 parent eae9b87 commit 3f056a9

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎Lib/test/test_compile.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,16 @@ def check_same_constant(const):
816816
f2.__code__.co_consts[1])
817817

818818
# {0} is converted to a constant frozenset({0}) by the peephole
819-
# optimizer. Frozenset-bearing co_consts are intentionally not shared
820-
# across code units because later string interning can replace them.
821-
f1, f2 = lambda x: x in {0}, lambda x: x in {0}
822-
self.assertIsNot(f1.__code__.co_consts, f2.__code__.co_consts)
819+
# optimizer. In free-threaded builds, frozenset-bearing co_consts are
820+
# not shared across code units because later string interning can
821+
# replace them.
822+
ns = {}
823+
exec("f1, f2 = lambda x: x in {0}, lambda x: x in {0}", ns)
824+
f1, f2 = ns["f1"], ns["f2"]
825+
if support.Py_GIL_DISABLED:
826+
self.assertIsNot(f1.__code__.co_consts, f2.__code__.co_consts)
827+
else:
828+
self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
823829
self.check_constant(f1, frozenset({0}))
824830
self.check_constant(f2, frozenset({0}))
825831
self.assertTrue(f1(0))

‎Lib/test/test_marshal.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ def test_code_reference_tracking_does_not_depend_on_refcount(self):
405405
)
406406
self.assertIs(extra_reference, code.co_consts[0])
407407

408+
@unittest.skipUnless(
409+
support.Py_GIL_DISABLED,
410+
"free-threaded builds require compile-order-independent constants",
411+
)
408412
def test_code_serialization_does_not_depend_on_compile_order(self):
409413
script = textwrap.dedent("""
410414
import marshal
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Make reference tracking for marshalled code objects in free-threaded builds
22
independent of live reference counts. Avoid unstable sharing of immutable
3-
constants in all builds. This makes bytecode compilation deterministic across
4-
compilation order and concurrent free-threaded compilation.
3+
constants in free-threaded compiler output. This makes free-threaded bytecode
4+
compilation deterministic across compilation order and concurrent compilation.

‎Python/compile.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,13 @@ _PyCompile_SetQualname(compiler *c)
342342
static int
343343
const_cache_contains_rebuilt_container(PyObject *o)
344344
{
345+
#ifdef Py_GIL_DISABLED
345346
if (PyFrozenSet_CheckExact(o)) {
346347
return 1;
347348
}
348-
#ifdef Py_GIL_DISABLED
349349
if (PySlice_Check(o)) {
350350
return 1;
351351
}
352-
#endif
353352
if (PyTuple_CheckExact(o)) {
354353
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(o); i++) {
355354
if (const_cache_contains_rebuilt_container(
@@ -359,6 +358,9 @@ const_cache_contains_rebuilt_container(PyObject *o)
359358
}
360359
}
361360
}
361+
#else
362+
(void)o;
363+
#endif
362364
return 0;
363365
}
364366

0 commit comments

Comments
 (0)