blob: d73b2117d3ccdbb5ef8b2ab80cb604ccc96ff726 [file] [log] [blame]
Nick Coghlan85e729e2012-07-15 18:09:52 +10001from test.support import run_unittest, unload, check_warnings
Christian Heimesdae2a892008-04-19 00:55:37 +00002import unittest
3import sys
Nick Coghlanc4e0d982013-04-14 22:30:42 +10004import importlib
Christian Heimesdae2a892008-04-19 00:55:37 +00005import pkgutil
6import os
7import os.path
8import tempfile
Brett Cannon9529fbf2013-06-15 17:11:25 -04009import types
Christian Heimesdae2a892008-04-19 00:55:37 +000010import 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
Christian Heimesdae2a892008-04-19 00:55:37 +0000103class PkgutilPEP302Tests(unittest.TestCase):
104
105 class MyTestLoader(object):
106 def load_module(self, fullname):
107 # Create an empty module
Brett Cannon9529fbf2013-06-15 17:11:25 -0400108 mod = sys.modules.setdefault(fullname, types.ModuleType(fullname))
Christian Heimesdae2a892008-04-19 00:55:37 +0000109 mod.__file__ = "<%s>" % self.__class__.__name__
110 mod.__loader__ = self
111 # Make it a package
112 mod.__path__ = []
113 # Count how many times the module is reloaded
114 mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
115 return mod
116
117 def get_data(self, path):
118 return "Hello, world!"
119
120 class MyTestImporter(object):
121 def find_module(self, fullname, path=None):
122 return PkgutilPEP302Tests.MyTestLoader()
123
124 def setUp(self):
125 sys.meta_path.insert(0, self.MyTestImporter())
126
127 def tearDown(self):
128 del sys.meta_path[0]
129
130 def test_getdata_pep302(self):
131 # Use a dummy importer/loader
132 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
133 del sys.modules['foo']
134
135 def test_alreadyloaded(self):
136 # Ensure that get_data works without reloading - the "loads" module
137 # variable in the example loader should count how many times a reload
138 # occurs.
139 import foo
140 self.assertEqual(foo.loads, 1)
141 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
142 self.assertEqual(foo.loads, 1)
143 del sys.modules['foo']
144
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400145
Eric V. Smith984b11f2012-05-24 20:21:04 -0400146# These tests, especially the setup and cleanup, are hideous. They
147# need to be cleaned up once issue 14715 is addressed.
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400148class ExtendPathTests(unittest.TestCase):
149 def create_init(self, pkgname):
150 dirname = tempfile.mkdtemp()
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400151 sys.path.insert(0, dirname)
152
153 pkgdir = os.path.join(dirname, pkgname)
154 os.mkdir(pkgdir)
155 with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
156 fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
157
158 return dirname
159
160 def create_submodule(self, dirname, pkgname, submodule_name, value):
161 module_name = os.path.join(dirname, pkgname, submodule_name + '.py')
162 with open(module_name, 'w') as fl:
163 print('value={}'.format(value), file=fl)
164
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400165 def test_simple(self):
Eric V. Smith984b11f2012-05-24 20:21:04 -0400166 pkgname = 'foo'
167 dirname_0 = self.create_init(pkgname)
168 dirname_1 = self.create_init(pkgname)
169 self.create_submodule(dirname_0, pkgname, 'bar', 0)
170 self.create_submodule(dirname_1, pkgname, 'baz', 1)
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400171 import foo.bar
172 import foo.baz
173 # Ensure we read the expected values
174 self.assertEqual(foo.bar.value, 0)
175 self.assertEqual(foo.baz.value, 1)
176
177 # Ensure the path is set up correctly
178 self.assertEqual(sorted(foo.__path__),
Eric V. Smith984b11f2012-05-24 20:21:04 -0400179 sorted([os.path.join(dirname_0, pkgname),
180 os.path.join(dirname_1, pkgname)]))
181
182 # Cleanup
183 shutil.rmtree(dirname_0)
184 shutil.rmtree(dirname_1)
185 del sys.path[0]
186 del sys.path[0]
187 del sys.modules['foo']
188 del sys.modules['foo.bar']
189 del sys.modules['foo.baz']
190
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000191
192 # Another awful testing hack to be cleaned up once the test_runpy
193 # helpers are factored out to a common location
194 def test_iter_importers(self):
195 iter_importers = pkgutil.iter_importers
196 get_importer = pkgutil.get_importer
197
198 pkgname = 'spam'
199 modname = 'eggs'
200 dirname = self.create_init(pkgname)
201 pathitem = os.path.join(dirname, pkgname)
202 fullname = '{}.{}'.format(pkgname, modname)
Eric Snow2ba66eb2013-11-22 13:55:23 -0700203 sys.modules.pop(fullname, None)
204 sys.modules.pop(pkgname, None)
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000205 try:
206 self.create_submodule(dirname, pkgname, modname, 0)
207
208 importlib.import_module(fullname)
209
210 importers = list(iter_importers(fullname))
211 expected_importer = get_importer(pathitem)
212 for finder in importers:
Eric Snowb523f842013-11-22 09:05:39 -0700213 loader = finder.find_module(fullname)
214 try:
215 loader = loader.loader
216 except AttributeError:
217 # For now we still allow raw loaders from
218 # find_module().
219 pass
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000220 self.assertIsInstance(finder, importlib.machinery.FileFinder)
221 self.assertEqual(finder, expected_importer)
Eric Snowb523f842013-11-22 09:05:39 -0700222 self.assertIsInstance(loader,
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000223 importlib.machinery.SourceFileLoader)
224 self.assertIsNone(finder.find_module(pkgname))
225
226 with self.assertRaises(ImportError):
227 list(iter_importers('invalid.module'))
228
229 with self.assertRaises(ImportError):
230 list(iter_importers('.spam'))
231 finally:
232 shutil.rmtree(dirname)
233 del sys.path[0]
Eric Snowb523f842013-11-22 09:05:39 -0700234 try:
235 del sys.modules['spam']
236 del sys.modules['spam.eggs']
237 except KeyError:
238 pass
Nick Coghlanc4e0d982013-04-14 22:30:42 +1000239
240
Eric V. Smith984b11f2012-05-24 20:21:04 -0400241 def test_mixed_namespace(self):
242 pkgname = 'foo'
243 dirname_0 = self.create_init(pkgname)
244 dirname_1 = self.create_init(pkgname)
245 self.create_submodule(dirname_0, pkgname, 'bar', 0)
246 # Turn this into a PEP 420 namespace package
247 os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
248 self.create_submodule(dirname_1, pkgname, 'baz', 1)
249 import foo.bar
250 import foo.baz
251 # Ensure we read the expected values
252 self.assertEqual(foo.bar.value, 0)
253 self.assertEqual(foo.baz.value, 1)
254
255 # Ensure the path is set up correctly
256 self.assertEqual(sorted(foo.__path__),
257 sorted([os.path.join(dirname_0, pkgname),
258 os.path.join(dirname_1, pkgname)]))
259
260 # Cleanup
261 shutil.rmtree(dirname_0)
262 shutil.rmtree(dirname_1)
263 del sys.path[0]
264 del sys.path[0]
265 del sys.modules['foo']
266 del sys.modules['foo.bar']
267 del sys.modules['foo.baz']
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400268
269 # XXX: test .pkg files
270
271
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200272class NestedNamespacePackageTest(unittest.TestCase):
273
274 def setUp(self):
275 self.basedir = tempfile.mkdtemp()
276 self.old_path = sys.path[:]
277
278 def tearDown(self):
279 sys.path[:] = self.old_path
280 shutil.rmtree(self.basedir)
281
282 def create_module(self, name, contents):
283 base, final = name.rsplit('.', 1)
284 base_path = os.path.join(self.basedir, base.replace('.', os.path.sep))
285 os.makedirs(base_path, exist_ok=True)
286 with open(os.path.join(base_path, final + ".py"), 'w') as f:
287 f.write(contents)
288
289 def test_nested(self):
290 pkgutil_boilerplate = (
291 'import pkgutil; '
292 '__path__ = pkgutil.extend_path(__path__, __name__)')
293 self.create_module('a.pkg.__init__', pkgutil_boilerplate)
294 self.create_module('b.pkg.__init__', pkgutil_boilerplate)
295 self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate)
296 self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate)
297 self.create_module('a.pkg.subpkg.c', 'c = 1')
298 self.create_module('b.pkg.subpkg.d', 'd = 2')
299 sys.path.insert(0, os.path.join(self.basedir, 'a'))
300 sys.path.insert(0, os.path.join(self.basedir, 'b'))
301 import pkg
302 self.addCleanup(unload, 'pkg')
303 self.assertEqual(len(pkg.__path__), 2)
304 import pkg.subpkg
305 self.addCleanup(unload, 'pkg.subpkg')
306 self.assertEqual(len(pkg.subpkg.__path__), 2)
307 from pkg.subpkg.c import c
308 from pkg.subpkg.d import d
309 self.assertEqual(c, 1)
310 self.assertEqual(d, 2)
311
312
Nick Coghlan85e729e2012-07-15 18:09:52 +1000313class ImportlibMigrationTests(unittest.TestCase):
314 # With full PEP 302 support in the standard import machinery, the
315 # PEP 302 emulation in this module is in the process of being
316 # deprecated in favour of importlib proper
317
318 def check_deprecated(self):
319 return check_warnings(
320 ("This emulation is deprecated, use 'importlib' instead",
321 DeprecationWarning))
322
323 def test_importer_deprecated(self):
324 with self.check_deprecated():
325 x = pkgutil.ImpImporter("")
326
327 def test_loader_deprecated(self):
328 with self.check_deprecated():
329 x = pkgutil.ImpLoader("", "", "", "")
330
331 def test_get_loader_avoids_emulation(self):
332 with check_warnings() as w:
333 self.assertIsNotNone(pkgutil.get_loader("sys"))
334 self.assertIsNotNone(pkgutil.get_loader("os"))
335 self.assertIsNotNone(pkgutil.get_loader("test.support"))
336 self.assertEqual(len(w.warnings), 0)
337
338 def test_get_importer_avoids_emulation(self):
Nick Coghlan94554922012-07-17 21:37:58 +1000339 # We use an illegal path so *none* of the path hooks should fire
Nick Coghlan85e729e2012-07-15 18:09:52 +1000340 with check_warnings() as w:
Nick Coghlan94554922012-07-17 21:37:58 +1000341 self.assertIsNone(pkgutil.get_importer("*??"))
Nick Coghlan85e729e2012-07-15 18:09:52 +1000342 self.assertEqual(len(w.warnings), 0)
343
344 def test_iter_importers_avoids_emulation(self):
345 with check_warnings() as w:
346 for importer in pkgutil.iter_importers(): pass
347 self.assertEqual(len(w.warnings), 0)
348
349
Christian Heimesdae2a892008-04-19 00:55:37 +0000350def test_main():
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200351 run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000352 NestedNamespacePackageTest, ImportlibMigrationTests)
Christian Heimese57950f2008-04-21 13:08:03 +0000353 # this is necessary if test is run repeated (like when finding leaks)
354 import zipimport
Nick Coghlan85e729e2012-07-15 18:09:52 +1000355 import importlib
Christian Heimese57950f2008-04-21 13:08:03 +0000356 zipimport._zip_directory_cache.clear()
Nick Coghlan85e729e2012-07-15 18:09:52 +1000357 importlib.invalidate_caches()
358
Christian Heimesdae2a892008-04-19 00:55:37 +0000359
360if __name__ == '__main__':
361 test_main()