Eric Snow | 658af31 | 2014-04-19 00:13:23 -0600 | [diff] [blame] | 1 | from test.support import run_unittest, unload, check_warnings, CleanImport |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | import sys |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 4 | import importlib |
Eric Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 5 | from importlib.util import spec_from_file_location |
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 |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 10 | import types |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 11 | import shutil |
| 12 | import zipfile |
| 13 | |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 14 | # 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 19 | |
| 20 | class PkgutilTests(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.dirname = tempfile.mkdtemp() |
Ned Deily | 7010a07 | 2011-10-07 12:01:40 -0700 | [diff] [blame] | 24 | self.addCleanup(shutil.rmtree, self.dirname) |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 25 | sys.path.insert(0, self.dirname) |
| 26 | |
| 27 | def tearDown(self): |
| 28 | del sys.path[0] |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 29 | |
| 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 Vassalotti | 515a74f | 2009-07-05 06:42:44 +0000 | [diff] [blame] | 83 | |
| 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 89 | del sys.path[0] |
| 90 | |
| 91 | del sys.modules[pkg] |
| 92 | |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 93 | 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 Deily | 7010a07 | 2011-10-07 12:01:40 -0700 | [diff] [blame] | 100 | self.addCleanup(os.rmdir, d) |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 101 | for t in pkgutil.walk_packages(path=[self.dirname]): |
| 102 | self.fail("unexpected package found") |
Ned Deily | caf5a22 | 2011-10-06 14:19:06 -0700 | [diff] [blame] | 103 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 104 | class PkgutilPEP302Tests(unittest.TestCase): |
| 105 | |
| 106 | class MyTestLoader(object): |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 107 | def create_module(self, spec): |
| 108 | return None |
| 109 | |
Eric Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 110 | def exec_module(self, mod): |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 111 | # Count how many times the module is reloaded |
Eric Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 112 | mod.__dict__['loads'] = mod.__dict__.get('loads', 0) + 1 |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 113 | |
| 114 | def get_data(self, path): |
| 115 | return "Hello, world!" |
| 116 | |
| 117 | class MyTestImporter(object): |
Eric Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 118 | 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 124 | |
| 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 146 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 147 | # These tests, especially the setup and cleanup, are hideous. They |
| 148 | # need to be cleaned up once issue 14715 is addressed. |
Eric V. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 149 | class ExtendPathTests(unittest.TestCase): |
| 150 | def create_init(self, pkgname): |
| 151 | dirname = tempfile.mkdtemp() |
Eric V. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 152 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 166 | def test_simple(self): |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 167 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 172 | 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. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 180 | 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 Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 192 | |
| 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 Snow | 2ba66eb | 2013-11-22 13:55:23 -0700 | [diff] [blame] | 204 | sys.modules.pop(fullname, None) |
| 205 | sys.modules.pop(pkgname, None) |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 206 | 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 Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 214 | spec = pkgutil._get_spec(finder, fullname) |
| 215 | loader = spec.loader |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 216 | try: |
| 217 | loader = loader.loader |
| 218 | except AttributeError: |
| 219 | # For now we still allow raw loaders from |
| 220 | # find_module(). |
| 221 | pass |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 222 | self.assertIsInstance(finder, importlib.machinery.FileFinder) |
| 223 | self.assertEqual(finder, expected_importer) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 224 | self.assertIsInstance(loader, |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 225 | importlib.machinery.SourceFileLoader) |
Eric Snow | 37148b2 | 2014-01-04 15:09:53 -0700 | [diff] [blame] | 226 | self.assertIsNone(pkgutil._get_spec(finder, pkgname)) |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 227 | |
| 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 Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 236 | try: |
| 237 | del sys.modules['spam'] |
| 238 | del sys.modules['spam.eggs'] |
| 239 | except KeyError: |
| 240 | pass |
Nick Coghlan | c4e0d98 | 2013-04-14 22:30:42 +1000 | [diff] [blame] | 241 | |
| 242 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 243 | 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. Smith | a790c9b | 2012-05-15 20:44:06 -0400 | [diff] [blame] | 270 | |
| 271 | # XXX: test .pkg files |
| 272 | |
| 273 | |
Antoine Pitrou | b2dd880 | 2012-07-09 21:23:58 +0200 | [diff] [blame] | 274 | class 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 Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 315 | class 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 Coghlan | dc855b7 | 2014-03-04 20:39:42 +1000 | [diff] [blame] | 340 | 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 Snow | 658af31 | 2014-04-19 00:13:23 -0600 | [diff] [blame] | 351 | 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 Coghlan | dc855b7 | 2014-03-04 20:39:42 +1000 | [diff] [blame] | 368 | |
Brett Cannon | 8447c70 | 2014-05-23 12:30:37 -0400 | [diff] [blame] | 369 | 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 Coghlan | dc855b7 | 2014-03-04 20:39:42 +1000 | [diff] [blame] | 383 | 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 Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 390 | def test_get_importer_avoids_emulation(self): |
Nick Coghlan | 9455492 | 2012-07-17 21:37:58 +1000 | [diff] [blame] | 391 | # 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] | 392 | with check_warnings() as w: |
Nick Coghlan | 9455492 | 2012-07-17 21:37:58 +1000 | [diff] [blame] | 393 | self.assertIsNone(pkgutil.get_importer("*??")) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 394 | 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 Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 402 | def test_main(): |
Antoine Pitrou | b2dd880 | 2012-07-09 21:23:58 +0200 | [diff] [blame] | 403 | run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 404 | NestedNamespacePackageTest, ImportlibMigrationTests) |
Benjamin Peterson | cf62603 | 2014-02-16 14:52:01 -0500 | [diff] [blame] | 405 | # 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 Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 410 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 411 | |
| 412 | if __name__ == '__main__': |
| 413 | test_main() |