Skip to content

Commit

Permalink
Merge pull request #2557 from blueyed/EncodedFile-name
Browse files Browse the repository at this point in the history
capture: ensure name of EncodedFile being a string
  • Loading branch information
nicoddemus authored Jul 26, 2017
2 parents 2c2cf81 + 0603d1d commit dd294aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ def writelines(self, linelist):
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)

Expand Down
1 change: 1 addition & 0 deletions changelog/2555.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
capture: ensure that EncodedFile.name is a string.
10 changes: 10 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,27 +716,37 @@ 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():
io = py.io.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():
io = py.io.TextIO()
f = capture.safe_text_dupfile(io, "wb")
f.write("hello")
assert io.getvalue() == "hello"
assert not hasattr(f, 'name')


@contextlib.contextmanager
Expand Down

0 comments on commit dd294aa

Please sign in to comment.