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
logging encoding failes some situation #51240
Comments
When stream is codecs.writer object, stream.write(string) does Then, fallback to utf-8 is not good.
Attached patch contains this logic, refactoring and test. |
Thanks, but I'm not sure I understand the reasoning. |
Please see and execute an attached foo.py. In Python 2.6.2, this cause following error:
>python foo.py
Traceback (most recent call last):
File "foo.py", line 3, in <module>
f.write('\xaa')
File "C:\usr\Python2.6\lib\codecs.py", line 686, in write
return self.writer.write(data)
File "C:\usr\Python2.6\lib\codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xaa in position 0:
ordinal not in ran
ge(128) |
There seems to be a problem with your foo.py. In it, you are writing a >>> import codecs
>>> f = codecs.open('foo.txt', 'w', encoding='utf-8')
>>> f.write(u'\u76F4\u6A39\u7A32\u7530')
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.read()
'\xe7\x9b\xb4\xe6\xa8\xb9\xe7\xa8\xb2\xe7\x94\xb0' As you can see, the Unicode has been converted using UTF-8. |
Another sample. Traceback (most recent call last):
File "C:\usr\Python2.6\lib\logging\__init__.py", line 790, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xaa in position 0:
ordinal not in range(128) This is because logging.FileHandler uses codecs.open internally. |
Your second example (logging_error.py) fails for the same reason - In summary: If you open a stream via codecs.open, whether directly or |
OK, you're right. And logging may be used for debbuging usually. So I think |
It's not about logging - your first example (foo.py) didn't have any The problem is caused only when someone doesn't understand how Unicode The rule is: If you use a stream without encoding and byte strings under For use anywhere, you really have to work in Unicode internally, decode If third-party libraries which you are using don't use Unicode properly, I'm closing this issue, as it's not really logging-related. Hope that's OK. |
OK, I agree. |
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: