New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate UTF-16 BOM if a file is open in append mode #49256
Comments
Copy/paste of message79330 from the issue bpo-4862: >>> f = open('utf16.txt', 'w', encoding='utf-16')
>>> f.write('abc')
3
>>> f.close()
>>> f = open('utf16.txt', 'a', encoding='utf-16')
>>> f.write('def')
3
>>> f.close()
>>> open('utf16.txt', 'r', encoding='utf-16').read()
'abc\ufeffdef' |
Bug is reproductible with:
With utf-7 or utf-8, no BOM is written. Note: with UTF-32, the BOM is 4 bytes long (0xff 0xfe 0x00 0x00 on |
One possible solution would be to call tell() in the TextIOWrapper |
@pitrou: You're right, but the state have to be changed for the if self._seekable and self.writable():
position = self.buffer.tell()
if position != 0:
self._encoder = self._get_encoder()
self._encoder.setstate(0) |
As for the C version of the patch:
|
tell() requires self->ok=1. I choosed to reset self->ok to zero on error, but
self>encodefunc value have to be changed because <encoder>.setstate() changes I don't know how to get the new value of self>encodefunc. I will try to write |
Le jeudi 26 mars 2009 à 17:26 +0000, STINNER Victor a écrit :
You are calling tell() on the underlying binary buffer, not on the
I know, but then the logic should probably be changed and use an |
Faster patch!
|
Hum, it's a detail, but is it a good idea to keep the reference the int(0)? Or |
It's better now, although I think it's not good to duplicate the Also, I'm adding Amaury to the nosy list so that he tells us whether he |
Here is a new patch catering to more cases (seek()) in addition to just |
seek() has also the problem? It's really hard to encode UTF-16/32 |
Updated patch against py3k. |
If no-one objects, I'm going to commit this in a couple of days. |
As I said on IRC a few days ago, I think the patch is ready to go. |
Ok, committed in r72635. |
Shouldn't this fix be back-ported to the 2.6 branch too? |
I fixed bpo-6213 in 2.6 and 2.7, and so it's now possible to backport this fix to 2.6 => r83200 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: