pytest 无法在 Python 2.7 下的 README 中的 doctest 中处理 Unicode
pytest can't handle Unicode in doctest in README under Python 2.7
我有一个 README.rst
文件,其中包含我的 Python 库的多个文档测试。它们都有效,除了最后一个 doctest,print
s 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 下设置 运行ning 没有任何改变;将 doctest_encoding = utf-8
添加到 tox.ini
的 [pytest]
部分也不会。我看不到与我的困境相关的 doctest 选项。如何在 Python 2.7 下成功测试到 运行?
更新:导致此问题的错误已在 pytest 3.6.2 中修复。
是的,print
是罪魁祸首。异常中最有趣的部分是:
def getvalue(self):
result = _SpoofOut.getvalue(self)
if encoding:
result = result.decode(encoding)
local/lib/python2.7/site-packages/_pytest/doctest.py:509:
pytest
尝试解码 unicode,因此 Python 尝试首先对其进行编码——但失败了。我认为这是 pytest 中的一个错误:它应该测试 result
是 bytes 还是 unicode:
if encoding and isinstance(result, bytes):
result = result.decode(encoding)
请将其报告给 pytest 问题跟踪器。您可以测试修复,如果它
可以发送拉取请求。
我有一个 README.rst
文件,其中包含我的 Python 库的多个文档测试。它们都有效,除了最后一个 doctest,print
s 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 下设置 运行ning 没有任何改变;将 doctest_encoding = utf-8
添加到 tox.ini
的 [pytest]
部分也不会。我看不到与我的困境相关的 doctest 选项。如何在 Python 2.7 下成功测试到 运行?
更新:导致此问题的错误已在 pytest 3.6.2 中修复。
是的,print
是罪魁祸首。异常中最有趣的部分是:
def getvalue(self):
result = _SpoofOut.getvalue(self)
if encoding:
result = result.decode(encoding)
local/lib/python2.7/site-packages/_pytest/doctest.py:509:
pytest
尝试解码 unicode,因此 Python 尝试首先对其进行编码——但失败了。我认为这是 pytest 中的一个错误:它应该测试 result
是 bytes 还是 unicode:
if encoding and isinstance(result, bytes):
result = result.decode(encoding)
请将其报告给 pytest 问题跟踪器。您可以测试修复,如果它 可以发送拉取请求。