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): ...@@ -89,6 +89,13 @@ class AutoFileTests(unittest.TestCase):
self.assertRaises(TypeError, self.f.writelines, self.assertRaises(TypeError, self.f.writelines,
[NonString(), NonString()]) [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): def testRepr(self):
# verify repr works # verify repr works
self.assertTrue(repr(self.f).startswith("<open file '" + TESTFN)) self.assertTrue(repr(self.f).startswith("<open file '" + TESTFN))
......
...@@ -10,6 +10,9 @@ What's New in Python 2.7.7? ...@@ -10,6 +10,9 @@ What's New in Python 2.7.7?
Core and Builtins 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 #20437: Fixed 43 potential bugs when deleting objects references.
- Issue #21134: Fix segfault when str is called on an uninitialized - Issue #21134: Fix segfault when str is called on an uninitialized
......
...@@ -1941,13 +1941,13 @@ file_writelines(PyFileObject *f, PyObject *seq) ...@@ -1941,13 +1941,13 @@ file_writelines(PyFileObject *f, PyObject *seq)
PyObject *v = PyList_GET_ITEM(list, i); PyObject *v = PyList_GET_ITEM(list, i);
if (!PyString_Check(v)) { if (!PyString_Check(v)) {
const char *buffer; const char *buffer;
if (((f->f_binary && int res;
PyObject_AsReadBuffer(v, if (f->f_binary) {
(const void**)&buffer, res = PyObject_AsReadBuffer(v, (const void**)&buffer, &len);
&len)) || } else {
PyObject_AsCharBuffer(v, res = PyObject_AsCharBuffer(v, &buffer, &len);
&buffer, }
&len))) { if (res) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"writelines() argument must be a sequence of strings"); "writelines() argument must be a sequence of strings");
goto error; 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