(Merge 3.1) Issue #11650: PyOS_StdioReadline() retries fgets() if it was

interrupted (EINTR), for example if the program is stopped with CTRL+z on Mac
OS X. Patch written by Charles-Francois Natali.
This commit is contained in:
Victor Stinner 2011-04-09 15:59:25 +02:00
commit a870e35a7d
2 changed files with 59 additions and 51 deletions

View File

@ -10,6 +10,10 @@ What's New in Python 3.2.1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #11650: PyOS_StdioReadline() retries fgets() if it was interrupted
(EINTR), for example if the program is stopped with CTRL+z on Mac OS X. Patch
written by Charles-Francois Natali.
- Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on - Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
Windows if the file is a TTY to workaround a Windows bug. The Windows console Windows if the file is a TTY to workaround a Windows bug. The Windows console
returns an error (12: not enough space error) on writing into stdout if returns an error (12: not enough space error) on writing into stdout if

View File

@ -36,63 +36,67 @@ static int
my_fgets(char *buf, int len, FILE *fp) my_fgets(char *buf, int len, FILE *fp)
{ {
char *p; char *p;
if (PyOS_InputHook != NULL) while (1) {
(void)(PyOS_InputHook)(); if (PyOS_InputHook != NULL)
errno = 0; (void)(PyOS_InputHook)();
p = fgets(buf, len, fp); errno = 0;
if (p != NULL) p = fgets(buf, len, fp);
return 0; /* No error */ if (p != NULL)
return 0; /* No error */
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
/* In the case of a Ctrl+C or some other external event /* In the case of a Ctrl+C or some other external event
interrupting the operation: interrupting the operation:
Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
error code (and feof() returns TRUE). error code (and feof() returns TRUE).
Win9x: Ctrl+C seems to have no effect on fgets() returning Win9x: Ctrl+C seems to have no effect on fgets() returning
early - the signal handler is called, but the fgets() early - the signal handler is called, but the fgets()
only returns "normally" (ie, when Enter hit or feof()) only returns "normally" (ie, when Enter hit or feof())
*/
if (GetLastError()==ERROR_OPERATION_ABORTED) {
/* Signals come asynchronously, so we sleep a brief
moment before checking if the handler has been
triggered (we cant just return 1 before the
signal handler has been called, as the later
signal may be treated as a separate interrupt).
*/ */
Sleep(1); if (GetLastError()==ERROR_OPERATION_ABORTED) {
/* Signals come asynchronously, so we sleep a brief
moment before checking if the handler has been
triggered (we cant just return 1 before the
signal handler has been called, as the later
signal may be treated as a separate interrupt).
*/
Sleep(1);
if (PyOS_InterruptOccurred()) {
return 1; /* Interrupt */
}
/* Either the sleep wasn't long enough (need a
short loop retrying?) or not interrupted at all
(in which case we should revisit the whole thing!)
Logging some warning would be nice. assert is not
viable as under the debugger, the various dialogs
mean the condition is not true.
*/
}
#endif /* MS_WINDOWS */
if (feof(fp)) {
return -1; /* EOF */
}
#ifdef EINTR
if (errno == EINTR) {
int s;
#ifdef WITH_THREAD
PyEval_RestoreThread(_PyOS_ReadlineTState);
#endif
s = PyErr_CheckSignals();
#ifdef WITH_THREAD
PyEval_SaveThread();
#endif
if (s < 0)
return 1;
/* try again */
continue;
}
#endif
if (PyOS_InterruptOccurred()) { if (PyOS_InterruptOccurred()) {
return 1; /* Interrupt */ return 1; /* Interrupt */
} }
/* Either the sleep wasn't long enough (need a return -2; /* Error */
short loop retrying?) or not interrupted at all
(in which case we should revisit the whole thing!)
Logging some warning would be nice. assert is not
viable as under the debugger, the various dialogs
mean the condition is not true.
*/
} }
#endif /* MS_WINDOWS */ /* NOTREACHED */
if (feof(fp)) {
return -1; /* EOF */
}
#ifdef EINTR
if (errno == EINTR) {
int s;
#ifdef WITH_THREAD
PyEval_RestoreThread(_PyOS_ReadlineTState);
#endif
s = PyErr_CheckSignals();
#ifdef WITH_THREAD
PyEval_SaveThread();
#endif
if (s < 0) {
return 1;
}
}
#endif
if (PyOS_InterruptOccurred()) {
return 1; /* Interrupt */
}
return -2; /* Error */
} }