[mypyc] Set dunder attrs when adding module to sys.modules#21275
Open
p-sawicki wants to merge 5 commits intopython:masterfrom
Open
[mypyc] Set dunder attrs when adding module to sys.modules#21275p-sawicki wants to merge 5 commits intopython:masterfrom
p-sawicki wants to merge 5 commits intopython:masterfrom
Conversation
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.
Recently there was a change to add native modules to
sys.modulesbefore they are executed to be able to detect circular imports. This introduced a regression when the module is a package that imports objects from other files within the package, eg.from pkg.file import somethinginsidepkg/__init__.py. Such imports result in an exceptionModuleNotFoundError: No module named 'pkg.file'; 'pkg' is not a package., for example when trying to upgrade mypy in black.This error is raised because Python expects the parent module of
fileto have the__path__attribute set when resolving the import but we don't set this attribute before adding thepkgmodule tosys.modules.So use existing functions to set relevant dunder attributes (
__path__for packages and__file__,__spec__, and__package__for all) before registering the module insys.modules.Don't skip compilation for
__init__.pyfiles in separate compilation mode to make this possible to test.Use
Py_CLEARinstead ofPy_DECREFon the import object on failure inCPyImport_ImportNativeas the import object might be freed when deleting it fromsys.modules. This triggered an assertion when running tests with a debug build of cpython.