Merge pull request #2438 from nicoddemus/issue-2434
Fix unicode issue while running doctests in Python 2
This commit is contained in:
commit
6117930642
|
@ -1,7 +1,9 @@
|
|||
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)
|
||||
|
|
|
@ -181,6 +181,7 @@ class DoctestTextfile(pytest.Module):
|
|||
optionflags = get_optionflags(self)
|
||||
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
|
||||
checker=_get_checker())
|
||||
_fix_spoof_python2(runner, encoding)
|
||||
|
||||
parser = doctest.DocTestParser()
|
||||
test = parser.get_doctest(text, globs, name, filename, 0)
|
||||
|
@ -216,6 +217,10 @@ class DoctestModule(pytest.Module):
|
|||
optionflags = get_optionflags(self)
|
||||
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
|
||||
checker=_get_checker())
|
||||
|
||||
encoding = self.config.getini("doctest_encoding")
|
||||
_fix_spoof_python2(runner, encoding)
|
||||
|
||||
for test in finder.find(module, module.__name__):
|
||||
if test.examples: # skip empty doctests
|
||||
yield DoctestItem(test.name, self, runner, test)
|
||||
|
@ -324,6 +329,30 @@ def _get_report_choice(key):
|
|||
DOCTEST_REPORT_CHOICE_NONE: 0,
|
||||
}[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')
|
||||
def doctest_namespace():
|
||||
"""
|
||||
|
|
|
@ -505,6 +505,28 @@ class TestDoctests(object):
|
|||
"--junit-xml=junit.xml")
|
||||
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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue