-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-120678: pyrepl: Include globals from modules passed with -i
#120904
Changes from 8 commits
4dc6663
03f04bf
bc03ac0
8c9a878
5af30e3
d1396b4
baacb0d
6bb7da5
d3ba1e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Important: don't add things to this module, as they will end up in the REPL's | ||
# default globals. Use _pyrepl.main instead. | ||
|
||
if __name__ == "__main__": | ||
from .main import interactive_console as __pyrepl_interactive_console | ||
__pyrepl_interactive_console() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix regression in the new REPL that meant that globals from files passed | ||
using the ``-i`` argument would not be included in the REPL's global | ||
namespace. Patch by Alex Waygood. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
#include "pycore_call.h" // _PyObject_CallNoArgs() | ||
#include "pycore_initconfig.h" // _PyArgv | ||
#include "pycore_interp.h" // _PyInterpreterState.sysdict | ||
#include "pycore_long.h" // _PyLong_GetOne() | ||
#include "pycore_pathconfig.h" // _PyPathConfig_ComputeSysPath0() | ||
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromPyArgv() | ||
#include "pycore_pystate.h" // _PyInterpreterState_GET() | ||
|
@@ -259,6 +260,53 @@ | |
} | ||
|
||
|
||
static int | ||
pymain_start_pyrepl_no_main() | ||
Check warning on line 264 in Modules/main.c GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)
Check warning on line 264 in Modules/main.c GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)
Check warning on line 264 in Modules/main.c GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)
Check warning on line 264 in Modules/main.c GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)
Check warning on line 264 in Modules/main.c GitHub Actions / Hypothesis tests on Ubuntu
Check warning on line 264 in Modules/main.c GitHub Actions / Ubuntu / build and test
Check warning on line 264 in Modules/main.c GitHub Actions / Address sanitizer
|
||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
int res = 0; | ||
PyObject *pyrepl, *console, *empty_tuple, *kwargs, *console_result; | ||
pyrepl = PyImport_ImportModule("_pyrepl.main"); | ||
if (pyrepl == NULL) { | ||
fprintf(stderr, "Could not import _pyrepl.main\n"); | ||
res = pymain_exit_err_print(); | ||
goto done; | ||
} | ||
console = PyObject_GetAttrString(pyrepl, "interactive_console"); | ||
if (console == NULL) { | ||
fprintf(stderr, "Could not access _pyrepl.main.interactive_console\n"); | ||
res = pymain_exit_err_print(); | ||
goto done; | ||
} | ||
empty_tuple = PyTuple_New(0); | ||
if (empty_tuple == NULL) { | ||
res = pymain_exit_err_print(); | ||
goto done; | ||
} | ||
kwargs = PyDict_New(); | ||
if (kwargs == NULL) { | ||
res = pymain_exit_err_print(); | ||
goto done; | ||
} | ||
if (!PyDict_SetItemString(kwargs, "pythonstartup", _PyLong_GetOne())) { | ||
_PyRuntime.signals.unhandled_keyboard_interrupt = 0; | ||
console_result = PyObject_Call(console, empty_tuple, kwargs); | ||
if (!console_result && PyErr_Occurred() == PyExc_KeyboardInterrupt) { | ||
_PyRuntime.signals.unhandled_keyboard_interrupt = 1; | ||
} | ||
if (console_result == NULL) { | ||
res = pymain_exit_err_print(); | ||
} | ||
} | ||
done: | ||
Py_XDECREF(console_result); | ||
Py_XDECREF(kwargs); | ||
Py_XDECREF(empty_tuple); | ||
Py_XDECREF(console); | ||
Py_XDECREF(pyrepl); | ||
return res; | ||
} | ||
|
||
|
||
static int | ||
pymain_run_module(const wchar_t *modname, int set_argv0) | ||
{ | ||
|
@@ -549,7 +597,7 @@ | |
*exitcode = (run != 0); | ||
return; | ||
} | ||
int run = pymain_run_module(L"_pyrepl", 0); | ||
int run = pymain_start_pyrepl_no_main(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My first intuition was to simply So instead we do the full dance of running
|
||
*exitcode = (run != 0); | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was helpful to me when debugging weird behavior so I decided to keep it.