Skip to content
Merged
8 changes: 8 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,14 @@ def test_issue105979(self):
self.assertIn("Frozen object named 'x' is invalid",
str(cm.exception))

def test_frozen_module_from_import_error(self):
with self.assertRaises(ImportError) as cm:
from os import this_will_never_exist
self.assertRegex(
str(cm.exception),
r"cannot import name 'this_will_never_exist' from 'os' \(.*Lib[\\/]os\.py\)"
)

def test_script_shadowing_stdlib(self):
script_errors = [
(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When raising ImportError for missing symbols in ``from`` imports, use ``__file__`` in the error message if `__spec__.origin` is not a location
13 changes: 13 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,19 @@ _PyEval_ImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
}
else {
assert(rc == 0);
if (origin == NULL) {
// Fall back to __file__ for diagnostics if we don't have
// an origin that is a location
origin = PyModule_GetFilenameObject(v);
if (origin == NULL) {
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
goto done;
}
// PyModule_GetFilenameObject raised "module filename missing"
_PyErr_Clear(tstate);
}
assert(origin == NULL || PyUnicode_Check(origin));
}
if (origin) {
errmsg = PyUnicode_FromFormat(
"cannot import name %R from %R (%S)",
Expand Down
Loading