Skip to content

Commit 96fa029

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 294c7c4 commit 96fa029

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

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
.. versionchanged:: 3.12
158157
This function is only called from the

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);
@@ -313,6 +307,13 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
313307
}
314308
fflush(stderr);
315309

310+
if (PyOS_InputHook != NULL &&
311+
// GH-104668: See PyOS_ReadlineFunctionPointer's comment below...
312+
_Py_IsMainInterpreter(tstate->interp))
313+
{
314+
(void)(PyOS_InputHook)();
315+
}
316+
316317
n = 0;
317318
p = NULL;
318319
do {
@@ -333,7 +334,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
333334
return NULL;
334335
}
335336
p = pr;
336-
int err = my_fgets(tstate, p + n, (int)incr, sys_stdin, n);
337+
int err = my_fgets(tstate, p + n, (int)incr, sys_stdin);
337338
if (err == 1) {
338339
// Interrupt
339340
PyMem_RawFree(p);

0 commit comments

Comments
 (0)