python / cpython

The Python programming language
https://www.python.org
Other
62.59k stars 30.03k forks source link

PyModule_GetState doesn't work with LazyLoader #85963

Open encukou opened 4 years ago

encukou commented 4 years ago
BPO 41797
Nosy @brettcannon, @encukou, @corona10, @shihai1991

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['expert-C-API'] title = "PyModule_GetState doesn't work with LazyLoader" updated_at = user = 'https://github.com/encukou' ``` bugs.python.org fields: ```python activity = actor = 'corona10' assignee = 'none' closed = False closed_date = None closer = None components = ['C API'] creation = creator = 'petr.viktorin' dependencies = [] files = [] hgrepos = [] issue_num = 41797 keywords = [] message_count = 2.0 messages = ['376991', '377009'] nosy_count = 4.0 nosy_names = ['brett.cannon', 'petr.viktorin', 'corona10', 'shihai1991'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue41797' versions = [] ```

encukou commented 4 years ago

One underlying issue from bpo-41631 is that importlib.util.LazyLoader produces modules that are only fully loaded after an attribute is accessed (through __getattribute__).

C-API functions like PyModule_GetState, PyModule_GetDict and such do not get attributes, and when called on a _LazyModule, they might try to access uninitialized C-level state.

I see two possible ways to fix this:

Brett, does that analysis make sense from the importlib POV?

brettcannon commented 4 years ago

Probably making LazyLoader skip being lazy for non-source modules probably makes the most sense and would be easiest to implement since it's an explicit opt-out.

Lincoln-developer commented 4 months ago

Hey I would love to work on this, thanks.