[py-svn] apipkg commit 5fc11da5b2f6: add a test for ronny's bpython '__makeattr' work around

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sun Jan 10 13:44:12 CET 2010


# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview/
# User holger krekel <holger at merlinux.eu>
# Date 1263127131 -3600
# Node ID 5fc11da5b2f62f4bd225672b3f01b4ff3747afb3
# Parent 9e411e7c844a86a4ebe16786fc9f6f1964dd2da9
add a test for ronny's bpython '__makeattr' work around

--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -259,3 +259,14 @@ def test_onfirstaccess_setsnewattr(tmpdi
         assert not hasattr(mod, '__onfirstaccess__')
         assert not hasattr(mod, '__onfirstaccess__')
     assert '__onfirstaccess__' not in vars(mod)
+
+def test_bpython_getattr_override(tmpdir, monkeypatch):
+    def patchgetattr(self, name):
+        raise AttributeError(name)
+    monkeypatch.setattr(apipkg.ApiModule, '__getattr__', patchgetattr)
+    api = apipkg.ApiModule('bpy', {
+        'abspath': 'os.path:abspath',
+        })
+    d = api.__dict__
+    assert 'abspath' in d
+    

--- a/apipkg.py
+++ b/apipkg.py
@@ -55,14 +55,7 @@ class ApiModule(ModuleType):
         return '<ApiModule %r>' % (self.__name__,)
 
     def __makeattr(self, name):
-        '''
-        load the attribute `name`
-        assign it to self and return is
-
-        also aliased to __getattr__
-        the name __makeattr__ is used
-        '''
-
+        """lazily compute value for name or raise AttributeError if unknown."""
         target = None
         if '__onfirstaccess__' in self.__map__:
             target = self.__map__.pop('__onfirstaccess__')
@@ -80,7 +73,7 @@ class ApiModule(ModuleType):
             del self.__map__[name]
             return result
 
-    __getattr__ = __makeattr # support getattr by aliasing
+    __getattr__ = __makeattr
 
     def __dict__(self):
         # force all the content of the module to be loaded when __dict__ is read
@@ -89,7 +82,6 @@ class ApiModule(ModuleType):
         if dict is not None:
             hasattr(self, 'some')
             for name in self.__all__:
-                # force attribute load, ignore errors
                 try:
                     self.__makeattr(name)
                 except AttributeError:



More information about the pytest-commit mailing list