[Zope-Checkins] SVN: Zope/branches/philikon-aq/lib/python/Acquisition/ Make Acquisition.aq_inContextOf aware of __parent__ pointers.

Philipp von Weitershausen philikon at philikon.de
Sat Jul 28 08:12:25 EDT 2007


Log message for revision 78423:
  Make Acquisition.aq_inContextOf aware of __parent__ pointers.
  

Changed:
  U   Zope/branches/philikon-aq/lib/python/Acquisition/_Acquisition.c
  U   Zope/branches/philikon-aq/lib/python/Acquisition/tests.py

-=-
Modified: Zope/branches/philikon-aq/lib/python/Acquisition/_Acquisition.c
===================================================================
--- Zope/branches/philikon-aq/lib/python/Acquisition/_Acquisition.c	2007-07-28 10:33:33 UTC (rev 78422)
+++ Zope/branches/philikon-aq/lib/python/Acquisition/_Acquisition.c	2007-07-28 12:12:24 UTC (rev 78423)
@@ -1190,55 +1190,9 @@
 # endif
 }
 
-static PyObject *
-capi_aq_inContextOf(PyObject *self, PyObject *o, int inner)
-{
-  PyObject *next, *c;
+/* forward declaration so that we can use it in Wrapper_inContextOf */
+static PyObject * capi_aq_inContextOf(PyObject *self, PyObject *o, int inner);
 
-  if (inner) {
-    /* next = self
-       o = aq_base(o) */
-    next = self;
-    while (isWrapper(o) && WRAPPER(o)->obj)
-      o=WRAPPER(o)->obj;
-
-    /* while 1: */
-    while (1) {
-
-      /*   if aq_base(next) is o: return 1 */
-      c = next;
-      while (isWrapper(c) && WRAPPER(c)->obj) c = WRAPPER(c)->obj;
-      if (c == o) return PyInt_FromLong(1);
-
-      /*   self = aq_inner(next) */
-      /*   if self is None: break */
-      if (isWrapper(next)) {
-        self = next;
-        while (WRAPPER(self)->obj && isWrapper(WRAPPER(self)->obj))
-          self = WRAPPER(self)->obj;
-      }
-      else break;
-
-      /*   next = aq_parent(self) */
-      /*   if next is None: break */
-      if (WRAPPER(self)->container)
-        next = WRAPPER(self)->container;
-      else break;
-    }
-  }
-  else {
-    /* Follow wrappers instead. */
-    c = (PyObject*)self;
-    while (1) {
-      if (c==o) return PyInt_FromLong(1);
-      if (c && isWrapper(c)) c=WRAPPER(c)->container;
-      else break;
-    }
-  }
-
-  return PyInt_FromLong(0);
-}
-
 static PyObject *
 Wrapper_inContextOf(Wrapper *self, PyObject *args)
 {
@@ -1753,6 +1707,41 @@
 }
 
 static PyObject *
+capi_aq_inContextOf(PyObject *self, PyObject *o, int inner)
+{
+  PyObject *next, *c;
+
+  /* next = self
+     o = aq_base(o) */
+  next = self;
+  while (isWrapper(o) && WRAPPER(o)->obj)
+    o=WRAPPER(o)->obj;
+
+  while (1) {
+
+    /*   if aq_base(next) is o: return 1 */
+    c = next;
+    while (isWrapper(c) && WRAPPER(c)->obj) c = WRAPPER(c)->obj;
+    if (c == o) return PyInt_FromLong(1);
+
+    if (inner)
+      {
+        self = capi_aq_inner(next);
+        Py_DECREF(self);  /* We're not holding on to the inner wrapper */
+        if (self == Py_None) break;
+      }
+    else
+      self = next;
+
+    next = capi_aq_parent(self);
+    Py_DECREF(next); /* We're not holding on to the parent */
+    if (next == Py_None) break;
+  }
+
+  return PyInt_FromLong(0);
+}
+
+static PyObject *
 module_aq_inContextOf(PyObject *ignored, PyObject *args)
 {
   PyObject *self, *o;

Modified: Zope/branches/philikon-aq/lib/python/Acquisition/tests.py
===================================================================
--- Zope/branches/philikon-aq/lib/python/Acquisition/tests.py	2007-07-28 10:33:33 UTC (rev 78422)
+++ Zope/branches/philikon-aq/lib/python/Acquisition/tests.py	2007-07-28 12:12:24 UTC (rev 78423)
@@ -1232,6 +1232,9 @@
     ...     def hi(self):
     ...         print "%s()" % self.__class__.__name__, self.color
 
+    >>> class Location(object):
+    ...     __parent__ = None
+
     >>> b=B()
     >>> b.a=A()
     >>> b.a.hi()
@@ -1261,6 +1264,8 @@
     >>> b.c == c
     1
 
+    >>> l = Location()
+    >>> l.__parent__ = b.c
 
     >>> def checkContext(self, o):
     ...     # Python equivalent to aq_inContextOf
@@ -1276,6 +1281,7 @@
     ...         next = aq_parent(self)
     ...         if next is None:
     ...             break
+    ...     return 0
 
 
     >>> checkContext(b.c, b)
@@ -1283,13 +1289,27 @@
     >>> not checkContext(b.c, b.a)
     1
 
+    >>> checkContext(l, b)
+    1
+    >>> checkContext(l, b.c)
+    1
+    >>> not checkContext(l, b.a)
+    1
+
     Acquisition.aq_inContextOf works the same way:
 
     >>> Acquisition.aq_inContextOf(b.c, b)
     1
-    >>> not Acquisition.aq_inContextOf(b.c, b.a)
+    >>> Acquisition.aq_inContextOf(b.c, b.a)
+    0
+
+    >>> Acquisition.aq_inContextOf(l, b)
     1
-    
+    >>> Acquisition.aq_inContextOf(l, b.c)
+    1
+    >>> Acquisition.aq_inContextOf(l, b.a)
+    0
+
     >>> b.a.aq_inContextOf(b)
     1
     >>> b.c.aq_inContextOf(b)
@@ -1300,12 +1320,12 @@
     1
     >>> b.c.d.aq_inContextOf(b.c)
     1
-    >>> not b.c.aq_inContextOf(foo)
-    1
-    >>> not b.c.aq_inContextOf(b.a)
-    1
-    >>> not b.a.aq_inContextOf('somestring')
-    1
+    >>> b.c.aq_inContextOf(foo)
+    0
+    >>> b.c.aq_inContextOf(b.a)
+    0
+    >>> b.a.aq_inContextOf('somestring')
+    0
     """
 
 def test_AqAlg():



More information about the Zope-Checkins mailing list