1 | n/a | from test.support import run_unittest, unload, check_warnings, CleanImport |
---|
2 | n/a | import unittest |
---|
3 | n/a | import sys |
---|
4 | n/a | import importlib |
---|
5 | n/a | from importlib.util import spec_from_file_location |
---|
6 | n/a | import pkgutil |
---|
7 | n/a | import os |
---|
8 | n/a | import os.path |
---|
9 | n/a | import tempfile |
---|
10 | n/a | import shutil |
---|
11 | n/a | import zipfile |
---|
12 | n/a | |
---|
13 | n/a | # Note: pkgutil.walk_packages is currently tested in test_runpy. This is |
---|
14 | n/a | # a hack to get a major issue resolved for 3.3b2. Longer term, it should |
---|
15 | n/a | # be moved back here, perhaps by factoring out the helper code for |
---|
16 | n/a | # creating interesting package layouts to a separate module. |
---|
17 | n/a | # Issue #15348 declares this is indeed a dodgy hack ;) |
---|
18 | n/a | |
---|
19 | n/a | class PkgutilTests(unittest.TestCase): |
---|
20 | n/a | |
---|
21 | n/a | def setUp(self): |
---|
22 | n/a | self.dirname = tempfile.mkdtemp() |
---|
23 | n/a | self.addCleanup(shutil.rmtree, self.dirname) |
---|
24 | n/a | sys.path.insert(0, self.dirname) |
---|
25 | n/a | |
---|
26 | n/a | def tearDown(self): |
---|
27 | n/a | del sys.path[0] |
---|
28 | n/a | |
---|
29 | n/a | def test_getdata_filesys(self): |
---|
30 | n/a | pkg = 'test_getdata_filesys' |
---|
31 | n/a | |
---|
32 | n/a | # Include a LF and a CRLF, to test that binary data is read back |
---|
33 | n/a | RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' |
---|
34 | n/a | |
---|
35 | n/a | # Make a package with some resources |
---|
36 | n/a | package_dir = os.path.join(self.dirname, pkg) |
---|
37 | n/a | os.mkdir(package_dir) |
---|
38 | n/a | # Empty init.py |
---|
39 | n/a | f = open(os.path.join(package_dir, '__init__.py'), "wb") |
---|
40 | n/a | f.close() |
---|
41 | n/a | # Resource files, res.txt, sub/res.txt |
---|
42 | n/a | f = open(os.path.join(package_dir, 'res.txt'), "wb") |
---|
43 | n/a | f.write(RESOURCE_DATA) |
---|
44 | n/a | f.close() |
---|
45 | n/a | os.mkdir(os.path.join(package_dir, 'sub')) |
---|
46 | n/a | f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb") |
---|
47 | n/a | f.write(RESOURCE_DATA) |
---|
48 | n/a | f.close() |
---|
49 | n/a | |
---|
50 | n/a | # Check we can read the resources |
---|
51 | n/a | res1 = pkgutil.get_data(pkg, 'res.txt') |
---|
52 | n/a | self.assertEqual(res1, RESOURCE_DATA) |
---|
53 | n/a | res2 = pkgutil.get_data(pkg, 'sub/res.txt') |
---|
54 | n/a | self.assertEqual(res2, RESOURCE_DATA) |
---|
55 | n/a | |
---|
56 | n/a | del sys.modules[pkg] |
---|
57 | n/a | |
---|
58 | n/a | def test_getdata_zipfile(self): |
---|
59 | n/a | zip = 'test_getdata_zipfile.zip' |
---|
60 | n/a | pkg = 'test_getdata_zipfile' |
---|
61 | n/a | |
---|
62 | n/a | # Include a LF and a CRLF, to test that binary data is read back |
---|
63 | n/a | RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line' |
---|
64 | n/a | |
---|
65 | n/a | # Make a package with some resources |
---|
66 | n/a | zip_file = os.path.join(self.dirname, zip) |
---|
67 | n/a | z = zipfile.ZipFile(zip_file, 'w') |
---|
68 | n/a | |
---|
69 | n/a | # Empty init.py |
---|
70 | n/a | z.writestr(pkg + '/__init__.py', "") |
---|
71 | n/a | # Resource files, res.txt, sub/res.txt |
---|
72 | n/a | z.writestr(pkg + '/res.txt', RESOURCE_DATA) |
---|
73 | n/a | z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA) |
---|
74 | n/a | z.close() |
---|
75 | n/a | |
---|
76 | n/a | # Check we can read the resources |
---|
77 | n/a | sys.path.insert(0, zip_file) |
---|
78 | n/a | res1 = pkgutil.get_data(pkg, 'res.txt') |
---|
79 | n/a | self.assertEqual(res1, RESOURCE_DATA) |
---|
80 | n/a | res2 = pkgutil.get_data(pkg, 'sub/res.txt') |
---|
81 | n/a | self.assertEqual(res2, RESOURCE_DATA) |
---|
82 | n/a | |
---|
83 | n/a | names = [] |
---|
84 | n/a | for moduleinfo in pkgutil.iter_modules([zip_file]): |
---|
85 | n/a | self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) |
---|
86 | n/a | names.append(moduleinfo.name) |
---|
87 | n/a | self.assertEqual(names, ['test_getdata_zipfile']) |
---|
88 | n/a | |
---|
89 | n/a | del sys.path[0] |
---|
90 | n/a | |
---|
91 | n/a | del sys.modules[pkg] |
---|
92 | n/a | |
---|
93 | n/a | def test_unreadable_dir_on_syspath(self): |
---|
94 | n/a | # issue7367 - walk_packages failed if unreadable dir on sys.path |
---|
95 | n/a | package_name = "unreadable_package" |
---|
96 | n/a | d = os.path.join(self.dirname, package_name) |
---|
97 | n/a | # this does not appear to create an unreadable dir on Windows |
---|
98 | n/a | # but the test should not fail anyway |
---|
99 | n/a | os.mkdir(d, 0) |
---|
100 | n/a | self.addCleanup(os.rmdir, d) |
---|
101 | n/a | for t in pkgutil.walk_packages(path=[self.dirname]): |
---|
102 | n/a | self.fail("unexpected package found") |
---|
103 | n/a | |
---|
104 | n/a | def test_walkpackages_filesys(self): |
---|
105 | n/a | pkg1 = 'test_walkpackages_filesys' |
---|
106 | n/a | pkg1_dir = os.path.join(self.dirname, pkg1) |
---|
107 | n/a | os.mkdir(pkg1_dir) |
---|
108 | n/a | f = open(os.path.join(pkg1_dir, '__init__.py'), "wb") |
---|
109 | n/a | f.close() |
---|
110 | n/a | os.mkdir(os.path.join(pkg1_dir, 'sub')) |
---|
111 | n/a | f = open(os.path.join(pkg1_dir, 'sub', '__init__.py'), "wb") |
---|
112 | n/a | f.close() |
---|
113 | n/a | f = open(os.path.join(pkg1_dir, 'sub', 'mod.py'), "wb") |
---|
114 | n/a | f.close() |
---|
115 | n/a | |
---|
116 | n/a | # Now, to juice it up, let's add the opposite packages, too. |
---|
117 | n/a | pkg2 = 'sub' |
---|
118 | n/a | pkg2_dir = os.path.join(self.dirname, pkg2) |
---|
119 | n/a | os.mkdir(pkg2_dir) |
---|
120 | n/a | f = open(os.path.join(pkg2_dir, '__init__.py'), "wb") |
---|
121 | n/a | f.close() |
---|
122 | n/a | os.mkdir(os.path.join(pkg2_dir, 'test_walkpackages_filesys')) |
---|
123 | n/a | f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', '__init__.py'), "wb") |
---|
124 | n/a | f.close() |
---|
125 | n/a | f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', 'mod.py'), "wb") |
---|
126 | n/a | f.close() |
---|
127 | n/a | |
---|
128 | n/a | expected = [ |
---|
129 | n/a | 'sub', |
---|
130 | n/a | 'sub.test_walkpackages_filesys', |
---|
131 | n/a | 'sub.test_walkpackages_filesys.mod', |
---|
132 | n/a | 'test_walkpackages_filesys', |
---|
133 | n/a | 'test_walkpackages_filesys.sub', |
---|
134 | n/a | 'test_walkpackages_filesys.sub.mod', |
---|
135 | n/a | ] |
---|
136 | n/a | actual= [e[1] for e in pkgutil.walk_packages([self.dirname])] |
---|
137 | n/a | self.assertEqual(actual, expected) |
---|
138 | n/a | |
---|
139 | n/a | for pkg in expected: |
---|
140 | n/a | if pkg.endswith('mod'): |
---|
141 | n/a | continue |
---|
142 | n/a | del sys.modules[pkg] |
---|
143 | n/a | |
---|
144 | n/a | def test_walkpackages_zipfile(self): |
---|
145 | n/a | """Tests the same as test_walkpackages_filesys, only with a zip file.""" |
---|
146 | n/a | |
---|
147 | n/a | zip = 'test_walkpackages_zipfile.zip' |
---|
148 | n/a | pkg1 = 'test_walkpackages_zipfile' |
---|
149 | n/a | pkg2 = 'sub' |
---|
150 | n/a | |
---|
151 | n/a | zip_file = os.path.join(self.dirname, zip) |
---|
152 | n/a | z = zipfile.ZipFile(zip_file, 'w') |
---|
153 | n/a | z.writestr(pkg2 + '/__init__.py', "") |
---|
154 | n/a | z.writestr(pkg2 + '/' + pkg1 + '/__init__.py', "") |
---|
155 | n/a | z.writestr(pkg2 + '/' + pkg1 + '/mod.py', "") |
---|
156 | n/a | z.writestr(pkg1 + '/__init__.py', "") |
---|
157 | n/a | z.writestr(pkg1 + '/' + pkg2 + '/__init__.py', "") |
---|
158 | n/a | z.writestr(pkg1 + '/' + pkg2 + '/mod.py', "") |
---|
159 | n/a | z.close() |
---|
160 | n/a | |
---|
161 | n/a | sys.path.insert(0, zip_file) |
---|
162 | n/a | expected = [ |
---|
163 | n/a | 'sub', |
---|
164 | n/a | 'sub.test_walkpackages_zipfile', |
---|
165 | n/a | 'sub.test_walkpackages_zipfile.mod', |
---|
166 | n/a | 'test_walkpackages_zipfile', |
---|
167 | n/a | 'test_walkpackages_zipfile.sub', |
---|
168 | n/a | 'test_walkpackages_zipfile.sub.mod', |
---|
169 | n/a | ] |
---|
170 | n/a | actual= [e[1] for e in pkgutil.walk_packages([zip_file])] |
---|
171 | n/a | self.assertEqual(actual, expected) |
---|
172 | n/a | del sys.path[0] |
---|
173 | n/a | |
---|
174 | n/a | for pkg in expected: |
---|
175 | n/a | if pkg.endswith('mod'): |
---|
176 | n/a | continue |
---|
177 | n/a | del sys.modules[pkg] |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | |
---|
181 | n/a | class PkgutilPEP302Tests(unittest.TestCase): |
---|
182 | n/a | |
---|
183 | n/a | class MyTestLoader(object): |
---|
184 | n/a | def create_module(self, spec): |
---|
185 | n/a | return None |
---|
186 | n/a | |
---|
187 | n/a | def exec_module(self, mod): |
---|
188 | n/a | # Count how many times the module is reloaded |
---|
189 | n/a | mod.__dict__['loads'] = mod.__dict__.get('loads', 0) + 1 |
---|
190 | n/a | |
---|
191 | n/a | def get_data(self, path): |
---|
192 | n/a | return "Hello, world!" |
---|
193 | n/a | |
---|
194 | n/a | class MyTestImporter(object): |
---|
195 | n/a | def find_spec(self, fullname, path=None, target=None): |
---|
196 | n/a | loader = PkgutilPEP302Tests.MyTestLoader() |
---|
197 | n/a | return spec_from_file_location(fullname, |
---|
198 | n/a | '<%s>' % loader.__class__.__name__, |
---|
199 | n/a | loader=loader, |
---|
200 | n/a | submodule_search_locations=[]) |
---|
201 | n/a | |
---|
202 | n/a | def setUp(self): |
---|
203 | n/a | sys.meta_path.insert(0, self.MyTestImporter()) |
---|
204 | n/a | |
---|
205 | n/a | def tearDown(self): |
---|
206 | n/a | del sys.meta_path[0] |
---|
207 | n/a | |
---|
208 | n/a | def test_getdata_pep302(self): |
---|
209 | n/a | # Use a dummy finder/loader |
---|
210 | n/a | self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!") |
---|
211 | n/a | del sys.modules['foo'] |
---|
212 | n/a | |
---|
213 | n/a | def test_alreadyloaded(self): |
---|
214 | n/a | # Ensure that get_data works without reloading - the "loads" module |
---|
215 | n/a | # variable in the example loader should count how many times a reload |
---|
216 | n/a | # occurs. |
---|
217 | n/a | import foo |
---|
218 | n/a | self.assertEqual(foo.loads, 1) |
---|
219 | n/a | self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!") |
---|
220 | n/a | self.assertEqual(foo.loads, 1) |
---|
221 | n/a | del sys.modules['foo'] |
---|
222 | n/a | |
---|
223 | n/a | |
---|
224 | n/a | # These tests, especially the setup and cleanup, are hideous. They |
---|
225 | n/a | # need to be cleaned up once issue 14715 is addressed. |
---|
226 | n/a | class ExtendPathTests(unittest.TestCase): |
---|
227 | n/a | def create_init(self, pkgname): |
---|
228 | n/a | dirname = tempfile.mkdtemp() |
---|
229 | n/a | sys.path.insert(0, dirname) |
---|
230 | n/a | |
---|
231 | n/a | pkgdir = os.path.join(dirname, pkgname) |
---|
232 | n/a | os.mkdir(pkgdir) |
---|
233 | n/a | with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl: |
---|
234 | n/a | fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n') |
---|
235 | n/a | |
---|
236 | n/a | return dirname |
---|
237 | n/a | |
---|
238 | n/a | def create_submodule(self, dirname, pkgname, submodule_name, value): |
---|
239 | n/a | module_name = os.path.join(dirname, pkgname, submodule_name + '.py') |
---|
240 | n/a | with open(module_name, 'w') as fl: |
---|
241 | n/a | print('value={}'.format(value), file=fl) |
---|
242 | n/a | |
---|
243 | n/a | def test_simple(self): |
---|
244 | n/a | pkgname = 'foo' |
---|
245 | n/a | dirname_0 = self.create_init(pkgname) |
---|
246 | n/a | dirname_1 = self.create_init(pkgname) |
---|
247 | n/a | self.create_submodule(dirname_0, pkgname, 'bar', 0) |
---|
248 | n/a | self.create_submodule(dirname_1, pkgname, 'baz', 1) |
---|
249 | n/a | import foo.bar |
---|
250 | n/a | import foo.baz |
---|
251 | n/a | # Ensure we read the expected values |
---|
252 | n/a | self.assertEqual(foo.bar.value, 0) |
---|
253 | n/a | self.assertEqual(foo.baz.value, 1) |
---|
254 | n/a | |
---|
255 | n/a | # Ensure the path is set up correctly |
---|
256 | n/a | self.assertEqual(sorted(foo.__path__), |
---|
257 | n/a | sorted([os.path.join(dirname_0, pkgname), |
---|
258 | n/a | os.path.join(dirname_1, pkgname)])) |
---|
259 | n/a | |
---|
260 | n/a | # Cleanup |
---|
261 | n/a | shutil.rmtree(dirname_0) |
---|
262 | n/a | shutil.rmtree(dirname_1) |
---|
263 | n/a | del sys.path[0] |
---|
264 | n/a | del sys.path[0] |
---|
265 | n/a | del sys.modules['foo'] |
---|
266 | n/a | del sys.modules['foo.bar'] |
---|
267 | n/a | del sys.modules['foo.baz'] |
---|
268 | n/a | |
---|
269 | n/a | |
---|
270 | n/a | # Another awful testing hack to be cleaned up once the test_runpy |
---|
271 | n/a | # helpers are factored out to a common location |
---|
272 | n/a | def test_iter_importers(self): |
---|
273 | n/a | iter_importers = pkgutil.iter_importers |
---|
274 | n/a | get_importer = pkgutil.get_importer |
---|
275 | n/a | |
---|
276 | n/a | pkgname = 'spam' |
---|
277 | n/a | modname = 'eggs' |
---|
278 | n/a | dirname = self.create_init(pkgname) |
---|
279 | n/a | pathitem = os.path.join(dirname, pkgname) |
---|
280 | n/a | fullname = '{}.{}'.format(pkgname, modname) |
---|
281 | n/a | sys.modules.pop(fullname, None) |
---|
282 | n/a | sys.modules.pop(pkgname, None) |
---|
283 | n/a | try: |
---|
284 | n/a | self.create_submodule(dirname, pkgname, modname, 0) |
---|
285 | n/a | |
---|
286 | n/a | importlib.import_module(fullname) |
---|
287 | n/a | |
---|
288 | n/a | importers = list(iter_importers(fullname)) |
---|
289 | n/a | expected_importer = get_importer(pathitem) |
---|
290 | n/a | for finder in importers: |
---|
291 | n/a | spec = pkgutil._get_spec(finder, fullname) |
---|
292 | n/a | loader = spec.loader |
---|
293 | n/a | try: |
---|
294 | n/a | loader = loader.loader |
---|
295 | n/a | except AttributeError: |
---|
296 | n/a | # For now we still allow raw loaders from |
---|
297 | n/a | # find_module(). |
---|
298 | n/a | pass |
---|
299 | n/a | self.assertIsInstance(finder, importlib.machinery.FileFinder) |
---|
300 | n/a | self.assertEqual(finder, expected_importer) |
---|
301 | n/a | self.assertIsInstance(loader, |
---|
302 | n/a | importlib.machinery.SourceFileLoader) |
---|
303 | n/a | self.assertIsNone(pkgutil._get_spec(finder, pkgname)) |
---|
304 | n/a | |
---|
305 | n/a | with self.assertRaises(ImportError): |
---|
306 | n/a | list(iter_importers('invalid.module')) |
---|
307 | n/a | |
---|
308 | n/a | with self.assertRaises(ImportError): |
---|
309 | n/a | list(iter_importers('.spam')) |
---|
310 | n/a | finally: |
---|
311 | n/a | shutil.rmtree(dirname) |
---|
312 | n/a | del sys.path[0] |
---|
313 | n/a | try: |
---|
314 | n/a | del sys.modules['spam'] |
---|
315 | n/a | del sys.modules['spam.eggs'] |
---|
316 | n/a | except KeyError: |
---|
317 | n/a | pass |
---|
318 | n/a | |
---|
319 | n/a | |
---|
320 | n/a | def test_mixed_namespace(self): |
---|
321 | n/a | pkgname = 'foo' |
---|
322 | n/a | dirname_0 = self.create_init(pkgname) |
---|
323 | n/a | dirname_1 = self.create_init(pkgname) |
---|
324 | n/a | self.create_submodule(dirname_0, pkgname, 'bar', 0) |
---|
325 | n/a | # Turn this into a PEP 420 namespace package |
---|
326 | n/a | os.unlink(os.path.join(dirname_0, pkgname, '__init__.py')) |
---|
327 | n/a | self.create_submodule(dirname_1, pkgname, 'baz', 1) |
---|
328 | n/a | import foo.bar |
---|
329 | n/a | import foo.baz |
---|
330 | n/a | # Ensure we read the expected values |
---|
331 | n/a | self.assertEqual(foo.bar.value, 0) |
---|
332 | n/a | self.assertEqual(foo.baz.value, 1) |
---|
333 | n/a | |
---|
334 | n/a | # Ensure the path is set up correctly |
---|
335 | n/a | self.assertEqual(sorted(foo.__path__), |
---|
336 | n/a | sorted([os.path.join(dirname_0, pkgname), |
---|
337 | n/a | os.path.join(dirname_1, pkgname)])) |
---|
338 | n/a | |
---|
339 | n/a | # Cleanup |
---|
340 | n/a | shutil.rmtree(dirname_0) |
---|
341 | n/a | shutil.rmtree(dirname_1) |
---|
342 | n/a | del sys.path[0] |
---|
343 | n/a | del sys.path[0] |
---|
344 | n/a | del sys.modules['foo'] |
---|
345 | n/a | del sys.modules['foo.bar'] |
---|
346 | n/a | del sys.modules['foo.baz'] |
---|
347 | n/a | |
---|
348 | n/a | # XXX: test .pkg files |
---|
349 | n/a | |
---|
350 | n/a | |
---|
351 | n/a | class NestedNamespacePackageTest(unittest.TestCase): |
---|
352 | n/a | |
---|
353 | n/a | def setUp(self): |
---|
354 | n/a | self.basedir = tempfile.mkdtemp() |
---|
355 | n/a | self.old_path = sys.path[:] |
---|
356 | n/a | |
---|
357 | n/a | def tearDown(self): |
---|
358 | n/a | sys.path[:] = self.old_path |
---|
359 | n/a | shutil.rmtree(self.basedir) |
---|
360 | n/a | |
---|
361 | n/a | def create_module(self, name, contents): |
---|
362 | n/a | base, final = name.rsplit('.', 1) |
---|
363 | n/a | base_path = os.path.join(self.basedir, base.replace('.', os.path.sep)) |
---|
364 | n/a | os.makedirs(base_path, exist_ok=True) |
---|
365 | n/a | with open(os.path.join(base_path, final + ".py"), 'w') as f: |
---|
366 | n/a | f.write(contents) |
---|
367 | n/a | |
---|
368 | n/a | def test_nested(self): |
---|
369 | n/a | pkgutil_boilerplate = ( |
---|
370 | n/a | 'import pkgutil; ' |
---|
371 | n/a | '__path__ = pkgutil.extend_path(__path__, __name__)') |
---|
372 | n/a | self.create_module('a.pkg.__init__', pkgutil_boilerplate) |
---|
373 | n/a | self.create_module('b.pkg.__init__', pkgutil_boilerplate) |
---|
374 | n/a | self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate) |
---|
375 | n/a | self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate) |
---|
376 | n/a | self.create_module('a.pkg.subpkg.c', 'c = 1') |
---|
377 | n/a | self.create_module('b.pkg.subpkg.d', 'd = 2') |
---|
378 | n/a | sys.path.insert(0, os.path.join(self.basedir, 'a')) |
---|
379 | n/a | sys.path.insert(0, os.path.join(self.basedir, 'b')) |
---|
380 | n/a | import pkg |
---|
381 | n/a | self.addCleanup(unload, 'pkg') |
---|
382 | n/a | self.assertEqual(len(pkg.__path__), 2) |
---|
383 | n/a | import pkg.subpkg |
---|
384 | n/a | self.addCleanup(unload, 'pkg.subpkg') |
---|
385 | n/a | self.assertEqual(len(pkg.subpkg.__path__), 2) |
---|
386 | n/a | from pkg.subpkg.c import c |
---|
387 | n/a | from pkg.subpkg.d import d |
---|
388 | n/a | self.assertEqual(c, 1) |
---|
389 | n/a | self.assertEqual(d, 2) |
---|
390 | n/a | |
---|
391 | n/a | |
---|
392 | n/a | class ImportlibMigrationTests(unittest.TestCase): |
---|
393 | n/a | # With full PEP 302 support in the standard import machinery, the |
---|
394 | n/a | # PEP 302 emulation in this module is in the process of being |
---|
395 | n/a | # deprecated in favour of importlib proper |
---|
396 | n/a | |
---|
397 | n/a | def check_deprecated(self): |
---|
398 | n/a | return check_warnings( |
---|
399 | n/a | ("This emulation is deprecated, use 'importlib' instead", |
---|
400 | n/a | DeprecationWarning)) |
---|
401 | n/a | |
---|
402 | n/a | def test_importer_deprecated(self): |
---|
403 | n/a | with self.check_deprecated(): |
---|
404 | n/a | pkgutil.ImpImporter("") |
---|
405 | n/a | |
---|
406 | n/a | def test_loader_deprecated(self): |
---|
407 | n/a | with self.check_deprecated(): |
---|
408 | n/a | pkgutil.ImpLoader("", "", "", "") |
---|
409 | n/a | |
---|
410 | n/a | def test_get_loader_avoids_emulation(self): |
---|
411 | n/a | with check_warnings() as w: |
---|
412 | n/a | self.assertIsNotNone(pkgutil.get_loader("sys")) |
---|
413 | n/a | self.assertIsNotNone(pkgutil.get_loader("os")) |
---|
414 | n/a | self.assertIsNotNone(pkgutil.get_loader("test.support")) |
---|
415 | n/a | self.assertEqual(len(w.warnings), 0) |
---|
416 | n/a | |
---|
417 | n/a | @unittest.skipIf(__name__ == '__main__', 'not compatible with __main__') |
---|
418 | n/a | def test_get_loader_handles_missing_loader_attribute(self): |
---|
419 | n/a | global __loader__ |
---|
420 | n/a | this_loader = __loader__ |
---|
421 | n/a | del __loader__ |
---|
422 | n/a | try: |
---|
423 | n/a | with check_warnings() as w: |
---|
424 | n/a | self.assertIsNotNone(pkgutil.get_loader(__name__)) |
---|
425 | n/a | self.assertEqual(len(w.warnings), 0) |
---|
426 | n/a | finally: |
---|
427 | n/a | __loader__ = this_loader |
---|
428 | n/a | |
---|
429 | n/a | def test_get_loader_handles_missing_spec_attribute(self): |
---|
430 | n/a | name = 'spam' |
---|
431 | n/a | mod = type(sys)(name) |
---|
432 | n/a | del mod.__spec__ |
---|
433 | n/a | with CleanImport(name): |
---|
434 | n/a | sys.modules[name] = mod |
---|
435 | n/a | loader = pkgutil.get_loader(name) |
---|
436 | n/a | self.assertIsNone(loader) |
---|
437 | n/a | |
---|
438 | n/a | def test_get_loader_handles_spec_attribute_none(self): |
---|
439 | n/a | name = 'spam' |
---|
440 | n/a | mod = type(sys)(name) |
---|
441 | n/a | mod.__spec__ = None |
---|
442 | n/a | with CleanImport(name): |
---|
443 | n/a | sys.modules[name] = mod |
---|
444 | n/a | loader = pkgutil.get_loader(name) |
---|
445 | n/a | self.assertIsNone(loader) |
---|
446 | n/a | |
---|
447 | n/a | def test_get_loader_None_in_sys_modules(self): |
---|
448 | n/a | name = 'totally bogus' |
---|
449 | n/a | sys.modules[name] = None |
---|
450 | n/a | try: |
---|
451 | n/a | loader = pkgutil.get_loader(name) |
---|
452 | n/a | finally: |
---|
453 | n/a | del sys.modules[name] |
---|
454 | n/a | self.assertIsNone(loader) |
---|
455 | n/a | |
---|
456 | n/a | def test_find_loader_missing_module(self): |
---|
457 | n/a | name = 'totally bogus' |
---|
458 | n/a | loader = pkgutil.find_loader(name) |
---|
459 | n/a | self.assertIsNone(loader) |
---|
460 | n/a | |
---|
461 | n/a | def test_find_loader_avoids_emulation(self): |
---|
462 | n/a | with check_warnings() as w: |
---|
463 | n/a | self.assertIsNotNone(pkgutil.find_loader("sys")) |
---|
464 | n/a | self.assertIsNotNone(pkgutil.find_loader("os")) |
---|
465 | n/a | self.assertIsNotNone(pkgutil.find_loader("test.support")) |
---|
466 | n/a | self.assertEqual(len(w.warnings), 0) |
---|
467 | n/a | |
---|
468 | n/a | def test_get_importer_avoids_emulation(self): |
---|
469 | n/a | # We use an illegal path so *none* of the path hooks should fire |
---|
470 | n/a | with check_warnings() as w: |
---|
471 | n/a | self.assertIsNone(pkgutil.get_importer("*??")) |
---|
472 | n/a | self.assertEqual(len(w.warnings), 0) |
---|
473 | n/a | |
---|
474 | n/a | def test_iter_importers_avoids_emulation(self): |
---|
475 | n/a | with check_warnings() as w: |
---|
476 | n/a | for importer in pkgutil.iter_importers(): pass |
---|
477 | n/a | self.assertEqual(len(w.warnings), 0) |
---|
478 | n/a | |
---|
479 | n/a | |
---|
480 | n/a | def test_main(): |
---|
481 | n/a | run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests, |
---|
482 | n/a | NestedNamespacePackageTest, ImportlibMigrationTests) |
---|
483 | n/a | # this is necessary if test is run repeated (like when finding leaks) |
---|
484 | n/a | import zipimport |
---|
485 | n/a | import importlib |
---|
486 | n/a | zipimport._zip_directory_cache.clear() |
---|
487 | n/a | importlib.invalidate_caches() |
---|
488 | n/a | |
---|
489 | n/a | |
---|
490 | n/a | if __name__ == '__main__': |
---|
491 | n/a | test_main() |
---|