This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bug in file.writelines accepting buffers
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bdkearns, pitrou, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2014-04-25 16:40 by bdkearns, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fix_file_writelines-py27.patch bdkearns, 2014-04-25 16:44
Messages (5)
msg217162 - (view) Author: Brian Kearns (bdkearns) * Date: 2014-04-25 16:40
In file.writelines, the conditions in this if statement are bogus. If f->f_binary and AsReadBuffer succeeds (returns 0), AsCharBuf is still tried. So, for example, passing an array('c') to a file('wb').writelines fails, when it seems the intention of the code/comments was to have it succeed.
msg218061 - (view) Author: Brian Kearns (bdkearns) * Date: 2014-05-07 16:29
So, for example:
>>> f = open('blah', 'wb')
>>> f.write(array.array('c', 'test'))
>>> f.writelines([array.array('c', 'test')])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: writelines() argument must be a sequence of strings

While the comment in writelines says:
Check that all entries are indeed strings. If not,
apply the same rules as for file.write() and
convert the results to strings.
msg218117 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-08 17:23
Thanks for the report and the patch! For a bit of trivia, this code has been there since 2000 (63ea2a2df06f).
msg218118 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-08 17:26
New changeset db842f730432 by Antoine Pitrou in branch '2.7':
Issue #21350: Fix file.writelines() to accept arbitrary buffer objects, as advertised.
http://hg.python.org/cpython/rev/db842f730432
msg266635 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-29 20:39
Unfortunately this caused a regression in unicode handling.  See issue 27154.
History
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65549
2016-05-29 20:39:44r.david.murraysetnosy: + r.david.murray
messages: + msg266635
2014-05-08 17:26:28pitrousetstatus: open -> closed
resolution: fixed
stage: resolved
2014-05-08 17:26:13python-devsetnosy: + python-dev
messages: + msg218118
2014-05-08 17:23:32pitrousetmessages: + msg218117
2014-05-07 16:29:36bdkearnssetmessages: + msg218061
2014-04-27 00:32:15vstinnersetnosy: + pitrou
2014-04-25 16:44:50bdkearnssetfiles: + fix_file_writelines-py27.patch
2014-04-25 16:44:44bdkearnssetfiles: - fix_file_writelines-py27.patch
2014-04-25 16:40:21bdkearnscreate