[ python-Bugs-1678647 ] Weird behavior Exception with unicode string

SourceForge.net noreply at sourceforge.net
Mon Mar 12 16:39:31 CET 2007


Bugs item #1678647, was opened at 2007-03-11 21:20
Message generated for change (Comment added) made by llucax
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1678647&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Unicode
Group: None
Status: Closed
Resolution: Wont Fix
Priority: 5
Private: No
Submitted By: Leandro Lucarella (llucax)
Assigned to: M.-A. Lemburg (lemburg)
Summary: Weird behavior Exception with unicode string

Initial Comment:
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).

----------------------------------------------------------------------

>Comment By: Leandro Lucarella (llucax)
Date: 2007-03-12 12:39

Message:
Logged In: YES 
user_id=240225
Originator: YES

> 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?

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-03-12 12:18

Message:
Logged In: YES 
user_id=849994
Originator: NO

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.

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-03-12 11:14

Message:
Logged In: YES 
user_id=849994
Originator: NO

Yes, or more precisely, the unicode string is converted to a byte string
using Python's default encoding, which is ASCII per default.

----------------------------------------------------------------------

Comment By: Leandro Lucarella (llucax)
Date: 2007-03-12 11:03

Message:
Logged In: YES 
user_id=240225
Originator: YES

But why the conversion is failing in the first place? Because just plain 7
bits ASCII is expected?

----------------------------------------------------------------------

Comment By: Hye-Shik Chang (perky)
Date: 2007-03-12 01:43

Message:
Logged In: YES 
user_id=55188
Originator: NO

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.)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1678647&group_id=5470


More information about the Python-bugs-list mailing list