Raise PackageNotFoundError for a non-zip stream, not BadZipFile - #1596
Open
eeshsaxena wants to merge 1 commit into
Open
Raise PackageNotFoundError for a non-zip stream, not BadZipFile#1596eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
Opening a document from a path that is not a package raises PackageNotFoundError,
but opening one from a *stream* that is not a zip fell through to ZipFile and
raised a bare zipfile.BadZipFile instead. PhysPkgReader.__new__ passes streams
straight to the zip reader ("pass it to Zip reader to sort out"), but the reader
never sorted out the not-a-zip case.
Catch BadZipFile in _ZipPkgReader and raise PackageNotFoundError, so a bad stream
and a bad path fail the same way. Added a test.
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.
Opening a document from a path that isn't a package raises
PackageNotFoundError, but opening one from a stream that isn't a zip leaks a barezipfile.BadZipFile:PhysPkgReader.__new__checksis_zipfile()for a path and raisesPackageNotFoundErrorwhen it isn't a package, but for a stream it hands the file straight to the zip reader ("assume it's a stream and pass it to Zip reader to sort out"). The zip reader never sorted out the not-a-zip case, so callers catchingPackageNotFoundError(the documented error for a bad package) miss the stream case.This catches
BadZipFilein_ZipPkgReaderand re-raisesPackageNotFoundError, so a bad stream and a bad path fail the same way. Valid packages are unaffected (verified a normal open/save/read round-trip still works). Added a test mirroring the existing not-a-package-path test.