diff options
-rw-r--r-- | pkg_resources.py | 11 | ||||
-rw-r--r-- | setuptools/tests/test_resources.py | 28 |
2 files changed, 33 insertions, 6 deletions
diff --git a/pkg_resources.py b/pkg_resources.py index 30dbc188..5eb19df3 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1773,11 +1773,12 @@ def declare_namespace(packageName): if '.' in packageName: parent = '.'.join(packageName.split('.')[:-1]) declare_namespace(parent) - __import__(parent) - try: - path = sys.modules[parent].__path__ - except AttributeError: - raise TypeError("Not a package:", parent) + if parent not in _namespace_packages: + __import__(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 883cfad1..e02bd8d5 100644 --- a/setuptools/tests/test_resources.py +++ b/setuptools/tests/test_resources.py @@ -3,7 +3,7 @@ # NOTE: the shebang and encoding lines are for ScriptHeaderTests; do not remove from unittest import TestCase, makeSuite; from pkg_resources import * from setuptools.command.easy_install import get_script_header, is_sh -import os, pkg_resources, sys, StringIO +import os, pkg_resources, sys, StringIO, tempfile, shutil try: frozenset except NameError: from sets import ImmutableSet as frozenset @@ -563,3 +563,29 @@ class ScriptHeaderTests(TestCase): sys.platform = platform sys.stdout = stdout + + + +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) + + def tearDown(self): + shutil.rmtree(self._tmpdir) + pkg_resources._namespace_packages = self._ns_pkgs.copy() + sys.path.remove(self._tmpdir) + + def test_two_levels_deep(self): + os.makedirs(os.path.join(self._tmpdir, "pkg1", "pkg2")) + declare_namespace("pkg1") + self.assertTrue("pkg1" in pkg_resources._namespace_packages.keys()) + try: + declare_namespace("pkg1.pkg2") + except ImportError, e: + self.fail("Distribute tried to import the parent namespace package") + self.assertTrue("pkg1.pkg2" in pkg_resources._namespace_packages.keys()) + self.assertEqual(pkg_resources._namespace_packages["pkg1"], ["pkg1.pkg2"]) + |