Commit 606015e8 authored by Aurelien Bompard's avatar Aurelien Bompard

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

--HG--
branch : distribute
extra : rebase_source : 1644e937b2d0b2fae58e27740d0fddfe082586cd
parent 44c1998e
......@@ -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
......
......@@ -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"])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment