【问题标题】:pytest can't handle Unicode in doctest in README under Python 2.7pytest 无法在 Python 2.7 下的 README 中处理 doctest 中的 Unicode
【发布时间】: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


【解决方案1】:

是的,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 是字节还是 unicode:

    if encoding and isinstance(result, bytes):
        result = result.decode(encoding)

请将其报告给 pytest 问题跟踪器。您可以测试修复程序,如果它 你可以发送一个拉取请求。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多