Skip to content

Commit 2925f30

Browse files
authored
Merge pull request #3584 from jwodder/fix-3583
Fix encoding error with `print` statements in doctests
2 parents a93ad1f + d382f3e commit 2925f30

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

changelog/3583.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix encoding error with `print` statements in doctests

src/_pytest/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ class UnicodeSpoof(_SpoofOut):
505505

506506
def getvalue(self):
507507
result = _SpoofOut.getvalue(self)
508-
if encoding:
508+
if encoding and isinstance(result, bytes):
509509
result = result.decode(encoding)
510510
return result
511511

testing/test_doctest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,22 @@ def fix_bad_unicode(text):
655655
result = testdir.runpytest(p, "--doctest-modules")
656656
result.stdout.fnmatch_lines(["* 1 passed *"])
657657

658+
def test_print_unicode_value(self, testdir):
659+
"""
660+
Test case for issue 3583: Printing Unicode in doctest under Python 2.7
661+
doesn't work
662+
"""
663+
p = testdir.maketxtfile(
664+
test_print_unicode_value=r"""
665+
Here is a doctest::
666+
667+
>>> print(u'\xE5\xE9\xEE\xF8\xFC')
668+
åéîøü
669+
"""
670+
)
671+
result = testdir.runpytest(p)
672+
result.stdout.fnmatch_lines(["* 1 passed *"])
673+
658674
def test_reportinfo(self, testdir):
659675
"""
660676
Test case to make sure that DoctestItem.reportinfo() returns lineno.

0 commit comments

Comments
 (0)