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