From 4b2b445f28937ae555700c2ac2280eeaa0521245 Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Wed, 29 Nov 2000 02:53:22 +0000 Subject: Patch #102469: Use glibc's getline() extension when reading unbounded lines --- Objects/fileobject.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 61023dc..d008ff7 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -645,13 +645,40 @@ file_readinto(PyFileObject *f, PyObject *args) static PyObject * get_line(PyFileObject *f, int n) { - register FILE *fp; + register FILE *fp = f->f_fp; register int c; - register char *buf, *end; + char *buf, *end; size_t n1, n2; PyObject *v; - fp = f->f_fp; +#ifdef HAVE_GETLINE + /* Use GNU libc extension getline() for arbitrary-sized lines */ + if (n == 0) { + size_t size = 0; + buf = NULL; + Py_BEGIN_ALLOW_THREADS + n1 = getline(&buf, &size, fp); + Py_END_ALLOW_THREADS + if (n1 == -1) { + clearerr(fp); + if (PyErr_CheckSignals()) { + return NULL; + } + if (n < 0 && feof(fp)) { + PyErr_SetString(PyExc_EOFError, + "EOF when reading a line"); + return NULL; + } + return PyString_FromStringAndSize(NULL, 0); + } + /* No error */ + + v = PyString_FromStringAndSize(buf, n1); + free(buf); + return v; + } +#endif + n2 = n > 0 ? n : 100; v = PyString_FromStringAndSize((char *)NULL, n2); if (v == NULL) -- cgit v0.12