Skip to content

Commit 6f30f79

Browse files
committed
Fix Modules/main.c to pass the main module object to the Python-based REPL.
The Python-based REPL (_pyrepl.main.interactive_console) no longer loads the real __main__ module so it should be passed from its caller.
1 parent 8222889 commit 6f30f79

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Modules/main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ pymain_run_command(wchar_t *command)
269269

270270

271271
static int
272-
pymain_start_pyrepl_no_main(void)
272+
pymain_start_pyrepl(void)
273273
{
274274
int res = 0;
275275
PyObject *console = NULL;
276276
PyObject *empty_tuple = NULL;
277277
PyObject *kwargs = NULL;
278278
PyObject *console_result = NULL;
279+
PyObject *main_module = NULL;
279280

280281
PyObject *pyrepl = PyImport_ImportModule("_pyrepl.main");
281282
if (pyrepl == NULL) {
@@ -299,7 +300,16 @@ pymain_start_pyrepl_no_main(void)
299300
res = pymain_exit_err_print();
300301
goto done;
301302
}
303+
main_module = PyImport_AddModuleRef("__main__");
304+
if (main_module == NULL) {
305+
res = pymain_exit_err_print();
306+
goto done;
307+
}
302308
if (!PyDict_SetItemString(kwargs, "pythonstartup", _PyLong_GetOne())) {
309+
if (PyDict_SetItemString(kwargs, "mainmodule", main_module) < 0) {
310+
res = pymain_exit_err_print();
311+
goto done;
312+
}
303313
console_result = PyObject_Call(console, empty_tuple, kwargs);
304314
if (console_result == NULL) {
305315
res = pymain_exit_err_print();
@@ -311,6 +321,7 @@ pymain_start_pyrepl_no_main(void)
311321
Py_XDECREF(empty_tuple);
312322
Py_XDECREF(console);
313323
Py_XDECREF(pyrepl);
324+
Py_XDECREF(main_module);
314325
return res;
315326
}
316327

@@ -595,7 +606,7 @@ pymain_repl(PyConfig *config, int *exitcode)
595606
*exitcode = (run != 0);
596607
return;
597608
}
598-
int run = pymain_start_pyrepl_no_main();
609+
int run = pymain_start_pyrepl();
599610
*exitcode = (run != 0);
600611
return;
601612
}

0 commit comments

Comments
 (0)