Merge pull request #2438 from nicoddemus/issue-2434

Fix unicode issue while running doctests in Python 2
This commit is contained in:
Floris Bruynooghe 2017-05-29 13:36:07 +02:00 committed by GitHub
commit 6117930642
3 changed files with 54 additions and 1 deletions

View File

@ -1,7 +1,9 @@
3.1.1 (unreleased) 3.1.1 (unreleased)
================== ==================
* Fix encoding errors for unicode warnings in Python 2. * Fix encoding errors for unicode warnings in Python 2. (towncrier: 2436.bugfix)
* Fix issue with non-ascii contents in doctest text files. (towncrier: 2434.bugfix)
3.1.0 (2017-05-22) 3.1.0 (2017-05-22)

View File

@ -181,6 +181,7 @@ class DoctestTextfile(pytest.Module):
optionflags = get_optionflags(self) optionflags = get_optionflags(self)
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
checker=_get_checker()) checker=_get_checker())
_fix_spoof_python2(runner, encoding)
parser = doctest.DocTestParser() parser = doctest.DocTestParser()
test = parser.get_doctest(text, globs, name, filename, 0) test = parser.get_doctest(text, globs, name, filename, 0)
@ -216,6 +217,10 @@ class DoctestModule(pytest.Module):
optionflags = get_optionflags(self) optionflags = get_optionflags(self)
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
checker=_get_checker()) checker=_get_checker())
encoding = self.config.getini("doctest_encoding")
_fix_spoof_python2(runner, encoding)
for test in finder.find(module, module.__name__): for test in finder.find(module, module.__name__):
if test.examples: # skip empty doctests if test.examples: # skip empty doctests
yield DoctestItem(test.name, self, runner, test) yield DoctestItem(test.name, self, runner, test)
@ -324,6 +329,30 @@ def _get_report_choice(key):
DOCTEST_REPORT_CHOICE_NONE: 0, DOCTEST_REPORT_CHOICE_NONE: 0,
}[key] }[key]
def _fix_spoof_python2(runner, encoding):
"""
Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output.
This fixes the problem related in issue #2434.
"""
from _pytest.compat import _PY2
if not _PY2:
return
from doctest import _SpoofOut
class UnicodeSpoof(_SpoofOut):
def getvalue(self):
result = _SpoofOut.getvalue(self)
if encoding:
result = result.decode(encoding)
return result
runner._fakeout = UnicodeSpoof()
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def doctest_namespace(): def doctest_namespace():
""" """

View File

@ -505,6 +505,28 @@ class TestDoctests(object):
"--junit-xml=junit.xml") "--junit-xml=junit.xml")
reprec.assertoutcome(failed=1) reprec.assertoutcome(failed=1)
def test_unicode_doctest(self, testdir):
"""
Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii
characters.
"""
p = testdir.maketxtfile(test_unicode_doctest="""
.. doctest::
>>> print(
... "Hi\\n\\nByé")
Hi
...
Byé
>>> 1/0 # Byé
1
""")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines([
'*UNEXPECTED EXCEPTION: ZeroDivisionError*',
'*1 failed*',
])
class TestLiterals(object): class TestLiterals(object):