Debian Codegraph @

lib-python/3/test/test_importlib/import_/test_api.py

raw symbols SCIP
1 from test.test_importlib import util
2
3 from importlib import machinery
4 import sys
5 import types
6 import unittest
7 import warnings
8
9 PKG_NAME = 'fine'
10 SUBMOD_NAME = 'fine.bogus'
11
12
13 class BadSpecFinderLoader:
14 @classmethod
15 def find_spec(cls, fullname, path=None, target=None):
16 if fullname == SUBMOD_NAME:
17 spec = machinery.ModuleSpec(fullname, cls)
18 return spec
19
20 @staticmethod
21 def create_module(spec):
22 return None
23
24 @staticmethod
25 def exec_module(module):
26 if module.__name__ == SUBMOD_NAME:
27 raise ImportError('I cannot be loaded!')
28
29
30 class BadLoaderFinder:
31 @classmethod
32 def find_module(cls, fullname, path):
33 if fullname == SUBMOD_NAME:
34 return cls
35
36 @classmethod
37 def load_module(cls, fullname):
38 if fullname == SUBMOD_NAME:
39 raise ImportError('I cannot be loaded!')
40
41
42 class APITest:
43
44 """Test API-specific details for __import__ (e.g. raising the right
45 exception when passing in an int for the module name)."""
46
47 def test_raises_ModuleNotFoundError(self):
48 with self.assertRaises(ModuleNotFoundError):
49 util.import_importlib('some module that does not exist')
50
51 def test_name_requires_rparition(self):
52 # Raise TypeError if a non-string is passed in for the module name.
53 with self.assertRaises(TypeError):
54 self.__import__(42)
55
56 def test_negative_level(self):
57 # Raise ValueError when a negative level is specified.
58 # PEP 328 did away with sys.module None entries and the ambiguity of
59 # absolute/relative imports.
60 with self.assertRaises(ValueError):
61 self.__import__('os', globals(), level=-1)
62
63 def test_nonexistent_fromlist_entry(self):
64 # If something in fromlist doesn't exist, that's okay.
65 # issue15715
66 mod = types.ModuleType(PKG_NAME)
67 mod.__path__ = ['XXX']
68 with util.import_state(meta_path=[self.bad_finder_loader]):
69 with util.uncache(PKG_NAME):
70 sys.modules[PKG_NAME] = mod
71 self.__import__(PKG_NAME, fromlist=['not here'])
72
73 def test_fromlist_load_error_propagates(self):
74 # If something in fromlist triggers an exception not related to not
75 # existing, let that exception propagate.
76 # issue15316
77 mod = types.ModuleType(PKG_NAME)
78 mod.__path__ = ['XXX']
79 with util.import_state(meta_path=[self.bad_finder_loader]):
80 with util.uncache(PKG_NAME):
81 sys.modules[PKG_NAME] = mod
82 with self.assertRaises(ImportError):
83 self.__import__(PKG_NAME,
84 fromlist=[SUBMOD_NAME.rpartition('.')[-1]])
85
86 def test_blocked_fromlist(self):
87 # If fromlist entry is None, let a ModuleNotFoundError propagate.
88 # issue31642
89 mod = types.ModuleType(PKG_NAME)
90 mod.__path__ = []
91 with util.import_state(meta_path=[self.bad_finder_loader]):
92 with util.uncache(PKG_NAME, SUBMOD_NAME):
93 sys.modules[PKG_NAME] = mod
94 sys.modules[SUBMOD_NAME] = None
95 with self.assertRaises(ModuleNotFoundError) as cm:
96 self.__import__(PKG_NAME,
97 fromlist=[SUBMOD_NAME.rpartition('.')[-1]])
98 self.assertEqual(cm.exception.name, SUBMOD_NAME)
99
100
101 class OldAPITests(APITest):
102 bad_finder_loader = BadLoaderFinder
103
104 def test_raises_ModuleNotFoundError(self):
105 with warnings.catch_warnings():
106 warnings.simplefilter("ignore", ImportWarning)
107 super().test_raises_ModuleNotFoundError()
108
109 def test_name_requires_rparition(self):
110 with warnings.catch_warnings():
111 warnings.simplefilter("ignore", ImportWarning)
112 super().test_name_requires_rparition()
113
114 def test_negative_level(self):
115 with warnings.catch_warnings():
116 warnings.simplefilter("ignore", ImportWarning)
117 super().test_negative_level()
118
119 def test_nonexistent_fromlist_entry(self):
120 with warnings.catch_warnings():
121 warnings.simplefilter("ignore", ImportWarning)
122 super().test_nonexistent_fromlist_entry()
123
124 def test_fromlist_load_error_propagates(self):
125 with warnings.catch_warnings():
126 warnings.simplefilter("ignore", ImportWarning)
127 super().test_fromlist_load_error_propagates
128
129 def test_blocked_fromlist(self):
130 with warnings.catch_warnings():
131 warnings.simplefilter("ignore", ImportWarning)
132 super().test_blocked_fromlist()
133
134
135 (Frozen_OldAPITests,
136 Source_OldAPITests
137 ) = util.test_both(OldAPITests, __import__=util.__import__)
138
139
140 class SpecAPITests(APITest):
141 bad_finder_loader = BadSpecFinderLoader
142
143
144 (Frozen_SpecAPITests,
145 Source_SpecAPITests
146 ) = util.test_both(SpecAPITests, __import__=util.__import__)
147
148
149 if __name__ == '__main__':
150 unittest.main()