diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-11-20 22:06:22 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-11-20 22:06:22 (GMT) |
commit | ad100c3acb566a8a9a6094a9ace96c6399fab394 (patch) | |
tree | 21dff80c1f818f11ab0bc4844f02fde2e30e2c10 | |
parent | d3b5a7985d81fd28fe970ab5d7429fa57e723e2c (diff) | |
download | cpython-ad100c3acb566a8a9a6094a9ace96c6399fab394.zip cpython-ad100c3acb566a8a9a6094a9ace96c6399fab394.tar.gz cpython-ad100c3acb566a8a9a6094a9ace96c6399fab394.tar.bz2 |
backport r67300
-rw-r--r-- | Lib/io.py | 25 | ||||
-rw-r--r-- | Lib/test/test_io.py | 31 |
2 files changed, 47 insertions, 9 deletions
@@ -238,8 +238,6 @@ def open(file, mode="r", buffering=None, encoding=None, errors=None, raise ValueError("invalid buffering size") if buffering == 0: if binary: - raw._name = file - raw._mode = mode return raw raise ValueError("can't have unbuffered text I/O") if updating: @@ -251,11 +249,8 @@ def open(file, mode="r", buffering=None, encoding=None, errors=None, else: raise ValueError("unknown mode: %r" % mode) if binary: - buffer.name = file - buffer.mode = mode return buffer text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) - text.name = file text.mode = mode return text @@ -622,6 +617,10 @@ class FileIO(_fileio._FileIO, RawIOBase): # that _fileio._FileIO inherits from io.RawIOBase (which would be hard # to do since _fileio.c is written in C). + def __init__(self, name, mode="r", closefd=True): + _fileio._FileIO.__init__(self, name, mode, closefd) + self._name = name + def close(self): _fileio._FileIO.close(self) RawIOBase.close(self) @@ -630,10 +629,6 @@ class FileIO(_fileio._FileIO, RawIOBase): def name(self): return self._name - @property - def mode(self): - return self._mode - class BufferedIOBase(IOBase): @@ -767,6 +762,14 @@ class _BufferedIOMixin(BufferedIOBase): def closed(self): return self.raw.closed + @property + def name(self): + return self.raw.name + + @property + def mode(self): + return self.raw.mode + ### Lower-level APIs ### def fileno(self): @@ -1473,6 +1476,10 @@ class TextIOWrapper(TextIOBase): def closed(self): return self.buffer.closed + @property + def name(self): + return self.buffer.name + def fileno(self): return self.buffer.fileno() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 9ef2a81..378d994 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1225,6 +1225,9 @@ class TextIOWrapperTest(unittest.TestCase): class MiscIOTest(unittest.TestCase): + def tearDown(self): + test_support.unlink(test_support.TESTFN) + def testImport__all__(self): for name in io.__all__: obj = getattr(io, name, None) @@ -1237,6 +1240,34 @@ class MiscIOTest(unittest.TestCase): self.assert_(issubclass(obj, io.IOBase)) + def test_attributes(self): + f = io.open(test_support.TESTFN, "wb", buffering=0) + self.assertEquals(f.mode, "w") + f.close() + + f = io.open(test_support.TESTFN, "U") + self.assertEquals(f.name, test_support.TESTFN) + self.assertEquals(f.buffer.name, test_support.TESTFN) + self.assertEquals(f.buffer.raw.name, test_support.TESTFN) + self.assertEquals(f.mode, "U") + self.assertEquals(f.buffer.mode, "r") + self.assertEquals(f.buffer.raw.mode, "r") + f.close() + + f = io.open(test_support.TESTFN, "w+") + self.assertEquals(f.mode, "w+") + self.assertEquals(f.buffer.mode, "r+") # Does it really matter? + self.assertEquals(f.buffer.raw.mode, "r+") + + g = io.open(f.fileno(), "wb", closefd=False) + self.assertEquals(g.mode, "w") + self.assertEquals(g.raw.mode, "w") + self.assertEquals(g.name, f.fileno()) + self.assertEquals(g.raw.name, f.fileno()) + f.close() + g.close() + + def test_main(): test_support.run_unittest(IOTest, BytesIOTest, StringIOTest, BufferedReaderTest, BufferedWriterTest, |