mirror of
https://github.com/python/cpython.git
synced 2025-11-27 13:45:25 +00:00
make sure files are closed using the with statement
This commit is contained in:
parent
14c7bc2ad2
commit
b91e8ede7a
1 changed files with 23 additions and 25 deletions
|
|
@ -1047,28 +1047,27 @@ class ZipFile:
|
||||||
self.fp.write(zinfo.FileHeader())
|
self.fp.write(zinfo.FileHeader())
|
||||||
return
|
return
|
||||||
|
|
||||||
fp = open(filename, "rb")
|
with open(filename, "rb") as fp:
|
||||||
# Must overwrite CRC and sizes with correct data later
|
# Must overwrite CRC and sizes with correct data later
|
||||||
zinfo.CRC = CRC = 0
|
zinfo.CRC = CRC = 0
|
||||||
zinfo.compress_size = compress_size = 0
|
zinfo.compress_size = compress_size = 0
|
||||||
zinfo.file_size = file_size = 0
|
zinfo.file_size = file_size = 0
|
||||||
self.fp.write(zinfo.FileHeader())
|
self.fp.write(zinfo.FileHeader())
|
||||||
if zinfo.compress_type == ZIP_DEFLATED:
|
if zinfo.compress_type == ZIP_DEFLATED:
|
||||||
cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
|
cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
|
||||||
zlib.DEFLATED, -15)
|
zlib.DEFLATED, -15)
|
||||||
else:
|
else:
|
||||||
cmpr = None
|
cmpr = None
|
||||||
while 1:
|
while 1:
|
||||||
buf = fp.read(1024 * 8)
|
buf = fp.read(1024 * 8)
|
||||||
if not buf:
|
if not buf:
|
||||||
break
|
break
|
||||||
file_size = file_size + len(buf)
|
file_size = file_size + len(buf)
|
||||||
CRC = crc32(buf, CRC) & 0xffffffff
|
CRC = crc32(buf, CRC) & 0xffffffff
|
||||||
if cmpr:
|
if cmpr:
|
||||||
buf = cmpr.compress(buf)
|
buf = cmpr.compress(buf)
|
||||||
compress_size = compress_size + len(buf)
|
compress_size = compress_size + len(buf)
|
||||||
self.fp.write(buf)
|
self.fp.write(buf)
|
||||||
fp.close()
|
|
||||||
if cmpr:
|
if cmpr:
|
||||||
buf = cmpr.flush()
|
buf = cmpr.flush()
|
||||||
compress_size = compress_size + len(buf)
|
compress_size = compress_size + len(buf)
|
||||||
|
|
@ -1388,9 +1387,8 @@ def main(args = None):
|
||||||
tgtdir = os.path.dirname(tgt)
|
tgtdir = os.path.dirname(tgt)
|
||||||
if not os.path.exists(tgtdir):
|
if not os.path.exists(tgtdir):
|
||||||
os.makedirs(tgtdir)
|
os.makedirs(tgtdir)
|
||||||
fp = open(tgt, 'wb')
|
with open(tgt, 'wb') as fp:
|
||||||
fp.write(zf.read(path))
|
fp.write(zf.read(path))
|
||||||
fp.close()
|
|
||||||
zf.close()
|
zf.close()
|
||||||
|
|
||||||
elif args[0] == '-c':
|
elif args[0] == '-c':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue