[py-svn] apipkg commit f7ad3ae4e30f: dont load __doc__ early, making ApiModules now fully lazy

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Nov 4 22:50:05 CET 2010


# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview
# User holger krekel <holger at merlinux.eu>
# Date 1288907454 -3600
# Node ID f7ad3ae4e30f937c2e789331e80ae22315b17c2c
# Parent  fd590ef45812b3b031ec3412e99b6f03324e9bf9
dont load __doc__ early, making ApiModules now fully lazy
also bump and normalize __version__ setting

--- a/setup.py
+++ b/setup.py
@@ -13,15 +13,13 @@ try:
 except ImportError:
     from distutils.core import setup
 
-from apipkg import __version__
-
 def main():
     setup(
         name='apipkg',
         description=
         'apipkg: namespace control and lazy-import mechanism',
         long_description = open('README.txt').read(),
-        version= __version__,
+        version='1.2.dev4',
         url='http://bitbucket.org/hpk42/apipkg',
         license='MIT License',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

--- a/apipkg.py
+++ b/apipkg.py
@@ -25,7 +25,7 @@ def initpkg(pkgname, exportdefs, attr=di
         d['__loader__'] = oldmod.__loader__
     if hasattr(oldmod, '__path__'):
         d['__path__'] = [os.path.abspath(p) for p in oldmod.__path__]
-    if hasattr(oldmod, '__doc__'):
+    if '__doc__' not in exportdefs and getattr(oldmod, '__doc__', None):
         d['__doc__'] = oldmod.__doc__
     d.update(attr)
     if hasattr(oldmod, "__dict__"):
@@ -45,6 +45,16 @@ def importobj(modpath, attrname):
     return retval
 
 class ApiModule(ModuleType):
+    def __docget(self):
+        try:
+            return self.__doc
+        except AttributeError:
+            if '__doc__' in self.__map__:
+                return self.__makeattr('__doc__')
+    def __docset(self, value):
+        self.__doc = value
+    __doc__ = property(__docget, __docset)
+
     def __init__(self, name, importspec, implprefix=None, attr=None):
         self.__name__ = name
         self.__all__ = [x for x in importspec if x != '__onfirstaccess__']
@@ -73,10 +83,7 @@ class ApiModule(ModuleType):
                     sys.modules[subname] = apimod
                     setattr(self, name, apimod)
                 else:
-                    if name == '__doc__':
-                        self.__doc__ = importobj(modpath, attrname)
-                    else:
-                        self.__map__[name] = (modpath, attrname)
+                    self.__map__[name] = (modpath, attrname)
 
     def __repr__(self):
         l = []

--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -115,12 +115,12 @@ class TestScenarios:
             })
         """))
         pkgdir.join('submod.py').write(py.code.Source("""
-            import recmodule 
+            import recmodule
             class someclass: pass
             print (recmodule.__dict__)
         """))
         monkeypatch.syspath_prepend(tmpdir)
-        import recmodule 
+        import recmodule
         assert isinstance(recmodule, apipkg.ApiModule)
         assert recmodule.some.__name__ == "someclass"
 
@@ -224,14 +224,22 @@ def test_initpkg_transfers_attrs(monkeyp
     assert newmod.__loader__ == mod.__loader__
     assert newmod.__doc__ == mod.__doc__
 
-def test_initpkg_not_overwrite_exportdefs(monkeypatch):
+def test_initpkg_nodoc(monkeypatch):
     mod = type(sys)('hello')
-    mod.__doc__ = "this is the documentation"
+    mod.__file__ = "hello.py"
     monkeypatch.setitem(sys.modules, 'hello', mod)
+    apipkg.initpkg('hello', {})
+    newmod = sys.modules['hello']
+    assert not newmod.__doc__
+
+def test_initpkg_overwrite_doc(monkeypatch):
+    hello = type(sys)('hello')
+    hello.__doc__ = "this is the documentation"
+    monkeypatch.setitem(sys.modules, 'hello', hello)
     apipkg.initpkg('hello', {"__doc__": "sys:__doc__"})
-    newmod = sys.modules['hello']
-    assert newmod != mod
-    assert newmod.__doc__ == sys.__doc__
+    newhello = sys.modules['hello']
+    assert newhello != hello
+    assert newhello.__doc__ == sys.__doc__
 
 def test_initpkg_not_transfers_not_existing_attrs(monkeypatch):
     mod = type(sys)('hello')
@@ -289,7 +297,7 @@ def test_onfirstaccess(tmpdir, monkeypat
     """))
     pkgdir.join('submod.py').write(py.code.Source("""
         l = []
-        def init(): 
+        def init():
             l.append(1)
     """))
     monkeypatch.syspath_prepend(tmpdir)
@@ -311,9 +319,9 @@ def test_onfirstaccess_setsnewattr(tmpdi
         )
     """))
     pkgdir.join('submod.py').write(py.code.Source("""
-        def init(): 
+        def init():
             import %s as pkg
-            pkg.newattr = 42 
+            pkg.newattr = 42
     """ % pkgname))
     monkeypatch.syspath_prepend(tmpdir)
     mod = __import__(pkgname)

--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,7 +1,8 @@
-1.2
+1.2.dev
 ----------------------------------------
 
 - Allow to import from Aliasmodules  (thanks Ralf Schmitt)
+- avoid loading __doc__ early, so ApiModule is now fully lazy
 
 1.1
 ----------------------------------------



More information about the pytest-commit mailing list