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

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

#countcontent
1n/afrom .. import util
2n/aimport importlib._bootstrap
3n/aimport sys
4n/afrom types import MethodType
5n/aimport unittest
6n/aimport warnings
7n/a
8n/a
9n/aclass CallingOrder:
10n/a
11n/a """Calls to the importers on sys.meta_path happen in order that they are
12n/a specified in the sequence, starting with the first importer
13n/a [first called], and then continuing on down until one is found that doesn't
14n/a return None [continuing]."""
15n/a
16n/a
17n/a def test_first_called(self):
18n/a # [first called]
19n/a mod = 'top_level'
20n/a with util.mock_spec(mod) as first, util.mock_spec(mod) as second:
21n/a with util.import_state(meta_path=[first, second]):
22n/a self.assertIs(self.__import__(mod), first.modules[mod])
23n/a
24n/a def test_continuing(self):
25n/a # [continuing]
26n/a mod_name = 'for_real'
27n/a with util.mock_spec('nonexistent') as first, \
28n/a util.mock_spec(mod_name) as second:
29n/a first.find_spec = lambda self, fullname, path=None, parent=None: None
30n/a with util.import_state(meta_path=[first, second]):
31n/a self.assertIs(self.__import__(mod_name), second.modules[mod_name])
32n/a
33n/a def test_empty(self):
34n/a # Raise an ImportWarning if sys.meta_path is empty.
35n/a module_name = 'nothing'
36n/a try:
37n/a del sys.modules[module_name]
38n/a except KeyError:
39n/a pass
40n/a with util.import_state(meta_path=[]):
41n/a with warnings.catch_warnings(record=True) as w:
42n/a warnings.simplefilter('always')
43n/a self.assertIsNone(importlib._bootstrap._find_spec('nothing',
44n/a None))
45n/a self.assertEqual(len(w), 1)
46n/a self.assertTrue(issubclass(w[-1].category, ImportWarning))
47n/a
48n/a
49n/a(Frozen_CallingOrder,
50n/a Source_CallingOrder
51n/a ) = util.test_both(CallingOrder, __import__=util.__import__)
52n/a
53n/a
54n/aclass CallSignature:
55n/a
56n/a """If there is no __path__ entry on the parent module, then 'path' is None
57n/a [no path]. Otherwise, the value for __path__ is passed in for the 'path'
58n/a argument [path set]."""
59n/a
60n/a def log_finder(self, importer):
61n/a fxn = getattr(importer, self.finder_name)
62n/a log = []
63n/a def wrapper(self, *args, **kwargs):
64n/a log.append([args, kwargs])
65n/a return fxn(*args, **kwargs)
66n/a return log, wrapper
67n/a
68n/a def test_no_path(self):
69n/a # [no path]
70n/a mod_name = 'top_level'
71n/a assert '.' not in mod_name
72n/a with self.mock_modules(mod_name) as importer:
73n/a log, wrapped_call = self.log_finder(importer)
74n/a setattr(importer, self.finder_name, MethodType(wrapped_call, importer))
75n/a with util.import_state(meta_path=[importer]):
76n/a self.__import__(mod_name)
77n/a assert len(log) == 1
78n/a args = log[0][0]
79n/a # Assuming all arguments are positional.
80n/a self.assertEqual(args[0], mod_name)
81n/a self.assertIsNone(args[1])
82n/a
83n/a def test_with_path(self):
84n/a # [path set]
85n/a pkg_name = 'pkg'
86n/a mod_name = pkg_name + '.module'
87n/a path = [42]
88n/a assert '.' in mod_name
89n/a with self.mock_modules(pkg_name+'.__init__', mod_name) as importer:
90n/a importer.modules[pkg_name].__path__ = path
91n/a log, wrapped_call = self.log_finder(importer)
92n/a setattr(importer, self.finder_name, MethodType(wrapped_call, importer))
93n/a with util.import_state(meta_path=[importer]):
94n/a self.__import__(mod_name)
95n/a assert len(log) == 2
96n/a args = log[1][0]
97n/a kwargs = log[1][1]
98n/a # Assuming all arguments are positional.
99n/a self.assertFalse(kwargs)
100n/a self.assertEqual(args[0], mod_name)
101n/a self.assertIs(args[1], path)
102n/a
103n/a
104n/aclass CallSignaturePEP302(CallSignature):
105n/a mock_modules = util.mock_modules
106n/a finder_name = 'find_module'
107n/a
108n/a
109n/a(Frozen_CallSignaturePEP302,
110n/a Source_CallSignaturePEP302
111n/a ) = util.test_both(CallSignaturePEP302, __import__=util.__import__)
112n/a
113n/a
114n/aclass CallSignaturePEP451(CallSignature):
115n/a mock_modules = util.mock_spec
116n/a finder_name = 'find_spec'
117n/a
118n/a
119n/a(Frozen_CallSignaturePEP451,
120n/a Source_CallSignaturePEP451
121n/a ) = util.test_both(CallSignaturePEP451, __import__=util.__import__)
122n/a
123n/a
124n/aif __name__ == '__main__':
125n/a unittest.main()