[Python-checkins] cpython: Issue #15715: Ignore failed imports triggered by the use of fromlist.
brett.cannon
python-checkins at python.org
Fri Aug 17 19:21:24 CEST 2012
http://hg.python.org/cpython/rev/0d52f125dd32
changeset: 78619:0d52f125dd32
user: Brett Cannon <brett at python.org>
date: Fri Aug 17 13:21:16 2012 -0400
summary:
Issue #15715: Ignore failed imports triggered by the use of fromlist.
When the fromlist argument is specified for __import__() and the
attribute doesn't already exist, an import is attempted. If that fails
(e.g. module doesn't exist), the ImportError will now be silenced (for
backwards-compatibility). This *does not* affect
``from ... import ...`` statements.
Thanks to Eric Snow for the patch and Simon Feltman for reporting the
regression.
files:
Lib/importlib/_bootstrap.py | 9 +-
Lib/test/test_import.py | 6 +
Misc/NEWS | 3 +
Python/importlib.h | 914 ++++++++++++-----------
4 files changed, 474 insertions(+), 458 deletions(-)
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1573,8 +1573,13 @@
fromlist.extend(module.__all__)
for x in fromlist:
if not hasattr(module, x):
- _call_with_frames_removed(import_,
- '{}.{}'.format(module.__name__, x))
+ try:
+ _call_with_frames_removed(import_,
+ '{}.{}'.format(module.__name__, x))
+ except ImportError:
+ # Backwards-compatibility dictates we ignore failed
+ # imports triggered by fromlist.
+ pass
return module
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
@@ -334,6 +334,12 @@
del sys.path[0]
remove_files(TESTFN)
+ def test_bogus_fromlist(self):
+ try:
+ __import__('http', fromlist=['blah'])
+ except ImportError:
+ self.fail("fromlist must allow bogus names")
+
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
@@ -16,6 +16,9 @@
Library
-------
+- Issue #15715: importlib.__import__() will silence an ImportError when the use
+ of fromlist leads to a failed import.
+
- Issue #14669: Fix pickling of connections and sockets on MacOSX
by sending/receiving an acknowledgment after file descriptor transfer.
TestPicklingConnection has been reenabled for MacOSX.
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list