From 5e15c86cc6fb800adf87e5eff0a36896b2cb895e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Gaven=C4=8Diak?= Date: Fri, 24 Jan 2020 13:50:56 +0100 Subject: [PATCH] Fix EncodedFile.write return value Make EncodedFile, used for captured output streams, method .write return the number of characters written. Add test for captured stderr write. Fixes #6557. Co-Authored-By: Bruno Oliveira --- AUTHORS | 1 + changelog/6557.bugfix.rst | 1 + src/_pytest/capture.py | 2 +- testing/test_capture.py | 5 +++++ 4 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/6557.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 6288f8c1b..aa2237c68 100644 --- a/AUTHORS +++ b/AUTHORS @@ -256,6 +256,7 @@ Tim Hoffmann Tim Strazny Tom Dalton Tom Viner +Tomáš Gavenčiak Tomer Keren Trevor Bekolay Tyler Goodlet diff --git a/changelog/6557.bugfix.rst b/changelog/6557.bugfix.rst new file mode 100644 index 000000000..fb7527d99 --- /dev/null +++ b/changelog/6557.bugfix.rst @@ -0,0 +1 @@ +Make capture output streams ``.write()`` method return the same return value from original streams. diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 0cd3ce604..c79bfeef0 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -424,7 +424,7 @@ class EncodedFile: raise TypeError( "write() argument must be str, not {}".format(type(obj).__name__) ) - self.buffer.write(obj) + return self.buffer.write(obj) def writelines(self, linelist): data = "".join(linelist) diff --git a/testing/test_capture.py b/testing/test_capture.py index 27f8d7d56..7d459e91c 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1492,3 +1492,8 @@ def test_typeerror_encodedfile_write(testdir): result_with_capture.stdout.fnmatch_lines( ["E * TypeError: write() argument must be str, not bytes"] ) + + +def test_stderr_write_returns_len(capsys): + """Write on Encoded files, namely captured stderr, should return number of characters written.""" + assert sys.stderr.write("Foo") == 3