[issue32723] codecs.open silently ignores argument errors

Xiang Zhang report at bugs.python.org
Tue Jan 30 05:32:34 EST 2018


Xiang Zhang <angwerzx at 126.com> added the comment:

I don't understand Josh. Looking from the code only when *passing* encoding binary mode is forced, although in the comment it's saying always.

>>> f = codecs.open('/tmp/a', 'w')
>>> f
<open file '/tmp/a', mode 'w' at 0x7f516efbb6f0>

For example I want to use 'replace' instead of 'strict' for default encoding, I can't simply do:

>>> import codecs
>>> f = codecs.open('/tmp/a', 'w', errors='replace')
>>> f.write(u'\udc80')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\udc80' in position 0: ordinal not in range(128)

I have to specify the default encoding explicitly to make errors function:

>>> f = codecs.open('/tmp/a', 'w', encoding='ascii', errors='replace')
>>> f.write(u'\udc80')

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32723>
_______________________________________


More information about the Python-bugs-list mailing list