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

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

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