[Python-checkins] cpython: Fix #10854. Make use of the new path and name attributes on ImportError
brian.curtin
python-checkins at python.org
Mon Apr 16 07:10:31 CEST 2012
http://hg.python.org/cpython/rev/f341b99bb370
changeset: 76344:f341b99bb370
user: Brian Curtin <brian at python.org>
date: Mon Apr 16 00:10:17 2012 -0500
summary:
Fix #10854. Make use of the new path and name attributes on ImportError
for extension modules on Windows.
files:
Lib/test/test_import.py | 18 ++++++++++++++++++
Misc/NEWS | 4 ++++
Python/dynload_win.c | 5 +++--
3 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -337,6 +337,24 @@
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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
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.
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -254,8 +254,9 @@
theLength));
}
if (message != NULL) {
- PyErr_SetObject(PyExc_ImportError, message);
- Py_DECREF(message);
+ PyErr_SetFromImportErrorWithNameAndPath(message,
+ PyUnicode_FromString(shortname),
+ pathname);
}
return NULL;
} else {
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list