| 1 | n/a | from .. import util |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from importlib import machinery |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import types |
|---|
| 6 | n/a | import unittest |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | PKG_NAME = 'fine' |
|---|
| 9 | n/a | SUBMOD_NAME = 'fine.bogus' |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | class BadSpecFinderLoader: |
|---|
| 13 | n/a | @classmethod |
|---|
| 14 | n/a | def find_spec(cls, fullname, path=None, target=None): |
|---|
| 15 | n/a | if fullname == SUBMOD_NAME: |
|---|
| 16 | n/a | spec = machinery.ModuleSpec(fullname, cls) |
|---|
| 17 | n/a | return spec |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | @staticmethod |
|---|
| 20 | n/a | def create_module(spec): |
|---|
| 21 | n/a | return None |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | @staticmethod |
|---|
| 24 | n/a | def exec_module(module): |
|---|
| 25 | n/a | if module.__name__ == SUBMOD_NAME: |
|---|
| 26 | n/a | raise ImportError('I cannot be loaded!') |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | class BadLoaderFinder: |
|---|
| 30 | n/a | @classmethod |
|---|
| 31 | n/a | def find_module(cls, fullname, path): |
|---|
| 32 | n/a | if fullname == SUBMOD_NAME: |
|---|
| 33 | n/a | return cls |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | @classmethod |
|---|
| 36 | n/a | def load_module(cls, fullname): |
|---|
| 37 | n/a | if fullname == SUBMOD_NAME: |
|---|
| 38 | n/a | raise ImportError('I cannot be loaded!') |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | class APITest: |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | """Test API-specific details for __import__ (e.g. raising the right |
|---|
| 44 | n/a | exception when passing in an int for the module name).""" |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def test_raises_ModuleNotFoundError(self): |
|---|
| 47 | n/a | with self.assertRaises(ModuleNotFoundError): |
|---|
| 48 | n/a | util.import_importlib('some module that does not exist') |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def test_name_requires_rparition(self): |
|---|
| 51 | n/a | # Raise TypeError if a non-string is passed in for the module name. |
|---|
| 52 | n/a | with self.assertRaises(TypeError): |
|---|
| 53 | n/a | self.__import__(42) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def test_negative_level(self): |
|---|
| 56 | n/a | # Raise ValueError when a negative level is specified. |
|---|
| 57 | n/a | # PEP 328 did away with sys.module None entries and the ambiguity of |
|---|
| 58 | n/a | # absolute/relative imports. |
|---|
| 59 | n/a | with self.assertRaises(ValueError): |
|---|
| 60 | n/a | self.__import__('os', globals(), level=-1) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def test_nonexistent_fromlist_entry(self): |
|---|
| 63 | n/a | # If something in fromlist doesn't exist, that's okay. |
|---|
| 64 | n/a | # issue15715 |
|---|
| 65 | n/a | mod = types.ModuleType(PKG_NAME) |
|---|
| 66 | n/a | mod.__path__ = ['XXX'] |
|---|
| 67 | n/a | with util.import_state(meta_path=[self.bad_finder_loader]): |
|---|
| 68 | n/a | with util.uncache(PKG_NAME): |
|---|
| 69 | n/a | sys.modules[PKG_NAME] = mod |
|---|
| 70 | n/a | self.__import__(PKG_NAME, fromlist=['not here']) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def test_fromlist_load_error_propagates(self): |
|---|
| 73 | n/a | # If something in fromlist triggers an exception not related to not |
|---|
| 74 | n/a | # existing, let that exception propagate. |
|---|
| 75 | n/a | # issue15316 |
|---|
| 76 | n/a | mod = types.ModuleType(PKG_NAME) |
|---|
| 77 | n/a | mod.__path__ = ['XXX'] |
|---|
| 78 | n/a | with util.import_state(meta_path=[self.bad_finder_loader]): |
|---|
| 79 | n/a | with util.uncache(PKG_NAME): |
|---|
| 80 | n/a | sys.modules[PKG_NAME] = mod |
|---|
| 81 | n/a | with self.assertRaises(ImportError): |
|---|
| 82 | n/a | self.__import__(PKG_NAME, |
|---|
| 83 | n/a | fromlist=[SUBMOD_NAME.rpartition('.')[-1]]) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | class OldAPITests(APITest): |
|---|
| 87 | n/a | bad_finder_loader = BadLoaderFinder |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | (Frozen_OldAPITests, |
|---|
| 91 | n/a | Source_OldAPITests |
|---|
| 92 | n/a | ) = util.test_both(OldAPITests, __import__=util.__import__) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | class SpecAPITests(APITest): |
|---|
| 96 | n/a | bad_finder_loader = BadSpecFinderLoader |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | (Frozen_SpecAPITests, |
|---|
| 100 | n/a | Source_SpecAPITests |
|---|
| 101 | n/a | ) = util.test_both(SpecAPITests, __import__=util.__import__) |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | if __name__ == '__main__': |
|---|
| 105 | n/a | unittest.main() |
|---|