diff options
author | Brett Cannon <brett@python.org> | 2012-08-24 22:25:59 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-08-24 22:25:59 (GMT) |
commit | 12c6bda4f0e96c4bea285d8c664044753ea81bf1 (patch) | |
tree | 8f7f0d38129542f4c8388c4ad20863ee9c457df9 /Lib/test/test_importlib | |
parent | 7a54d16dc5684a2279bf3bd1e6bbb10c74c09850 (diff) | |
download | cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.zip cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.tar.gz cpython-12c6bda4f0e96c4bea285d8c664044753ea81bf1.tar.bz2 |
Issue #15316: Let exceptions raised during imports triggered by the
fromlist of __import__ propagate.
The problem previously was that if something listed in fromlist didn't
exist then that's okay. The fix for that was too broad in terms of
catching ImportError.
The trick with the solution to this issue is that the proper
refactoring of import thanks to importlib doesn't allow for a way to
distinguish (portably) between an ImportError because finders couldn't
find a loader, or a loader raised the exception. In Python 3.4 the
hope is to introduce a new exception (e.g. ModuleNotFound) to make it
clean to differentiate why ImportError was raised.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r-- | Lib/test/test_importlib/import_/test_api.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py index 2fa1f90..3d4cd94 100644 --- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -1,7 +1,22 @@ +from .. import util as importlib_test_util from . import util +import imp +import sys import unittest +class BadLoaderFinder: + bad = 'fine.bogus' + @classmethod + def find_module(cls, fullname, path): + if fullname == cls.bad: + return cls + @classmethod + def load_module(cls, fullname): + if fullname == cls.bad: + raise ImportError('I cannot be loaded!') + + class APITest(unittest.TestCase): """Test API-specific details for __import__ (e.g. raising the right @@ -19,6 +34,29 @@ class APITest(unittest.TestCase): with self.assertRaises(ValueError): util.import_('os', globals(), level=-1) + def test_nonexistent_fromlist_entry(self): + # If something in fromlist doesn't exist, that's okay. + # issue15715 + mod = imp.new_module('fine') + mod.__path__ = ['XXX'] + with importlib_test_util.import_state(meta_path=[BadLoaderFinder]): + with importlib_test_util.uncache('fine'): + sys.modules['fine'] = mod + util.import_('fine', fromlist=['not here']) + + def test_fromlist_load_error_propagates(self): + # If something in fromlist triggers an exception not related to not + # existing, let that exception propagate. + # issue15316 + mod = imp.new_module('fine') + mod.__path__ = ['XXX'] + with importlib_test_util.import_state(meta_path=[BadLoaderFinder]): + with importlib_test_util.uncache('fine'): + sys.modules['fine'] = mod + with self.assertRaises(ImportError): + util.import_('fine', fromlist=['bogus']) + + def test_main(): from test.support import run_unittest |