Skip to content

Commit 5a5759c

Browse files
committed
Fix EncodedFile.writelines
This is implemented by the underlying stream already, which additionally checks if the stream is not closed, and calls `write` per line. Ref/via: pytest-dev#6558 (comment)
1 parent b687f20 commit 5a5759c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/_pytest/capture.py

-4
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,6 @@ def write(self, obj):
426426
)
427427
return self.buffer.write(obj)
428428

429-
def writelines(self, linelist):
430-
data = "".join(linelist)
431-
self.write(data)
432-
433429
@property
434430
def name(self):
435431
"""Ensure that file.name is a string."""

testing/test_capture.py

+12
Original file line numberDiff line numberDiff line change
@@ -1497,3 +1497,15 @@ def test_fails():
14971497
def test_stderr_write_returns_len(capsys):
14981498
"""Write on Encoded files, namely captured stderr, should return number of characters written."""
14991499
assert sys.stderr.write("Foo") == 3
1500+
1501+
1502+
def test_encodedfile_writelines(tmpfile):
1503+
ef = capture.EncodedFile(tmpfile, None)
1504+
with pytest.raises(TypeError):
1505+
ef.writelines(["line1", "line2"])
1506+
assert ef.writelines([b"line1", b"line2"]) is None
1507+
tmpfile.seek(0)
1508+
assert tmpfile.read() == b"line1line2"
1509+
tmpfile.close()
1510+
with pytest.raises(ValueError):
1511+
ef.read()

0 commit comments

Comments
 (0)