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.

classification
Title: Weird behavior Exception with unicode string
Type: Stage:
Components: Unicode Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: lemburg Nosy List: georg.brandl, hyeshik.chang, lemburg, llucax
Priority: normal Keywords:

Created on 2007-03-12 00:20 by llucax, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg31493 - (view) Author: Leandro Lucarella (llucax) Date: 2007-03-12 00:20
This simple python program:
-------------------------------
raise Exception(u'Muri\u00f3')
-------------------------------

prints:
-----------------------------------
Traceback (most recent call last):
  File "pylog.py", line 1, in ?
    raise Exception(u'Muri\u00f3')
Exception
-----------------------------------

While not using an unicode character, seems to work fine (even if using an unicode parameter):
--------------------------
raise Exception(u'Murio')
--------------------------

prints:
-----------------------------------
Traceback (most recent call last):
  File "pylog.py", line 1, in ?
    raise Exception(u'Murio')
Exception: Murio
-----------------------------------

This seems to break the logging module when calling logging.exception(u'Muri\u00f3'), for example.

Tested in Python 2.4.4 and 2.5 (debian).
msg31494 - (view) Author: Hyeshik Chang (hyeshik.chang) * (Python committer) Date: 2007-03-12 04:43
That's from line 1216-1230 of Python/pythonrun.c as in trunk.
It tries PyObject_Str(exception) and skips printing line terminator when it failed.
And the behavior came from r8084 (http://svn.python.org/view/python/trunk/Python/pythonrun.c?rev=8084&r1=8070&r2=8084).

Hmm. I think it should put a LF even if the conversion failed. (encoding the unicode with "ignore" or "replace" would make some confusion and putting it in PyObject_Repr() makes inconsistency.)
msg31495 - (view) Author: Leandro Lucarella (llucax) Date: 2007-03-12 14:03
But why the conversion is failing in the first place? Because just plain 7 bits ASCII is expected?
msg31496 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-12 14:14
Yes, or more precisely, the unicode string is converted to a byte string using Python's default encoding, which is ASCII per default.
msg31497 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-12 15:18
I fixed the "prints no newline" problem in rev. 54288.
There's nothing sensible we can do about the unicode conversion, so closing as "won't fix".

You should always encode or repr() unicode exception arguments.
msg31498 - (view) Author: Leandro Lucarella (llucax) Date: 2007-03-12 15:39
> You should always encode or repr() unicode exception arguments.

That's odd... :S

At least it's any way to change the default Python encoding?
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44700
2007-03-12 00:20:55llucaxcreate