Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions _pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def collect(self):
name = self.fspath.basename
globs = {'__name__': '__main__'}


optionflags = get_optionflags(self)
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags,
checker=_get_checker())
Expand Down Expand Up @@ -218,9 +217,6 @@ def collect(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)
Expand Down Expand Up @@ -332,7 +328,10 @@ def _get_report_choice(key):

def _fix_spoof_python2(runner, encoding):
"""
Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output.
Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output. This
should patch only doctests for text files because they don't have a way to declare their
encoding. Doctests in docstrings from Python modules don't have the same problem given that
Python already decoded the strings.

This fixes the problem related in issue #2434.
"""
Expand Down
1 change: 1 addition & 0 deletions changelog/2434.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix decode error in Python 2 for doctests in docstrings.
19 changes: 19 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,25 @@ def test_unicode_doctest(self, testdir):
'*1 failed*',
])

def test_unicode_doctest_module(self, testdir):
"""
Test case for issue 2434: DecodeError on Python 2 when doctest docstring
contains non-ascii characters.
"""
p = testdir.makepyfile(test_unicode_doctest_module="""
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals

def fix_bad_unicode(text):
'''
>>> print(fix_bad_unicode('único'))
único
'''
return "único"
""")
result = testdir.runpytest(p, '--doctest-modules')
result.stdout.fnmatch_lines(['* 1 passed *'])


class TestLiterals(object):

Expand Down