From 0603d1d500c14a67715e486568b50f15b2086616 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 5 Jul 2017 21:54:33 +0200 Subject: [PATCH] capture: ensure name of EncodedFile being a string Fixes https://github.com/pytest-dev/pytest/issues/2555. --- _pytest/capture.py | 5 +++++ changelog/2555.bugfix | 1 + testing/test_capture.py | 10 ++++++++++ 3 files changed, 16 insertions(+) create mode 100644 changelog/2555.bugfix diff --git a/_pytest/capture.py b/_pytest/capture.py index b627e5102..a4171f0fa 100644 --- a/_pytest/capture.py +++ b/_pytest/capture.py @@ -254,6 +254,11 @@ class EncodedFile(object): data = ''.join(linelist) self.write(data) + @property + def name(self): + """Ensure that file.name is a string.""" + return repr(self.buffer) + def __getattr__(self, name): return getattr(object.__getattribute__(self, "buffer"), name) diff --git a/changelog/2555.bugfix b/changelog/2555.bugfix new file mode 100644 index 000000000..8c20bbc67 --- /dev/null +++ b/changelog/2555.bugfix @@ -0,0 +1 @@ +capture: ensure that EncodedFile.name is a string. diff --git a/testing/test_capture.py b/testing/test_capture.py index 313819a96..4dd5d8e09 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -716,13 +716,21 @@ def test_dupfile(tmpfile): assert nf not in flist print(i, end="", file=nf) flist.append(nf) + + fname_open = flist[0].name + assert fname_open == repr(flist[0].buffer) + for i in range(5): f = flist[i] f.close() + fname_closed = flist[0].name + assert fname_closed == repr(flist[0].buffer) + assert fname_closed != fname_open tmpfile.seek(0) s = tmpfile.read() assert "01234" in repr(s) tmpfile.close() + assert fname_closed == repr(flist[0].buffer) def test_dupfile_on_bytesio(): @@ -730,6 +738,7 @@ def test_dupfile_on_bytesio(): f = capture.safe_text_dupfile(io, "wb") f.write("hello") assert io.getvalue() == b"hello" + assert 'BytesIO object' in f.name def test_dupfile_on_textio(): @@ -737,6 +746,7 @@ def test_dupfile_on_textio(): f = capture.safe_text_dupfile(io, "wb") f.write("hello") assert io.getvalue() == "hello" + assert not hasattr(f, 'name') @contextlib.contextmanager