Skip to content
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

Merged
merged 9 commits into from
Dec 19, 2020

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Sep 11, 2020

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

@iritkatriel
Copy link
Member Author

Travis is complaining about "Generated files not up to date".
Do I need to commit and push Python/importlib_zipimport.h as well?

@brettcannon
Copy link
Member

@iritkatriel yes, you need to update the .h file as well.

@gpshead gpshead self-requested a review September 11, 2020 19:16
@@ -239,12 +239,9 @@ def testBadMagic2(self):
badmagic_pyc = bytearray(test_pyc)
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

Copy link
Member Author

@iritkatriel iritkatriel Sep 12, 2020

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

Copy link
Member Author

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.

Copy link
Member

@brettcannon brettcannon left a 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!

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@iritkatriel
Copy link
Member Author

I have made the requested changes; please review again. I also rebased to resolve merge conflicts.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@brettcannon brettcannon changed the title bpo-24792: Fix zipimoprter masking cause of import errors bpo-24792: Fix zipimporter masking the cause of import errors Dec 18, 2020
@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: Pull Request is not mergeable.

@brettcannon
Copy link
Member

@iritkatriel sorry for the delay in approval! If you can regenerate importlib_zipimport.h then I will merge this straight-away.

@iritkatriel
Copy link
Member Author

I've rebased and updated importlib_zipimport.h. Thanks.

@miss-islington
Copy link
Contributor

@iritkatriel: Status check is done, and it's a success ✅ .

@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: 2 of 4 required status checks are expected..

@iritkatriel
Copy link
Member Author

I've rebased and updated importlib_zipimport.h. Thanks.

Now I did. (It was the wrong h file before).

@miss-islington
Copy link
Contributor

@iritkatriel: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit fb34096 into python:master Dec 19, 2020
@iritkatriel iritkatriel deleted the bpo-24792 branch December 20, 2020 17:05
@brettcannon
Copy link
Member

Thanks!

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
…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
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.

6 participants