Kaydet (Commit) a79e4fb3 authored tarafından Brett Cannon's avatar Brett Cannon

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.
üst f0cb6927
...@@ -321,6 +321,14 @@ class ImportTests(unittest.TestCase): ...@@ -321,6 +321,14 @@ class ImportTests(unittest.TestCase):
stdout, stderr = popen.communicate() stdout, stderr = popen.communicate()
self.assertIn(b"ImportError", stdout) 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 @skip_if_dont_write_bytecode
class FilePermissionTests(unittest.TestCase): class FilePermissionTests(unittest.TestCase):
......
...@@ -786,6 +786,7 @@ Andrew I MacIntyre ...@@ -786,6 +786,7 @@ Andrew I MacIntyre
Tim MacKenzie Tim MacKenzie
Nick Maclaren Nick Maclaren
Don MacMillen Don MacMillen
Tomasz Maćkowiak
Steve Majewski Steve Majewski
Grzegorz Makarewicz Grzegorz Makarewicz
David Malcolm David Malcolm
......
...@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1? ...@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
Core and Builtins 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 - Issue #17872: Fix a segfault in marshal.load() when input stream returns
more bytes than requested. more bytes than requested.
......
...@@ -4602,7 +4602,7 @@ import_from(PyObject *v, PyObject *name) ...@@ -4602,7 +4602,7 @@ import_from(PyObject *v, PyObject *name)
x = PyObject_GetAttr(v, name); x = PyObject_GetAttr(v, name);
if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { 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; return x;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment