Skip to content

Commit 97766ec

Browse files
authored
[mypyc] Fix segfault when importing package with module-level __getattr__ (#22205)
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.
1 parent 457f913 commit 97766ec

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎mypyc/lib-rt/misc_ops.c‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,14 +1224,26 @@ static int CPyImport_InitSpecClasses(void) {
12241224
return 0;
12251225
}
12261226

1227+
// Look up an attribute directly from the module dict. Unlike
1228+
// PyObject_GetOptionalAttrString, this doesn't call a module-level __getattr__,
1229+
// which isn't safe to call before the module has been initialized.
1230+
static int CPyImport_GetModuleDictItem(PyObject *modobj, const char *name, PyObject **result) {
1231+
PyObject *dict = PyModule_GetDict(modobj); // Borrowed
1232+
if (dict == NULL) {
1233+
*result = NULL;
1234+
return -1;
1235+
}
1236+
return PyDict_GetItemStringRef(dict, name, result);
1237+
}
1238+
12271239
// Set __package__ before executing the module body so it is available
12281240
// during module initialization. For a package, __package__ is the module
12291241
// name itself. For a non-package submodule "a.b.c", it is "a.b". For a
12301242
// top-level non-package module, it is "".
12311243
static int CPyImport_SetModulePackage(PyObject *modobj, PyObject *module_name,
12321244
Py_ssize_t is_package) {
12331245
PyObject *pkg = NULL;
1234-
int rc = PyObject_GetOptionalAttrString(modobj, "__package__", &pkg);
1246+
int rc = CPyImport_GetModuleDictItem(modobj, "__package__", &pkg);
12351247
if (rc < 0) {
12361248
return -1;
12371249
}
@@ -1271,7 +1283,7 @@ static int CPyImport_SetModuleFile(PyObject *modobj, PyObject *module_name,
12711283
PyObject *shared_lib_file, PyObject *ext_suffix,
12721284
Py_ssize_t is_package) {
12731285
PyObject *file = NULL;
1274-
int rc = PyObject_GetOptionalAttrString(modobj, "__file__", &file);
1286+
int rc = CPyImport_GetModuleDictItem(modobj, "__file__", &file);
12751287
if (rc < 0) {
12761288
return -1;
12771289
}
@@ -1396,7 +1408,7 @@ static int CPyImport_SetModuleFile(PyObject *modobj, PyObject *module_name,
13961408
// Returns 0 on success, -1 on error.
13971409
static int CPyImport_SetModulePath(PyObject *modobj) {
13981410
PyObject *existing_path = NULL;
1399-
int rc = PyObject_GetOptionalAttrString(modobj, "__path__", &existing_path);
1411+
int rc = CPyImport_GetModuleDictItem(modobj, "__path__", &existing_path);
14001412
if (rc < 0) {
14011413
return -1;
14021414
}
@@ -1405,7 +1417,7 @@ static int CPyImport_SetModulePath(PyObject *modobj) {
14051417
return 0;
14061418
}
14071419
PyObject *file = NULL;
1408-
rc = PyObject_GetOptionalAttrString(modobj, "__file__", &file);
1420+
rc = CPyImport_GetModuleDictItem(modobj, "__file__", &file);
14091421
if (rc <= 0) {
14101422
return rc;
14111423
}
@@ -1435,7 +1447,7 @@ static int CPyImport_SetModulePath(PyObject *modobj) {
14351447
static int CPyImport_SetModuleSpec(PyObject *modobj, PyObject *module_name,
14361448
Py_ssize_t is_package) {
14371449
PyObject *spec = NULL;
1438-
int rc = PyObject_GetOptionalAttrString(modobj, "__spec__", &spec);
1450+
int rc = CPyImport_GetModuleDictItem(modobj, "__spec__", &spec);
14391451
if (rc < 0) {
14401452
return -1;
14411453
}
@@ -1449,7 +1461,7 @@ static int CPyImport_SetModuleSpec(PyObject *modobj, PyObject *module_name,
14491461
return -1;
14501462
}
14511463
PyObject *file = NULL;
1452-
if (PyObject_GetOptionalAttrString(modobj, "__file__", &file) < 0) {
1464+
if (CPyImport_GetModuleDictItem(modobj, "__file__", &file) < 0) {
14531465
return -1;
14541466
}
14551467
if (file == NULL) {
@@ -1479,7 +1491,7 @@ static int CPyImport_SetModuleSpec(PyObject *modobj, PyObject *module_name,
14791491
if (is_package) {
14801492
// Set submodule_search_locations from __path__
14811493
PyObject *path = NULL;
1482-
if (PyObject_GetOptionalAttrString(modobj, "__path__", &path) < 0) {
1494+
if (CPyImport_GetModuleDictItem(modobj, "__path__", &path) < 0) {
14831495
Py_DECREF(spec);
14841496
Py_DECREF(loader);
14851497
return -1;

‎mypyc/test-data/run-multimodule.test‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,3 +4198,23 @@ assert o.ITEMS == [1, 2]
41984198
b: Any = Base()
41994199
assert b.COMPUTED == 7
42004200
assert b.ITEMS is Base.ITEMS
4201+
4202+
[case testImportPackageWithModuleGetattr]
4203+
[file other_pkg/__init__.py]
4204+
def __getattr__(name: str) -> str:
4205+
if name.startswith("__"):
4206+
raise AttributeError(name)
4207+
return name
4208+
[file other_pkg/other_sub.py]
4209+
x = 1
4210+
[file driver.py]
4211+
import importlib.machinery
4212+
import other_pkg.other_sub
4213+
4214+
assert other_pkg.other_sub.x == 1
4215+
ext_suffixes = importlib.machinery.EXTENSION_SUFFIXES
4216+
assert any(other_pkg.__file__.endswith(s) for s in ext_suffixes), other_pkg.__file__
4217+
assert other_pkg.__path__
4218+
assert other_pkg.__spec__ is not None
4219+
assert other_pkg.__package__ == "other_pkg"
4220+
assert other_pkg.missing == "missing"

0 commit comments

Comments
 (0)