Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b2b445

Browse files
committed
Patch #102469: Use glibc's getline() extension when reading unbounded lines
1 parent 78a1442 commit 4b2b445

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

Objects/fileobject.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,40 @@ file_readinto(PyFileObject *f, PyObject *args)
645645
static PyObject *
646646
get_line(PyFileObject *f, int n)
647647
{
648-
register FILE *fp;
648+
register FILE *fp = f->f_fp;
649649
register int c;
650-
register char *buf, *end;
650+
char *buf, *end;
651651
size_t n1, n2;
652652
PyObject *v;
653653

654-
fp = f->f_fp;
654+
#ifdef HAVE_GETLINE
655+
/* Use GNU libc extension getline() for arbitrary-sized lines */
656+
if (n == 0) {
657+
size_t size = 0;
658+
buf = NULL;
659+
Py_BEGIN_ALLOW_THREADS
660+
n1 = getline(&buf, &size, fp);
661+
Py_END_ALLOW_THREADS
662+
if (n1 == -1) {
663+
clearerr(fp);
664+
if (PyErr_CheckSignals()) {
665+
return NULL;
666+
}
667+
if (n < 0 && feof(fp)) {
668+
PyErr_SetString(PyExc_EOFError,
669+
"EOF when reading a line");
670+
return NULL;
671+
}
672+
return PyString_FromStringAndSize(NULL, 0);
673+
}
674+
/* No error */
675+
676+
v = PyString_FromStringAndSize(buf, n1);
677+
free(buf);
678+
return v;
679+
}
680+
#endif
681+
655682
n2 = n > 0 ? n : 100;
656683
v = PyString_FromStringAndSize((char *)NULL, n2);
657684
if (v == NULL)

0 commit comments

Comments
 (0)