Skip to content

capture: ensure name of EncodedFile being a string #2557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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