Skip to content

Commit 8849b3b

Browse files
authored
[mypyc] Propagate errors in import helpers (#22349)
In `CPyImport_ImportFrom`, only set `ImportError` when the imported attribute lookup fails with no other error, otherwise propagate the error. Add missing error checks when calling cpython APIs in import-related functions in misc_ops.c and locks.c.
1 parent 1e6f020 commit 8849b3b

3 files changed

Lines changed: 71 additions & 25 deletions

File tree

‎mypyc/lib-rt/locks.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ CPyModuleLockAPI *CPyModuleLockAPI_Alloc(void) {
1818
return NULL;
1919
}
2020
api->get_module_lock = PyObject_GetAttrString(bootstrap, "_get_module_lock");
21+
if (api->get_module_lock == NULL) {
22+
Py_DECREF(bootstrap);
23+
CPyModuleLockAPI_Free(api);
24+
return NULL;
25+
}
2126
api->deadlock_error = PyObject_GetAttrString(bootstrap, "_DeadlockError");
2227
Py_DECREF(bootstrap);
23-
if (api->get_module_lock == NULL || api->deadlock_error == NULL) {
28+
if (api->deadlock_error == NULL) {
2429
CPyModuleLockAPI_Free(api);
2530
return NULL;
2631
}

‎mypyc/lib-rt/misc_ops.c‎

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -880,34 +880,40 @@ bool CPyImport_ImportMany(PyObject *modules, CPyModuleCache *statics[], PyObject
880880
static PyObject *CPyImport_ImportFrom(PyObject *module, PyObject *package_name,
881881
PyObject *import_name, PyObject *as_name) {
882882
// check if the imported module has an attribute by that name
883-
PyObject *x = PyObject_GetAttr(module, import_name);
884-
if (x == NULL) {
883+
PyObject *x;
884+
int found = PyObject_GetOptionalAttr(module, import_name, &x);
885+
if (found < 0) {
886+
return NULL;
887+
}
888+
if (!found) {
885889
// Attribute lookup failed. The name may still be a submodule that's
886890
// been imported already; look it up directly in sys.modules.
887891
PyObject *fullmodname = PyUnicode_FromFormat("%U.%U", package_name, import_name);
888892
if (fullmodname == NULL) {
889-
goto fail;
893+
return NULL;
890894
}
891-
PyErr_Clear();
892895
x = PyImport_GetModule(fullmodname);
893896
Py_DECREF(fullmodname);
894897
if (x == NULL) {
895-
goto fail;
898+
if (PyErr_Occurred()) {
899+
return NULL;
900+
}
901+
PyObject *package_path = PyModule_GetFilenameObject(module);
902+
if (package_path == NULL) {
903+
PyErr_Clear();
904+
}
905+
PyObject *path_for_msg = package_path != NULL ? package_path : Py_None;
906+
PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
907+
import_name, package_name, path_for_msg);
908+
if (errmsg != NULL) {
909+
PyErr_SetImportError(errmsg, package_name, package_path);
910+
}
911+
Py_XDECREF(package_path);
912+
Py_XDECREF(errmsg);
913+
return NULL;
896914
}
897915
}
898916
return x;
899-
900-
fail:
901-
PyErr_Clear();
902-
PyObject *package_path = PyModule_GetFilenameObject(module);
903-
PyObject *path_for_msg = package_path != NULL ? package_path : Py_None;
904-
PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
905-
import_name, package_name, path_for_msg);
906-
// NULL checks for errmsg and package_name done by PyErr_SetImportError.
907-
PyErr_SetImportError(errmsg, package_name, package_path);
908-
Py_XDECREF(package_path);
909-
Py_XDECREF(errmsg);
910-
return NULL;
911917
}
912918

913919
PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names,
@@ -1217,6 +1223,10 @@ static int CPyImport_InitSpecClasses(void) {
12171223
return -1;
12181224
}
12191225
CPyImport_ModuleSpecClass = PyObject_GetAttrString(machinery, "ModuleSpec");
1226+
if (CPyImport_ModuleSpecClass == NULL) {
1227+
Py_DECREF(machinery);
1228+
return -1;
1229+
}
12201230
CPyImport_ExtFileLoaderClass = PyObject_GetAttrString(machinery, "ExtensionFileLoader");
12211231
Py_DECREF(machinery);
12221232
if (CPyImport_ModuleSpecClass == NULL || CPyImport_ExtFileLoaderClass == NULL) {
@@ -1362,20 +1372,33 @@ static int CPyImport_SetModuleFile(PyObject *modobj, PyObject *module_name,
13621372
// Compare shared_lib_file basename against "<last_segment>__mypyc<ext>".
13631373
PyObject *expected_basename = PyUnicode_FromFormat(
13641374
"%U__mypyc%U", last_segment, ext_suffix);
1375+
if (expected_basename == NULL) {
1376+
Py_DECREF(last_segment);
1377+
Py_DECREF(module_path);
1378+
return -1;
1379+
}
13651380
PyObject *actual_basename;
13661381
if (sep >= 0) {
13671382
actual_basename = PyUnicode_Substring(shared_lib_file, sep + 1, sf_len);
13681383
} else {
13691384
actual_basename = shared_lib_file;
13701385
Py_INCREF(actual_basename);
13711386
}
1372-
int is_per_module_lib = 0;
1373-
if (expected_basename != NULL && actual_basename != NULL) {
1374-
is_per_module_lib =
1375-
(PyUnicode_Compare(expected_basename, actual_basename) == 0);
1387+
if (actual_basename == NULL) {
1388+
Py_DECREF(expected_basename);
1389+
Py_DECREF(last_segment);
1390+
Py_DECREF(module_path);
1391+
return -1;
1392+
}
1393+
int comparison = PyUnicode_Compare(expected_basename, actual_basename);
1394+
Py_DECREF(expected_basename);
1395+
Py_DECREF(actual_basename);
1396+
if (comparison == -1 && PyErr_Occurred()) {
1397+
Py_DECREF(last_segment);
1398+
Py_DECREF(module_path);
1399+
return -1;
13761400
}
1377-
Py_XDECREF(expected_basename);
1378-
Py_XDECREF(actual_basename);
1401+
int is_per_module_lib = comparison == 0;
13791402

13801403
// For packages, __file__ should point to __init__<ext>,
13811404
// e.g. "a/b/__init__.cpython-312-x86_64-linux-gnu.so".

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,30 @@ from testmodule import g
9292

9393
def f(x: int) -> int:
9494
return g(x)
95+
96+
def read_value() -> int:
97+
from badmodule import value
98+
return value
99+
100+
[file badmodule.pyi]
101+
value: int
102+
103+
[file badmodule.py]
104+
def __getattr__(name):
105+
if name == "value":
106+
raise RuntimeError("module attribute failed")
107+
raise AttributeError(name)
108+
95109
[file testmodule.py]
96110
def g(x: int) -> int:
97111
return x + 1
98112
[file driver.py]
99-
from native import f
113+
from native import f, read_value
114+
from testutil import assertRaises
115+
100116
assert f(1) == 2
117+
with assertRaises(RuntimeError, "module attribute failed"):
118+
read_value()
101119

102120
[case testFromImportWithUntypedModule]
103121

0 commit comments

Comments
 (0)