Skip to content

Commit

Permalink
capture: ensure name of EncodedFile being a string
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jul 5, 2017
1 parent db24a3b commit 1264971
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ def writelines(self, linelist):
self.write(data)

def __getattr__(self, name):
if name == 'name':
return '{!r}'.format(self.buffer)
return getattr(object.__getattribute__(self, "buffer"), name)


Expand Down
10 changes: 10 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,25 +715,35 @@ 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 f.name.startswith('<_io.BytesIO object at')

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 1264971

Please sign in to comment.