Skip to content

Commit f5cab78

Browse files
committed
Fix test and keep test_frozenmain.h stable
1 parent e3893b5 commit f5cab78

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

Lib/test/test_code.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,11 @@ def f(a='str_value'):
571571
self.assertIsInterned(f())
572572

573573
@cpython_only
574+
@unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
574575
def test_interned_string_with_null(self):
575576
co = compile(r'res = "str\0value!"', '?', 'exec')
576577
v = self.find_const(co.co_consts, 'str\0value!')
577-
if Py_GIL_DISABLED:
578-
# The free-threaded build interns all string constants
579-
self.assertIsInterned(v)
580-
else:
581-
self.assertIsNotInterned(v)
578+
self.assertIsNotInterned(v)
582579

583580
@cpython_only
584581
@unittest.skipUnless(Py_GIL_DISABLED, "does not intern all constants")

Objects/codeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ should_intern_string(PyObject *o)
110110
// unless we've disabled immortalizing objects that use deferred reference
111111
// counting.
112112
PyInterpreterState *interp = _PyInterpreterState_GET();
113-
if (!interp->gc.immortalize.enable_on_thread_created) {
113+
if (interp->gc.immortalize.enable_on_thread_created) {
114114
return 1;
115115
}
116116
#endif

Programs/freeze_test_frozenmain.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import tokenize
33
import os.path
44
import sys
5-
from test.support import suppress_immortalization
65

76
PROGRAM_DIR = os.path.dirname(__file__)
87
SRC_DIR = os.path.dirname(PROGRAM_DIR)
@@ -25,11 +24,7 @@ def dump(fp, filename, name):
2524

2625
with tokenize.open(filename) as source_fp:
2726
source = source_fp.read()
28-
# The output of marshal can depend on the reference count so suppress
29-
# immortalization in the free-threaded build to keep the output
30-
# consistent with the default build.
31-
with suppress_immortalization():
32-
code = compile(source, code_filename, 'exec')
27+
code = compile(source, code_filename, 'exec')
3328

3429
data = marshal.dumps(code)
3530
writecode(fp, name, data)

Python/bltinmodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,21 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
866866
if (str == NULL)
867867
goto error;
868868

869+
#ifdef Py_GIL_DISABLED
870+
// gh-118527: Disable immortalization of code constants for explicit
871+
// compile() calls to get consistent frozen outputs between the default
872+
// and free-threaded builds.
873+
PyInterpreterState *interp = _PyInterpreterState_GET();
874+
int old_value = interp->gc.immortalize.enable_on_thread_created;
875+
interp->gc.immortalize.enable_on_thread_created = 0;
876+
#endif
877+
869878
result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
870879

880+
#ifdef Py_GIL_DISABLED
881+
interp->gc.immortalize.enable_on_thread_created = old_value;
882+
#endif
883+
871884
Py_XDECREF(source_copy);
872885
goto finally;
873886

0 commit comments

Comments
 (0)