summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-05-10 02:29:00 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-05-10 02:29:00 (GMT)
commitb91e8ede7afe606ada64b394dbd7db030e536780 (patch)
tree6eea6fed6f89e061c3c547ddebc4e30ea4d322b7 /Lib
parent14c7bc2ad22f4fffdcf2b3d58b1358846a53079f (diff)
downloadcpython-b91e8ede7afe606ada64b394dbd7db030e536780.zip
cpython-b91e8ede7afe606ada64b394dbd7db030e536780.tar.gz
cpython-b91e8ede7afe606ada64b394dbd7db030e536780.tar.bz2
make sure files are closed using the with statement
Diffstat (limited to 'Lib')
-rw-r--r--Lib/zipfile.py48
1 files changed, 23 insertions, 25 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index a1e44c0..e3d40b4 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1047,28 +1047,27 @@ class ZipFile:
self.fp.write(zinfo.FileHeader())
return
- fp = open(filename, "rb")
- # Must overwrite CRC and sizes with correct data later
- zinfo.CRC = CRC = 0
- zinfo.compress_size = compress_size = 0
- zinfo.file_size = file_size = 0
- self.fp.write(zinfo.FileHeader())
- if zinfo.compress_type == ZIP_DEFLATED:
- cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
- zlib.DEFLATED, -15)
- else:
- cmpr = None
- while 1:
- buf = fp.read(1024 * 8)
- if not buf:
- break
- file_size = file_size + len(buf)
- CRC = crc32(buf, CRC) & 0xffffffff
- if cmpr:
- buf = cmpr.compress(buf)
- compress_size = compress_size + len(buf)
- self.fp.write(buf)
- fp.close()
+ with open(filename, "rb") as fp:
+ # Must overwrite CRC and sizes with correct data later
+ zinfo.CRC = CRC = 0
+ zinfo.compress_size = compress_size = 0
+ zinfo.file_size = file_size = 0
+ self.fp.write(zinfo.FileHeader())
+ if zinfo.compress_type == ZIP_DEFLATED:
+ cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
+ zlib.DEFLATED, -15)
+ else:
+ cmpr = None
+ while 1:
+ buf = fp.read(1024 * 8)
+ if not buf:
+ break
+ file_size = file_size + len(buf)
+ CRC = crc32(buf, CRC) & 0xffffffff
+ if cmpr:
+ buf = cmpr.compress(buf)
+ compress_size = compress_size + len(buf)
+ self.fp.write(buf)
if cmpr:
buf = cmpr.flush()
compress_size = compress_size + len(buf)
@@ -1388,9 +1387,8 @@ def main(args = None):
tgtdir = os.path.dirname(tgt)
if not os.path.exists(tgtdir):
os.makedirs(tgtdir)
- fp = open(tgt, 'wb')
- fp.write(zf.read(path))
- fp.close()
+ with open(tgt, 'wb') as fp:
+ fp.write(zf.read(path))
zf.close()
elif args[0] == '-c':