]> granicus.if.org Git - python/commitdiff
Fix #10854. Make use of the new path and name attributes on ImportError
authorBrian Curtin <brian@python.org>
Mon, 16 Apr 2012 05:10:17 +0000 (00:10 -0500)
committerBrian Curtin <brian@python.org>
Mon, 16 Apr 2012 05:10:17 +0000 (00:10 -0500)
for extension modules on Windows.

Lib/test/test_import.py
Misc/NEWS
Python/dynload_win.c

index 8510eb87564089d73901814bdb73f471c2d4a3c6..5053d49f4639982ede5468592ba6d2114b80f8ba 100644 (file)
@@ -337,6 +337,24 @@ class ImportTests(unittest.TestCase):
             del sys.path[0]
             remove_files(TESTFN)
 
+    @unittest.skipUnless(sys.platform == "win32", "Windows specific")
+    def test_extension_import_fail(self):
+        # Issue 1559549 added `name` and `path` attributes to ImportError
+        # in order to provide better detail. Issue 10854 implemented those
+        # attributes on import failures of extensions on Windows.
+        debug = True if sys.executable[-6:] == "_d.exe" else False
+        pkg_name = "extension"
+        pkg_file = pkg_name + "{}".format("_d.pyd" if debug else ".pyd")
+        with open(pkg_file, "w"): pass
+        try:
+            with self.assertRaises(ImportError) as err:
+                import extension
+            self.assertEqual(err.exception.name, pkg_name)
+            # The path we get back has the dot-slash, e.g., ".\\extension.pyd"
+            self.assertEqual(os.path.relpath(err.exception.path), pkg_file)
+        finally:
+            unlink(pkg_file)
+
 
 class PycRewritingTests(unittest.TestCase):
     # Test that the `co_filename` attribute on code objects always points
index 874f28720dd9ff8de8c2b0546d8d184371be262e..97ee354c9ba676fc11d4947dd3b188f4fc30472c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.3.0 Alpha 3?
 Core and Builtins
 -----------------
 
+- Issue #10854: The ImportError raised when an extension module on Windows
+  fails to import now uses the new path and name attributes from
+  Issue #1559549.
+
 - Issue #14582: Import directly returns the module as returned by a loader when
   possible instead of fetching it from sys.modules.
 
index 2cbfe9f30d21d2b5d2234a5785408a0e9c4ff56e..ef3e2c5958a344634598b607ca463d4691f24de7 100644 (file)
@@ -254,8 +254,9 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
                         theLength));
             }
             if (message != NULL) {
-                PyErr_SetObject(PyExc_ImportError, message);
-                Py_DECREF(message);
+                PyErr_SetFromImportErrorWithNameAndPath(message,
+                                        PyUnicode_FromString(shortname),
+                                        pathname);
             }
             return NULL;
         } else {