[pypy-commit] pypy win32-stdlib: be more careful about closing open files

mattip noreply at buildbot.pypy.org
Sun Apr 22 22:32:45 CEST 2012


Author: Matti Picus <matti.picus at gmail.com>
Branch: win32-stdlib
Changeset: r54626:36849667fd71
Date: 2012-04-22 23:27 +0300
http://bitbucket.org/pypy/pypy/changeset/36849667fd71/

Log:	be more careful about closing open files

diff --git a/lib-python/modified-2.7/tarfile.py b/lib-python/modified-2.7/tarfile.py
--- a/lib-python/modified-2.7/tarfile.py
+++ b/lib-python/modified-2.7/tarfile.py
@@ -425,10 +425,16 @@
                 raise CompressionError("zlib module is not available")
             self.zlib = zlib
             self.crc = zlib.crc32("") & 0xffffffffL
-            if mode == "r":
-                self._init_read_gz()
-            else:
-                self._init_write_gz()
+            try:
+                if mode == "r":
+                    self._init_read_gz()
+                else:
+                    self._init_write_gz()
+            except:
+                if not self._extfileobj:
+                    fileobj.close()
+                    self.closed = True
+                raise    
 
         if comptype == "bz2":
             try:
@@ -1682,13 +1688,14 @@
 
             if filemode not in "rw":
                 raise ValueError("mode must be 'r' or 'w'")
-
-            t = cls(name, filemode,
-                    _Stream(name, filemode, comptype, fileobj, bufsize),
-                    **kwargs)
-            t._extfileobj = False
-            return t
-
+            fid = _Stream(name, filemode, comptype, fileobj, bufsize)
+            try:
+                t = cls(name, filemode, fid, **kwargs)
+                t._extfileobj = False
+                return t
+            except:
+                fid.close()
+                raise
         elif mode in "aw":
             return cls.taropen(name, mode, fileobj, **kwargs)
 
@@ -1715,13 +1722,18 @@
             gzip.GzipFile
         except (ImportError, AttributeError):
             raise CompressionError("gzip module is not available")
-
+        gz_fid = None
         try:
-            t = cls.taropen(name, mode,
-                gzip.GzipFile(name, mode, compresslevel, fileobj),
-                **kwargs)
+            gz_fid = gzip.GzipFile(name, mode, compresslevel, fileobj)
+            t = cls.taropen(name, mode, gz_fid, **kwargs)
         except IOError:
+            if gz_fid:
+                gz_fid.close()
             raise ReadError("not a gzip file")
+        except:
+            if gz_fid:
+                gz_fid.close()
+            raise 
         t._extfileobj = False
         return t
 
@@ -1738,15 +1750,21 @@
         except ImportError:
             raise CompressionError("bz2 module is not available")
 
-        if fileobj is not None:
-            fileobj = _BZ2Proxy(fileobj, mode)
-        else:
-            fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
+        try:
+            if fileobj is not None:
+                bzfileobj = _BZ2Proxy(fileobj, mode)
+            else:
+                bzfileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
+            t = cls.taropen(name, mode, bzfileobj, **kwargs)
 
-        try:
-            t = cls.taropen(name, mode, fileobj, **kwargs)
         except (IOError, EOFError):
+            if fileobj is None:
+                bzfileobj.close()
             raise ReadError("not a bzip2 file")
+        except:
+            if fileobj is None:
+                bzfileobj.close()
+            raise 
         t._extfileobj = False
         return t
 
diff --git a/lib-python/modified-2.7/test/test_tarfile.py b/lib-python/modified-2.7/test/test_tarfile.py
--- a/lib-python/modified-2.7/test/test_tarfile.py
+++ b/lib-python/modified-2.7/test/test_tarfile.py
@@ -340,7 +340,8 @@
         # constructor in case of an error. For the test we rely on
         # the fact that opening an empty file raises a ReadError.
         empty = os.path.join(TEMPDIR, "empty")
-        open(empty, "wb").write("")
+        with open(empty, "wb") as fid:
+            fid.write("")
 
         try:
             tar = object.__new__(tarfile.TarFile)


More information about the pypy-commit mailing list