Skip to content

Commit 28d1c17

Browse files
miss-islingtoncorona10ZeroIntensity
authored andcommitted
[3.14] pythongh-138192: Fix Context initialization so that all subinterpreters are assigned the MISSING value. (pythongh-138503) (python#138505)
Co-authored-by: Donghee Na <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
1 parent fa47a85 commit 28d1c17

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

Lib/test/test_interpreters/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,16 @@ def test_whence(self):
22042204
whence = eval(text)
22052205
self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI)
22062206

2207+
def test_contextvars_missing(self):
2208+
script = f"""
2209+
import contextvars
2210+
print(getattr(contextvars.Token, "MISSING", "'doesn't exist'"))
2211+
"""
2212+
2213+
orig = _interpreters.create()
2214+
text = self.run_and_capture(orig, script)
2215+
self.assertEqual(text.strip(), "<Token.MISSING>")
2216+
22072217
def test_is_running(self):
22082218
def check(interpid, expected):
22092219
with self.assertRaisesRegex(InterpreterError, 'unrecognized'):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the
2+
:attr:`~contextvars.Token.MISSING` value.

Python/context.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,8 @@ get_token_missing(void)
13571357
PyStatus
13581358
_PyContext_Init(PyInterpreterState *interp)
13591359
{
1360-
if (!_Py_IsMainInterpreter(interp)) {
1361-
return _PyStatus_OK();
1362-
}
1363-
13641360
PyObject *missing = get_token_missing();
1361+
assert(PyUnstable_IsImmortal(missing));
13651362
if (PyDict_SetItemString(
13661363
_PyType_GetDict(&PyContextToken_Type), "MISSING", missing))
13671364
{

0 commit comments

Comments
 (0)