diff options
author | Matthias Bussonnier <bussonniermatthias@gmail.com> | 2017-02-22 15:06:50 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2017-02-22 15:06:50 (GMT) |
commit | 1bc156430bad8177b5beecf57979628c1d071230 (patch) | |
tree | 1f24f4fa81da3e01e94b15e138d13e7dcdb2bbcb /Python | |
parent | d37c068e695f8ec72b5c1b5a5a5ece2337fda768 (diff) | |
download | cpython-1bc156430bad8177b5beecf57979628c1d071230.zip cpython-1bc156430bad8177b5beecf57979628c1d071230.tar.gz cpython-1bc156430bad8177b5beecf57979628c1d071230.tar.bz2 |
bpo-29546: Improve from-import error message with location (#103)
bpo-29546: Improve from-import error message with location
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 69c9383..81c89df 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, *pkgpath; + PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown; x = PyObject_GetAttr(v, name); if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) @@ -5009,7 +5009,6 @@ import_from(PyObject *v, PyObject *name) goto error; } fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); - Py_DECREF(pkgname); if (fullmodname == NULL) { return NULL; } @@ -5018,18 +5017,36 @@ import_from(PyObject *v, PyObject *name) if (x == NULL) { goto error; } + Py_DECREF(pkgname); Py_INCREF(x); return x; error: pkgpath = PyModule_GetFilenameObject(v); + if (pkgname == NULL) { + pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); + if (pkgname_or_unknown == NULL) { + Py_XDECREF(pkgpath); + return NULL; + } + } else { + pkgname_or_unknown = pkgname; + } if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { PyErr_Clear(); - PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, NULL); + PyErr_SetImportError( + PyUnicode_FromFormat("cannot import name %R from %R (unknown location)", + name, pkgname_or_unknown), + pkgname, NULL); } else { - PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, pkgpath); + PyErr_SetImportError( + PyUnicode_FromFormat("cannot import name %R from %R (%S)", + name, pkgname_or_unknown, pkgpath), + pkgname, pkgpath); } + Py_XDECREF(pkgname_or_unknown); + Py_XDECREF(pkgpath); return NULL; } |