blob: 57ebf1fd7fa630060706818b5c15aa2081714d94 [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
Brett Cannon9529fbf2013-06-15 17:11:25 -040010import types
Christian Heimesdae2a892008-04-19 00:55:37 +000011import shutil
12import zipfile
13
Nick Coghlan8ecf5042012-07-15 21:19:18 +100014# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
15# a hack to get a major issue resolved for 3.3b2. Longer term, it should
16# be moved back here, perhaps by factoring out the helper code for
17# creating interesting package layouts to a separate module.
18# Issue #15348 declares this is indeed a dodgy hack ;)
Christian Heimesdae2a892008-04-19 00:55:37 +000019
20class PkgutilTests(unittest.TestCase):
21
22 def setUp(self):
23 self.dirname = tempfile.mkdtemp()
Ned Deily7010a072011-10-07 12:01:40 -070024 self.addCleanup(shutil.rmtree, self.dirname)
Christian Heimesdae2a892008-04-19 00:55:37 +000025 sys.path.insert(0, self.dirname)
26
27 def tearDown(self):
28 del sys.path[0]
Christian Heimesdae2a892008-04-19 00:55:37 +000029
30 def test_getdata_filesys(self):
31 pkg = 'test_getdata_filesys'
32
33 # Include a LF and a CRLF, to test that binary data is read back
34 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
35
36 # Make a package with some resources
37 package_dir = os.path.join(self.dirname, pkg)
38 os.mkdir(package_dir)
39 # Empty init.py
40 f = open(os.path.join(package_dir, '__init__.py'), "wb")
41 f.close()
42 # Resource files, res.txt, sub/res.txt
43 f = open(os.path.join(package_dir, 'res.txt'), "wb")
44 f.write(RESOURCE_DATA)
45 f.close()
46 os.mkdir(os.path.join(package_dir, 'sub'))
47 f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
48 f.write(RESOURCE_DATA)
49 f.close()
50
51 # Check we can read the resources
52 res1 = pkgutil.get_data(pkg, 'res.txt')
53 self.assertEqual(res1, RESOURCE_DATA)
54 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
55 self.assertEqual(res2, RESOURCE_DATA)
56
57 del sys.modules[pkg]
58
59 def test_getdata_zipfile(self):
60 zip = 'test_getdata_zipfile.zip'
61 pkg = 'test_getdata_zipfile'
62
63 # Include a LF and a CRLF, to test that binary data is read back
64 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
65
66 # Make a package with some resources
67 zip_file = os.path.join(self.dirname, zip)
68 z = zipfile.ZipFile(zip_file, 'w')
69
70 # Empty init.py
71 z.writestr(pkg + '/__init__.py', "")
72 # Resource files, res.txt, sub/res.txt
73 z.writestr(pkg + '/res.txt', RESOURCE_DATA)
74 z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
75 z.close()
76
77 # Check we can read the resources
78 sys.path.insert(0, zip_file)
79 res1 = pkgutil.get_data(pkg, 'res.txt')
80 self.assertEqual(res1, RESOURCE_DATA)
81 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
82 self.assertEqual(res2, RESOURCE_DATA)
Alexandre Vassalotti515a74f2009-07-05 06:42:44 +000083
84 names = []
85 for loader, name, ispkg in pkgutil.iter_modules([zip_file]):
86 names.append(name)
87 self.assertEqual(names, ['test_getdata_zipfile'])
88
Christian Heimesdae2a892008-04-19 00:55:37 +000089 del sys.path[0]
90
91 del sys.modules[pkg]
92
Ned Deilycaf5a222011-10-06 14:19:06 -070093 def test_unreadable_dir_on_syspath(self):
94 # issue7367 - walk_packages failed if unreadable dir on sys.path
95 package_name = "unreadable_package"
96 d = os.path.join(self.dirname, package_name)
97 # this does not appear to create an unreadable dir on Windows
98 # but the test should not fail anyway
99 os.mkdir(d, 0)
Ned Deily7010a072011-10-07 12:01:40 -0700100 self.addCleanup(os.rmdir, d)
Ned Deilycaf5a222011-10-06 14:19:06 -0700101 for t in pkgutil.walk_packages(path=[self.dirname]):
102 self.fail("unexpected package found")
Ned Deilycaf5a222011-10-06 14:19:06 -0700103
Christian Heimesdae2a892008-04-19 00:55:37 +0000104class PkgutilPEP302Tests(unittest.TestCase):
105
106 class MyTestLoader(object):
Brett Cannon02d84542015-01-09 11:39:21 -0500107 def create_module(self, spec):
108 return None
109
Eric Snow37148b22014-01-04 15:09:53 -0700110 def exec_module(self, mod):
Christian Heimesdae2a892008-04-19 00:55:37 +0000111 # Count how many times the module is reloaded
Eric Snow37148b22014-01-04 15:09:53 -0700112 mod.__dict__['loads'] = mod.__dict__.get('loads', 0) + 1
Christian Heimesdae2a892008-04-19 00:55:37 +0000113
114 def get_data(self, path):
115 return "Hello, world!"
116
117 class MyTestImporter(object):
Eric Snow37148b22014-01-04 15:09:53 -0700118 def find_spec(self, fullname, path=None, target=None):
119 loader = PkgutilPEP302Tests.MyTestLoader()
120 return spec_from_file_location(fullname,
121 '<%s>' % loader.__class__.__name__,
122 loader=loader,
123 submodule_search_locations=[])
Christian Heimesdae2a892008-04-19 00:55:37 +0000124
125 def setUp(self):
126 sys.meta_path.insert(0, self.MyTestImporter())
127
128 def tearDown(self):
129 del sys.meta_path[0]
130
131 def test_getdata_pep302(self):
132 # Use a dummy importer/loader
133 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
134 del sys.modules['foo']
135
136 def test_alreadyloaded(self):
137 # Ensure that get_data works without reloading - the "loads" module
138 # variable in the example loader should count how many times a reload
139 # occurs.
140 import foo
141 self.assertEqual(foo.loads, 1)
142 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
143 self.assertEqual(foo.loads, 1)
144 del sys.modules['foo']
145
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400146
Eric V. Smith984b11f2012-05-24 20:21:04 -0400147# These tests, especially the setup and cleanup, are hideous. They
148# need to be cleaned up once issue 14715 is addressed.
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400149class ExtendPathTests(unittest.TestCase):
150 def create_init(self, pkgname):
151 dirname = tempfile.mkdtemp()
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400152 sys.path.insert(0, dirname)
153
154 pkgdir = os.path.join(dirname, pkgname)
155 os.mkdir(pkgdir)
156 with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
157 fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
158
159 return dirname
160
161 def create_submodule(self, dirname, pkgname, submodule_name, value):
162 module_name = os.path.join(dirname, pkgname, submodule_name + '.py')
163 with open(module_name, 'w') as fl:
164 print('value={}'.format(value), file=fl)
165
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400166 def test_simple(self):
Eric V. Smith984b11f2012-05-24 20:21:04 -0400167 pkgname = 'foo'
168 dirname_0 = self.create_init(pkgname)
169 dirname_1 = self.create_init(pkgname)
170 self.create_submodule(dirname_0, pkgname, 'bar', 0)
171 self.create_submodule(dirname_1, pkgname, 'baz', 1)
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400172 import foo.bar
173 import foo.baz
174 # Ensure we read the expected values
175 self.assertEqual(foo.bar.value, 0)
176 self.assertEqual(foo.baz.value, 1)
177
178 # Ensure the path is set up correctly
179 self.assertEqual(sorted(foo.__path__),
Eric V. Smith984b11f2012-05-24 20:21:04 -0400180 sorted([os.path.join(dirname_0, pkgname),
181 os.path.join(dirname_1, pkgname)]))
182
183 # Cleanup
184 shutil.rmtree(dirname_0)
185 shutil.rmtree(dirname_1)
186 del sys.path[0]
187 del sys.path[0]
188 del sys.modules['foo']
189 del sys.modules['foo.bar']
190 del sys.modules['foo.baz']
191
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000192
193 # Another awful testing hack to be cleaned up once the test_runpy
194 # helpers are factored out to a common location
195 def test_iter_importers(self):
196 iter_importers = pkgutil.iter_importers
197 get_importer = pkgutil.get_importer
198
199 pkgname = 'spam'
200 modname = 'eggs'
201 dirname = self.create_init(pkgname)
202 pathitem = os.path.join(dirname, pkgname)
203 fullname = '{}.{}'.format(pkgname, modname)
Eric Snow2ba66eb2013-11-22 13:55:23 -0700204 sys.modules.pop(fullname, None)
205 sys.modules.pop(pkgname, None)
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000206 try:
207 self.create_submodule(dirname, pkgname, modname, 0)
208
209 importlib.import_module(fullname)
210
211 importers = list(iter_importers(fullname))
212 expected_importer = get_importer(pathitem)
213 for finder in importers:
Eric Snow37148b22014-01-04 15:09:53 -0700214 spec = pkgutil._get_spec(finder, fullname)
215 loader = spec.loader
Eric Snowb523f842013-11-22 09:05:39 -0700216 try:
217 loader = loader.loader
218 except AttributeError:
219 # For now we still allow raw loaders from
220 # find_module().
221 pass
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000222 self.assertIsInstance(finder, importlib.machinery.FileFinder)
223 self.assertEqual(finder, expected_importer)
Eric Snowb523f842013-11-22 09:05:39 -0700224 self.assertIsInstance(loader,
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000225 importlib.machinery.SourceFileLoader)
Eric Snow37148b22014-01-04 15:09:53 -0700226 self.assertIsNone(pkgutil._get_spec(finder, pkgname))
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000227
228 with self.assertRaises(ImportError):
229 list(iter_importers('invalid.module'))
230
231 with self.assertRaises(ImportError):
232 list(iter_importers('.spam'))
233 finally:
234 shutil.rmtree(dirname)
235 del sys.path[0]
Eric Snowb523f842013-11-22 09:05:39 -0700236 try:
237 del sys.modules['spam']
238 del sys.modules['spam.eggs']
239 except KeyError:
240 pass
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000241
242
Eric V. Smith984b11f2012-05-24 20:21:04 -0400243 def test_mixed_namespace(self):
244 pkgname = 'foo'
245 dirname_0 = self.create_init(pkgname)
246 dirname_1 = self.create_init(pkgname)
247 self.create_submodule(dirname_0, pkgname, 'bar', 0)
248 # Turn this into a PEP 420 namespace package
249 os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
250 self.create_submodule(dirname_1, pkgname, 'baz', 1)
251 import foo.bar
252 import foo.baz
253 # Ensure we read the expected values
254 self.assertEqual(foo.bar.value, 0)
255 self.assertEqual(foo.baz.value, 1)
256
257 # Ensure the path is set up correctly
258 self.assertEqual(sorted(foo.__path__),
259 sorted([os.path.join(dirname_0, pkgname),
260 os.path.join(dirname_1, pkgname)]))
261
262 # Cleanup
263 shutil.rmtree(dirname_0)
264 shutil.rmtree(dirname_1)
265 del sys.path[0]
266 del sys.path[0]
267 del sys.modules['foo']
268 del sys.modules['foo.bar']
269 del sys.modules['foo.baz']
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400270
271 # XXX: test .pkg files
272
273
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200274class NestedNamespacePackageTest(unittest.TestCase):
275
276 def setUp(self):
277 self.basedir = tempfile.mkdtemp()
278 self.old_path = sys.path[:]
279
280 def tearDown(self):
281 sys.path[:] = self.old_path
282 shutil.rmtree(self.basedir)
283
284 def create_module(self, name, contents):
285 base, final = name.rsplit('.', 1)
286 base_path = os.path.join(self.basedir, base.replace('.', os.path.sep))
287 os.makedirs(base_path, exist_ok=True)
288 with open(os.path.join(base_path, final + ".py"), 'w') as f:
289 f.write(contents)
290
291 def test_nested(self):
292 pkgutil_boilerplate = (
293 'import pkgutil; '
294 '__path__ = pkgutil.extend_path(__path__, __name__)')
295 self.create_module('a.pkg.__init__', pkgutil_boilerplate)
296 self.create_module('b.pkg.__init__', pkgutil_boilerplate)
297 self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate)
298 self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate)
299 self.create_module('a.pkg.subpkg.c', 'c = 1')
300 self.create_module('b.pkg.subpkg.d', 'd = 2')
301 sys.path.insert(0, os.path.join(self.basedir, 'a'))
302 sys.path.insert(0, os.path.join(self.basedir, 'b'))
303 import pkg
304 self.addCleanup(unload, 'pkg')
305 self.assertEqual(len(pkg.__path__), 2)
306 import pkg.subpkg
307 self.addCleanup(unload, 'pkg.subpkg')
308 self.assertEqual(len(pkg.subpkg.__path__), 2)
309 from pkg.subpkg.c import c
310 from pkg.subpkg.d import d
311 self.assertEqual(c, 1)
312 self.assertEqual(d, 2)
313
314
Nick Coghlan85e729e2012-07-15 18:09:52 +1000315class ImportlibMigrationTests(unittest.TestCase):
316 # With full PEP 302 support in the standard import machinery, the
317 # PEP 302 emulation in this module is in the process of being
318 # deprecated in favour of importlib proper
319
320 def check_deprecated(self):
321 return check_warnings(
322 ("This emulation is deprecated, use 'importlib' instead",
323 DeprecationWarning))
324
325 def test_importer_deprecated(self):
326 with self.check_deprecated():
327 x = pkgutil.ImpImporter("")
328
329 def test_loader_deprecated(self):
330 with self.check_deprecated():
331 x = pkgutil.ImpLoader("", "", "", "")
332
333 def test_get_loader_avoids_emulation(self):
334 with check_warnings() as w:
335 self.assertIsNotNone(pkgutil.get_loader("sys"))
336 self.assertIsNotNone(pkgutil.get_loader("os"))
337 self.assertIsNotNone(pkgutil.get_loader("test.support"))
338 self.assertEqual(len(w.warnings), 0)
339
Nick Coghlandc855b72014-03-04 20:39:42 +1000340 def test_get_loader_handles_missing_loader_attribute(self):
341 global __loader__
342 this_loader = __loader__
343 del __loader__
344 try:
345 with check_warnings() as w:
346 self.assertIsNotNone(pkgutil.get_loader(__name__))
347 self.assertEqual(len(w.warnings), 0)
348 finally:
349 __loader__ = this_loader
350
Eric Snow658af312014-04-19 00:13:23 -0600351 def test_get_loader_handles_missing_spec_attribute(self):
352 name = 'spam'
353 mod = type(sys)(name)
354 del mod.__spec__
355 with CleanImport(name):
356 sys.modules[name] = mod
357 loader = pkgutil.get_loader(name)
358 self.assertIsNone(loader)
359
360 def test_get_loader_handles_spec_attribute_none(self):
361 name = 'spam'
362 mod = type(sys)(name)
363 mod.__spec__ = None
364 with CleanImport(name):
365 sys.modules[name] = mod
366 loader = pkgutil.get_loader(name)
367 self.assertIsNone(loader)
Nick Coghlandc855b72014-03-04 20:39:42 +1000368
Brett Cannon8447c702014-05-23 12:30:37 -0400369 def test_get_loader_None_in_sys_modules(self):
370 name = 'totally bogus'
371 sys.modules[name] = None
372 try:
373 loader = pkgutil.get_loader(name)
374 finally:
375 del sys.modules[name]
376 self.assertIsNone(loader)
377
378 def test_find_loader_missing_module(self):
379 name = 'totally bogus'
380 loader = pkgutil.find_loader(name)
381 self.assertIsNone(loader)
382
Nick Coghlandc855b72014-03-04 20:39:42 +1000383 def test_find_loader_avoids_emulation(self):
384 with check_warnings() as w:
385 self.assertIsNotNone(pkgutil.find_loader("sys"))
386 self.assertIsNotNone(pkgutil.find_loader("os"))
387 self.assertIsNotNone(pkgutil.find_loader("test.support"))
388 self.assertEqual(len(w.warnings), 0)
389
Nick Coghlan85e729e2012-07-15 18:09:52 +1000390 def test_get_importer_avoids_emulation(self):
Nick Coghlan94554922012-07-17 21:37:58 +1000391 # We use an illegal path so *none* of the path hooks should fire
Nick Coghlan85e729e2012-07-15 18:09:52 +1000392 with check_warnings() as w:
Nick Coghlan94554922012-07-17 21:37:58 +1000393 self.assertIsNone(pkgutil.get_importer("*??"))
Nick Coghlan85e729e2012-07-15 18:09:52 +1000394 self.assertEqual(len(w.warnings), 0)
395
396 def test_iter_importers_avoids_emulation(self):
397 with check_warnings() as w:
398 for importer in pkgutil.iter_importers(): pass
399 self.assertEqual(len(w.warnings), 0)
400
401
Christian Heimesdae2a892008-04-19 00:55:37 +0000402def test_main():
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200403 run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000404 NestedNamespacePackageTest, ImportlibMigrationTests)
Benjamin Petersoncf626032014-02-16 14:52:01 -0500405 # this is necessary if test is run repeated (like when finding leaks)
406 import zipimport
407 import importlib
408 zipimport._zip_directory_cache.clear()
409 importlib.invalidate_caches()
Nick Coghlan85e729e2012-07-15 18:09:52 +1000410
Christian Heimesdae2a892008-04-19 00:55:37 +0000411
412if __name__ == '__main__':
413 test_main()