diff options
author | Aurelien Bompard <aurelien@bompard.org> | 2011-06-01 09:18:22 +0200 |
---|---|---|
committer | Aurelien Bompard <aurelien@bompard.org> | 2011-06-01 09:18:22 +0200 |
commit | c7470ef715ed90469e7ee8f215d5307834479d8c (patch) | |
tree | 2c60d22d49976d404e522f2f51752e455f20a23f | |
parent | 0343339ddae02847a24ef3726eaf1df6789aa4a8 (diff) | |
download | external_python_setuptools-c7470ef715ed90469e7ee8f215d5307834479d8c.tar.gz external_python_setuptools-c7470ef715ed90469e7ee8f215d5307834479d8c.tar.bz2 external_python_setuptools-c7470ef715ed90469e7ee8f215d5307834479d8c.zip |
Update the child's __path__ in declare_namespace, even if the parent is
already a namespace package -- fixes #204
--HG--
branch : distribute
extra : rebase_source : 48e1ed2309ae053b8987a664231e72c35a0bbb40
-rw-r--r-- | pkg_resources.py | 8 | ||||
-rw-r--r-- | setuptools/tests/test_resources.py | 37 |
2 files changed, 36 insertions, 9 deletions
diff --git a/pkg_resources.py b/pkg_resources.py index 636d6ff9..52d92669 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1811,10 +1811,10 @@ def declare_namespace(packageName): declare_namespace(parent) if parent not in _namespace_packages: __import__(parent) - try: - path = sys.modules[parent].__path__ - except AttributeError: - raise TypeError("Not a package:", parent) + try: + path = sys.modules[parent].__path__ + except AttributeError: + raise TypeError("Not a package:", parent) # Track what packages are namespaces, so when new path items are added, # they can be updated diff --git a/setuptools/tests/test_resources.py b/setuptools/tests/test_resources.py index e02bd8d5..c10ca210 100644 --- a/setuptools/tests/test_resources.py +++ b/setuptools/tests/test_resources.py @@ -571,21 +571,48 @@ class NamespaceTests(TestCase): def setUp(self): self._ns_pkgs = pkg_resources._namespace_packages.copy() self._tmpdir = tempfile.mkdtemp(prefix="tests-distribute-") - sys.path.append(self._tmpdir) + os.makedirs(os.path.join(self._tmpdir, "site-pkgs")) + self._prev_sys_path = sys.path[:] + sys.path.append(os.path.join(self._tmpdir, "site-pkgs")) def tearDown(self): shutil.rmtree(self._tmpdir) pkg_resources._namespace_packages = self._ns_pkgs.copy() - sys.path.remove(self._tmpdir) + sys.path = self._prev_sys_path[:] def test_two_levels_deep(self): - os.makedirs(os.path.join(self._tmpdir, "pkg1", "pkg2")) - declare_namespace("pkg1") + """ + Test nested namespace packages + Create namespace packages in the following tree : + site-packages-1/pkg1/pkg2 + site-packages-2/pkg1/pkg2 + Check both are in the _namespace_packages dict and that their __path__ + is correct + """ + sys.path.append(os.path.join(self._tmpdir, "site-pkgs2")) + os.makedirs(os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2")) + os.makedirs(os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2")) + ns_str = "__import__('pkg_resources').declare_namespace(__name__)\n" + for site in ["site-pkgs", "site-pkgs2"]: + pkg1_init = open(os.path.join(self._tmpdir, site, + "pkg1", "__init__.py"), "w") + pkg1_init.write(ns_str) + pkg1_init.close() + pkg2_init = open(os.path.join(self._tmpdir, site, + "pkg1", "pkg2", "__init__.py"), "w") + pkg2_init.write(ns_str) + pkg2_init.close() + import pkg1 self.assertTrue("pkg1" in pkg_resources._namespace_packages.keys()) try: - declare_namespace("pkg1.pkg2") + import pkg1.pkg2 except ImportError, e: self.fail("Distribute tried to import the parent namespace package") + # check the _namespace_packages dict self.assertTrue("pkg1.pkg2" in pkg_resources._namespace_packages.keys()) self.assertEqual(pkg_resources._namespace_packages["pkg1"], ["pkg1.pkg2"]) + # check the __path__ attribute contains both paths + self.assertEqual(pkg1.pkg2.__path__, [ + os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"), + os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2") ]) |