Skip to content

Commit bea3f06

Browse files
committed
MNT: move the PyOS_InputHook call up a loop level
If we have returned from the input hook then there is something to read in the stdin so read it out to the new line before calling the input hook again.
1 parent 7094a36 commit bea3f06

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

‎Doc/c-api/veryhigh.rst‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ the same library that the Python runtime is using.
151151
event loops, as done in :file:`Modules/_tkinter.c` in the
152152
Python source code.
153153
154-
This function should block until stdin is readable but may return
155-
early.
154+
This function should block until stdin is readable.
156155
157156
The function is called without an :term:`attached thread state`.
158157

‎Parser/myreadline.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int (*PyOS_InputHook)(void) = NULL;
3838
except if _PyOS_InterruptOccurred() returns true. */
3939

4040
static int
41-
my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp, int n)
41+
my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
4242
{
4343
#ifdef MS_WINDOWS
4444
HANDLE handle;
@@ -53,12 +53,6 @@ my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp, int n)
5353
#endif
5454

5555
while (1) {
56-
if (PyOS_InputHook != NULL && n == 0 &&
57-
// GH-104668: See PyOS_ReadlineFunctionPointer's comment below...
58-
_Py_IsMainInterpreter(tstate->interp))
59-
{
60-
(void)(PyOS_InputHook)();
61-
}
6256

6357
errno = 0;
6458
clearerr(fp);
@@ -319,6 +313,13 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
319313
}
320314
fflush(stderr);
321315

316+
if (PyOS_InputHook != NULL &&
317+
// GH-104668: See PyOS_ReadlineFunctionPointer's comment below...
318+
_Py_IsMainInterpreter(tstate->interp))
319+
{
320+
(void)(PyOS_InputHook)();
321+
}
322+
322323
n = 0;
323324
p = NULL;
324325
do {
@@ -339,7 +340,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
339340
return NULL;
340341
}
341342
p = pr;
342-
int err = my_fgets(tstate, p + n, (int)incr, sys_stdin, n);
343+
int err = my_fgets(tstate, p + n, (int)incr, sys_stdin);
343344
if (err == 1) {
344345
// Interrupt
345346
PyMem_RawFree(p);

0 commit comments

Comments
 (0)