Skip to content

Commit 52777ba

Browse files
committed
fix: use context manager for ZipFile in extract_zipped_paths
Fix resource leak in extract_zipped_paths() where the ZipFile object was never properly closed. The function opened a ZipFile but returned early in multiple code paths without closing it, potentially leaking file descriptors. This change wraps the ZipFile usage in a context manager to ensure proper cleanup regardless of which code path is taken.
1 parent 7029833 commit 52777ba

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/requests/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,18 @@ def extract_zipped_paths(path):
278278
if not zipfile.is_zipfile(archive):
279279
return path
280280

281-
zip_file = zipfile.ZipFile(archive)
282-
if member not in zip_file.namelist():
283-
return path
284-
285-
# we have a valid zip archive and a valid member of that archive
286-
tmp = tempfile.gettempdir()
287-
extracted_path = os.path.join(tmp, member.split("/")[-1])
288-
if not os.path.exists(extracted_path):
289-
# use read + write to avoid the creating nested folders, we only want the file, avoids mkdir racing condition
290-
with atomic_open(extracted_path) as file_handler:
291-
file_handler.write(zip_file.read(member))
292-
return extracted_path
281+
with zipfile.ZipFile(archive) as zip_file:
282+
if member not in zip_file.namelist():
283+
return path
284+
285+
# we have a valid zip archive and a valid member of that archive
286+
tmp = tempfile.gettempdir()
287+
extracted_path = os.path.join(tmp, member.split("/")[-1])
288+
if not os.path.exists(extracted_path):
289+
# use read + write to avoid the creating nested folders, we only want the file, avoids mkdir racing condition
290+
with atomic_open(extracted_path) as file_handler:
291+
file_handler.write(zip_file.read(member))
292+
return extracted_path
293293

294294

295295
@contextlib.contextmanager

0 commit comments

Comments
 (0)