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

Skip to content

Commit a79e4fb

Browse files
committed
Issue #18342: Use the repr of a module name for ``from ... import
...`` when an ImportError occurs. Other cases had already been switched over to using the repr. Thanks to Tomasz Maćkowiak for the patch.
1 parent f0cb692 commit a79e4fb

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_import.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ def test_delete_builtins_import(self):
321321
stdout, stderr = popen.communicate()
322322
self.assertIn(b"ImportError", stdout)
323323

324+
def test_from_import_message_for_nonexistent_module(self):
325+
with self.assertRaisesRegexp(ImportError, "^No module named 'bogus'"):
326+
from bogus import foo
327+
328+
def test_from_import_message_for_existing_module(self):
329+
with self.assertRaisesRegexp(ImportError, "^cannot import name 'bogus'"):
330+
from re import bogus
331+
324332

325333
@skip_if_dont_write_bytecode
326334
class FilePermissionTests(unittest.TestCase):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ Andrew I MacIntyre
786786
Tim MacKenzie
787787
Nick Maclaren
788788
Don MacMillen
789+
Tomasz Maćkowiak
789790
Steve Majewski
790791
Grzegorz Makarewicz
791792
David Malcolm

Misc/NEWS

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

13+
- Issue #18342: Use the repr of a module name when an import fails when using
14+
``from ... import ...``.
15+
1316
- Issue #17872: Fix a segfault in marshal.load() when input stream returns
1417
more bytes than requested.
1518

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4602,7 +4602,7 @@ import_from(PyObject *v, PyObject *name)
46024602

46034603
x = PyObject_GetAttr(v, name);
46044604
if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4605-
PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4605+
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
46064606
}
46074607
return x;
46084608
}

0 commit comments

Comments
 (0)