Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b206a80

Browse files
committed
Fix #10854. Make use of the new path and name attributes on ImportError
for extension modules on Windows.
1 parent 1543981 commit b206a80

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lib/test/test_import.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,24 @@ 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+
try:
350+
with self.assertRaises(ImportError) as err:
351+
import extension
352+
self.assertEqual(err.exception.name, pkg_name)
353+
# The path we get back has the dot-slash, e.g., ".\\extension.pyd"
354+
self.assertEqual(os.path.relpath(err.exception.path), pkg_file)
355+
finally:
356+
unlink(pkg_file)
357+
340358

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

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.3.0 Alpha 3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10854: The ImportError raised when an extension module on Windows
14+
fails to import now uses the new path and name attributes from
15+
Issue #1559549.
16+
1317
- Issue #14582: Import directly returns the module as returned by a loader when
1418
possible instead of fetching it from sys.modules.
1519

Python/dynload_win.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
254254
theLength));
255255
}
256256
if (message != NULL) {
257-
PyErr_SetObject(PyExc_ImportError, message);
258-
Py_DECREF(message);
257+
PyErr_SetFromImportErrorWithNameAndPath(message,
258+
PyUnicode_FromString(shortname),
259+
pathname);
259260
}
260261
return NULL;
261262
} else {

0 commit comments

Comments
 (0)