[Python-3000-checkins] r56232 - in python/branches/py3k-struni/Lib: io.py test/test_file.py

guido.van.rossum python-3000-checkins at python.org
Tue Jul 10 11:12:49 CEST 2007


Author: guido.van.rossum
Date: Tue Jul 10 11:12:49 2007
New Revision: 56232

Modified:
   python/branches/py3k-struni/Lib/io.py
   python/branches/py3k-struni/Lib/test/test_file.py
Log:
Add proper tests for closed files to various I/O operations,
restoring a disabled test.
This was necessary to make test_pickle.py pass.


Modified: python/branches/py3k-struni/Lib/io.py
==============================================================================
--- python/branches/py3k-struni/Lib/io.py	(original)
+++ python/branches/py3k-struni/Lib/io.py	Tue Jul 10 11:12:49 2007
@@ -157,6 +157,10 @@
     return text
 
 
+class UnsupportedOperation(ValueError, IOError):
+    pass
+
+
 class IOBase:
 
     """Base class for all I/O classes.
@@ -177,8 +181,8 @@
 
     def _unsupported(self, name: str) -> IOError:
         """Internal: raise an exception for unsupported operations."""
-        raise IOError("%s.%s() not supported" % (self.__class__.__name__,
-                                                 name))
+        raise UnsupportedOperation("%s.%s() not supported" %
+                                   (self.__class__.__name__, name))
 
     ### Positioning ###
 
@@ -327,6 +331,8 @@
         return res
 
     def __iter__(self):
+        if self.closed:
+            raise ValueError("__iter__ on closed file")
         return self
 
     def __next__(self):
@@ -348,6 +354,8 @@
         return lines
 
     def writelines(self, lines):
+        if self.closed:
+            raise ValueError("write to closed file")
         for line in lines:
             self.write(line)
 
@@ -587,8 +595,9 @@
         self.raw.flush()
 
     def close(self):
-        self.flush()
-        self.raw.close()
+        if not self.closed:
+            self.flush()
+            self.raw.close()
 
     ### Inquiries ###
 
@@ -644,6 +653,8 @@
         return self.read(n)
 
     def write(self, b):
+        if self.closed:
+            raise ValueError("write to closed file")
         n = len(b)
         newpos = self._pos + n
         self._buffer[self._pos:newpos] = b
@@ -779,6 +790,8 @@
         self._write_buf = b""
 
     def write(self, b):
+        if self.closed:
+            raise ValueError("write to closed file")
         if not isinstance(b, bytes):
             if hasattr(b, "__index__"):
                 raise TypeError("Can't write object of type %s" %
@@ -809,6 +822,8 @@
         return written
 
     def flush(self):
+        if self.closed:
+            raise ValueError("flush of closed file")
         written = 0
         try:
             while self._write_buf:
@@ -1040,6 +1055,8 @@
         return self.buffer.isatty()
 
     def write(self, s: str):
+        if self.closed:
+            raise ValueError("write to closed file")
         # XXX What if we were just reading?
         b = s.encode(self._encoding)
         if isinstance(b, str):

Modified: python/branches/py3k-struni/Lib/test/test_file.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_file.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_file.py	Tue Jul 10 11:12:49 2007
@@ -89,21 +89,31 @@
         self.assert_(f.closed)
 
     def testMethods(self):
-        methods = ['fileno', 'flush', 'isatty', '__next__', 'read', 'readinto',
-                   'readline', 'readlines', 'seek', 'tell', 'truncate',
-                   'write', '__iter__']
-        if sys.platform.startswith('atheos'):
-            methods.remove('truncate')
+        methods = [('fileno', ()),
+                   ('flush', ()),
+                   ('isatty', ()),
+                   ('__next__', ()),
+                   ('read', ()),
+                   ('write', (b"",)),
+                   ('readline', ()),
+                   ('readlines', ()),
+                   ('seek', (0,)),
+                   ('tell', ()),
+                   ('write', (b"",)),
+                   ('writelines', ([],)),
+                   ('__iter__', ()),
+                   ]
+        if not sys.platform.startswith('atheos'):
+            methods.append(('truncate', ()))
 
         # __exit__ should close the file
         self.f.__exit__(None, None, None)
         self.assert_(self.f.closed)
 
-##         for methodname in methods:
-##             method = getattr(self.f, methodname)
-##             # should raise on closed file
-##             self.assertRaises(ValueError, method)
-##         self.assertRaises(ValueError, self.f.writelines, [])
+        for methodname, args in methods:
+            method = getattr(self.f, methodname)
+            # should raise on closed file
+            self.assertRaises(ValueError, method, *args)
 
         # file is closed, __exit__ shouldn't do anything
         self.assertEquals(self.f.__exit__(None, None, None), None)


More information about the Python-3000-checkins mailing list