UnicodeError instead of UnicodeWarning

Peter Otten __peter__ at web.de
Tue Oct 25 04:22:11 EDT 2011


Michael Ströder wrote:
> For tracking the cause of a UnicodeWarning I'd like to make the Python
> interpreter to raise an UnicodeError exception with full stack trace. Is
> there a short trick to achieve this?

You can control warning behaviour with the -W commandline option:
$ cat unicodewarning_demo.py
# -*- coding: utf-8 -*-
def g():
 "ä" == u"ä"
def f(n=3):
 if n:
 f(n-1)
 else:
 g()
if __name__ == "__main__":
 f()
$ python unicodewarning_demo.py
unicodewarning_demo.py:4: UnicodeWarning: Unicode equal comparison failed to 
convert both arguments to Unicode - interpreting them as being unequal
 "ä" == u"ä"
$ python -W error::UnicodeWarning unicodewarning_demo.py
Traceback (most recent call last):
 File "unicodewarning_demo.py", line 13, in <module>
 f()
 File "unicodewarning_demo.py", line 8, in f
 f(n-1)
 File "unicodewarning_demo.py", line 8, in f
 f(n-1)
 File "unicodewarning_demo.py", line 8, in f
 f(n-1)
 File "unicodewarning_demo.py", line 10, in f
 g()
 File "unicodewarning_demo.py", line 4, in g
 "ä" == u"ä"
UnicodeWarning: Unicode equal comparison failed to convert both arguments to 
Unicode - interpreting them as being unequal
$


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /