closes bpo-45479: Degunkify Py_UniversalNewlineFgets. (GH-28965)

Remove dead variables and control flow.
This commit is contained in:
Benjamin Peterson 2021-10-14 21:28:52 -07:00 committed by GitHub
parent 77b24ba505
commit 160c38df7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -239,13 +239,7 @@ _PyLong_FileDescriptor_Converter(PyObject *o, void *ptr)
** Py_UniversalNewlineFgets is an fgets variation that understands ** Py_UniversalNewlineFgets is an fgets variation that understands
** all of \r, \n and \r\n conventions. ** all of \r, \n and \r\n conventions.
** The stream should be opened in binary mode. ** The stream should be opened in binary mode.
** If fobj is NULL the routine always does newline conversion, and ** The fobj parameter exists solely for legacy reasons and must be NULL.
** it may peek one char ahead to gobble the second char in \r\n.
** If fobj is non-NULL it must be a PyFileObject. In this case there
** is no readahead but in stead a flag is used to skip a following
** \n on the next read. Also, if the file is open in binary mode
** the whole conversion is skipped. Finally, the routine keeps track of
** the different types of newlines seen.
** Note that we need no error handling: fgets() treats error and eof ** Note that we need no error handling: fgets() treats error and eof
** identically. ** identically.
*/ */
@ -254,7 +248,6 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
{ {
char *p = buf; char *p = buf;
int c; int c;
int newlinetypes = 0;
int skipnextlf = 0; int skipnextlf = 0;
if (fobj) { if (fobj) {
@ -262,24 +255,15 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
return NULL; return NULL;
} }
FLOCKFILE(stream); FLOCKFILE(stream);
c = 'x'; /* Shut up gcc warning */
while (--n > 0 && (c = GETC(stream)) != EOF ) { while (--n > 0 && (c = GETC(stream)) != EOF ) {
if (skipnextlf ) { if (skipnextlf) {
skipnextlf = 0; skipnextlf = 0;
if (c == '\n') { if (c == '\n') {
/* Seeing a \n here with skipnextlf true /* Seeing a \n here with skipnextlf true
** means we saw a \r before. ** means we saw a \r before.
*/ */
newlinetypes |= NEWLINE_CRLF;
c = GETC(stream); c = GETC(stream);
if (c == EOF) break; if (c == EOF) break;
} else {
/*
** Note that c == EOF also brings us here,
** so we're okay if the last char in the file
** is a CR.
*/
newlinetypes |= NEWLINE_CR;
} }
} }
if (c == '\r') { if (c == '\r') {
@ -289,26 +273,15 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
*/ */
skipnextlf = 1; skipnextlf = 1;
c = '\n'; c = '\n';
} else if ( c == '\n') {
newlinetypes |= NEWLINE_LF;
} }
*p++ = c; *p++ = c;
if (c == '\n') break; if (c == '\n') break;
} }
/* if ( c == EOF && skipnextlf )
newlinetypes |= NEWLINE_CR; */
FUNLOCKFILE(stream); FUNLOCKFILE(stream);
*p = '\0'; *p = '\0';
if ( skipnextlf ) { if (skipnextlf) {
/* If we have no file object we cannot save the int c = GETC(stream);
** skipnextlf flag. We have to readahead, which if (c != '\n')
** will cause a pause if we're reading from an
** interactive stream, but that is very unlikely
** unless we're doing something silly like
** exec(open("/dev/tty").read()).
*/
c = GETC(stream);
if ( c != '\n' )
ungetc(c, stream); ungetc(c, stream);
} }
if (p == buf) if (p == buf)