diff options
author | Brandt Bucher <brandtbucher@microsoft.com> | 2023-05-22 19:34:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-22 19:34:34 (GMT) |
commit | 357bed0bcd3c5d7c4a8caad451754a9a172aca3e (patch) | |
tree | c0f022e38113eba6b885873eff045eb5784c7d61 /Parser | |
parent | 2c4e29e32260783207568dc8581d65f0022f773a (diff) | |
download | cpython-357bed0bcd3c5d7c4a8caad451754a9a172aca3e.zip cpython-357bed0bcd3c5d7c4a8caad451754a9a172aca3e.tar.gz cpython-357bed0bcd3c5d7c4a8caad451754a9a172aca3e.tar.bz2 |
GH-104668: Don't call PyOS_* hooks in subinterpreters (GH-104674)
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/myreadline.c | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/Parser/myreadline.c b/Parser/myreadline.c index 3f0e29f..7074aba 100644 --- a/Parser/myreadline.c +++ b/Parser/myreadline.c @@ -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 && + // GH-104668: See PyOS_ReadlineFunctionPointer's comment below... + _Py_IsMainInterpreter(tstate->interp)) + { (void)(PyOS_InputHook)(); } @@ -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 && + // GH-104668: See PyOS_ReadlineFunctionPointer's comment below... + _Py_IsMainInterpreter(tstate->interp)) + { (void)(PyOS_InputHook)(); } if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) { @@ -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)) || + // GH-104668: Don't call global 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); |