Skip to content

gh-161112: Fix shutil.make_archive() adding a zip archive to itself - #161113

Merged
gpshead merged 1 commit into
python:mainfrom
gpshead:shutil-make-archive-zip-self
Nov 1, 2026
Merged

gpshead merged 1 commit into
python:mainfrom
gpshead:shutil-make-archive-zip-self

Conversation

@gpshead

@gpshead gpshead commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

_make_zipfile() now stats the archive once after creating it and skips any walked file that is os.path.samestat() with it, which also catches a symlink or hard link to the archive. The os.path.isfile() call that already stats every file is replaced by os.stat() plus S_ISREG, so no syscalls are added per file. The check stays in shutil instead of ZipFile.write(), where it was declined in gh-104527 because of its per-file cost.

Fixes #161112

…self

Skip any walked file that is the archive being written, whether it is
created inside the archived tree or a symlink in the tree points at it.
@gpshead gpshead added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 1, 2026
@gpshead gpshead self-assigned this Nov 1, 2026
@gpshead
gpshead marked this pull request as ready for review November 1, 2026 18:45
@gpshead
gpshead requested a review from giampaolo as a code owner November 1, 2026 18:45
@gpshead
gpshead enabled auto-merge (squash) November 1, 2026 18:45
@gpshead
gpshead merged commit 521e15c into python:main Nov 1, 2026
103 of 105 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 1, 2026

Copy link
Copy Markdown

GH-161139 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 1, 2026
@bedevere-app

bedevere-app Bot commented Nov 1, 2026

Copy link
Copy Markdown

GH-161140 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 1, 2026
gpshead added a commit that referenced this pull request Nov 1, 2026
…tself (GH-161113) (#161140)

Skip any walked file that is the archive being written, whether it is
created inside the archived tree or a symlink in the tree points at it.

(cherry picked from commit 521e15c)
gpshead added a commit that referenced this pull request Nov 1, 2026
…tself (GH-161113) (#161139)

Skip any walked file that is the archive being written, whether it is
created inside the archived tree or a symlink in the tree points at it.

(cherry picked from commit 521e15c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shutil.make_archive() adds the zip archive to itself when it is inside the archived tree

1 participant