blob: 88e2d338dcf284ebe7029dde181e802fb745875e [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
4import imp
5import pkgutil
6import os
7import os.path
8import tempfile
9import shutil
10import zipfile
11
Nick Coghlan8ecf5042012-07-15 21:19:18 +100012# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
13# a hack to get a major issue resolved for 3.3b2. Longer term, it should
14# be moved back here, perhaps by factoring out the helper code for
15# creating interesting package layouts to a separate module.
16# Issue #15348 declares this is indeed a dodgy hack ;)
Christian Heimesdae2a892008-04-19 00:55:37 +000017
18class PkgutilTests(unittest.TestCase):
19
20 def setUp(self):
21 self.dirname = tempfile.mkdtemp()
Ned Deily7010a072011-10-07 12:01:40 -070022 self.addCleanup(shutil.rmtree, self.dirname)
Christian Heimesdae2a892008-04-19 00:55:37 +000023 sys.path.insert(0, self.dirname)
24
25 def tearDown(self):
26 del sys.path[0]
Christian Heimesdae2a892008-04-19 00:55:37 +000027
28 def test_getdata_filesys(self):
29 pkg = 'test_getdata_filesys'
30
31 # Include a LF and a CRLF, to test that binary data is read back
32 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
33
34 # Make a package with some resources
35 package_dir = os.path.join(self.dirname, pkg)
36 os.mkdir(package_dir)
37 # Empty init.py
38 f = open(os.path.join(package_dir, '__init__.py'), "wb")
39 f.close()
40 # Resource files, res.txt, sub/res.txt
41 f = open(os.path.join(package_dir, 'res.txt'), "wb")
42 f.write(RESOURCE_DATA)
43 f.close()
44 os.mkdir(os.path.join(package_dir, 'sub'))
45 f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
46 f.write(RESOURCE_DATA)
47 f.close()
48
49 # Check we can read the resources
50 res1 = pkgutil.get_data(pkg, 'res.txt')
51 self.assertEqual(res1, RESOURCE_DATA)
52 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
53 self.assertEqual(res2, RESOURCE_DATA)
54
55 del sys.modules[pkg]
56
57 def test_getdata_zipfile(self):
58 zip = 'test_getdata_zipfile.zip'
59 pkg = 'test_getdata_zipfile'
60
61 # Include a LF and a CRLF, to test that binary data is read back
62 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
63
64 # Make a package with some resources
65 zip_file = os.path.join(self.dirname, zip)
66 z = zipfile.ZipFile(zip_file, 'w')
67
68 # Empty init.py
69 z.writestr(pkg + '/__init__.py', "")
70 # Resource files, res.txt, sub/res.txt
71 z.writestr(pkg + '/res.txt', RESOURCE_DATA)
72 z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
73 z.close()
74
75 # Check we can read the resources
76 sys.path.insert(0, zip_file)
77 res1 = pkgutil.get_data(pkg, 'res.txt')
78 self.assertEqual(res1, RESOURCE_DATA)
79 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
80 self.assertEqual(res2, RESOURCE_DATA)
Alexandre Vassalotti515a74f2009-07-05 06:42:44 +000081
82 names = []
83 for loader, name, ispkg in pkgutil.iter_modules([zip_file]):
84 names.append(name)
85 self.assertEqual(names, ['test_getdata_zipfile'])
86
Christian Heimesdae2a892008-04-19 00:55:37 +000087 del sys.path[0]
88
89 del sys.modules[pkg]
90
Ned Deilycaf5a222011-10-06 14:19:06 -070091 def test_unreadable_dir_on_syspath(self):
92 # issue7367 - walk_packages failed if unreadable dir on sys.path
93 package_name = "unreadable_package"
94 d = os.path.join(self.dirname, package_name)
95 # this does not appear to create an unreadable dir on Windows
96 # but the test should not fail anyway
97 os.mkdir(d, 0)
Ned Deily7010a072011-10-07 12:01:40 -070098 self.addCleanup(os.rmdir, d)
Ned Deilycaf5a222011-10-06 14:19:06 -070099 for t in pkgutil.walk_packages(path=[self.dirname]):
100 self.fail("unexpected package found")
Ned Deilycaf5a222011-10-06 14:19:06 -0700101
Christian Heimesdae2a892008-04-19 00:55:37 +0000102class PkgutilPEP302Tests(unittest.TestCase):
103
104 class MyTestLoader(object):
105 def load_module(self, fullname):
106 # Create an empty module
107 mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
108 mod.__file__ = "<%s>" % self.__class__.__name__
109 mod.__loader__ = self
110 # Make it a package
111 mod.__path__ = []
112 # Count how many times the module is reloaded
113 mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
114 return mod
115
116 def get_data(self, path):
117 return "Hello, world!"
118
119 class MyTestImporter(object):
120 def find_module(self, fullname, path=None):
121 return PkgutilPEP302Tests.MyTestLoader()
122
123 def setUp(self):
124 sys.meta_path.insert(0, self.MyTestImporter())
125
126 def tearDown(self):
127 del sys.meta_path[0]
128
129 def test_getdata_pep302(self):
130 # Use a dummy importer/loader
131 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
132 del sys.modules['foo']
133
134 def test_alreadyloaded(self):
135 # Ensure that get_data works without reloading - the "loads" module
136 # variable in the example loader should count how many times a reload
137 # occurs.
138 import foo
139 self.assertEqual(foo.loads, 1)
140 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
141 self.assertEqual(foo.loads, 1)
142 del sys.modules['foo']
143
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400144
Eric V. Smith984b11f2012-05-24 20:21:04 -0400145# These tests, especially the setup and cleanup, are hideous. They
146# need to be cleaned up once issue 14715 is addressed.
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400147class ExtendPathTests(unittest.TestCase):
148 def create_init(self, pkgname):
149 dirname = tempfile.mkdtemp()
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400150 sys.path.insert(0, dirname)
151
152 pkgdir = os.path.join(dirname, pkgname)
153 os.mkdir(pkgdir)
154 with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
155 fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
156
157 return dirname
158
159 def create_submodule(self, dirname, pkgname, submodule_name, value):
160 module_name = os.path.join(dirname, pkgname, submodule_name + '.py')
161 with open(module_name, 'w') as fl:
162 print('value={}'.format(value), file=fl)
163
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400164 def test_simple(self):
Eric V. Smith984b11f2012-05-24 20:21:04 -0400165 pkgname = 'foo'
166 dirname_0 = self.create_init(pkgname)
167 dirname_1 = self.create_init(pkgname)
168 self.create_submodule(dirname_0, pkgname, 'bar', 0)
169 self.create_submodule(dirname_1, pkgname, 'baz', 1)
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400170 import foo.bar
171 import foo.baz
172 # Ensure we read the expected values
173 self.assertEqual(foo.bar.value, 0)
174 self.assertEqual(foo.baz.value, 1)
175
176 # Ensure the path is set up correctly
177 self.assertEqual(sorted(foo.__path__),
Eric V. Smith984b11f2012-05-24 20:21:04 -0400178 sorted([os.path.join(dirname_0, pkgname),
179 os.path.join(dirname_1, pkgname)]))
180
181 # Cleanup
182 shutil.rmtree(dirname_0)
183 shutil.rmtree(dirname_1)
184 del sys.path[0]
185 del sys.path[0]
186 del sys.modules['foo']
187 del sys.modules['foo.bar']
188 del sys.modules['foo.baz']
189
190 def test_mixed_namespace(self):
191 pkgname = 'foo'
192 dirname_0 = self.create_init(pkgname)
193 dirname_1 = self.create_init(pkgname)
194 self.create_submodule(dirname_0, pkgname, 'bar', 0)
195 # Turn this into a PEP 420 namespace package
196 os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
197 self.create_submodule(dirname_1, pkgname, 'baz', 1)
198 import foo.bar
199 import foo.baz
200 # Ensure we read the expected values
201 self.assertEqual(foo.bar.value, 0)
202 self.assertEqual(foo.baz.value, 1)
203
204 # Ensure the path is set up correctly
205 self.assertEqual(sorted(foo.__path__),
206 sorted([os.path.join(dirname_0, pkgname),
207 os.path.join(dirname_1, pkgname)]))
208
209 # Cleanup
210 shutil.rmtree(dirname_0)
211 shutil.rmtree(dirname_1)
212 del sys.path[0]
213 del sys.path[0]
214 del sys.modules['foo']
215 del sys.modules['foo.bar']
216 del sys.modules['foo.baz']
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400217
218 # XXX: test .pkg files
219
220
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200221class NestedNamespacePackageTest(unittest.TestCase):
222
223 def setUp(self):
224 self.basedir = tempfile.mkdtemp()
225 self.old_path = sys.path[:]
226
227 def tearDown(self):
228 sys.path[:] = self.old_path
229 shutil.rmtree(self.basedir)
230
231 def create_module(self, name, contents):
232 base, final = name.rsplit('.', 1)
233 base_path = os.path.join(self.basedir, base.replace('.', os.path.sep))
234 os.makedirs(base_path, exist_ok=True)
235 with open(os.path.join(base_path, final + ".py"), 'w') as f:
236 f.write(contents)
237
238 def test_nested(self):
239 pkgutil_boilerplate = (
240 'import pkgutil; '
241 '__path__ = pkgutil.extend_path(__path__, __name__)')
242 self.create_module('a.pkg.__init__', pkgutil_boilerplate)
243 self.create_module('b.pkg.__init__', pkgutil_boilerplate)
244 self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate)
245 self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate)
246 self.create_module('a.pkg.subpkg.c', 'c = 1')
247 self.create_module('b.pkg.subpkg.d', 'd = 2')
248 sys.path.insert(0, os.path.join(self.basedir, 'a'))
249 sys.path.insert(0, os.path.join(self.basedir, 'b'))
250 import pkg
251 self.addCleanup(unload, 'pkg')
252 self.assertEqual(len(pkg.__path__), 2)
253 import pkg.subpkg
254 self.addCleanup(unload, 'pkg.subpkg')
255 self.assertEqual(len(pkg.subpkg.__path__), 2)
256 from pkg.subpkg.c import c
257 from pkg.subpkg.d import d
258 self.assertEqual(c, 1)
259 self.assertEqual(d, 2)
260
261
Nick Coghlan85e729e2012-07-15 18:09:52 +1000262class ImportlibMigrationTests(unittest.TestCase):
263 # With full PEP 302 support in the standard import machinery, the
264 # PEP 302 emulation in this module is in the process of being
265 # deprecated in favour of importlib proper
266
267 def check_deprecated(self):
268 return check_warnings(
269 ("This emulation is deprecated, use 'importlib' instead",
270 DeprecationWarning))
271
272 def test_importer_deprecated(self):
273 with self.check_deprecated():
274 x = pkgutil.ImpImporter("")
275
276 def test_loader_deprecated(self):
277 with self.check_deprecated():
278 x = pkgutil.ImpLoader("", "", "", "")
279
280 def test_get_loader_avoids_emulation(self):
281 with check_warnings() as w:
282 self.assertIsNotNone(pkgutil.get_loader("sys"))
283 self.assertIsNotNone(pkgutil.get_loader("os"))
284 self.assertIsNotNone(pkgutil.get_loader("test.support"))
285 self.assertEqual(len(w.warnings), 0)
286
287 def test_get_importer_avoids_emulation(self):
Nick Coghlan94554922012-07-17 21:37:58 +1000288 # We use an illegal path so *none* of the path hooks should fire
Nick Coghlan85e729e2012-07-15 18:09:52 +1000289 with check_warnings() as w:
Nick Coghlan94554922012-07-17 21:37:58 +1000290 self.assertIsNone(pkgutil.get_importer("*??"))
Nick Coghlan85e729e2012-07-15 18:09:52 +1000291 self.assertEqual(len(w.warnings), 0)
292
293 def test_iter_importers_avoids_emulation(self):
294 with check_warnings() as w:
295 for importer in pkgutil.iter_importers(): pass
296 self.assertEqual(len(w.warnings), 0)
297
298
Christian Heimesdae2a892008-04-19 00:55:37 +0000299def test_main():
Antoine Pitroub2dd8802012-07-09 21:23:58 +0200300 run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000301 NestedNamespacePackageTest, ImportlibMigrationTests)
Christian Heimese57950f2008-04-21 13:08:03 +0000302 # this is necessary if test is run repeated (like when finding leaks)
303 import zipimport
Nick Coghlan85e729e2012-07-15 18:09:52 +1000304 import importlib
Christian Heimese57950f2008-04-21 13:08:03 +0000305 zipimport._zip_directory_cache.clear()
Nick Coghlan85e729e2012-07-15 18:09:52 +1000306 importlib.invalidate_caches()
307
Christian Heimesdae2a892008-04-19 00:55:37 +0000308
309if __name__ == '__main__':
310 test_main()