mirror of
https://github.com/AdaCore/cpython.git
synced 2026-02-12 12:57:15 -08:00
Issue #24492: make sure that `from ... import ... raises an
ImportError if __name__ is not defined on a package. Thanks to Armin Rigo for the bug report and diagnosing the cause.
This commit is contained in:
@@ -5085,19 +5085,24 @@ import_from(PyObject *v, PyObject *name)
|
||||
sys.modules. */
|
||||
PyErr_Clear();
|
||||
pkgname = _PyObject_GetAttrId(v, &PyId___name__);
|
||||
if (pkgname == NULL)
|
||||
return NULL;
|
||||
if (pkgname == NULL) {
|
||||
goto error;
|
||||
}
|
||||
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
|
||||
Py_DECREF(pkgname);
|
||||
if (fullmodname == NULL)
|
||||
if (fullmodname == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
|
||||
if (x == NULL)
|
||||
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
|
||||
else
|
||||
Py_INCREF(x);
|
||||
Py_DECREF(fullmodname);
|
||||
if (x == NULL) {
|
||||
goto error;
|
||||
}
|
||||
Py_INCREF(x);
|
||||
return x;
|
||||
error:
|
||||
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
|
||||
Reference in New Issue
Block a user