Skip to content

Commit f0434e6

Browse files
committed
Issue #14599: Generalize a test for ImportError.path and add support
in Python/dynload_shlibs.c. This should fix the remaining importlib test failure on Windows. Support in AIX and HP-UX will be in a separate checkin.
1 parent 3c23a87 commit f0434e6

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

Lib/test/test_imp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ def test_issue9319(self):
179179
self.assertRaises(SyntaxError,
180180
imp.find_module, "badsyntax_pep3120", [path])
181181

182+
def test_load_dynamic_ImportError_path(self):
183+
# Issue #1559549 added `name` and `path` attributes to ImportError
184+
# in order to provide better detail. Issue #10854 implemented those
185+
# attributes on import failures of extensions on Windows.
186+
path = 'bogus file path'
187+
name = 'extension'
188+
with self.assertRaises(ImportError) as err:
189+
imp.load_dynamic(name, path)
190+
self.assertIn(path, err.exception.path)
191+
self.assertEqual(name, err.exception.name)
192+
182193

183194
class ReloadTests(unittest.TestCase):
184195

Lib/test/test_import.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,28 +337,6 @@ def test_timestamp_overflow(self):
337337
del sys.path[0]
338338
remove_files(TESTFN)
339339

340-
@unittest.skipUnless(sys.platform == "win32", "Windows-specific")
341-
def test_extension_import_fail(self):
342-
# Issue 1559549 added `name` and `path` attributes to ImportError
343-
# in order to provide better detail. Issue 10854 implemented those
344-
# attributes on import failures of extensions on Windows.
345-
debug = True if sys.executable[-6:] == "_d.exe" else False
346-
pkg_name = "extension"
347-
pkg_file = pkg_name + "{}".format("_d.pyd" if debug else ".pyd")
348-
with open(pkg_file, "w"): pass
349-
importlib.invalidate_caches()
350-
try:
351-
with self.assertRaises(ImportError) as err:
352-
import extension
353-
self.assertEqual(err.exception.name, pkg_name)
354-
# The path we get back has the dot-slash, e.g., ".\\extension.pyd"
355-
self.assertIsNotNone(err.exception.path,
356-
'unexpected None for ImportError.path: '
357-
'{!r}'.format(err.exception))
358-
self.assertEqual(os.path.relpath(err.exception.path), pkg_file)
359-
finally:
360-
unlink(pkg_file)
361-
362340

363341
class PycRewritingTests(unittest.TestCase):
364342
# Test that the `co_filename` attribute on code objects always points

Python/dynload_shlib.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,19 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
129129
handle = dlopen(pathname, dlopenflags);
130130

131131
if (handle == NULL) {
132+
PyObject *mod_name = NULL;
133+
PyObject *path = NULL;
134+
PyObject *error_ob = NULL;
132135
const char *error = dlerror();
133136
if (error == NULL)
134137
error = "unknown dlopen() error";
135-
PyErr_SetString(PyExc_ImportError, error);
138+
error_ob = PyUnicode_FromString(error);
139+
path = PyUnicode_FromString(pathname);
140+
mod_name = PyUnicode_FromString(shortname);
141+
PyErr_SetImportError(error_ob, mod_name, path);
142+
Py_DECREF(error_ob);
143+
Py_DECREF(path);
144+
Py_DECREF(mod_name);
136145
return NULL;
137146
}
138147
if (fp != NULL && nhandles < 128)

Python/importdl.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
7474
if (PyErr_Occurred())
7575
goto error;
7676
if (p == NULL) {
77-
PyErr_Format(PyExc_ImportError,
78-
"dynamic module does not define init function"
79-
" (PyInit_%s)",
80-
shortname);
77+
PyObject *msg = PyUnicode_FromFormat("dynamic module does not define "
78+
"init function (PyInit_%s)",
79+
shortname);
80+
PyErr_SetImportError(msg, name, path);
81+
Py_DECREF(msg);
8182
goto error;
8283
}
8384
oldcontext = _Py_PackageContext;

0 commit comments

Comments
 (0)