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

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

#countcontent
1n/afrom .. import util
2n/a
3n/aimportlib = util.import_importlib('importlib')
4n/amachinery = util.import_importlib('importlib.machinery')
5n/a
6n/aimport os
7n/aimport sys
8n/aimport tempfile
9n/afrom types import ModuleType
10n/aimport unittest
11n/aimport warnings
12n/aimport zipimport
13n/a
14n/a
15n/aclass FinderTests:
16n/a
17n/a """Tests for PathFinder."""
18n/a
19n/a find = None
20n/a check_found = None
21n/a
22n/a def test_failure(self):
23n/a # Test None returned upon not finding a suitable loader.
24n/a module = '<test module>'
25n/a with util.import_state():
26n/a self.assertIsNone(self.find(module))
27n/a
28n/a def test_sys_path(self):
29n/a # Test that sys.path is used when 'path' is None.
30n/a # Implicitly tests that sys.path_importer_cache is used.
31n/a module = '<test module>'
32n/a path = '<test path>'
33n/a importer = util.mock_spec(module)
34n/a with util.import_state(path_importer_cache={path: importer},
35n/a path=[path]):
36n/a found = self.find(module)
37n/a self.check_found(found, importer)
38n/a
39n/a def test_path(self):
40n/a # Test that 'path' is used when set.
41n/a # Implicitly tests that sys.path_importer_cache is used.
42n/a module = '<test module>'
43n/a path = '<test path>'
44n/a importer = util.mock_spec(module)
45n/a with util.import_state(path_importer_cache={path: importer}):
46n/a found = self.find(module, [path])
47n/a self.check_found(found, importer)
48n/a
49n/a def test_empty_list(self):
50n/a # An empty list should not count as asking for sys.path.
51n/a module = 'module'
52n/a path = '<test path>'
53n/a importer = util.mock_spec(module)
54n/a with util.import_state(path_importer_cache={path: importer},
55n/a path=[path]):
56n/a self.assertIsNone(self.find('module', []))
57n/a
58n/a def test_path_hooks(self):
59n/a # Test that sys.path_hooks is used.
60n/a # Test that sys.path_importer_cache is set.
61n/a module = '<test module>'
62n/a path = '<test path>'
63n/a importer = util.mock_spec(module)
64n/a hook = util.mock_path_hook(path, importer=importer)
65n/a with util.import_state(path_hooks=[hook]):
66n/a found = self.find(module, [path])
67n/a self.check_found(found, importer)
68n/a self.assertIn(path, sys.path_importer_cache)
69n/a self.assertIs(sys.path_importer_cache[path], importer)
70n/a
71n/a def test_empty_path_hooks(self):
72n/a # Test that if sys.path_hooks is empty a warning is raised,
73n/a # sys.path_importer_cache gets None set, and PathFinder returns None.
74n/a path_entry = 'bogus_path'
75n/a with util.import_state(path_importer_cache={}, path_hooks=[],
76n/a path=[path_entry]):
77n/a with warnings.catch_warnings(record=True) as w:
78n/a warnings.simplefilter('always')
79n/a self.assertIsNone(self.find('os'))
80n/a self.assertIsNone(sys.path_importer_cache[path_entry])
81n/a self.assertEqual(len(w), 1)
82n/a self.assertTrue(issubclass(w[-1].category, ImportWarning))
83n/a
84n/a def test_path_importer_cache_empty_string(self):
85n/a # The empty string should create a finder using the cwd.
86n/a path = ''
87n/a module = '<test module>'
88n/a importer = util.mock_spec(module)
89n/a hook = util.mock_path_hook(os.getcwd(), importer=importer)
90n/a with util.import_state(path=[path], path_hooks=[hook]):
91n/a found = self.find(module)
92n/a self.check_found(found, importer)
93n/a self.assertIn(os.getcwd(), sys.path_importer_cache)
94n/a
95n/a def test_None_on_sys_path(self):
96n/a # Putting None in sys.path[0] caused an import regression from Python
97n/a # 3.2: http://bugs.python.org/issue16514
98n/a new_path = sys.path[:]
99n/a new_path.insert(0, None)
100n/a new_path_importer_cache = sys.path_importer_cache.copy()
101n/a new_path_importer_cache.pop(None, None)
102n/a new_path_hooks = [zipimport.zipimporter,
103n/a self.machinery.FileFinder.path_hook(
104n/a *self.importlib._bootstrap_external._get_supported_file_loaders())]
105n/a missing = object()
106n/a email = sys.modules.pop('email', missing)
107n/a try:
108n/a with util.import_state(meta_path=sys.meta_path[:],
109n/a path=new_path,
110n/a path_importer_cache=new_path_importer_cache,
111n/a path_hooks=new_path_hooks):
112n/a module = self.importlib.import_module('email')
113n/a self.assertIsInstance(module, ModuleType)
114n/a finally:
115n/a if email is not missing:
116n/a sys.modules['email'] = email
117n/a
118n/a def test_finder_with_find_module(self):
119n/a class TestFinder:
120n/a def find_module(self, fullname):
121n/a return self.to_return
122n/a failing_finder = TestFinder()
123n/a failing_finder.to_return = None
124n/a path = 'testing path'
125n/a with util.import_state(path_importer_cache={path: failing_finder}):
126n/a self.assertIsNone(
127n/a self.machinery.PathFinder.find_spec('whatever', [path]))
128n/a success_finder = TestFinder()
129n/a success_finder.to_return = __loader__
130n/a with util.import_state(path_importer_cache={path: success_finder}):
131n/a spec = self.machinery.PathFinder.find_spec('whatever', [path])
132n/a self.assertEqual(spec.loader, __loader__)
133n/a
134n/a def test_finder_with_find_loader(self):
135n/a class TestFinder:
136n/a loader = None
137n/a portions = []
138n/a def find_loader(self, fullname):
139n/a return self.loader, self.portions
140n/a path = 'testing path'
141n/a with util.import_state(path_importer_cache={path: TestFinder()}):
142n/a self.assertIsNone(
143n/a self.machinery.PathFinder.find_spec('whatever', [path]))
144n/a success_finder = TestFinder()
145n/a success_finder.loader = __loader__
146n/a with util.import_state(path_importer_cache={path: success_finder}):
147n/a spec = self.machinery.PathFinder.find_spec('whatever', [path])
148n/a self.assertEqual(spec.loader, __loader__)
149n/a
150n/a def test_finder_with_find_spec(self):
151n/a class TestFinder:
152n/a spec = None
153n/a def find_spec(self, fullname, target=None):
154n/a return self.spec
155n/a path = 'testing path'
156n/a with util.import_state(path_importer_cache={path: TestFinder()}):
157n/a self.assertIsNone(
158n/a self.machinery.PathFinder.find_spec('whatever', [path]))
159n/a success_finder = TestFinder()
160n/a success_finder.spec = self.machinery.ModuleSpec('whatever', __loader__)
161n/a with util.import_state(path_importer_cache={path: success_finder}):
162n/a got = self.machinery.PathFinder.find_spec('whatever', [path])
163n/a self.assertEqual(got, success_finder.spec)
164n/a
165n/a def test_deleted_cwd(self):
166n/a # Issue #22834
167n/a old_dir = os.getcwd()
168n/a self.addCleanup(os.chdir, old_dir)
169n/a new_dir = tempfile.mkdtemp()
170n/a try:
171n/a os.chdir(new_dir)
172n/a try:
173n/a os.rmdir(new_dir)
174n/a except OSError:
175n/a # EINVAL on Solaris, EBUSY on AIX, ENOTEMPTY on Windows
176n/a self.skipTest("platform does not allow "
177n/a "the deletion of the cwd")
178n/a except:
179n/a os.chdir(old_dir)
180n/a os.rmdir(new_dir)
181n/a raise
182n/a
183n/a with util.import_state(path=['']):
184n/a # Do not want FileNotFoundError raised.
185n/a self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
186n/a
187n/a
188n/aclass FindModuleTests(FinderTests):
189n/a def find(self, *args, **kwargs):
190n/a return self.machinery.PathFinder.find_module(*args, **kwargs)
191n/a def check_found(self, found, importer):
192n/a self.assertIs(found, importer)
193n/a
194n/a
195n/a(Frozen_FindModuleTests,
196n/a Source_FindModuleTests
197n/a) = util.test_both(FindModuleTests, importlib=importlib, machinery=machinery)
198n/a
199n/a
200n/aclass FindSpecTests(FinderTests):
201n/a def find(self, *args, **kwargs):
202n/a return self.machinery.PathFinder.find_spec(*args, **kwargs)
203n/a def check_found(self, found, importer):
204n/a self.assertIs(found.loader, importer)
205n/a
206n/a
207n/a(Frozen_FindSpecTests,
208n/a Source_FindSpecTests
209n/a ) = util.test_both(FindSpecTests, importlib=importlib, machinery=machinery)
210n/a
211n/a
212n/aclass PathEntryFinderTests:
213n/a
214n/a def test_finder_with_failing_find_spec(self):
215n/a # PathEntryFinder with find_module() defined should work.
216n/a # Issue #20763.
217n/a class Finder:
218n/a path_location = 'test_finder_with_find_module'
219n/a def __init__(self, path):
220n/a if path != self.path_location:
221n/a raise ImportError
222n/a
223n/a @staticmethod
224n/a def find_module(fullname):
225n/a return None
226n/a
227n/a
228n/a with util.import_state(path=[Finder.path_location]+sys.path[:],
229n/a path_hooks=[Finder]):
230n/a self.machinery.PathFinder.find_spec('importlib')
231n/a
232n/a def test_finder_with_failing_find_module(self):
233n/a # PathEntryFinder with find_module() defined should work.
234n/a # Issue #20763.
235n/a class Finder:
236n/a path_location = 'test_finder_with_find_module'
237n/a def __init__(self, path):
238n/a if path != self.path_location:
239n/a raise ImportError
240n/a
241n/a @staticmethod
242n/a def find_module(fullname):
243n/a return None
244n/a
245n/a
246n/a with util.import_state(path=[Finder.path_location]+sys.path[:],
247n/a path_hooks=[Finder]):
248n/a self.machinery.PathFinder.find_module('importlib')
249n/a
250n/a
251n/a(Frozen_PEFTests,
252n/a Source_PEFTests
253n/a ) = util.test_both(PathEntryFinderTests, machinery=machinery)
254n/a
255n/a
256n/aif __name__ == '__main__':
257n/a unittest.main()