Message266605
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:19 | snaury | set | recipients:
+ snaury |
2016-05-29 17:41:19 | snaury | set | messageid: <1464543679.71.0.415502254928.issue27154@psf.upfronthosting.co.za> |
2016-05-29 17:41:19 | snaury | link | issue27154 messages |
2016-05-29 17:41:19 | snaury | create | |
|