Merge pull request #2557 from blueyed/EncodedFile-name

capture: ensure name of EncodedFile being a string
This commit is contained in:
Bruno Oliveira 2017-07-26 10:39:10 -03:00 committed by GitHub
commit dd294aafb3
3 changed files with 16 additions and 0 deletions

View File

@ -254,6 +254,11 @@ class EncodedFile(object):
data = ''.join(linelist) data = ''.join(linelist)
self.write(data) self.write(data)
@property
def name(self):
"""Ensure that file.name is a string."""
return repr(self.buffer)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(object.__getattribute__(self, "buffer"), name) return getattr(object.__getattribute__(self, "buffer"), name)

1
changelog/2555.bugfix Normal file
View File

@ -0,0 +1 @@
capture: ensure that EncodedFile.name is a string.

View File

@ -716,13 +716,21 @@ def test_dupfile(tmpfile):
assert nf not in flist assert nf not in flist
print(i, end="", file=nf) print(i, end="", file=nf)
flist.append(nf) flist.append(nf)
fname_open = flist[0].name
assert fname_open == repr(flist[0].buffer)
for i in range(5): for i in range(5):
f = flist[i] f = flist[i]
f.close() f.close()
fname_closed = flist[0].name
assert fname_closed == repr(flist[0].buffer)
assert fname_closed != fname_open
tmpfile.seek(0) tmpfile.seek(0)
s = tmpfile.read() s = tmpfile.read()
assert "01234" in repr(s) assert "01234" in repr(s)
tmpfile.close() tmpfile.close()
assert fname_closed == repr(flist[0].buffer)
def test_dupfile_on_bytesio(): def test_dupfile_on_bytesio():
@ -730,6 +738,7 @@ def test_dupfile_on_bytesio():
f = capture.safe_text_dupfile(io, "wb") f = capture.safe_text_dupfile(io, "wb")
f.write("hello") f.write("hello")
assert io.getvalue() == b"hello" assert io.getvalue() == b"hello"
assert 'BytesIO object' in f.name
def test_dupfile_on_textio(): def test_dupfile_on_textio():
@ -737,6 +746,7 @@ def test_dupfile_on_textio():
f = capture.safe_text_dupfile(io, "wb") f = capture.safe_text_dupfile(io, "wb")
f.write("hello") f.write("hello")
assert io.getvalue() == "hello" assert io.getvalue() == "hello"
assert not hasattr(f, 'name')
@contextlib.contextmanager @contextlib.contextmanager