-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-24792: Fix zipimporter masking the cause of import errors #22204
Conversation
Travis is complaining about "Generated files not up to date". |
@iritkatriel yes, you need to update the . |
Lib/test/test_zipimport.py
Outdated
@@ -239,12 +239,9 @@ def testBadMagic2(self): | |||
badmagic_pyc = bytearray(test_pyc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also add an additional test_bad_pyc_hash
that is like a combination of test_checked_hash_based_change_pyc
and this testBadMagic2
where instead of flipping a bit in the magic number, we flip a bit in the pyc hash and expect the error raised by _bootstrap_external._validate_hash_pyc
to make its way to us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the user will never see this exception because the check only happens when the zipfile has a .py file to compare the hash with. And then, if the hash doesn't match it just loads the '.py'.
I can get the exception in a test by temporarily removing the '.py' loader. Is this going too much into internals?
I added this to the end of test_checked_hash_based_change_pyc:
# now remove the '.py' importer so that the '.pyc'
# failure propagates the exception
del sys.modules[TESTMOD]
prev_zip_searchorder = zipimport._zip_searchorder
zipimport._zip_searchorder = zipimport._zip_searchorder[0:3]
try:
self.doTest(None, files, TESTMOD)
raise AssertionError("This should not be reached")
except zipimport.ZipImportError as exc:
cause = "hash in bytecode doesn't match hash of source"
cause = f"{cause} {TESTMOD!r}"
expected_msg = f"module load failed: {cause}"
self.assertIn(expected_msg, exc.msg)
self.assertIsInstance(exc.__cause__, ImportError)
self.assertIn(cause, exc.__cause__.msg)
finally:
zipimport._zip_searchorder = prev_zip_searchorder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test should not be added because it asserts behavior which is not visible to a user. test_checked_hash_based_change_pyc already checks this error case, which this PR does not change because the exception always swallowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slight test tweak to make sure we are not too rigid on the exception message, otherwise LGTM!
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 |
b3e2064
to
6468be7
Compare
I have made the requested changes; please review again. I also rebased to resolve merge conflicts. |
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
Sorry, I can't merge this PR. Reason: |
@iritkatriel sorry for the delay in approval! If you can regenerate |
6468be7
to
d6399c7
Compare
I've rebased and updated importlib_zipimport.h. Thanks. |
Co-authored-by: Brett Cannon <brett@python.org>
d6399c7
to
57d69d6
Compare
@iritkatriel: Status check is done, and it's a success ✅ . |
Sorry, I can't merge this PR. Reason: |
Now I did. (It was the wrong h file before). |
@iritkatriel: Status check is done, and it's a success ✅ . |
Thanks! |
…GH-22204) zipimport's _unmarshal_code swallows import errors and then _get_module_code doesn't know the cause of the error, and returns the generic, and sometimes incorrect, 'could not find...'. Automerge-Triggered-By: GH:brettcannon
zipimport's _unmarshal_code swallows import errors and then _get_module_code doesn't know the cause of the error, and returns the generic, and sometimes incorrect, 'could not find...'.
Automerge-Triggered-By: GH:brettcannon
https://bugs.python.org/issue24792