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.

Author erickt
Recipients erickt
Date 2008-01-07.08:50:32
SpamBayes Score 0.018796457
Marked as misclassified No
Message-id <1199695834.32.0.627180179119.issue1753@psf.upfronthosting.co.za>
In-reply-to
Content
I was playing around with python 3's io functions, and I found that when trying to write to 
an encoded utf-16 file that TextIOWrapper.write re-writes the utf-16 bom for every string:

>>> f=open('foo', 'w', encoding='utf-16')
>>> print('1234', file=f)
>>> print('5678', file=f)
>>> open('foo', 'rb').read()
b'\xff\xfe1\x002\x003\x004\x00\xff\xfe\n\x00\xff\xfe5\x006\x007\x008\x00\xff\xfe\n\x00'
>>> open('foo', 'r', encoding='utf-16').read()
'1234\ufeff\n\ufeff5678\ufeff\n'
>>> 

With the attached patch, it appears to generate the correct file:

>>> f=open('foo', 'w', encoding='utf-16')
>>> print('1234', file=f)
>>> print('5678', file=f)
>>> open('foo', 'rb').read()
b'\xff\xfe1\x002\x003\x004\x00\n\x005\x006\x007\x008\x00\n\x00'
>>> open('foo', 'r', encoding='utf-16').read()
'1234\n5678\n'
>>>
History
Date User Action Args
2008-01-07 08:50:34ericktsetspambayes_score: 0.0187965 -> 0.018796457
recipients: + erickt
2008-01-07 08:50:34ericktsetspambayes_score: 0.0187965 -> 0.0187965
messageid: <1199695834.32.0.627180179119.issue1753@psf.upfronthosting.co.za>
2008-01-07 08:50:33ericktlinkissue1753 messages
2008-01-07 08:50:32ericktcreate