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

Skip to content

Commit e47df7a

Browse files
committed
StringIO patch #462596: let's [c]StringIO accept read buffers on
input to .write() too.
1 parent c72d4cd commit e47df7a

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

Lib/StringIO.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838

3939
class StringIO:
4040
def __init__(self, buf = ''):
41-
self.buf = buf
41+
# Force self.buf to be a string
42+
self.buf = str(buf)
4243
self.len = len(buf)
4344
self.buflist = []
4445
self.pos = 0
@@ -134,6 +135,8 @@ def write(self, s):
134135
if self.closed:
135136
raise ValueError, "I/O operation on closed file"
136137
if not s: return
138+
# Force s to be a string
139+
s = str(s)
137140
if self.pos > self.len:
138141
self.buflist.append('\0'*(self.pos - self.len))
139142
self.len = self.pos

Lib/test/test_StringIO.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@
1010
class TestGenericStringIO(unittest.TestCase):
1111
# use a class variable MODULE to define which module is being tested
1212

13+
# Line of data to test as string
14+
_line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
15+
16+
# Constructor to use for the test data (._line is passed to this
17+
# constructor)
18+
constructor = str
19+
1320
def setUp(self):
14-
self._line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
15-
self._lines = (self._line + '\n') * 5
21+
self._line = self.constructor(self._line)
22+
self._lines = self.constructor((self._line + '\n') * 5)
1623
self._fp = self.MODULE.StringIO(self._lines)
1724

1825
def test_reads(self):
1926
eq = self.assertEqual
20-
eq(self._fp.read(10), 'abcdefghij')
21-
eq(self._fp.readline(), 'klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n')
27+
eq(self._fp.read(10), self._line[:10])
28+
eq(self._fp.readline(), self._line[10:] + '\n')
2229
eq(len(self._fp.readlines(60)), 2)
2330

2431
def test_writes(self):
2532
f = self.MODULE.StringIO()
26-
f.write('abcdef')
33+
f.write(self._line[:6])
2734
f.seek(3)
28-
f.write('uvwxyz')
29-
f.write('!')
35+
f.write(self._line[20:26])
36+
f.write(self._line[52])
3037
self.assertEqual(f.getvalue(), 'abcuvwxyz!')
3138

3239
def test_writelines(self):
3340
f = self.MODULE.StringIO()
34-
f.writelines(['a', 'b', 'c'])
41+
f.writelines([self._line[0], self._line[1], self._line[2]])
3542
f.seek(0)
3643
self.assertEqual(f.getvalue(), 'abc')
3744

@@ -64,10 +71,18 @@ class TestStringIO(TestGenericStringIO):
6471
class TestcStringIO(TestGenericStringIO):
6572
MODULE = cStringIO
6673

74+
class TestBufferStringIO(TestStringIO):
75+
constructor = buffer
76+
77+
class TestBuffercStringIO(TestcStringIO):
78+
constructor = buffer
79+
6780

6881
def test_main():
6982
test_support.run_unittest(TestStringIO)
7083
test_support.run_unittest(TestcStringIO)
84+
test_support.run_unittest(TestBufferStringIO)
85+
test_support.run_unittest(TestBuffercStringIO)
7186

7287
if __name__ == '__main__':
7388
test_main()

Modules/cStringIO.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ typedef struct { /* Subtype of IOobject */
120120
PyObject_HEAD
121121
char *buf;
122122
int pos, string_size;
123-
123+
/* We store a reference to the object here in order to keep
124+
the buffer alive during the lifetime of the Iobject. */
124125
PyObject *pbuf;
125126
} Iobject;
126127

@@ -424,14 +425,11 @@ O_cwrite(PyObject *self, char *c, int l) {
424425

425426
static PyObject *
426427
O_write(Oobject *self, PyObject *args) {
427-
PyObject *s;
428428
char *c;
429429
int l;
430430

431-
UNLESS (PyArg_ParseTuple(args, "O:write", &s)) return NULL;
431+
UNLESS (PyArg_ParseTuple(args, "s#:write", &c, &l)) return NULL;
432432

433-
UNLESS (-1 != (l=PyString_Size(s))) return NULL;
434-
UNLESS (c=PyString_AsString(s)) return NULL;
435433
if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
436434

437435
Py_INCREF(Py_None);
@@ -713,13 +711,11 @@ newIobject(PyObject *s) {
713711
char *buf;
714712
int size;
715713

716-
if (!PyString_Check(s)) {
717-
PyErr_Format(PyExc_TypeError, "expected string, %.200s found",
714+
if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
715+
PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
718716
s->ob_type->tp_name);
719717
return NULL;
720718
}
721-
buf = PyString_AS_STRING(s);
722-
size = PyString_GET_SIZE(s);
723719
UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
724720
Py_INCREF(s);
725721
self->buf=buf;

0 commit comments

Comments
 (0)