[Python-checkins] cpython (2.7): Issue #21350: Fix file.writelines() to accept arbitrary buffer objects, as
antoine.pitrou
python-checkins at python.org
Thu May 8 19:26:13 CEST 2014
http://hg.python.org/cpython/rev/db842f730432
changeset: 90591:db842f730432
branch: 2.7
parent: 90579:faef1da30c6d
user: Antoine Pitrou <solipsis at pitrou.net>
date: Thu May 08 19:26:04 2014 +0200
summary:
Issue #21350: Fix file.writelines() to accept arbitrary buffer objects, as advertised.
Patch by Brian Kearns.
files:
Lib/test/test_file2k.py | 7 +++++++
Misc/NEWS | 3 +++
Objects/fileobject.c | 14 +++++++-------
3 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py
--- a/Lib/test/test_file2k.py
+++ b/Lib/test/test_file2k.py
@@ -89,6 +89,13 @@
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))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
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
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1941,13 +1941,13 @@
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;
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list