【发布时间】:2018-06-14 14:41:13
【问题描述】:
我有一个README.rst 文件,其中包含我的 Python 库的多个文档测试。它们都可以工作,除了最后一个 doctest,prints Unicode 输出,以 UTF-8 编码:
Here is a failing example::
>>> print(u'\xE5\xE9\xEE\xF8\xFC')
åéîøü
(使用print 而不仅仅是一个字符串对我的实际用例非常重要,因为真正的字符串包含嵌入的换行符,我需要展示不同行上的内容是如何对齐的。)
运行pytest README.rst 在 Python 3.6.5 和 pytest 3.6.1 上成功运行,但在 Python 2.7.10 下,它会失败并以很长的回溯结束:
input = 'åéîøü
', errors = 'strict'
def decode(input, errors='strict'):
> return codecs.utf_8_decode(input, errors, True)
E UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py:16: UnicodeEncodeError
在tox.ini 中设置setenv = LC_ALL=en_US.UTF-8 并在tox 下运行没有任何改变;也不会将doctest_encoding = utf-8 添加到tox.ini 的[pytest] 部分。我没有看到与我的困境相关的 doctest 选项。如何让测试在 Python 2.7 下成功运行?
更新:导致此问题的错误已在 pytest 3.6.2 中修复。
【问题讨论】:
-
使用 UTF-8 test.rst 对我有用,即使我的语言环境是 ru_RU.KOI8-R。无需额外配置。 Python 2.7.13,pytest 3.6.1。请展示一个失败的例子。
-
@phd:添加了示例。我认为这里的关键可能是
print的使用。
标签: python python-2.7 unicode pytest doctest