Skip to content

gh-162732: Propagate errors when traversing directories in shutil.make_archive(..., 'zip') - #162795

Closed
anisayakmitra-in wants to merge 1 commit into
python:mainfrom
anisayakmitra-in:fix-shutil-make-archive-zip-unreadable-dir-162732
Closed

anisayakmitra-in wants to merge 1 commit into
python:mainfrom
anisayakmitra-in:fix-shutil-make-archive-zip-unreadable-dir-162732

Conversation

@anisayakmitra-in

Copy link
Copy Markdown

When shutil.make_archive(..., 'zip') encounters an unreadable directory during os.walk(base_dir), the default onerror=None in os.walk silently ignores the error and returns a truncated archive where unlisted directories appear empty. Other formats (such as tar) and command-line zip utilities raise when directories cannot be listed.

This PR provides an onerror handler in _make_zipfile to propagate OSError (e.g. PermissionError) when traversing source directories, matching tarball behavior and preventing silent data loss during archive creation.

Closes gh-162732.

…il.make_archive(..., 'zip')

When shutil.make_archive(..., 'zip') cannot list a source directory,
propagate the OSError encountered during traversal rather than silently
omitting files and creating an incomplete archive.

Signed-off-by: ANISAYAK MITRA <266799942+anisayakmitra-in@users.noreply.github.com>
@anisayakmitra-in
anisayakmitra-in force-pushed the fix-shutil-make-archive-zip-unreadable-dir-162732 branch from 1cf14a8 to cc8b97f Compare November 28, 2026 06:34

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm no longer sure that it's a real issue. I need more context about that on the issue, especially with the fact that we explicitly suppress an OSError when trying os.stat().

Comment thread Lib/shutil.py
logger.info("adding '%s'", base_dir)
for dirpath, dirnames, filenames in os.walk(base_dir):

def _onerror(err):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why making it a private function? it doesn't make sense.

Comment thread Lib/shutil.py
Comment on lines 1155 to 1158
try:
st = os.stat(path)
except OSError:
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here, this looks like a deliberate choice for ignoring this specific OSError. Maybe this is actually the legitimate behavior? I think we need more careful investigation.

@@ -0,0 +1,3 @@
:func:`shutil.make_archive` with format ``"zip"`` now propagates any
:exc:`OSError` encountered when traversing source directories instead of

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not true since there are some that are silenced.

@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz

picnixz commented Nov 28, 2026

Copy link
Copy Markdown
Member

For now, I'm closing this. Please avoid using an LLM to address recent issues automatically.

@picnixz picnixz closed this Nov 28, 2026
@anisayakmitra-in
anisayakmitra-in deleted the fix-shutil-make-archive-zip-unreadable-dir-162732 branch November 29, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shutil.make_archive() silently creates an incomplete ZIP when it cannot list a source directory

2 participants