Aurélien Bompard avatar Aurélien Bompard committed a3f0d30

Don't try to import the parent of a namespace package in declare_namespace -- fixes #204

Comments (0)

Files changed (2)

         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

setuptools/tests/test_resources.py

 # 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
             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"])
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.