diff options
| author | Chris McDonough <chrism@plope.com> | 2013-09-06 22:33:49 -0700 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2013-09-06 22:33:49 -0700 |
| commit | 60945d27ec06ca5c1855e7d8341d010f51153e60 (patch) | |
| tree | 2bfbe7e944decfc889e043e520bc98ecd0e590b9 | |
| parent | 1bd339e1b3c09ff62e1cd15623815448ab091d2a (diff) | |
| parent | c062d5ace6adedcf0f6434cffc07fb24cc608733 (diff) | |
| download | pyramid-60945d27ec06ca5c1855e7d8341d010f51153e60.tar.gz pyramid-60945d27ec06ca5c1855e7d8341d010f51153e60.tar.bz2 pyramid-60945d27ec06ca5c1855e7d8341d010f51153e60.zip | |
Merge pull request #1112 from dhellmann/bug/namespace-packages
Update package_name() to work with namespace pkgs
| -rw-r--r-- | CHANGES.txt | 6 | ||||
| -rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
| -rw-r--r-- | pyramid/path.py | 6 | ||||
| -rw-r--r-- | pyramid/tests/test_path.py | 16 |
4 files changed, 29 insertions, 1 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 6392c7b3b..5c058a9c5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,12 @@ Next Release ============ +Bug Fixes +--------- + +- Fix an exception in ``package_name()`` when resolving the package + name for namespace packages. + Backwards Incompatibilities --------------------------- diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 0ddaebf15..1a5b975d7 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -220,3 +220,5 @@ Contributors - Matthew Wilkes, 2013/08/23 - Takahiro Fujiwara, 2013/08/28 + +- Doug Hellmann, 2013/09/06 diff --git a/pyramid/path.py b/pyramid/path.py index eb92ea62b..470e766f8 100644 --- a/pyramid/path.py +++ b/pyramid/path.py @@ -33,8 +33,12 @@ def package_name(pkg_or_module): name of the package itself.""" if pkg_or_module is None or pkg_or_module.__name__ == '__main__': return '__main__' - pkg_filename = pkg_or_module.__file__ pkg_name = pkg_or_module.__name__ + pkg_filename = getattr(pkg_or_module, '__file__', None) + if pkg_filename is None: + # Namespace packages do not have __init__.py* files, + # and so have no __file__ attribute + return pkg_name splitted = os.path.split(pkg_filename) if splitted[-1] in init_names: # it's a package diff --git a/pyramid/tests/test_path.py b/pyramid/tests/test_path.py index a07ebeffa..fd927996a 100644 --- a/pyramid/tests/test_path.py +++ b/pyramid/tests/test_path.py @@ -154,6 +154,12 @@ class TestPackageName(unittest.TestCase): package = DummyPackageOrModule(tests) result = self._callFUT(package) self.assertEqual(result, 'pyramid.tests') + + def test_it_namespace_package(self): + from pyramid import tests + package = DummyNamespacePackage(tests) + result = self._callFUT(package) + self.assertEqual(result, 'pyramid.tests') def test_it_module(self): from pyramid.tests import test_path @@ -558,3 +564,13 @@ class DummyPackageOrModule: if self.raise_exc is not None: raise self.raise_exc self.__dict__[key] = val + +class DummyNamespacePackage: + """Has no __file__ attribute. + """ + + def __init__(self, real_package_or_module): + self.__name__ = real_package_or_module.__name__ + import os + self.package_path = os.path.dirname( + os.path.abspath(real_package_or_module.__file__)) |
