Commit b0acc1b0 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #21350: Fix file.writelines() to accept arbitrary buffer objects, as advertised.

Patch by Brian Kearns.
parent 9ba90c9f
......@@ -89,6 +89,13 @@ class AutoFileTests(unittest.TestCase):
self.assertRaises(TypeError, self.f.writelines,
[NonString(), NonString()])
def testWritelinesBuffer(self):
self.f.writelines([array('c', 'abc')])
self.f.close()
self.f = open(TESTFN, 'rb')
buf = self.f.read()
self.assertEqual(buf, 'abc')
def testRepr(self):
# verify repr works
self.assertTrue(repr(self.f).startswith("<open file '" + TESTFN))
......
......@@ -10,6 +10,9 @@ What's New in Python 2.7.7?
Core and Builtins
-----------------
- Issue #21350: Fix file.writelines() to accept arbitrary buffer objects,
as advertised. Patch by Brian Kearns.
- Issue #20437: Fixed 43 potential bugs when deleting objects references.
- Issue #21134: Fix segfault when str is called on an uninitialized
......
......@@ -1941,13 +1941,13 @@ file_writelines(PyFileObject *f, PyObject *seq)
PyObject *v = PyList_GET_ITEM(list, i);
if (!PyString_Check(v)) {
const char *buffer;
if (((f->f_binary &&
PyObject_AsReadBuffer(v,
(const void**)&buffer,
&len)) ||
PyObject_AsCharBuffer(v,
&buffer,
&len))) {
int res;
if (f->f_binary) {
res = PyObject_AsReadBuffer(v, (const void**)&buffer, &len);
} else {
res = PyObject_AsCharBuffer(v, &buffer, &len);
}
if (res) {
PyErr_SetString(PyExc_TypeError,
"writelines() argument must be a sequence of strings");
goto error;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment