mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
SF patch #907403: Improvements to cStringIO.writelines()
The writelines() method now accepts any iterable argument and writes the lines one at a time rather than using ''.join(lines) followed by a single write. Results in considerable memory savings and makes the method suitable for use with generator expressions.
This commit is contained in:
parent
73360a3e61
commit
6ec099658a
2 changed files with 29 additions and 32 deletions
|
|
@ -178,8 +178,10 @@ class StringIO:
|
||||||
self.len = newpos
|
self.len = newpos
|
||||||
self.pos = newpos
|
self.pos = newpos
|
||||||
|
|
||||||
def writelines(self, list):
|
def writelines(self, iterable):
|
||||||
self.write(''.join(list))
|
write = self.write
|
||||||
|
for line in iterable:
|
||||||
|
write(line)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
_complain_ifclosed(self.closed)
|
_complain_ifclosed(self.closed)
|
||||||
|
|
|
||||||
|
|
@ -416,40 +416,35 @@ O_close(Oobject *self, PyObject *unused) {
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(O_writelines__doc__,
|
PyDoc_STRVAR(O_writelines__doc__,
|
||||||
"writelines(sequence_of_strings): write each string");
|
"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
|
||||||
|
"\n"
|
||||||
|
"Note that newlines are not added. The sequence can be any iterable object\n"
|
||||||
|
"producing strings. This is equivalent to calling write() for each string.");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
O_writelines(Oobject *self, PyObject *args) {
|
O_writelines(Oobject *self, PyObject *args) {
|
||||||
PyObject *tmp = 0;
|
PyObject *it, *s;
|
||||||
static PyObject *joiner = NULL;
|
|
||||||
|
it = PyObject_GetIter(args);
|
||||||
if (!joiner) {
|
if (it == NULL)
|
||||||
PyObject *empty_string = PyString_FromString("");
|
|
||||||
if (empty_string == NULL)
|
|
||||||
return NULL;
|
|
||||||
joiner = PyObject_GetAttrString(empty_string, "join");
|
|
||||||
Py_DECREF(empty_string);
|
|
||||||
if (joiner == NULL)
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PyObject_Size(args) < 0) return NULL;
|
|
||||||
|
|
||||||
args = PyTuple_Pack(1, args);
|
|
||||||
if (args == NULL)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
tmp = PyObject_Call(joiner, args, NULL);
|
while ((s = PyIter_Next(it)) != NULL) {
|
||||||
Py_DECREF(args);
|
int n;
|
||||||
UNLESS (tmp) return NULL;
|
char *c;
|
||||||
|
if (PyString_AsStringAndSize(s, &c, &n) == -1) {
|
||||||
args = PyTuple_Pack(1, tmp);
|
Py_DECREF(it);
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(s);
|
||||||
UNLESS (args) return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
tmp = O_write(self, args);
|
if (O_cwrite((PyObject *)self, c, n) == -1) {
|
||||||
Py_DECREF(args);
|
Py_DECREF(it);
|
||||||
return tmp;
|
Py_DECREF(s);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_DECREF(s);
|
||||||
|
}
|
||||||
|
Py_DECREF(it);
|
||||||
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct PyMethodDef O_methods[] = {
|
static struct PyMethodDef O_methods[] = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue