Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 35ee948

Browse files
committed
restructure fp_setreadl so as to avoid refleaks (closes python#27981)
1 parent 7927e75 commit 35ee948

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Parser/tokenizer.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,12 @@ fp_readl(char *s, int size, struct tok_state *tok)
497497
static int
498498
fp_setreadl(struct tok_state *tok, const char* enc)
499499
{
500-
PyObject *readline = NULL, *stream = NULL, *io = NULL;
500+
PyObject *readline, *io, *stream;
501501
_Py_IDENTIFIER(open);
502502
_Py_IDENTIFIER(readline);
503503
int fd;
504504
long pos;
505505

506-
io = PyImport_ImportModuleNoBlock("io");
507-
if (io == NULL)
508-
goto cleanup;
509-
510506
fd = fileno(tok->fp);
511507
/* Due to buffering the file offset for fd can be different from the file
512508
* position of tok->fp. If tok->fp was opened in text mode on Windows,
@@ -517,27 +513,33 @@ fp_setreadl(struct tok_state *tok, const char* enc)
517513
if (pos == -1 ||
518514
lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
519515
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
520-
goto cleanup;
516+
return 0;
521517
}
522518

519+
io = PyImport_ImportModuleNoBlock("io");
520+
if (io == NULL)
521+
return 0;
522+
523523
stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO",
524524
fd, "r", -1, enc, Py_None, Py_None, Py_False);
525+
Py_DECREF(io);
525526
if (stream == NULL)
526-
goto cleanup;
527+
return 0;
527528

528529
readline = _PyObject_GetAttrId(stream, &PyId_readline);
530+
Py_DECREF(stream);
531+
if (readline == NULL)
532+
return 0;
529533
Py_XSETREF(tok->decoding_readline, readline);
534+
530535
if (pos > 0) {
531-
if (PyObject_CallObject(readline, NULL) == NULL) {
532-
readline = NULL;
533-
goto cleanup;
534-
}
536+
PyObject *bufobj = PyObject_CallObject(readline, NULL);
537+
if (bufobj == NULL)
538+
return 0;
539+
Py_DECREF(bufobj);
535540
}
536541

537-
cleanup:
538-
Py_XDECREF(stream);
539-
Py_XDECREF(io);
540-
return readline != NULL;
542+
return 1;
541543
}
542544

543545
/* Fetch the next byte from TOK. */

0 commit comments

Comments
 (0)