Skip to content

[mypyc] Fix segfault when importing package with module-level __getattr__ - #22205

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1198-module-getattr
Oct 15, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1198-module-getattr

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1198.

Importing a compiled package that defines a module-level __getattr__ could segfault:

# pkg/__init__.py
def __getattr__(name: str) -> object:
    raise AttributeError(name)

When initializing a module, mypyc checks whether __file__, __path__ and other module attributes are already set using getattr, and this calls the module __getattr__ if the attribute is not found. Since module-level functions are available before the module body has been executed, this calls compiled code before the module has been fully initialized.

Now these attributes are looked up directly from the module dict, so that __getattr__ is not called.

…attr__

Fixes mypyc/mypyc#1198.

Importing a compiled package that defines a module-level `__getattr__`
could segfault:

```python
# pkg/__init__.py
def __getattr__(name: str) -> object:
    raise AttributeError(name)
```

When initializing a module, mypyc checks whether `__file__`, `__path__`
and other module attributes are already set using `getattr`, and this
calls the module `__getattr__` if the attribute is not found. Since
module-level functions are available before the module body has been
executed, this calls compiled code before the module has been fully
initialized.

Now these attributes are looked up directly from the module dict, so
that `__getattr__` is not called.
@JukkaL
JukkaL merged commit 97766ec into master Oct 15, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1198-module-getattr branch October 15, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypyc segfaults importing compiled package with module-level __getattr__

2 participants