[Python-checkins] cpython (2.7): use with statement to ensure zipfile is always closed (closes #20102)

benjamin.peterson python-checkins at python.org
Sun Feb 2 21:31:25 CET 2014


http://hg.python.org/cpython/rev/767d034b3feb
changeset:   88900:767d034b3feb
branch:      2.7
parent:      88890:ae1c70135763
user:        Benjamin Peterson <benjamin at python.org>
date:        Sun Feb 02 15:30:22 2014 -0500
summary:
  use with statement to ensure zipfile is always closed (closes #20102)

files:
  Lib/shutil.py |  20 +++++++++-----------
  1 files changed, 9 insertions(+), 11 deletions(-)


diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -446,17 +446,15 @@
                         zip_filename, base_dir)
 
         if not dry_run:
-            zip = zipfile.ZipFile(zip_filename, "w",
-                                  compression=zipfile.ZIP_DEFLATED)
-
-            for dirpath, dirnames, filenames in os.walk(base_dir):
-                for name in filenames:
-                    path = os.path.normpath(os.path.join(dirpath, name))
-                    if os.path.isfile(path):
-                        zip.write(path, path)
-                        if logger is not None:
-                            logger.info("adding '%s'", path)
-            zip.close()
+            with zipfile.ZipFile(zip_filename, "w",
+                                 compression=zipfile.ZIP_DEFLATED) as zf:
+                for dirpath, dirnames, filenames in os.walk(base_dir):
+                    for name in filenames:
+                        path = os.path.normpath(os.path.join(dirpath, name))
+                        if os.path.isfile(path):
+                            zf.write(path, path)
+                            if logger is not None:
+                                logger.info("adding '%s'", path)
 
     return zip_filename
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list