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

Skip to content

Commit c912a3a

Browse files
committed
Implement readlines function. Closes Bug #110686.
1 parent e84b740 commit c912a3a

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Modules/cStringIO.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,40 @@ O_readline(Oobject *self, PyObject *args) {
224224
return PyString_FromStringAndSize(output, n);
225225
}
226226

227+
static char O_readlines__doc__[] =
228+
"readlines() -- Read all lines"
229+
;
230+
231+
static PyObject *
232+
O_readlines(Oobject *self, PyObject *args) {
233+
int n;
234+
char *output;
235+
PyObject *result, *line;
236+
int hint = 0, length = 0;
237+
238+
UNLESS(PyArg_ParseTuple(args, "|i:write", &hint)) return NULL;
239+
result = PyList_New(0);
240+
if (!result)
241+
return NULL;
242+
243+
while(1){
244+
n = O_creadline((PyObject*)self,&output);
245+
if (n == 0)
246+
break;
247+
line = PyString_FromStringAndSize (output, n);
248+
if (!line){
249+
Py_DECREF (result);
250+
return NULL;
251+
}
252+
PyList_Append (result, line);
253+
Py_DECREF (line);
254+
length += n;
255+
if (hint > 0 && length >= hint)
256+
break;
257+
}
258+
return result;
259+
}
260+
227261
static char O_write__doc__[] =
228262
"write(s) -- Write a string to the file"
229263
"\n\nNote (hack:) writing None resets the buffer"
@@ -390,6 +424,7 @@ static struct PyMethodDef O_methods[] = {
390424
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
391425
{"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
392426
{"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
427+
{"readlines", (PyCFunction)O_readlines, METH_VARARGS, O_readlines__doc__},
393428
{"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
394429
{"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
395430
{"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
@@ -522,6 +557,7 @@ I_seek(Oobject *self, PyObject *args) {
522557
static struct PyMethodDef I_methods[] = {
523558
{"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
524559
{"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
560+
{"readlines", (PyCFunction)O_readlines,METH_VARARGS, O_readlines__doc__},
525561
{"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
526562
{"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
527563
{"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},

0 commit comments

Comments
 (0)