From ba9a76fdb33de948113b0e1908c9a8f95fde1627 Mon Sep 17 00:00:00 2001 From: Llandy Riveron Del Risco Date: Sat, 15 Jul 2017 16:15:26 +0200 Subject: [PATCH] Provides encoding attribute on CaptureIO Fix #2375 --- AUTHORS | 2 ++ _pytest/compat.py | 11 ++++++++++- changelog/2375.trivial | 1 + testing/test_capture.py | 9 +++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelog/2375.trivial diff --git a/AUTHORS b/AUTHORS index ca282870f..aa4474d09 100644 --- a/AUTHORS +++ b/AUTHORS @@ -92,6 +92,7 @@ Kevin Cox Kodi B. Arfer Lee Kamentsky Lev Maximov +Llandy Riveron Del Risco Loic Esteve Lukas Bednar Luke Murphy @@ -165,3 +166,4 @@ Vitaly Lashmanov Vlad Dragos Wouter van Ackooy Xuecong Liao +Zoltán Máté diff --git a/_pytest/compat.py b/_pytest/compat.py index 269a23504..7fae7f252 100644 --- a/_pytest/compat.py +++ b/_pytest/compat.py @@ -283,7 +283,16 @@ def _setup_collect_fakemodule(): if _PY2: - from py.io import TextIO as CaptureIO + # Without this the test_dupfile_on_textio will fail, otherwise CaptureIO could directly inherit from StringIO. + from py.io import TextIO + + + class CaptureIO(TextIO): + + @property + def encoding(self): + return getattr(self, '_encoding', 'UTF-8') + else: import io diff --git a/changelog/2375.trivial b/changelog/2375.trivial new file mode 100644 index 000000000..a73ab6ccf --- /dev/null +++ b/changelog/2375.trivial @@ -0,0 +1 @@ +Provides encoding attribute on CaptureIO. diff --git a/testing/test_capture.py b/testing/test_capture.py index 8f6f2ccb2..2d0514e16 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1037,6 +1037,15 @@ def test_capture_not_started_but_reset(): capsys.stop_capturing() +def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): + test_text = 'test text' + + print(test_text.encode(sys.stdout.encoding, 'replace')) + (out, err) = capsys.readouterr() + assert out + assert err == '' + + @needsosdup @pytest.mark.parametrize('use', [True, False]) def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):