]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-138192: Fix Context initialization so that all subinterpreters are assigned...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 5 Sep 2025 12:30:04 +0000 (14:30 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Sep 2025 12:30:04 +0000 (15:30 +0300)
Co-authored-by: Donghee Na <donghee.na@python.org>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/test/test_interpreters/test_api.py
Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst [new file with mode: 0644]
Python/context.c

index a34b20beaca7a3f44d4bac3a4d5522c05db289c6..289e607ad3fad319c67a4d3a87947fc5c3c064e1 100644 (file)
@@ -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(), "<Token.MISSING>")
+
     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 (file)
index 0000000..05fa8ed
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the
+:attr:`~contextvars.Token.MISSING` value.
index 9927cab915cae7d482d9296ea7a1f9f4d24f9991..d1f8b7c2482181eb8828aa3b5419a4acc3ffd7c7 100644 (file)
@@ -1357,11 +1357,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))
     {