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

Skip to content

Commit 7385adc

Browse files
committed
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.
1 parent b391b24 commit 7385adc

4 files changed

Lines changed: 472 additions & 456 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,8 +1573,13 @@ def _handle_fromlist(module, fromlist, import_):
15731573
fromlist.extend(module.__all__)
15741574
for x in fromlist:
15751575
if not hasattr(module, x):
1576-
_call_with_frames_removed(import_,
1577-
'{}.{}'.format(module.__name__, x))
1576+
try:
1577+
_call_with_frames_removed(import_,
1578+
'{}.{}'.format(module.__name__, x))
1579+
except ImportError:
1580+
# Backwards-compatibility dictates we ignore failed
1581+
# imports triggered by fromlist.
1582+
pass
15781583
return module
15791584

15801585

Lib/test/test_import.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ def test_timestamp_overflow(self):
334334
del sys.path[0]
335335
remove_files(TESTFN)
336336

337+
def test_bogus_fromlist(self):
338+
try:
339+
__import__('http', fromlist=['blah'])
340+
except ImportError:
341+
self.fail("fromlist must allow bogus names")
342+
337343

338344
class PycRewritingTests(unittest.TestCase):
339345
# Test that the `co_filename` attribute on code objects always points

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #15715: importlib.__import__() will silence an ImportError when the use
20+
of fromlist leads to a failed import.
21+
1922
- Issue #14669: Fix pickling of connections and sockets on MacOSX
2023
by sending/receiving an acknowledgment after file descriptor transfer.
2124
TestPicklingConnection has been reenabled for MacOSX.

0 commit comments

Comments
 (0)