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

---------

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
Donghee Na 2025-09-05 02:19:30 +09:00 committed by GitHub
parent 9d6b94afe7
commit f070f54c5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 4 deletions

View 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'):

View file

@ -0,0 +1,2 @@
Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the
:attr:`~contextvars.Token.MISSING` value.

View file

@ -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))
{