[Python-3000-checkins] r67300 - in python/branches/py3k/Lib: io.py test/test_io.py

barry.warsaw python-3000-checkins at python.org
Thu Nov 20 21:14:51 CET 2008


Author: barry.warsaw
Date: Thu Nov 20 21:14:50 2008
New Revision: 67300

Log:
Fix for bug 4362 "FileIO object in io module";  Patch by amaury.forgeotdarc.


Modified:
   python/branches/py3k/Lib/io.py
   python/branches/py3k/Lib/test/test_io.py

Modified: python/branches/py3k/Lib/io.py
==============================================================================
--- python/branches/py3k/Lib/io.py	(original)
+++ python/branches/py3k/Lib/io.py	Thu Nov 20 21:14:50 2008
@@ -239,8 +239,6 @@
         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:
@@ -252,11 +250,8 @@
     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
 
@@ -616,6 +611,10 @@
     # 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)
@@ -624,11 +623,6 @@
     def name(self):
         return self._name
 
-    # XXX(gb): _FileIO already has a mode property
-    @property
-    def mode(self):
-        return self._mode
-
 
 class BufferedIOBase(IOBase):
 
@@ -762,6 +756,14 @@
     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):
@@ -1464,6 +1466,10 @@
     def closed(self):
         return self.buffer.closed
 
+    @property
+    def name(self):
+        return self.buffer.name
+
     def fileno(self):
         return self.buffer.fileno()
 

Modified: python/branches/py3k/Lib/test/test_io.py
==============================================================================
--- python/branches/py3k/Lib/test/test_io.py	(original)
+++ python/branches/py3k/Lib/test/test_io.py	Thu Nov 20 21:14:50 2008
@@ -1249,6 +1249,9 @@
 
 class MiscIOTest(unittest.TestCase):
 
+    def tearDown(self):
+        support.unlink(support.TESTFN)
+
     def testImport__all__(self):
         for name in io.__all__:
             obj = getattr(io, name, None)
@@ -1261,6 +1264,34 @@
                 self.assert_(issubclass(obj, io.IOBase))
 
 
+    def test_attributes(self):
+        f = io.open(support.TESTFN, "wb", buffering=0)
+        self.assertEquals(f.mode, "w")
+        f.close()
+
+        f = io.open(support.TESTFN, "U")
+        self.assertEquals(f.name,            support.TESTFN)
+        self.assertEquals(f.buffer.name,     support.TESTFN)
+        self.assertEquals(f.buffer.raw.name, 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(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():
     support.run_unittest(IOTest, BytesIOTest, StringIOTest,
                               BufferedReaderTest, BufferedWriterTest,


More information about the Python-3000-checkins mailing list