fix: wrap corrupt/truncated/encrypted zip read errors in PackageNotFoundError#1562
Open
gaoflow wants to merge 1 commit into
Open
fix: wrap corrupt/truncated/encrypted zip read errors in PackageNotFoundError#1562gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When parsing a corrupt, truncated, or unexpectedly encrypted .docx file, ZipFile.read() can raise zlib.error, EOFError, RuntimeError, or BadZipFile. These native exceptions previously escaped from Document(stream), requiring callers to catch multiple unrelated types. _ZipPkgReader.__init__ now wraps BadZipFile on construction, and blob_for() wraps BadZipFile, zlib.error, EOFError, and RuntimeError, re-raising them all as PackageNotFoundError so callers have a single consistent docx-level exception to handle. Fixes python-openxml#1561.
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.
Summary
When
docx.Document(stream)parses a malformed, corrupt, or unexpectedly encrypted.docxfile, low-level exceptions from Python'szipfile/zlibmodules escape the library boundary instead of being surfaced as a consistent python-docx exception. Callers are left catchingzlib.error,EOFError,RuntimeError, orBadZipFile— unrelated internal types they should never need to know about.This PR fixes the two callsites in
_ZipPkgReader:__init__:ZipFile(pkg_file, "r")can raiseBadZipFilewhen the stream is not a valid zip. This is now wrapped inPackageNotFoundError.blob_for:ZipFile.read()can raiseBadZipFile(CRC mismatch),zlib.error(corrupt compressed data),EOFError(truncated stream), orRuntimeError(encrypted entry without a password). All four are now wrapped inPackageNotFoundError.Callers already catch
PackageNotFoundError; no API change is required on their side.Closes #1561.
Test plan
tests/opc/test_phys_pkg.py::DescribeZipPkgReader::it_raises_PackageNotFoundError_when_stream_is_not_a_zip— newtests/opc/test_phys_pkg.py::DescribeZipPkgReader::it_raises_PackageNotFoundError_when_blob_has_bad_crc— new (BadZipFileCRC path)tests/opc/test_phys_pkg.py::DescribeZipPkgReader::it_raises_PackageNotFoundError_when_zlib_data_is_corrupt— new (zlib.errorpath)pytest tests/— 1612 passed (1609 existing + 3 new)