Skip to content

Commit 09a486b

Browse files
committed
gh-124160: Pass main_tstate to update_global_state_for_extension()
Otherwise it'll always return NULL if tstate != main_tstate due to _Py_IsMainInterpreter() check inside it. Also, add a regression test that makes sure that `readline` is importable in case that triggered the crash before.
1 parent 8b6c7c7 commit 09a486b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Programs/_testembed.c

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ static void _testembed_Py_Initialize(void)
9595
}
9696

9797

98+
static int test_import_in_subinterpreters(void)
99+
{
100+
_testembed_Py_InitializeFromConfig();
101+
PyThreadState_Swap(Py_NewInterpreter());
102+
return PyRun_SimpleString("import readline"); // gh-124160
103+
}
104+
105+
98106
/*****************************************************
99107
* Test repeated initialisation and subinterpreters
100108
*****************************************************/
@@ -2398,6 +2406,7 @@ static struct TestCase TestCases[] = {
23982406
{"test_repeated_init_exec", test_repeated_init_exec},
23992407
{"test_repeated_simple_init", test_repeated_simple_init},
24002408
{"test_forced_io_encoding", test_forced_io_encoding},
2409+
{"test_import_in_subinterpreters", test_import_in_subinterpreters},
24012410
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
24022411
{"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
24032412
{"test_pre_initialization_api", test_pre_initialization_api},

Python/import.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
20452045
singlephase.m_init = p0;
20462046
}
20472047
cached = update_global_state_for_extension(
2048-
tstate, info->path, info->name, def, &singlephase);
2048+
main_tstate, info->path, info->name, def, &singlephase);
20492049
if (cached == NULL) {
20502050
assert(PyErr_Occurred());
20512051
goto main_finally;

0 commit comments

Comments
 (0)