|
1 | | -from test.support import run_unittest |
| 1 | +from test.support import run_unittest, unload |
2 | 2 | import unittest |
3 | 3 | import sys |
4 | 4 | import imp |
@@ -214,8 +214,50 @@ def test_mixed_namespace(self): |
214 | 214 | # XXX: test .pkg files |
215 | 215 |
|
216 | 216 |
|
| 217 | +class NestedNamespacePackageTest(unittest.TestCase): |
| 218 | + |
| 219 | + def setUp(self): |
| 220 | + self.basedir = tempfile.mkdtemp() |
| 221 | + self.old_path = sys.path[:] |
| 222 | + |
| 223 | + def tearDown(self): |
| 224 | + sys.path[:] = self.old_path |
| 225 | + shutil.rmtree(self.basedir) |
| 226 | + |
| 227 | + def create_module(self, name, contents): |
| 228 | + base, final = name.rsplit('.', 1) |
| 229 | + base_path = os.path.join(self.basedir, base.replace('.', os.path.sep)) |
| 230 | + os.makedirs(base_path, exist_ok=True) |
| 231 | + with open(os.path.join(base_path, final + ".py"), 'w') as f: |
| 232 | + f.write(contents) |
| 233 | + |
| 234 | + def test_nested(self): |
| 235 | + pkgutil_boilerplate = ( |
| 236 | + 'import pkgutil; ' |
| 237 | + '__path__ = pkgutil.extend_path(__path__, __name__)') |
| 238 | + self.create_module('a.pkg.__init__', pkgutil_boilerplate) |
| 239 | + self.create_module('b.pkg.__init__', pkgutil_boilerplate) |
| 240 | + self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate) |
| 241 | + self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate) |
| 242 | + self.create_module('a.pkg.subpkg.c', 'c = 1') |
| 243 | + self.create_module('b.pkg.subpkg.d', 'd = 2') |
| 244 | + sys.path.insert(0, os.path.join(self.basedir, 'a')) |
| 245 | + sys.path.insert(0, os.path.join(self.basedir, 'b')) |
| 246 | + import pkg |
| 247 | + self.addCleanup(unload, 'pkg') |
| 248 | + self.assertEqual(len(pkg.__path__), 2) |
| 249 | + import pkg.subpkg |
| 250 | + self.addCleanup(unload, 'pkg.subpkg') |
| 251 | + self.assertEqual(len(pkg.subpkg.__path__), 2) |
| 252 | + from pkg.subpkg.c import c |
| 253 | + from pkg.subpkg.d import d |
| 254 | + self.assertEqual(c, 1) |
| 255 | + self.assertEqual(d, 2) |
| 256 | + |
| 257 | + |
217 | 258 | def test_main(): |
218 | | - run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests) |
| 259 | + run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests, |
| 260 | + NestedNamespacePackageTest) |
219 | 261 | # this is necessary if test is run repeated (like when finding leaks) |
220 | 262 | import zipimport |
221 | 263 | zipimport._zip_directory_cache.clear() |
|
0 commit comments