Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`contextvars` initialization so that all subinterpreters are assigned the
:attr:`~contextvars.Token.MISSING` value.
5 changes: 1 addition & 4 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_token_missing is using Singleton value so it is safe to use.

assert(PyUnstable_IsImmortal(missing));
if (PyDict_SetItemString(
_PyType_GetDict(&PyContextToken_Type), "MISSING", missing))
{
Expand Down
Loading