summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2000-11-29 02:53:22 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2000-11-29 02:53:22 (GMT)
commit4b2b445f28937ae555700c2ac2280eeaa0521245 (patch)
tree7a5529d9370f9ad33802f851cc19d26a316c4553
parent78a14423af6f0bea5386c75c7e3794328f90b257 (diff)
downloadcpython-4b2b445f28937ae555700c2ac2280eeaa0521245.zip
cpython-4b2b445f28937ae555700c2ac2280eeaa0521245.tar.gz
cpython-4b2b445f28937ae555700c2ac2280eeaa0521245.tar.bz2
Patch #102469: Use glibc's getline() extension when reading unbounded lines
-rw-r--r--Objects/fileobject.c33
1 files 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)