cpython/Lib/importlib
Eric Snow 79cf20e48d
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
Currently frozen modules do not have __file__ set.  In their spec, origin is set to "frozen" and they are marked as not having a location.  (Similarly, for frozen packages __path__ is set to an empty list.)  However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime.  (We now do so since gh-28586.)  Having __file__ set is helpful for a number of reasons.  Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module).

This change sets __file__ (and adds to __path__) for frozen stdlib modules.  It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655).  All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py). 
 Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known).  To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info.  __origname__ is popped off during importlib bootstrap.

(To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet.  This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped.  However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts.  Doing so will require changes in the order in which things happen during interpreter initialization.  That can be addressed separately.  Once it is, the file-related fixup code from this PR will kick in.)

Here are things this change does not do:

* set __file__ for non-stdlib modules (no way of knowing the parent dir)
* set __file__ if the stdlib dir is not known (nor assume the expense of finding it)
* relatedly, set __file__ if the stdlib is in a zip file
* verify that the filename set to __file__ actually exists (too expensive)
* update __path__ for frozen packages that alias a non-package (since there is no package dir)

Other things this change skips, but we may do later:

* set __file__ on modules imported using PyImport_ImportFrozenModule()
* set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines
* implement FrozenImporter.get_filename() and FrozenImporter.get_source()

https://bugs.python.org/issue21736
2021-10-14 15:32:18 -06:00
..
metadata bpo-44464: Remove special exclusion for flake8 in the deprecation warnings. (#26807) 2021-06-27 17:59:18 -04:00
__init__.py bpo-42135 Correct version slated for importlib.find_loader removal (GH-28312) 2021-09-13 15:57:50 -07:00
_abc.py bpo-42136: Deprecate module_repr() as found in importlib (GH-25022) 2021-03-26 11:55:07 -07:00
_adapters.py Fix typos in the Lib directory (GH-28775) 2021-10-06 16:13:48 -07:00
_bootstrap.py bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656) 2021-10-14 15:32:18 -06:00
_bootstrap_external.py Bump MAGIC_NUMBER to reflect change in JUMP_ABSOLUTE semantics. (GH-28829) 2021-10-09 14:17:22 +01:00
_common.py bpo-44771: Sync with importlib_resources 5.2.2, fixing refleak. (#27497) 2021-07-30 20:37:09 -04:00
_itertools.py bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436) 2021-07-29 21:05:05 -04:00
_legacy.py bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436) 2021-07-29 21:05:05 -04:00
abc.py bpo-42135: Deprecate implementations of find_module() and find_loader() (GH-25169) 2021-04-06 08:56:57 -07:00
machinery.py bpo-42403: Fix pyflakes warnings in importlib (GH-23396) 2020-11-19 11:20:57 +01:00
readers.py bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436) 2021-07-29 21:05:05 -04:00
resources.py bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436) 2021-07-29 21:05:05 -04:00
simple.py bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436) 2021-07-29 21:05:05 -04:00
util.py bpo-43720: Update import-related stdlib deprecation messages to say they will be removed in Python 3.12 (GH-25167) 2021-04-03 15:31:15 -07:00