Skip to content

Commit

Permalink
pythongh-124160: Pass main_tstate to update_global_state_for_extension()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
luk1337 committed Sep 18, 2024
1 parent 8b6c7c7 commit 09a486b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ static void _testembed_Py_Initialize(void)
}


static int test_import_in_subinterpreters(void)
{
_testembed_Py_InitializeFromConfig();
PyThreadState_Swap(Py_NewInterpreter());
return PyRun_SimpleString("import readline"); // gh-124160
}


/*****************************************************
* Test repeated initialisation and subinterpreters
*****************************************************/
Expand Down Expand Up @@ -2398,6 +2406,7 @@ static struct TestCase TestCases[] = {
{"test_repeated_init_exec", test_repeated_init_exec},
{"test_repeated_simple_init", test_repeated_simple_init},
{"test_forced_io_encoding", test_forced_io_encoding},
{"test_import_in_subinterpreters", test_import_in_subinterpreters},
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
{"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
{"test_pre_initialization_api", test_pre_initialization_api},
Expand Down
2 changes: 1 addition & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
singlephase.m_init = p0;
}
cached = update_global_state_for_extension(
tstate, info->path, info->name, def, &singlephase);
main_tstate, info->path, info->name, def, &singlephase);
if (cached == NULL) {
assert(PyErr_Occurred());
goto main_finally;
Expand Down

0 comments on commit 09a486b

Please sign in to comment.