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