8000 bpo-45479: Futher simplify Py_UniversalNewlineFgets. (GH-28967) · python/cpython@9ce9cfe · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ce9cfe

Browse files
authored
bpo-45479: Futher simplify Py_UniversalNewlineFgets. (GH-28967)
Thank you to Eryk Sun for the suggestions in #28965 (comment).
1 parent 160c38d commit 9ce9cfe

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

Objects/fileobject.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,42 +248,28 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
248248
{
249249
char *p = buf;
250250
int c;
251-
int skipnextlf = 0;
252251

253252
if (fobj) {
254253
errno = ENXIO; /* What can you do... */
255254
return NULL;
256255
}
257256
FLOCKFILE(stream);
258257
while (--n > 0 && (c = GETC(stream)) != EOF ) {
259-
if (skipnextlf) {
260-
skipnextlf = 0;
261-
if (c == '\n') {
262-
/* Seeing a \n here with skipnextlf true
263-
** means we saw a \r before.
264-
*/
265-
c = GETC(stream);
266-
if (c == EOF) break;
267-
}
268-
}
269258
if (c == '\r') {
270-
/* A \r is translated into a \n, and we skip
271-
** an adjacent \n, if any. We don't set the
272-
** newlinetypes flag until we've seen the next char.
273-
*/
274-
skipnextlf = 1;
275-
c = '\n';
259+
// A \r is translated into a \n, and we skip an adjacent \n, if any.
260+
c = GETC(stream);
261+
if (c != '\n') {
262+
ungetc(c, stream);
263+
c = '\n';
264+
}
276265
}
277266
*p++ = c;
278-
if (c == '\n') break;
267+
if (c == '\n') {
268+
break;
269+
}
279270
}
280271
FUNLOCKFILE(stream);
281272
*p = '\0';
282-
if (skipnextlf) {
283-
int c = GETC(stream);
284-
if (c != '\n')
285-
ungetc(c, stream);
286-
}
287273
if (p == buf)
288274
return NULL;
289275
return buf;

0 commit comments

Comments
 (0)
0