Repository navigation
Allow for built-in modules to be submodules #102700
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 14, 2023 - added3.11only security fixesonly security fixes3.10 (EOL)end of lifeend of life3.12only security fixesonly security fixes
on Mar 16, 2023 In GH-102768 @brettcannon wrote:
As discovered while trying to get NumPy statically compiled into CPython for WASI, the built-in importer automatically rejects any built-in module name with a
.. But honestly I don't know if there's a need to be so strict since there technically isn't a reason why a built-in module couldn't be a submodule.- changed the title
[-]Statically linked extensions are not loaded correctly if they are under a namespace[/-][+]Allow for built-in modules to be submodules[/+]on Mar 16, 2023 @kesmit13 thanks for opening the issue.
numpyandnumpy.coreare normal packages, with onlynumpy.core._multiarray_umathbeing a built-in module, right?the built-in importer automatically rejects any built-in module name with a
.The root issue here is that
pathis set, which happens because the parent module is a package.cpython/Lib/importlib/_bootstrap.py
Lines 1262 to 1269 in 405739f
try: path = parent_module.__path__ except AttributeError: msg = f'{_ERR_MSG_PREFIX} {name!r}; {parent!r} is not a package' raise ModuleNotFoundError(msg, name=name) from None parent_spec = parent_module.__spec__ child = name.rpartition('.')[2] spec = _find_spec(name, path) I think the question we want to ask here is if we want to support having a built-in module be a submodule of a normal package? If so, we should simply remove the path check from
BultinImporter.find_spec.Your comments about
numpy.core._multiarray_umathare correct. The issue that I'm running into is trying to create a statically linked Python binary that includes numpy and pandas. In order to do this, I have to usePyImport_AppendInittabwith the C extensions from those packages.Thanks, I think it makes sense to drop this limitation. I don't think there's anything else preventing built-in modules from being submodules other than the path check, so removing that check should be all that's needed.
Bug report
If you statically link a namespaced extension (such as
numpy.core._multiarray_umath) and add it to the inittab, it will not get loaded properly. In this case, the path to the extension is passed to theBultinImporter.find_specmethod which causes it to returnNone. The path is a pointer to the namespace of the extension.One possible fix would be to check for a
.in the name of the extension being loaded. If it contains that, consider it to be a fully-qualified import path and check to see if that fully-qualified name is in the builtins list before moving on to other checks.Your environment
Linked PRs