]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45479: Futher simplify Py_UniversalNewlineFgets. (GH-28967)
authorBenjamin Peterson <benjamin@python.org>
Fri, 15 Oct 2021 06:10:52 +0000 (23:10 -0700)
committerGitHub <noreply@github.com>
Fri, 15 Oct 2021 06:10:52 +0000 (23:10 -0700)
Thank you to Eryk Sun for the suggestions in https://github.com/python/cpython/pull/28965#discussion_r729527143.

Objects/fileobject.c

index 8eb62490c452b96d9db2fca58a8b4f6592cabc72..8ca56a802b9769f1081ad23acfddd5c4be16f928 100644 (file)
@@ -248,7 +248,6 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
 {
     char *p = buf;
     int c;
-    int skipnextlf = 0;
 
     if (fobj) {
         errno = ENXIO;          /* What can you do... */
@@ -256,34 +255,21 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
     }
     FLOCKFILE(stream);
     while (--n > 0 && (c = GETC(stream)) != EOF ) {
-        if (skipnextlf) {
-            skipnextlf = 0;
-            if (c == '\n') {
-                /* Seeing a \n here with skipnextlf true
-                ** means we saw a \r before.
-                */
-                c = GETC(stream);
-                if (c == EOF) break;
-            }
-        }
         if (c == '\r') {
-            /* A \r is translated into a \n, and we skip
-            ** an adjacent \n, if any. We don't set the
-            ** newlinetypes flag until we've seen the next char.
-            */
-            skipnextlf = 1;
-            c = '\n';
+            // A \r is translated into a \n, and we skip an adjacent \n, if any.
+            c = GETC(stream);
+            if (c != '\n') {
+                ungetc(c, stream);
+                c = '\n';
+            }
         }
         *p++ = c;
-        if (c == '\n') break;
+        if (c == '\n') {
+            break;
+        }
     }
     FUNLOCKFILE(stream);
     *p = '\0';
-    if (skipnextlf) {
-        int c = GETC(stream);
-        if (c != '\n')
-            ungetc(c, stream);
-    }
     if (p == buf)
         return NULL;
     return buf;