Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 1 | from test.support import run_unittest, unload, check_warnings |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | import sys |
| 4 | import imp |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 5 | import importlib |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 6 | import pkgutil |
| 7 | import os |
| 8 | import os.path |
| 9 | import tempfile |
| 10 | import shutil |
| 11 | import zipfile |
| 12 | |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 13 | # 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 18 | |
| 19 | class PkgutilTests(unittest.TestCase): |
| 20 | |
| 21 | def setUp(self): |
| 22 | self.dirname = tempfile.mkdtemp() |
Ned Deily | 7010a07 | 2011-10-07 12:01:40 -0700 | [diff] [blame] | 23 | self.addCleanup(shutil.rmtree, self.dirname) |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 24 | sys.path.insert(0, self.dirname) |
| 25 | |
| 26 | def tearDown(self): |
| 27 | del sys.path[0] |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 28 | |
| 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 Vassalotti | 515a74f | 2009-07-05 06:42:44 +0000 | [diff] [blame] | 82 | |
| 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 88 | del sys.path[0] |
| 89 | |
| 90 | del sys.modules[pkg] |
| 91 | |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 92 | 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 Deily | 7010a07 | 2011-10-07 12:01:40 -0700 | [diff] [blame] | 99 | self.addCleanup(os.rmdir, d) |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 100 | for t in pkgutil.walk_packages(path=[self.dirname]): |
| 101 | self.fail("unexpected package found") |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 102 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 103 | class PkgutilPEP302Tests(unittest.TestCase): |
| 104 | |
| 105 | class MyTestLoader(object): |
| 106 | def load_module(self, fullname): |
| 107 | # Create an empty module |
| 108 | mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) |
| 109 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 145 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 146 | # These tests, especially the setup and cleanup, are hideous. They |
| 147 | # need to be cleaned up once issue 14715 is addressed. |
Eric V. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 148 | class ExtendPathTests(unittest.TestCase): |
| 149 | def create_init(self, pkgname): |
| 150 | dirname = tempfile.mkdtemp() |
Eric V. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 151 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 165 | def test_simple(self): |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 166 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 171 | 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. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 179 | 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 Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 191 | |
| 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) |
| 203 | try: |
| 204 | self.create_submodule(dirname, pkgname, modname, 0) |
| 205 | |
| 206 | importlib.import_module(fullname) |
| 207 | |
| 208 | importers = list(iter_importers(fullname)) |
| 209 | expected_importer = get_importer(pathitem) |
| 210 | for finder in importers: |
| 211 | self.assertIsInstance(finder, importlib.machinery.FileFinder) |
| 212 | self.assertEqual(finder, expected_importer) |
| 213 | self.assertIsInstance(finder.find_module(fullname), |
| 214 | importlib.machinery.SourceFileLoader) |
| 215 | self.assertIsNone(finder.find_module(pkgname)) |
| 216 | |
| 217 | with self.assertRaises(ImportError): |
| 218 | list(iter_importers('invalid.module')) |
| 219 | |
| 220 | with self.assertRaises(ImportError): |
| 221 | list(iter_importers('.spam')) |
| 222 | finally: |
| 223 | shutil.rmtree(dirname) |
| 224 | del sys.path[0] |
| 225 | del sys.modules['spam'] |
| 226 | del sys.modules['spam.eggs'] |
| 227 | |
| 228 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 229 | def test_mixed_namespace(self): |
| 230 | pkgname = 'foo' |
| 231 | dirname_0 = self.create_init(pkgname) |
| 232 | dirname_1 = self.create_init(pkgname) |
| 233 | self.create_submodule(dirname_0, pkgname, 'bar', 0) |
| 234 | # Turn this into a PEP 420 namespace package |
| 235 | os.unlink(os.path.join(dirname_0, pkgname, '__init__.py')) |
| 236 | self.create_submodule(dirname_1, pkgname, 'baz', 1) |
| 237 | import foo.bar |
| 238 | import foo.baz |
| 239 | # Ensure we read the expected values |
| 240 | self.assertEqual(foo.bar.value, 0) |
| 241 | self.assertEqual(foo.baz.value, 1) |
| 242 | |
| 243 | # Ensure the path is set up correctly |
| 244 | self.assertEqual(sorted(foo.__path__), |
| 245 | sorted([os.path.join(dirname_0, pkgname), |
| 246 | os.path.join(dirname_1, pkgname)])) |
| 247 | |
| 248 | # Cleanup |
| 249 | shutil.rmtree(dirname_0) |
| 250 | shutil.rmtree(dirname_1) |
| 251 | del sys.path[0] |
| 252 | del sys.path[0] |
| 253 | del sys.modules['foo'] |
| 254 | del sys.modules['foo.bar'] |
| 255 | del sys.modules['foo.baz'] |
Eric V. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 256 | |
| 257 | # XXX: test .pkg files |
| 258 | |
| 259 | |
Antoine Pitrou | b2dd880 | 2012-07-09 21:23:58 +0200 | [diff] [blame] | 260 | class NestedNamespacePackageTest(unittest.TestCase): |
| 261 | |
| 262 | def setUp(self): |
| 263 | self.basedir = tempfile.mkdtemp() |
| 264 | self.old_path = sys.path[:] |
| 265 | |
| 266 | def tearDown(self): |
| 267 | sys.path[:] = self.old_path |
| 268 | shutil.rmtree(self.basedir) |
| 269 | |
| 270 | def create_module(self, name, contents): |
| 271 | base, final = name.rsplit('.', 1) |
| 272 | base_path = os.path.join(self.basedir, base.replace('.', os.path.sep)) |
| 273 | os.makedirs(base_path, exist_ok=True) |
| 274 | with open(os.path.join(base_path, final + ".py"), 'w') as f: |
| 275 | f.write(contents) |
| 276 | |
| 277 | def test_nested(self): |
| 278 | pkgutil_boilerplate = ( |
| 279 | 'import pkgutil; ' |
| 280 | '__path__ = pkgutil.extend_path(__path__, __name__)') |
| 281 | self.create_module('a.pkg.__init__', pkgutil_boilerplate) |
| 282 | self.create_module('b.pkg.__init__', pkgutil_boilerplate) |
| 283 | self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate) |
| 284 | self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate) |
| 285 | self.create_module('a.pkg.subpkg.c', 'c = 1') |
| 286 | self.create_module('b.pkg.subpkg.d', 'd = 2') |
| 287 | sys.path.insert(0, os.path.join(self.basedir, 'a')) |
| 288 | sys.path.insert(0, os.path.join(self.basedir, 'b')) |
| 289 | import pkg |
| 290 | self.addCleanup(unload, 'pkg') |
| 291 | self.assertEqual(len(pkg.__path__), 2) |
| 292 | import pkg.subpkg |
| 293 | self.addCleanup(unload, 'pkg.subpkg') |
| 294 | self.assertEqual(len(pkg.subpkg.__path__), 2) |
| 295 | from pkg.subpkg.c import c |
| 296 | from pkg.subpkg.d import d |
| 297 | self.assertEqual(c, 1) |
| 298 | self.assertEqual(d, 2) |
| 299 | |
| 300 | |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 301 | class ImportlibMigrationTests(unittest.TestCase): |
| 302 | # With full PEP 302 support in the standard import machinery, the |
| 303 | # PEP 302 emulation in this module is in the process of being |
| 304 | # deprecated in favour of importlib proper |
| 305 | |
| 306 | def check_deprecated(self): |
| 307 | return check_warnings( |
| 308 | ("This emulation is deprecated, use 'importlib' instead", |
| 309 | DeprecationWarning)) |
| 310 | |
| 311 | def test_importer_deprecated(self): |
| 312 | with self.check_deprecated(): |
| 313 | x = pkgutil.ImpImporter("") |
| 314 | |
| 315 | def test_loader_deprecated(self): |
| 316 | with self.check_deprecated(): |
| 317 | x = pkgutil.ImpLoader("", "", "", "") |
| 318 | |
| 319 | def test_get_loader_avoids_emulation(self): |
| 320 | with check_warnings() as w: |
| 321 | self.assertIsNotNone(pkgutil.get_loader("sys")) |
| 322 | self.assertIsNotNone(pkgutil.get_loader("os")) |
| 323 | self.assertIsNotNone(pkgutil.get_loader("test.support")) |
| 324 | self.assertEqual(len(w.warnings), 0) |
| 325 | |
| 326 | def test_get_importer_avoids_emulation(self): |
Nick Coghlan | 9455492 | 2012-07-17 21:37:58 +1000 | [diff] [blame] | 327 | # We use an illegal path so *none* of the path hooks should fire |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 328 | with check_warnings() as w: |
Nick Coghlan | 9455492 | 2012-07-17 21:37:58 +1000 | [diff] [blame] | 329 | self.assertIsNone(pkgutil.get_importer("*??")) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 330 | self.assertEqual(len(w.warnings), 0) |
| 331 | |
| 332 | def test_iter_importers_avoids_emulation(self): |
| 333 | with check_warnings() as w: |
| 334 | for importer in pkgutil.iter_importers(): pass |
| 335 | self.assertEqual(len(w.warnings), 0) |
| 336 | |
| 337 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 338 | def test_main(): |
Antoine Pitrou | b2dd880 | 2012-07-09 21:23:58 +0200 | [diff] [blame] | 339 | run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 340 | NestedNamespacePackageTest, ImportlibMigrationTests) |
Benjamin Peterson | cf62603 | 2014-02-16 14:52:01 -0500 | [diff] [blame] | 341 | # this is necessary if test is run repeated (like when finding leaks) |
| 342 | import zipimport |
| 343 | import importlib |
| 344 | zipimport._zip_directory_cache.clear() |
| 345 | importlib.invalidate_caches() |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 346 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 347 | |
| 348 | if __name__ == '__main__': |
| 349 | test_main() |