From: Donghee Na Date: Thu, 4 Sep 2025 17:19:30 +0000 (+0900) Subject: gh-138192: Fix Context initialization so that all subinterpreters are assigned the... X-Git-Tag: v3.15.0a1~489 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f070f54c5f4a42c7c61d1d5d3b8f3b7203b4a0fb;p=thirdparty%2FPython%2Fcpython.git gh-138192: Fix Context initialization so that all subinterpreters are assigned the MISSING value. (gh-138503) --------- Co-authored-by: Peter Bierma --- diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a34b20beaca7..289e607ad3fa 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -2204,6 +2204,16 @@ class LowLevelTests(TestBase): whence = eval(text) self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI) + def test_contextvars_missing(self): + script = f""" + import contextvars + print(getattr(contextvars.Token, "MISSING", "'doesn't exist'")) + """ + + orig = _interpreters.create() + text = self.run_and_capture(orig, script) + self.assertEqual(text.strip(), "") + def test_is_running(self): def check(interpid, expected): with self.assertRaisesRegex(InterpreterError, 'unrecognized'): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst new file mode 100644 index 000000000000..05fa8edacd68 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst @@ -0,0 +1,2 @@ +Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the +:attr:`~contextvars.Token.MISSING` value. diff --git a/Python/context.c b/Python/context.c index b764f5813fac..2f978b1c0abc 100644 --- a/Python/context.c +++ b/Python/context.c @@ -1360,11 +1360,8 @@ get_token_missing(void) PyStatus _PyContext_Init(PyInterpreterState *interp) { - if (!_Py_IsMainInterpreter(interp)) { - return _PyStatus_OK(); - } - PyObject *missing = get_token_missing(); + assert(PyUnstable_IsImmortal(missing)); if (PyDict_SetItemString( _PyType_GetDict(&PyContextToken_Type), "MISSING", missing)) {