diff options
author | Matthias Bussonnier <bussonniermatthias@gmail.com> | 2017-02-15 00:05:25 (GMT) |
---|---|---|
committer | Brett Cannon <brettcannon@users.noreply.github.com> | 2017-02-15 00:05:25 (GMT) |
commit | bc4bed440504597cac47d0a215ee094bfa99ba7e (patch) | |
tree | 0598b2b4776f859b7ab15f70e7cdb65475c5d8c1 /Python/ceval.c | |
parent | 5ec08cea9574cf53c985af5dbed6bc3d56ff58b7 (diff) | |
download | cpython-bc4bed440504597cac47d0a215ee094bfa99ba7e.zip cpython-bc4bed440504597cac47d0a215ee094bfa99ba7e.tar.gz cpython-bc4bed440504597cac47d0a215ee094bfa99ba7e.tar.bz2 |
bpo-29546: Set 'path' on ImportError for ``from ... import ...`` (GH-91)
Diffstat (limited to 'Python/ceval.c')
-rw-r--r-- | Python/ceval.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 66fd361..69c9383 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4995,7 +4995,7 @@ import_from(PyObject *v, PyObject *name) { PyObject *x; _Py_IDENTIFIER(__name__); - PyObject *fullmodname, *pkgname; + PyObject *fullmodname, *pkgname, *pkgpath; x = PyObject_GetAttr(v, name); if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) @@ -5021,7 +5021,15 @@ import_from(PyObject *v, PyObject *name) Py_INCREF(x); return x; error: - PyErr_Format(PyExc_ImportError, "cannot import name %R", name); + pkgpath = PyModule_GetFilenameObject(v); + + if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { + PyErr_Clear(); + PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, NULL); + } else { + PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, pkgpath); + } + return NULL; } |