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

Skip to content

Commit cafd495

Browse files
committed
In O_writelines: Replace use of string.joinfields with "".join.
1 parent 6f8ee59 commit cafd495

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

Lib/test/output/test_StringIO

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
44

55
2
66
'abcuvwxyz!'
7+
'abc'
78
'abcdefghij'
89
'abcde'
910
Caught expected ValueError writing to closed StringIO:
@@ -13,6 +14,7 @@ klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
1314

1415
2
1516
'abcuvwxyz!'
17+
'abc'
1618
'abcdefghij'
1719
'abcde'
1820
Caught expected ValueError writing to closed StringIO:

Lib/test/test_StringIO.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def do_test(module):
1414
f.write('!')
1515
print `f.getvalue()`
1616
f.close()
17+
18+
f = module.StringIO()
19+
f.writelines(["a", "b", "c"])
20+
f.seek(0)
21+
print `f.getvalue()`
22+
f.close()
23+
1724
f = module.StringIO()
1825
f.write(s)
1926
f.seek(10)
@@ -31,7 +38,6 @@ def do_test(module):
3138
else:
3239
print "Failed to catch ValueError writing to closed StringIO."
3340

34-
# Don't bother testing cStringIO without
3541
import StringIO, cStringIO
3642
do_test(StringIO)
3743
do_test(cStringIO)

Modules/cStringIO.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,23 @@ static char O_writelines__doc__[] =
460460
static PyObject *
461461
O_writelines(Oobject *self, PyObject *args) {
462462
PyObject *tmp = 0;
463-
static PyObject *string_joinfields = 0;
463+
static PyObject *joiner = NULL;
464464

465465
UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
466466

467-
if (!string_joinfields) {
468-
UNLESS (tmp = PyImport_ImportModule("string")) return NULL;
469-
string_joinfields=PyObject_GetAttrString(tmp, "joinfields");
470-
Py_DECREF(tmp);
471-
UNLESS (string_joinfields) return NULL;
472-
}
467+
if (!joiner) {
468+
PyObject *empty_string = PyString_FromString("");
469+
if (empty_string == NULL)
470+
return NULL;
471+
joiner = PyObject_GetAttrString(empty_string, "join");
472+
Py_DECREF(empty_string);
473+
if (joiner == NULL)
474+
return NULL;
475+
}
473476

474477
if (PyObject_Size(args) < 0) return NULL;
475478

476-
tmp = PyObject_CallFunction(string_joinfields, "Os", args, "");
479+
tmp = PyObject_CallFunction(joiner, "O", args);
477480
UNLESS (tmp) return NULL;
478481

479482
args = Py_BuildValue("(O)", tmp);

0 commit comments

Comments
 (0)