From 814dfde4735e07fb31498b784098c20e4761e60e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 29 Feb 2020 17:59:38 +0100 Subject: [PATCH 1/4] Re-harden test_typeerror_encodedfile_write Changed in 60ceec6eb. This ensures the crash gets reported for the `sys.stdout.write`. --- testing/test_capture.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index 7396fd26076..f75fac6c08d 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1533,7 +1533,10 @@ def test_capture({0}): def test_typeerror_encodedfile_write(testdir): - """It should behave the same with and without output capturing (#4861).""" + """It should behave the same with and without output capturing (#4861). + + The reported location differs however. + """ p = testdir.makepyfile( """ def test_fails(): @@ -1541,12 +1544,24 @@ def test_fails(): sys.stdout.write(b"foo") """ ) - result_without_capture = testdir.runpytest("-s", str(p)) result_with_capture = testdir.runpytest(str(p)) - - assert result_with_capture.ret == result_without_capture.ret result_with_capture.stdout.fnmatch_lines( - ["E * TypeError: write() argument must be str, not bytes"] + [ + '> sys.stdout.write(b"foo")', + "> raise TypeError*", + "E TypeError: write() argument must be str, not bytes", + "FAILED test_typeerror_encodedfile_write.py:3::test_fails" + " - TypeError: write() argument must be str, not bytes", + ] + ) + result_without_capture = testdir.runpytest("-s", str(p)) + result_without_capture.stdout.fnmatch_lines( + [ + '> sys.stdout.write(b"foo")', + "E TypeError: write() argument must be str, not bytes", + "FAILED test_typeerror_encodedfile_write.py:3::test_fails" + " - TypeError: write() argument must be str, not bytes", + ] ) From 7b8d678c1580c8100de864930aaa7a7d32a5ceda Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 8 Mar 2020 02:24:20 +0100 Subject: [PATCH 2/4] No line numbers here --- testing/test_capture.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index f75fac6c08d..53da43018da 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1550,8 +1550,7 @@ def test_fails(): '> sys.stdout.write(b"foo")', "> raise TypeError*", "E TypeError: write() argument must be str, not bytes", - "FAILED test_typeerror_encodedfile_write.py:3::test_fails" - " - TypeError: write() argument must be str, not bytes", + "FAILED test_typeerror_encodedfile_write.py::test_fails - *", ] ) result_without_capture = testdir.runpytest("-s", str(p)) @@ -1559,8 +1558,7 @@ def test_fails(): [ '> sys.stdout.write(b"foo")', "E TypeError: write() argument must be str, not bytes", - "FAILED test_typeerror_encodedfile_write.py:3::test_fails" - " - TypeError: write() argument must be str, not bytes", + "FAILED test_typeerror_encodedfile_write.py::test_fails - *", ] ) From 7a878720c2294227d0e9eaf96f9d534c0c2d80fc Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 8 Mar 2020 02:41:27 +0100 Subject: [PATCH 3/4] adjust for py38-- --- testing/test_capture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index 53da43018da..73ec82564ed 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1548,7 +1548,7 @@ def test_fails(): result_with_capture.stdout.fnmatch_lines( [ '> sys.stdout.write(b"foo")', - "> raise TypeError*", + " def write*", "E TypeError: write() argument must be str, not bytes", "FAILED test_typeerror_encodedfile_write.py::test_fails - *", ] From 6488e39d6d252cb1defe5f8a1698d3329ddc406f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 8 Mar 2020 13:07:13 +0100 Subject: [PATCH 4/4] fix for pypy3 --- testing/test_capture.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/testing/test_capture.py b/testing/test_capture.py index 73ec82564ed..978981e0093 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1554,10 +1554,14 @@ def test_fails(): ] ) result_without_capture = testdir.runpytest("-s", str(p)) + if getattr(sys, "pypy_version_info", None): + exp_exc = "TypeError: unicode argument expected, got 'bytes'" + else: + exp_exc = "TypeError: write() argument must be str, not bytes" result_without_capture.stdout.fnmatch_lines( [ '> sys.stdout.write(b"foo")', - "E TypeError: write() argument must be str, not bytes", + "E " + exp_exc, "FAILED test_typeerror_encodedfile_write.py::test_fails - *", ] )