https://github.com/python/cpython/commit/cddb7e6a7a6bb7a79063f1971772bc1583043376
commit: cddb7e6a7a6bb7a79063f1971772bc1583043376
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2025-09-05T15:30:04+03:00
summary:

[3.14] gh-138192: Fix Context initialization so that all subinterpreters are 
assigned the MISSING value. (gh-138503) (#138505)

Co-authored-by: Donghee Na <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-09-05-01-19-04.gh-issue-138192.erluq5.rst
M Lib/test/test_interpreters/test_api.py
M Python/context.c

diff --git a/Lib/test/test_interpreters/test_api.py 
b/Lib/test/test_interpreters/test_api.py
index a34b20beaca7a3..289e607ad3fad3 100644
--- a/Lib/test/test_interpreters/test_api.py
+++ b/Lib/test/test_interpreters/test_api.py
@@ -2204,6 +2204,16 @@ def test_whence(self):
             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
index 00000000000000..05fa8edacd680a
--- /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 9927cab915cae7..d1f8b7c2482181 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -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))
     {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to