Skip to content

Commit

Permalink
Use ZipFile.open instead of ZipFile.read (#5848)
Browse files Browse the repository at this point in the history
To avoid huge memory usage in unusual situations (e.g. a TensorFlow
wheel on a Raspberry Pi), use ZipFile.open and shutil.copyfileobj
instead of reading all the decompressed data into a byte-string.
  • Loading branch information
waveform80 authored and cjerdonek committed Oct 19, 2018
1 parent 2d545dd commit 62c27de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/5848.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Greatly reduce memory usage when installing wheels containing large files.
8 changes: 5 additions & 3 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ def unzip_file(filename, location, flatten=True):
leading = has_leading_dir(zip.namelist()) and flatten
for info in zip.infolist():
name = info.filename
data = zip.read(name)
fn = name
if leading:
fn = split_leading_dir(name)[1]
Expand All @@ -479,9 +478,12 @@ def unzip_file(filename, location, flatten=True):
ensure_dir(fn)
else:
ensure_dir(dir)
fp = open(fn, 'wb')
# Don't use read() to avoid allocating an arbitrarily large
# chunk of memory for the file's content
fp = zip.open(name)
try:
fp.write(data)
with open(fn, 'wb') as destfp:
shutil.copyfileobj(fp, destfp)
finally:
fp.close()
mode = info.external_attr >> 16
Expand Down

0 comments on commit 62c27de

Please sign in to comment.