Commit 7385adc8 authored by Brett Cannon's avatar Brett Cannon

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.
parent b391b24e
...@@ -1573,8 +1573,13 @@ def _handle_fromlist(module, fromlist, import_): ...@@ -1573,8 +1573,13 @@ def _handle_fromlist(module, fromlist, import_):
fromlist.extend(module.__all__) fromlist.extend(module.__all__)
for x in fromlist: for x in fromlist:
if not hasattr(module, x): if not hasattr(module, x):
_call_with_frames_removed(import_, try:
'{}.{}'.format(module.__name__, x)) _call_with_frames_removed(import_,
'{}.{}'.format(module.__name__, x))
except ImportError:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist.
pass
return module return module
......
...@@ -334,6 +334,12 @@ class ImportTests(unittest.TestCase): ...@@ -334,6 +334,12 @@ class ImportTests(unittest.TestCase):
del sys.path[0] del sys.path[0]
remove_files(TESTFN) 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): class PycRewritingTests(unittest.TestCase):
# Test that the `co_filename` attribute on code objects always points # Test that the `co_filename` attribute on code objects always points
......
...@@ -16,6 +16,9 @@ Core and Builtins ...@@ -16,6 +16,9 @@ Core and Builtins
Library 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 - Issue #14669: Fix pickling of connections and sockets on MacOSX
by sending/receiving an acknowledgment after file descriptor transfer. by sending/receiving an acknowledgment after file descriptor transfer.
TestPicklingConnection has been reenabled for MacOSX. TestPicklingConnection has been reenabled for MacOSX.
......
This diff is collapsed.
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