Skip to content

Commit bc4bed4

Browse files
Carreaubrettcannon
authored andcommitted
bpo-29546: Set 'path' on ImportError for from ... import ... (GH-91)
1 parent 5ec08ce commit bc4bed4

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Lib/test/test_import/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ def test_from_import_missing_attr_raises_ImportError(self):
8080
with self.assertRaises(ImportError):
8181
from importlib import something_that_should_not_exist_anywhere
8282

83+
def test_from_import_missing_attr_has_name_and_path(self):
84+
with self.assertRaises(ImportError) as cm:
85+
from os import i_dont_exist
86+
self.assertEqual(cm.exception.name, 'os')
87+
self.assertEqual(cm.exception.path, os.__file__)
88+
89+
def test_from_import_missing_attr_has_name(self):
90+
with self.assertRaises(ImportError) as cm:
91+
# _warning has no path as it's a built-in module.
92+
from _warning import i_dont_exist
93+
self.assertEqual(cm.exception.name, '_warning')
94+
self.assertIsNone(cm.exception.path)
95+
96+
def test_from_import_missing_attr_path_is_canonical(self):
97+
with self.assertRaises(ImportError) as cm:
98+
from os.path import i_dont_exist
99+
self.assertIn(cm.exception.name, {'posixpath', 'ntpath'})
100+
self.assertIsNotNone(cm.exception)
101+
83102
def test_case_sensitivity(self):
84103
# Brief digression to test that import is case-sensitive: if we got
85104
# this far, we know for sure that "random" exists.

Misc/NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Core and Builtins
1212

1313
- bpo-29438: Fixed use-after-free problem in key sharing dict.
1414

15+
- bpo-29546: Set the 'path' and 'name' attribute on ImportError for ``from ... import ...``.
16+
1517
- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
1618

1719
- Issue #29337: Fixed possible BytesWarning when compare the code objects.

Python/ceval.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -4995,7 +4995,7 @@ import_from(PyObject *v, PyObject *name)
49954995
{
49964996
PyObject *x;
49974997
_Py_IDENTIFIER(__name__);
4998-
PyObject *fullmodname, *pkgname;
4998+
PyObject *fullmodname, *pkgname, *pkgpath;
49994999

50005000
x = PyObject_GetAttr(v, name);
50015001
if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -5021,7 +5021,15 @@ import_from(PyObject *v, PyObject *name)
50215021
Py_INCREF(x);
50225022
return x;
50235023
error:
5024-
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5024+
pkgpath = PyModule_GetFilenameObject(v);
5025+
5026+
if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
5027+
PyErr_Clear();
5028+
PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, NULL);
5029+
} else {
5030+
PyErr_SetImportError(PyUnicode_FromFormat("cannot import name %R", name), pkgname, pkgpath);
5031+
}
5032+
50255033
return NULL;
50265034
}
50275035

0 commit comments

Comments
 (0)