[Checkins] SVN: zope.configuration/trunk/ use a package's __path__ instead of its __file__ if the latter is missing

David Glick davidglick at onenw.org
Fri Mar 11 14:33:23 EST 2011


Log message for revision 120870:
  use a package's __path__ instead of its __file__ if the latter is missing

Changed:
  U   zope.configuration/trunk/CHANGES.txt
  U   zope.configuration/trunk/src/zope/configuration/config.py
  U   zope.configuration/trunk/src/zope/configuration/tests/test_config.py

-=-
Modified: zope.configuration/trunk/CHANGES.txt
===================================================================
--- zope.configuration/trunk/CHANGES.txt	2011-03-11 15:39:28 UTC (rev 120869)
+++ zope.configuration/trunk/CHANGES.txt	2011-03-11 19:33:22 UTC (rev 120870)
@@ -5,6 +5,10 @@
 3.7.3 (unreleased)
 ------------------
 
+- Correctly locate packages with a __path__ attribute but no
+  __file__ attribute (such as namespace packages installed with setup.py
+  install --single-version-externally-managed).
+
 - Allow "info" and "includepath" to be passed optionally to context.action.
 
 3.7.2 (2010-04-30)

Modified: zope.configuration/trunk/src/zope/configuration/config.py
===================================================================
--- zope.configuration/trunk/src/zope/configuration/config.py	2011-03-11 15:39:28 UTC (rev 120869)
+++ zope.configuration/trunk/src/zope/configuration/config.py	2011-03-11 19:33:22 UTC (rev 120870)
@@ -241,7 +241,10 @@
             if self.package is None:
                 basepath = os.getcwd()
             else:
-                basepath = os.path.dirname(self.package.__file__)
+                if hasattr(self.package, '__path__'):
+                    basepath = self.package.__path__[0]
+                else:
+                    basepath = os.path.dirname(self.package.__file__)
                 basepath = os.path.abspath(basepath)
             self.basepath = basepath
 

Modified: zope.configuration/trunk/src/zope/configuration/tests/test_config.py
===================================================================
--- zope.configuration/trunk/src/zope/configuration/tests/test_config.py	2011-03-11 15:39:28 UTC (rev 120869)
+++ zope.configuration/trunk/src/zope/configuration/tests/test_config.py	2011-03-11 19:33:22 UTC (rev 120870)
@@ -244,7 +244,20 @@
     True
     """
 
+def test_basepath_uses_dunder_path():
+    """Determine package path using __path__ if __file__ isn't available.
+    (i.e. namespace package installed with --single-version-externally-managed)
 
+    >>> import os
+    >>> class stub:
+    ...     __path__ = [os.path.join('relative', 'path')]
+    >>> c = config.ConfigurationContext()
+    >>> c.package = stub()
+
+    >>> c.path('y/z').endswith('relative/path/y/z')
+    True
+    """
+
 def test_trailing_dot_in_resolve():
     """Dotted names are no longer allowed to end in dots
 



More information about the checkins mailing list