From 5221a147640ca2da975c32a238d27b295cc3e095 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Thu, 14 Jun 2018 20:00:38 +0000 Subject: [PATCH 1/2] Failing test case for #3583 --- testing/test_doctest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 8ef7cfd65..7f3aff3b0 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -655,6 +655,22 @@ class TestDoctests(object): result = testdir.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines(["* 1 passed *"]) + def test_print_unicode_value(self, testdir): + """ + Test case for issue 3583: Printing Unicode in doctest under Python 2.7 + doesn't work + """ + p = testdir.maketxtfile( + test_print_unicode_value=r""" + Here is a doctest:: + + >>> print(u'\xE5\xE9\xEE\xF8\xFC') + åéîøü + """ + ) + result = testdir.runpytest(p) + result.stdout.fnmatch_lines(["* 1 passed *"]) + def test_reportinfo(self, testdir): """ Test case to make sure that DoctestItem.reportinfo() returns lineno. From d382f3e77ffe6db49ebc6a93f81fcdeea8ea5458 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Thu, 14 Jun 2018 19:05:07 +0000 Subject: [PATCH 2/2] [#3583] Fix encoding error with `print` statements in doctests This fix was suggested by Stack Overflow user phd in . --- changelog/3583.bugfix.rst | 1 + src/_pytest/doctest.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changelog/3583.bugfix.rst diff --git a/changelog/3583.bugfix.rst b/changelog/3583.bugfix.rst new file mode 100644 index 000000000..cfa662580 --- /dev/null +++ b/changelog/3583.bugfix.rst @@ -0,0 +1 @@ +Fix encoding error with `print` statements in doctests diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 3b58f955f..b0a3ad08d 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -505,7 +505,7 @@ def _fix_spoof_python2(runner, encoding): def getvalue(self): result = _SpoofOut.getvalue(self) - if encoding: + if encoding and isinstance(result, bytes): result = result.decode(encoding) return result