KeyError 中错误消息的新行 - Python 3.3

New line on error message in KeyError - Python 3.3

我正在通过 IDLE 使用 Python 3.3。虽然 运行 代码看起来像:

raise KeyError('This is a \n Line break')

它输出:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    raise KeyError('This is a \n Line break')
KeyError: 'This is a \n Line break'

我希望它输出带有换行符的消息,如下所示:

This is a
 Line Break

我曾尝试在 os.linesep 之前或使用 os.linesep 将其转换为字符串,但似乎没有任何效果。有什么方法可以强制消息在 IDLE 上正确显示?


如果我提出 Exception(而不是 KeyError),那么输出就是我想要的,但如果可能的话,我仍想提出 KeyError

您的问题与IDLE无关。您看到的行为全部来自 Python。 运行 当前存储库 CPython 以交互方式,从命令行,我们看到您报告的行为。

Python 3.7.0a2+ (heads/pr_3947:01eae2f721, Oct 22 2017, 14:06:43)
[MSC v.1900 32 bit (Intel)] on win32

>>> raise KeyError('This is a \n Line break')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'This is a \n Line break'
>>> s = 'This is a \n Line break'

>>> s
'This is a \n Line break'
>>> print(s)
This is a
 Line break
>>> raise Exception('This is a \n Line break')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: This is a
 Line break
>>> raise IndexError(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: This is a
 Line break
>>> try:
...   raise  KeyError('This is a \n Line break')
... except KeyError as e:
...   print(e)

'This is a \n Line break'
>>> try:
...   raise  KeyError('This is a \n Line break')
... except KeyError as e:
...   print(e.args[0])

This is a
 Line break

我不知道为什么 KeyError 的行为甚至与 IndexError 不同,但打印 e.args[0] 应该适用于所有异常。

编辑

差异的原因在this old tracker issue中给出,其中引用了KeyError源代码中的注释:

/* If args is a tuple of exactly one item, apply repr to args[0].
       This is done so that e.g. the exception raised by {}[''] prints
         KeyError: ''
       rather than the confusing
         KeyError
       alone.  The downside is that if KeyError is raised with an
explanatory
       string, that string will be displayed in quotes.  Too bad.
       If args is anything else, use the default BaseException__str__().
    */

此部分出现在 Python 源代码的 Objects/exceptions.c 中的 KeyError_str 对象定义中。

我会提到你的问题作为这种差异的另一种表现。

一种获得您想要的行为的方法:只需子类化str并覆盖__repr__:

class KeyErrorMessage(str):
    def __repr__(self): return str(self)
msg = KeyErrorMessage('Newline\nin\nkey\nerror')
raise KeyError(msg)

# Prints:
# Traceback (most recent call last):
#   ...
#   File "<ipython-input-2-dab072137773>", line 5, in <module>
#     raise KeyError(msg)
# KeyError: Newline
# in
# key
# error