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

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


http://hg.python.org/cpython/rev/838674d15b5b
changeset: 88901:838674d15b5b
branch: 3.3
parent: 88891:412a8e2c2af6
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
@@ -680,17 +680,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

AltStyle によって変換されたページ (->オリジナル) /