aq_dynamic.patch 9.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
diff -uNr Acquisition-2.13.8/src/Acquisition/_Acquisition.c Acquisition-2.13.8nxd001/src/Acquisition/_Acquisition.c
--- Acquisition-2.13.8/src/Acquisition/_Acquisition.c	2011-06-11 17:19:14.000000000 +0200
+++ Acquisition-2.13.8nxd001/src/Acquisition/_Acquisition.c	2013-10-31 16:24:55.665085888 +0100
@@ -448,6 +448,64 @@
 }
 
 static PyObject *
+Wrapper_GetAttr(PyObject *self, PyObject *attr_name, PyObject *orig)
+{
+  /* This function retrieves an attribute from an object by PyObject_GetAttr.
+
+     The main difference between Wrapper_GetAttr and PyObject_GetAttr is that
+     Wrapper_GetAttr calls _aq_dynamic to generate an attribute dynamically, if
+     the attribute is not found.
+  */
+  PyObject *r, *v, *tb;
+  PyObject *d, *m;
+  PyObject *o;
+
+  if (isWrapper (self))
+    o = WRAPPER(self)->obj;
+  else
+    o = self;
+  
+  /* Try to get an attribute in the normal way first.  */
+  r = PyObject_GetAttr(o, attr_name);
+  if (r)
+    return r;
+
+  /* If an unexpected error happens, return immediately.  */
+  PyErr_Fetch(&r,&v,&tb);
+  if (r != PyExc_AttributeError)
+    {
+      PyErr_Restore(r,v,tb);
+      return NULL;
+    }
+
+  /* Try to get _aq_dynamic.  */
+  m = PyObject_GetAttrString(o, "_aq_dynamic");
+  if (! m) {
+    PyErr_Restore(r,v,tb);
+    return NULL;
+  }
+
+  /* Call _aq_dynamic in the context of the original acquisition wrapper.  */
+  if (PyECMethod_Check(m) && PyECMethod_Self(m)==o)
+    ASSIGN(m,PyECMethod_New(m,OBJECT(self)));
+  else if (has__of__(m)) ASSIGN(m,__of__(m,OBJECT(self)));
+  d = PyObject_CallFunction(m, "O", attr_name);
+  Py_DECREF(m);
+
+  /* In the case of None, assume that the attribute is not found.  */
+  if (d == Py_None) {
+    Py_DECREF(d);
+    PyErr_Restore(r,v,tb);
+    return NULL;
+  }
+
+  Py_XDECREF(r);
+  Py_XDECREF(v);
+  Py_XDECREF(tb);
+  return d;
+}
+
+static PyObject *
 Wrapper_acquire(Wrapper *self, PyObject *oname, 
 		PyObject *filter, PyObject *extra, PyObject *orig,
 		int explicit, int containment);
@@ -545,8 +603,8 @@
 	  Py_XDECREF(r); Py_XDECREF(v); Py_XDECREF(tb);
 	  r=NULL;
 	}
-	  /* normal attribute lookup */
-      else if ((r=PyObject_GetAttr(self->obj,oname)))
+      /* Give _aq_dynamic a chance, then normal attribute lookup */
+      else if ((r=Wrapper_GetAttr(OBJECT(self),oname,orig)))
 	{
 	  if (r==Acquired)
 	    {
@@ -670,7 +728,7 @@
           Py_XDECREF(r); Py_XDECREF(v); Py_XDECREF(tb);
           r=NULL;
 
-	  if ((r=PyObject_GetAttr(self->container,oname))) {
+	  if ((r=Wrapper_GetAttr(self->container,oname,orig))) {
 	    if (r == Acquired) {
 	      Py_DECREF(r);
 	    }
@@ -707,7 +765,7 @@
 Wrapper_getattro(Wrapper *self, PyObject *oname)
 {
   if (self->obj || self->container)
-    return Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 1, 0, 0);
+    return Wrapper_findattr(self, oname, NULL, NULL, OBJECT(self), 1, 1, 0, 0);
 
   /* Maybe we are getting initialized? */
   return Py_FindAttr(OBJECT(self),oname);
@@ -724,7 +782,7 @@
     return Py_FindAttr(OBJECT(self),oname);
 
   if (self->obj || self->container)
-    return Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 0, 0, 0);
+    return Wrapper_findattr(self, oname, NULL, NULL, OBJECT(self), 1, 0, 0, 0);
 
   /* Maybe we are getting initialized? */
   return Py_FindAttr(OBJECT(self),oname);
diff -uNr Acquisition-2.13.8/src/Acquisition/test_dynamic_acquisition.py Acquisition-2.13.8nxd001/src/Acquisition/test_dynamic_acquisition.py
--- Acquisition-2.13.8/src/Acquisition/test_dynamic_acquisition.py	1970-01-01 01:00:00.000000000 +0100
+++ Acquisition-2.13.8nxd001/src/Acquisition/test_dynamic_acquisition.py	2013-10-31 16:24:55.665085888 +0100
@@ -0,0 +1,160 @@
+##############################################################################
+#
+# Copyright (c) 1996-2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
+import Acquisition
+
+def checkContext(self, o):
+    # Python equivalent to aq_inContextOf
+    from Acquisition import aq_base, aq_parent, aq_inner
+    subob = self
+    o = aq_base(o)
+    while 1:
+        if aq_base(subob) is o:
+            return True
+        self = aq_inner(subob)
+        if self is None: break
+        subob = aq_parent(self)
+        if subob is None: break
+    return False
+
+class B(Acquisition.Implicit):
+    color='red'
+
+    def __init__(self, name='b'):
+        self.name = name
+
+    def _aq_dynamic(self, attr):
+        if attr == 'bonjour': return None
+
+        def dynmethod():
+            chain = ' <- '.join(repr(obj) for obj in Acquisition.aq_chain(self))
+            print repr(self) + '.' + attr
+            print 'chain:', chain
+
+        return dynmethod
+
+    def __repr__(self):
+        return "%s(%r)" % (self.__class__.__name__, self.name)
+
+class A(Acquisition.Implicit):
+
+    def __init__(self, name='a'):
+        self.name = name
+
+    def hi(self):
+        print self, self.color
+
+    def _aq_dynamic(self, attr):
+        return None
+
+    def __repr__(self):
+        return "%s(%r)" % (self.__class__.__name__, self.name)
+
+def test_dynamic():
+    r'''
+    The _aq_dynamic functionality allows an object to dynamically provide an
+    attribute.
+    
+    If an object doesn't have an attribute, Acquisition checks to see if the
+    object has a _aq_dynamic method, which is then called. It is functionally
+    equivalent to __getattr__, but _aq_dynamic is called with 'self' as the
+    acquisition wrapped object where as __getattr__ is called with self as the
+    unwrapped object.
+
+    Let's see how this works. In the examples below, the A class defines
+    '_aq_dynamic', but returns 'None' for all attempts, which means that no new
+    attributes should be generated dynamically. It also doesn't define 'color'
+    attribute, even though it uses it in the 'hi' method.
+
+        >>> A().hi()
+        Traceback (most recent call last):
+        ...
+        AttributeError: color
+    
+    The class B, on the other hand, generates all attributes dynamically,
+    except if it is called 'bonjour'.
+    
+    First we need to check that, even if an object provides '_aq_dynamic',
+    "regular" Aquisition attribute access should still work:
+
+        >>> b=B()
+        >>> b.a=A()
+        >>> b.a.hi()
+        A('a') red
+        >>> b.a.color='green'
+        >>> b.a.hi()
+        A('a') green
+
+    Now, let's see some dynamically generated action. B does not define a
+    'salut' method, but remember that it dynamically generates a method for
+    every attribute access:
+
+        >>> b.a.salut()
+        B('b').salut
+        chain: B('b')
+
+        >>> a=A('a1')
+        >>> a.b=B('b1')
+        >>> a.b.salut()
+        B('b1').salut
+        chain: B('b1') <- A('a1')
+
+        >>> b.a.bonjour()
+        Traceback (most recent call last):
+        ...
+        AttributeError: bonjour
+
+        >>> a.b.bonjour()
+        Traceback (most recent call last):
+        ...
+        AttributeError: bonjour
+
+    '''
+
+def test_wrapper_comparissons():
+    r'''
+
+    Test wrapper comparisons in presence of _aq_dynamic
+
+        >>> b=B()
+        >>> b.a=A()
+        >>> foo = b.a
+        >>> bar = b.a
+        >>> assert( foo == bar )
+        >>> c = A('c')
+        >>> b.c = c
+        >>> b.c.d = c
+        >>> b.c.d == c
+        True
+        >>> b.c.d == b.c
+        True
+        >>> b.c == c
+        True
+
+    Test contextuality in presence of _aq_dynamic
+
+        >>> checkContext(b.c, b)
+        True
+        >>> checkContext(b.c, b.a)
+        False
+
+        >>> assert b.a.aq_inContextOf(b)
+        >>> assert b.c.aq_inContextOf(b)
+        >>> assert b.c.d.aq_inContextOf(b)
+        >>> assert b.c.d.aq_inContextOf(c)
+        >>> assert b.c.d.aq_inContextOf(b.c)
+        >>> assert not b.c.aq_inContextOf(foo)
+        >>> assert not b.c.aq_inContextOf(b.a)
+        >>> assert not b.a.aq_inContextOf('somestring')
+'''
+
diff -uNr Acquisition-2.13.8/src/Acquisition/tests.py Acquisition-2.13.8nxd001/src/Acquisition/tests.py
--- Acquisition-2.13.8/src/Acquisition/tests.py	2011-06-11 17:09:38.000000000 +0200
+++ Acquisition-2.13.8nxd001/src/Acquisition/tests.py	2013-10-31 16:24:55.669085888 +0100
@@ -2552,6 +2552,7 @@
 def test_suite():
     return unittest.TestSuite((
         DocTestSuite(),
+        DocTestSuite('Acquisition.test_dynamic_acquisition'),
         DocFileSuite('README.txt', package='Acquisition'),
         unittest.makeSuite(TestParent),
         unittest.makeSuite(TestAcquire),
diff -uNr Acquisition-2.13.8/src/Acquisition.egg-info/SOURCES.txt Acquisition-2.13.8nxd001/src/Acquisition.egg-info/SOURCES.txt
--- Acquisition-2.13.8/src/Acquisition.egg-info/SOURCES.txt	2011-06-11 17:21:18.000000000 +0200
+++ Acquisition-2.13.8nxd001/src/Acquisition.egg-info/SOURCES.txt	2013-10-31 16:24:55.669085888 +0100
@@ -15,6 +15,7 @@
 src/Acquisition/_Acquisition.c
 src/Acquisition/__init__.py
 src/Acquisition/interfaces.py
+src/Acquisition/test_dynamic_acquisition.py
 src/Acquisition/tests.py
 src/Acquisition.egg-info/PKG-INFO
 src/Acquisition.egg-info/SOURCES.txt