在将字符串强制转换为 unicode 时,如何让 python 2.x 发出警告? [英] How can you make python 2.x warn when coercing strings to unicode?
问题描述
编码错误的一个非常常见的来源是,当您将字符串与 unicode
一起添加时,python 2 会默默地将字符串强制转换为 unicode
.这会导致混合编码问题并且很难调试.
A very common source of encoding errors is that python 2 will silently coerce strings to unicode
when you add them together with unicode
. This can cause mixed encoding problems and can be very hard to debug.
例如:
import urllib
import webbrowser
name = raw_input("What's your name?\nName: ")
greeting = "Hello, %s" % name
if name == "John":
greeting += u' (Feliz cumplea\xf1os!)'
webbrowser.open('http://lmgtf\x79.com?q=' + urllib.quote_plus(greeting))
如果您输入John",将会失败并显示一个神秘的错误:
will fail with a cryptic error if you enter "John":
/usr/lib/python2.7/urllib.py:1268: UnicodeWarning: Unicode equal comparison faile
d to convert both arguments to Unicode - interpreting them as being unequal
return ''.join(map(quoter, s))
Traceback (most recent call last):
File "feliz.py", line 7, in <module>
webbrowser.open('http://lmgtf\x79.com?q=' + urllib.quote_plus(greeting))
File "/usr/lib/python2.7/urllib.py", line 1273, in quote_plus
s = quote(s, safe + ' ')
File "/usr/lib/python2.7/urllib.py", line 1268, in quote
return ''.join(map(quoter, s))
KeyError: u'\xf1'
当实际错误远离实际强制发生的地方时,特别难以追踪.
It's particularly hard to track down when the actual errors come far down the road from where the actual coercion happened.
如何配置python在字符串被强制为unicode时立即给出警告或异常?
How can you configure python to give a warning or exception immediately when strings are coerced to unicode?
推荐答案
在提出这个问题后,我做了更多的研究并找到了完美的答案.Armin Ronacher 创建了一个很棒的小工具,名为 unicode-nazi.只需安装它并像这样运行你的程序:
I did a little more research after asking this question and hit on the perfect answer. Armin Ronacher created a wonderful little tool called unicode-nazi. Just install it and run your program like this:
python -Werror -municodenazi myprog.py
你会在强制发生的地方得到回溯:
and you get a traceback right where the coercion happened:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "SITE-PACKAGES/unicodenazi.py", line 128, in <module>
main()
File "SITE-PACKAGES/unicodenazi.py", line 119, in main
execfile(sys.argv[0], main_mod.__dict__)
File "myprog.py", line 4, in <module>
print foo()
File "myprog.py", line 2, in foo
return 'bar' + u'baz'
File "SITE-PACKAGES/unicodenazi.py", line 34, in warning_decode
stacklevel=2)
UnicodeWarning: Implicit conversion of str to unicode
<小时>
如果您正在处理本身会触发隐式强制转换的 Python 库,并且您无法捕获异常或以其他方式解决它们,则可以省略 -Werror
:
python -municodenazi myprog.py
并且至少在它发生时看到在 stderr 上打印的警告:
and at least see a warning printed out on stderr when it happens:
/SITE-PACKAGES/unicodenazi.py:119: UnicodeWarning: Implicit conversion of str to unicode
execfile(sys.argv[0], main_mod.__dict__)
barbaz
这篇关于在将字符串强制转换为 unicode 时,如何让 python 2.x 发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!