Author snaury
Recipients snaury
Date 2016-05-29.17:41:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
There's a regression in file.writelines behavior for binary files when writing unicode strings, which seems to have first appeared in Python 2.7.7. The problem is that when writing unicode strings the internal representation (UCS2 or UCS4) is written instead of the actual text, which also directly contradicts documentation, which states "This is equivalent to calling write() for each string". However on Python 2.7.7+ they are no longer equivalent:

>>> open('testfile.bin', 'wb').writelines([u'Hello, world!'])
>>> open('testfile.bin', 'rb').read()
'H\x00e\x00l\x00l\x00o\x00,\x00 \x00w\x00o\x00r\x00l\x00d\x00!\x00'
>>> open('testfile.bin', 'wb').write(u'Hello, world!')
>>> open('testfile.bin', 'rb').read()
'Hello, world!'

This code worked correctly no Python 2.7.6.
Date User Action Args
2016-05-29 17:41:19snaurysetrecipients: + snaury
2016-05-29 17:41:19snaurysetmessageid: <>
2016-05-29 17:41:19snaurylinkissue27154 messages
2016-05-29 17:41:19snaurycreate