Skip to content

Commit

Permalink
Fix test and keep test_frozenmain.h stable
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed May 6, 2024
1 parent e3893b5 commit f5cab78
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
7 changes: 2 additions & 5 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,11 @@ def f(a='str_value'):
self.assertIsInterned(f())

@cpython_only
@unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
def test_interned_string_with_null(self):
co = compile(r'res = "str\0value!"', '?', 'exec')
v = self.find_const(co.co_consts, 'str\0value!')
if Py_GIL_DISABLED:
# The free-threaded build interns all string constants
self.assertIsInterned(v)
else:
self.assertIsNotInterned(v)
self.assertIsNotInterned(v)

@cpython_only
@unittest.skipUnless(Py_GIL_DISABLED, "does not intern all constants")
Expand Down
2 changes: 1 addition & 1 deletion Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ should_intern_string(PyObject *o)
// unless we've disabled immortalizing objects that use deferred reference
// counting.
PyInterpreterState *interp = _PyInterpreterState_GET();
if (!interp->gc.immortalize.enable_on_thread_created) {
if (interp->gc.immortalize.enable_on_thread_created) {
return 1;
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions Programs/freeze_test_frozenmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import tokenize
import os.path
import sys
from test.support import suppress_immortalization

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

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

data = marshal.dumps(code)
writecode(fp, name, data)
Expand Down
13 changes: 13 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,21 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
if (str == NULL)
goto error;

#ifdef Py_GIL_DISABLED
// gh-118527: Disable immortalization of code constants for explicit
// compile() calls to get consistent frozen outputs between the default
// and free-threaded builds.
PyInterpreterState *interp = _PyInterpreterState_GET();
int old_value = interp->gc.immortalize.enable_on_thread_created;
interp->gc.immortalize.enable_on_thread_created = 0;
#endif

result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);

#ifdef Py_GIL_DISABLED
interp->gc.immortalize.enable_on_thread_created = old_value;
#endif

Py_XDECREF(source_copy);
goto finally;

Expand Down

0 comments on commit f5cab78

Please sign in to comment.