]> granicus.if.org Git - python/commitdiff
#2560: remove an unnecessary 'for' loop from my_fgets() in Parser/myreadline.c.
authorAndrew M. Kuchling <amk@amk.ca>
Mon, 22 Feb 2010 22:48:41 +0000 (22:48 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Mon, 22 Feb 2010 22:48:41 +0000 (22:48 +0000)
Noted by Joseph Armbruster; patch by Jessica McKellar.

The original code was 'for (;;) {...}', where ... ended
with a 'return -2' statement and did not contain a 'break' or 'continue'
statement.  Therefore, the body of the loop is always executed once.

Once upon a time there was a 'continue' in the loop, but it was removed in
rev36346, committed by mwh on Wed Jul 7 17:44:12 2004.

Misc/NEWS
Parser/myreadline.c

index 560aa017d45cb0df03603f13fe45056dce9f6071..89af2a4ab99d5c5132eaf21ec70e67353cb2a925 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 4?
 Core and Builtins
 -----------------
 
+- Issue #2560: remove an unnecessary 'for' loop from my_fgets() in
+  Parser/myreadline.c.
+
 - Issue #7988: Fix default alignment to be right aligned for
   complex.__format__. Now it matches other numeric types.
 
index 32a1088ad09b95fd697830b8bf073af2bb097170..433f995032eaa4337d0f34681b0da042cbcc3cd4 100644 (file)
@@ -40,66 +40,63 @@ static int
 my_fgets(char *buf, int len, FILE *fp)
 {
        char *p;
-       for (;;) {
-               if (PyOS_InputHook != NULL)
-                       (void)(PyOS_InputHook)();
-               errno = 0;
-               p = fgets(buf, len, fp);
-               if (p != NULL)
-                       return 0; /* No error */
+       if (PyOS_InputHook != NULL)
+               (void)(PyOS_InputHook)();
+       errno = 0;
+       p = fgets(buf, len, fp);
+       if (p != NULL)
+               return 0; /* No error */
 #ifdef MS_WINDOWS
-               /* In the case of a Ctrl+C or some other external event 
-                  interrupting the operation:
-                  Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 
-                  error code (and feof() returns TRUE).
-                  Win9x: Ctrl+C seems to have no effect on fgets() returning
-                  early - the signal handler is called, but the fgets()
-                  only returns "normally" (ie, when Enter hit or feof())
+       /* In the case of a Ctrl+C or some other external event 
+          interrupting the operation:
+          Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 
+          error code (and feof() returns TRUE).
+          Win9x: Ctrl+C seems to have no effect on fgets() returning
+          early - the signal handler is called, but the fgets()
+          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).
                */
-               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.
-                       */
+               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 */
-               }
+       if (feof(fp)) {
+               return -1; /* EOF */
+       }
 #ifdef EINTR
-               if (errno == EINTR) {
-                       int s;
+       if (errno == EINTR) {
+               int s;
 #ifdef WITH_THREAD
-                       PyEval_RestoreThread(_PyOS_ReadlineTState);
+               PyEval_RestoreThread(_PyOS_ReadlineTState);
 #endif
-                       s = PyErr_CheckSignals();
+               s = PyErr_CheckSignals();
 #ifdef WITH_THREAD
-                       PyEval_SaveThread();
+               PyEval_SaveThread();
 #endif
-                       if (s < 0) {
-                               return 1;
-                       }
+               if (s < 0) {
+                       return 1;
                }
+       }
 #endif
-               if (PyOS_InterruptOccurred()) {
-                       return 1; /* Interrupt */
-               }
-               return -2; /* Error */
+       if (PyOS_InterruptOccurred()) {
+               return 1; /* Interrupt */
        }
-       /* NOTREACHED */
+       return -2; /* Error */
 }