This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pyexpat.errors doesn't have __spec__ and __loader__ set
Type: Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, mjacob, ncoghlan
Priority: normal Keywords:

Created on 2017-03-16 22:35 by mjacob, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg289737 - (view) Author: Manuel Jacob (mjacob) * Date: 2017-03-16 22:35
The same applies to pyexpat.model.

It seems like pyexpat is the only builtin module which has submodules (errors, model).

Normally, as I understand it, the module gets imported given a spec and the import machinery ensures that this spec ends up in the __spec__ attribute of the module.  But in this case only pyexpat gets imported by importlib.  The submodules are added when initializing the module.

Also, importlib's BuiltinImporter assumes that a builtin module is never a package.  Is this reasonable in this case?
msg289773 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-17 17:49
The BuiltinImporter's assumption is reasonable because there are no built-ins that are packages. :) In pyexpat's case it's an extension module, not a built-in.

As for the expat issue, a patch that backfills the missing info would probably be reviewed.
msg289774 - (view) Author: Manuel Jacob (mjacob) * Date: 2017-03-17 18:03
You're of course right that pyexpat is an extension module and not a builtin module.  I was confused because on PyPy it's a builtin module.

But the same question applies for ExtensionFileLoader.is_package().  It returns False in the case of pyexpat.  This function looks a bit strange to me anyway.  It assumes the definition of what a package is for pure Python modules and applies it to extension modules.
msg289824 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-18 18:21
ExtensionFileLoader.is_package() is accurate because it's not typical for an extension module to inject its own submodules like pyexpat is. Usually people who use an extension module in a package use it just for a submodule and not even as an __init__.

IOW there's a long history of built-ins and extensions not being in packages that people bump up against that no one has taken the time to try and straighten out due to it not being an issue on a regular basis.
msg289845 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-03-19 11:22
Right, there's a longstanding RFE to allow builtin packages and submodules: https://bugs.python.org/issue1644818

At the moment, modules like pyexpat are more like the os module than they are packages: they eagerly set other modules as attributes at import time (akin to os.path), rather than defining an actual package with submodules that can be optionally loaded via the import system.

Given the import system changes in recent 3.x releases, I'd expect it to be feasible (but not necessarily easy) to come up with a viable specification and implementation for builtin and extension packages that work similarly to traditional packages (perhaps based on the handling of frozen packages).
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74016
2017-03-19 11:22:38ncoghlansetmessages: + msg289845
2017-03-18 18:21:17brett.cannonsetmessages: + msg289824
2017-03-17 18:03:30mjacobsetmessages: + msg289774
2017-03-17 17:49:11brett.cannonsetnosy: + brett.cannon, ncoghlan, eric.snow
messages: + msg289773
2017-03-16 22:35:12mjacobcreate