ยปCore Development>Code coverage>Lib/test/test_importlib/import_/test_api.py

Python code coverage for Lib/test/test_importlib/import_/test_api.py

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