]> granicus.if.org Git - python/commitdiff
Issue #18342: Use the repr of a module name for ``from ... import
authorBrett Cannon <brett@python.org>
Fri, 12 Jul 2013 15:22:26 +0000 (11:22 -0400)
committerBrett Cannon <brett@python.org>
Fri, 12 Jul 2013 15:22:26 +0000 (11:22 -0400)
...`` when an ImportError occurs.

Other cases had already been switched over to using the repr.

Thanks to Tomasz Maćkowiak for the patch.

Lib/test/test_import.py
Misc/ACKS
Misc/NEWS
Python/ceval.c

index e6a43792ee3ff07b6c27cd24b2262d42be3641ac..61df86a841ba84a69f72b32448adfe0b18f8861f 100644 (file)
@@ -321,6 +321,14 @@ class ImportTests(unittest.TestCase):
         stdout, stderr = popen.communicate()
         self.assertIn(b"ImportError", stdout)
 
+    def test_from_import_message_for_nonexistent_module(self):
+        with self.assertRaisesRegexp(ImportError, "^No module named 'bogus'"):
+            from bogus import foo
+
+    def test_from_import_message_for_existing_module(self):
+        with self.assertRaisesRegexp(ImportError, "^cannot import name 'bogus'"):
+            from re import bogus
+
 
 @skip_if_dont_write_bytecode
 class FilePermissionTests(unittest.TestCase):
index abfa659347258573cc483875f50c2eed7b67c62e..ef5fb5c816cfb847b7a2d325a17019b66ff6069d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -786,6 +786,7 @@ Andrew I MacIntyre
 Tim MacKenzie
 Nick Maclaren
 Don MacMillen
+Tomasz Maćkowiak
 Steve Majewski
 Grzegorz Makarewicz
 David Malcolm
index 751433fd3983651ad2a295da0a2a5aebc9527625..b65498abe463c21fe600475504a06ca11092b72b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #18342: Use the repr of a module name when an import fails when using
+  ``from ... import ...``.
+
 - Issue #17872: Fix a segfault in marshal.load() when input stream returns
   more bytes than requested.
 
index a53e377d4b7d39c49fe5973fd492898578b5dbd2..61928e39fe7722056de16387faa5ead670483fea 100644 (file)
@@ -4602,7 +4602,7 @@ import_from(PyObject *v, PyObject *name)
 
     x = PyObject_GetAttr(v, name);
     if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
-        PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
+        PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
     }
     return x;
 }