Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mypyc/lib-rt/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ CPyModuleLockAPI *CPyModuleLockAPI_Alloc(void) {
return NULL;
}
api->get_module_lock = PyObject_GetAttrString(bootstrap, "_get_module_lock");
if (api->get_module_lock == NULL) {
Py_DECREF(bootstrap);
CPyModuleLockAPI_Free(api);
return NULL;
}
api->deadlock_error = PyObject_GetAttrString(bootstrap, "_DeadlockError");
Py_DECREF(bootstrap);
if (api->get_module_lock == NULL || api->deadlock_error == NULL) {
if (api->deadlock_error == NULL) {
CPyModuleLockAPI_Free(api);
return NULL;
}
Expand Down
69 changes: 46 additions & 23 deletions mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,34 +880,40 @@ bool CPyImport_ImportMany(PyObject *modules, CPyModuleCache *statics[], PyObject
static PyObject *CPyImport_ImportFrom(PyObject *module, PyObject *package_name,
PyObject *import_name, PyObject *as_name) {
// check if the imported module has an attribute by that name
PyObject *x = PyObject_GetAttr(module, import_name);
if (x == NULL) {
PyObject *x;
int found = PyObject_GetOptionalAttr(module, import_name, &x);
if (found < 0) {
return NULL;
}
if (!found) {
// Attribute lookup failed. The name may still be a submodule that's
// been imported already; look it up directly in sys.modules.
PyObject *fullmodname = PyUnicode_FromFormat("%U.%U", package_name, import_name);
if (fullmodname == NULL) {
goto fail;
return NULL;
}
PyErr_Clear();
x = PyImport_GetModule(fullmodname);
Py_DECREF(fullmodname);
if (x == NULL) {
goto fail;
if (PyErr_Occurred()) {
return NULL;
}
PyObject *package_path = PyModule_GetFilenameObject(module);
if (package_path == NULL) {
PyErr_Clear();
}
PyObject *path_for_msg = package_path != NULL ? package_path : Py_None;
PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
import_name, package_name, path_for_msg);
if (errmsg != NULL) {
PyErr_SetImportError(errmsg, package_name, package_path);
}
Py_XDECREF(package_path);
Py_XDECREF(errmsg);
return NULL;
}
}
return x;

fail:
PyErr_Clear();
PyObject *package_path = PyModule_GetFilenameObject(module);
PyObject *path_for_msg = package_path != NULL ? package_path : Py_None;
PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
import_name, package_name, path_for_msg);
// NULL checks for errmsg and package_name done by PyErr_SetImportError.
PyErr_SetImportError(errmsg, package_name, package_path);
Py_XDECREF(package_path);
Py_XDECREF(errmsg);
return NULL;
}

PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names,
Expand Down Expand Up @@ -1217,6 +1223,10 @@ static int CPyImport_InitSpecClasses(void) {
return -1;
}
CPyImport_ModuleSpecClass = PyObject_GetAttrString(machinery, "ModuleSpec");
if (CPyImport_ModuleSpecClass == NULL) {
Py_DECREF(machinery);
return -1;
}
CPyImport_ExtFileLoaderClass = PyObject_GetAttrString(machinery, "ExtensionFileLoader");
Py_DECREF(machinery);
if (CPyImport_ModuleSpecClass == NULL || CPyImport_ExtFileLoaderClass == NULL) {
Expand Down Expand Up @@ -1362,20 +1372,33 @@ static int CPyImport_SetModuleFile(PyObject *modobj, PyObject *module_name,
// Compare shared_lib_file basename against "<last_segment>__mypyc<ext>".
PyObject *expected_basename = PyUnicode_FromFormat(
"%U__mypyc%U", last_segment, ext_suffix);
if (expected_basename == NULL) {
Py_DECREF(last_segment);
Py_DECREF(module_path);
return -1;
}
PyObject *actual_basename;
if (sep >= 0) {
actual_basename = PyUnicode_Substring(shared_lib_file, sep + 1, sf_len);
} else {
actual_basename = shared_lib_file;
Py_INCREF(actual_basename);
}
int is_per_module_lib = 0;
if (expected_basename != NULL && actual_basename != NULL) {
is_per_module_lib =
(PyUnicode_Compare(expected_basename, actual_basename) == 0);
if (actual_basename == NULL) {
Py_DECREF(expected_basename);
Py_DECREF(last_segment);
Py_DECREF(module_path);
return -1;
}
int comparison = PyUnicode_Compare(expected_basename, actual_basename);
Py_DECREF(expected_basename);
Py_DECREF(actual_basename);
if (comparison == -1 && PyErr_Occurred()) {
Py_DECREF(last_segment);
Py_DECREF(module_path);
return -1;
}
Py_XDECREF(expected_basename);
Py_XDECREF(actual_basename);
int is_per_module_lib = comparison == 0;

// For packages, __file__ should point to __init__<ext>,
// e.g. "a/b/__init__.cpython-312-x86_64-linux-gnu.so".
Expand Down
20 changes: 19 additions & 1 deletion mypyc/test-data/run-imports.test
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,30 @@ from testmodule import g

def f(x: int) -> int:
return g(x)

def read_value() -> int:
from badmodule import value
return value

[file badmodule.pyi]
value: int

[file badmodule.py]
def __getattr__(name):
if name == "value":
raise RuntimeError("module attribute failed")
raise AttributeError(name)

[file testmodule.py]
def g(x: int) -> int:
return x + 1
[file driver.py]
from native import f
from native import f, read_value
from testutil import assertRaises

assert f(1) == 2
with assertRaises(RuntimeError, "module attribute failed"):
read_value()

[case testFromImportWithUntypedModule]

Expand Down
Loading