可能引发在python 2中包含非英语字符的异常? [英] possible to raise exception that includes non-english characters in python 2?

查看:101
本文介绍了可能引发在python 2中包含非英语字符的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python 2.7.x中引发异常,该异常在消息中包含unicode。我似乎无法使其正常工作。

I'm trying to raise exception in python 2.7.x which includes a unicode in the message. I can't seem to make it work.

是否支持或不建议在错误消息中添加unicode?
还是我需要查看sys.stderr?

Is it not supported or not recommended to include unicode in error msg? Or do i need to be looking at sys.stderr?

 # -*- coding: utf-8 -*-
 class MyException(Exception):
  def __init__(self, value):
    self.value = value
  def __str__(self):
    return self.value
  def __repr__(self):
    return self.value
  def __unicode__(self):
    return self.value

desc = u'something bad with field \u4443'

try:
  raise MyException(desc)
except MyException as e:
  print(u'Inside try block : ' + unicode(e))

# here is what i wish to make work 
raise MyException(desc)

运行脚本将产生以下输出。
在我的try / except内部,我可以毫无问题地打印字符串。

Running script produces the output below. Inside my try/except i can print the string without problem.

我的问题不在try / except之外。

My problem is outside the try/except.

Inside try block : something bad with field 䑃
Traceback (most recent call last):
  File "C:\Python27\lib\bdb.py", line 387, in run
    exec cmd in globals, locals
  File "C:\Users\ghis3080\r.py", line 25, in <module>
    raise MyException(desc)
MyException: something bad with field \u4443

谢谢。

推荐答案

行为取决于Python版本和环境。在Python 3上, sys.stderr 的字符编码错误处理程序始终为'backslashreplace'

The behaviour depends on Python version and the environment. On Python 3 the character encoding error handler for sys.stderr is always 'backslashreplace':

from __future__ import unicode_literals, print_function
import sys

s = 'unicode "\u2323" smile'
print(s)
print(s, file=sys.stderr)
try:
    raise RuntimeError(s)
except Exception as e:
    print(e.args[0])
    print(e.args[0], file=sys.stderr)
    raise

python3:

$ PYTHONIOENCODING=ascii:ignore python3 raise_unicode.py
unicode "" smile
unicode "\u2323" smile
unicode "" smile
unicode "\u2323" smile
Traceback (most recent call last):
  File "raise_unicode.py", line 8, in <module>
    raise RuntimeError(s)
RuntimeError: unicode "\u2323" smile

python2

$ PYTHONIOENCODING=ascii:ignore python2 raise_unicode.py
unicode "" smile
unicode "" smile
unicode "" smile
unicode "" smile
Traceback (most recent call last):
  File "raise_unicode.py", line 8, in <module>
    raise RuntimeError(s)
RuntimeError

这是我系统上的错误消息在python2上被吃掉了。

That is on my system the error message is eaten on python2.

注意:在Windows上,您可以尝试:

Note: on Windows you could try:

T:\> set PYTHONIOENCODING=ascii:ignore
T:\> python raise_unicode.py

作比较:

$ python3 raise_unicode.py
unicode "⌣" smile
unicode "⌣" smile
unicode "⌣" smile
unicode "⌣" smile
Traceback (most recent call last):
  File "raise_unicode.py", line 8, in <module>
    raise RuntimeError(s)
RuntimeError: unicode "⌣" smile

这篇关于可能引发在python 2中包含非英语字符的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆