Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-112998: zipfile: fix extractall makedirs race condition #112966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,11 +1802,14 @@ def _extract_member(self, member, targetpath, pwd):
# Create all upper directories if necessary.
upperdirs = os.path.dirname(targetpath)
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)
os.makedirs(upperdirs, exist_ok=True)

if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
try:
os.mkdir(targetpath)
except FileExistsError:
pass
Comment on lines 1808 to +1812
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it raises FileExistsError if targetpath exists, but is not a directory. With this change it will be silent. It is not good, because it is an error.

Please add a test for extracting a directory in place of existing regular file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such test was added in #114960, and it fails with this PR.

return targetpath

with self.open(member, pwd=pwd) as source, \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``zipfile`` - Avoid race condition creating parent directories when
extracting concurrently.