gh-98758: Fix shutil.unpack_archive masking real filesystem errors - #155587
Open
SomSamantray wants to merge 3 commits into
Open
gh-98758: Fix shutil.unpack_archive masking real filesystem errors#155587SomSamantray wants to merge 3 commits into
SomSamantray wants to merge 3 commits into
Conversation
unpack_archive() raised a generic ReadError("Unknown archive
format") whenever the filename's extension didn't match a
registered format -- including when the real problem was that the
file didn't exist or wasn't readable, masking FileNotFoundError/
PermissionError the way open() would correctly raise them.
When _find_unpack_format() finds no match, stat the path first
(surfaces FileNotFoundError et al.) and, only for regular files,
briefly open() it (surfaces PermissionError) before falling back to
ReadError. Skipping the open() probe for non-regular files avoids
a hang on FIFOs (a blocking read-open with no writer) and preserves
the existing ReadError behavior for directories.
shutil.ReadError has no documented exception entry in the docs (only tarfile.ReadError does), so Sphinx's py:exc cross-reference role can't resolve it, failing the Docs CI check. Use plain code markup instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes gh-98758.
shutil.unpack_archive(filename)with no explicitformatresolves the format purely by pattern-matchingfilename's extension via_find_unpack_format(), which never touches the filesystem. When no extension matches — including the common case of a nonexistent or permission-denied file — it raises a genericshutil.ReadError("Unknown archive format '<filename>'"), masking the real cause:>>> shutil.unpack_archive("./enoent") shutil.ReadError: Unknown archive format './enoent'open()correctly raisesFileNotFoundError/PermissionErrorfor the same inputs; this bringsunpack_archivein line with that.Fix
When
_find_unpack_format()returnsNone:os.stat(filename)first — surfacesFileNotFoundError(and other stat-level errors) for a missing path.open(filename, 'rb')and close it (contents never read) — surfacesPermissionErrorfor a file that exists but isn't readable.ReadErroris still raised — the extension genuinely doesn't match any registered format.The
open()probe is skipped for non-regular files (directories, FIFOs, etc.) so:ReadError(notIsADirectoryError)The explicit
format=...argument path is completely untouched.Testing
check_unpack_archive_with_convertertest helper: a nonexistent path now correctly expectsFileNotFoundErrorinstead of the oldReadError(this assertion previously encoded the bug).ReadError), permission-denied (PermissionError, skipped under root/unsupported-chmod platforms), an unrecognized-extension directory (stillReadError), and an unrecognized-extension FIFO (stillReadError, and confirmed it doesn't hang).test_shutilsuite passes locally (237 tests).