Issue #14599: Generalize a test for ImportError.path and add support · python/cpython@f0434e6 · GitHub
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

4 files changed

Lines changed: 26 additions & 27 deletions

File tree

Lib/test/test_imp.py

Lines changed: 11 additions & 0 deletions

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

0 commit comments

Comments
 (0)