Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Only call PyOS_* hooks from the main interpreter
  • Loading branch information
brandtbucher committed May 19, 2023
commit 262ed9fc8db7f08f359025288e0356f8fc9d9f77
32 changes: 25 additions & 7 deletions Parser/myreadline.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
#endif

while (1) {
if (PyOS_InputHook != NULL) {
if (PyOS_InputHook != NULL &&
// See the comment for PyOS_ReadlineFunctionPointer below:
_Py_IsMainInterpreter(tstate->interp))
{
(void)(PyOS_InputHook)();
}

Expand Down Expand Up @@ -131,7 +134,10 @@ _PyOS_WindowsConsoleReadline(PyThreadState *tstate, HANDLE hStdIn)
wbuf = wbuf_local;
wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
while (1) {
if (PyOS_InputHook != NULL) {
if (PyOS_InputHook != NULL &&
// See the comment for PyOS_ReadlineFunctionPointer below:
_Py_IsMainInterpreter(tstate->interp))
{
(void)(PyOS_InputHook)();
}
if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) {
Expand Down Expand Up @@ -389,11 +395,23 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
* a tty. This can happen, for example if python is run like
* this: python -i < test1.py
*/
if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
else
rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
prompt);
if (!isatty(fileno(sys_stdin)) || !isatty(fileno(sys_stdout)) ||
// Don't call globally-registered callbacks like PyOS_InputHook or
// PyOS_ReadlineFunctionPointer from subinterpreters, since it seems
// like there's no good way for users (like readline and tkinter) to
// avoid using global state to manage them. Plus, we generally don't
// want to cause trouble for libraries that don't know/care about
// subinterpreter support. If libraries really need better APIs that
// work per-interpreter and have ways to access module state, we can
// certainly add them later (but for now we'll cross our fingers and
// hope that nobody actually cares):
!_Py_IsMainInterpreter(tstate->interp))
{
rv = PyOS_StdioReadline(sys_stdin, sys_stdout, prompt);
}
else {
rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, prompt);
}
Py_END_ALLOW_THREADS

PyThread_release_lock(_PyOS_ReadlineLock);
Expand Down