Skip to content

Commit

Permalink
Step 3: Make aq_parent aware of __parent__ pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
philikon committed Nov 21, 2006
1 parent 8f2eae2 commit 8deb579
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
28 changes: 23 additions & 5 deletions _Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ Wrapper_acquire(Wrapper *self, PyObject *oname,
{
/* we need to clean up the AttributeError from the previous
getattr (because it has clearly failed) */
/* perhaps it's overkill to only catch AttributeErrors */
PyErr_Fetch(&r,&v,&tb);
if (r && (r != PyExc_AttributeError))
{
Expand Down Expand Up @@ -1507,13 +1506,32 @@ module_aq_base(PyObject *ignored, PyObject *args)
static PyObject *
capi_aq_parent(PyObject *self)
{
PyObject *result=Py_None;
PyObject *result, *v, *tb;

if (isWrapper(self) && WRAPPER(self)->container)
result=WRAPPER(self)->container;
{
result=WRAPPER(self)->container;
Py_INCREF(result);
return result;
}
else if ((result=PyObject_GetAttr(self, py__parent__)))
return result;
else
{
/* we need to clean up the AttributeError from the previous
getattr (because it has clearly failed) */
PyErr_Fetch(&result,&v,&tb);
if (result && (result != PyExc_AttributeError))
{
PyErr_Restore(result,v,tb);
return NULL;
}
Py_XDECREF(result); Py_XDECREF(v); Py_XDECREF(tb);

Py_INCREF(result);
return result;
result=Py_None;
Py_INCREF(result);
return result;
}
}

static PyObject *
Expand Down
49 changes: 42 additions & 7 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,14 @@ def test___parent__no_wrappers():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
TODO aq_parent, aq_chain
as does ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
>>> Acquisition.aq_parent(y) is z
True
TODO aq_chain
"""

def test_implicit_wrapper_as___parent__():
Expand Down Expand Up @@ -1734,6 +1741,13 @@ def test_implicit_wrapper_as___parent__():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
>>> Acquisition.aq_parent(y) is z
True
Note that also the (implicit) acquisition wrapper has a __parent__
pointer, which is automatically computed from the acquisition
container (it's identical to aq_parent):
Expand All @@ -1759,7 +1773,7 @@ def test_implicit_wrapper_as___parent__():
...
AttributeError: __parent__
TODO aq_parent, aq_chain
TODO aq_chain
"""

def test_explicit_wrapper_as___parent__():
Expand Down Expand Up @@ -1794,6 +1808,13 @@ def test_explicit_wrapper_as___parent__():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
>>> Acquisition.aq_parent(y) is z
True
Note that also the (explicit) acquisition wrapper has a __parent__
pointer, which is automatically computed from the acquisition
container (it's identical to aq_parent):
Expand All @@ -1819,7 +1840,7 @@ def test_explicit_wrapper_as___parent__():
...
AttributeError: __parent__
TODO aq_parent, aq_chain
TODO aq_chain
"""

def test_implicit_wrapper_has_nonwrapper_as_aq_parent():
Expand All @@ -1839,7 +1860,7 @@ def test_implicit_wrapper_has_nonwrapper_as_aq_parent():
... hello = 'world'
>>> x = Impl().__of__(y)
Again, acquiring objects work as usual:
Again, acquiring objects works as usual:
>>> Acquisition.aq_acquire(x, 'hello')
'world'
Expand All @@ -1848,6 +1869,13 @@ def test_implicit_wrapper_has_nonwrapper_as_aq_parent():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
>>> Acquisition.aq_parent(x) == y
True
>>> Acquisition.aq_parent(y) is z
True
Because the outmost object, ``x``, is wrapped in an implicit
acquisition wrapper, we can also use direct attribute access:
Expand All @@ -1858,7 +1886,7 @@ def test_implicit_wrapper_has_nonwrapper_as_aq_parent():
>>> x.bar
3.145
TODO aq_parent, aq_chain
TODO aq_chain
"""

def test_explicit_wrapper_has_nonwrapper_as_aq_parent():
Expand All @@ -1878,7 +1906,7 @@ def test_explicit_wrapper_has_nonwrapper_as_aq_parent():
... hello = 'world'
>>> x = Expl().__of__(y)
Again, acquiring objects work as usual:
Again, acquiring objects works as usual:
>>> Acquisition.aq_acquire(x, 'hello')
'world'
Expand All @@ -1887,7 +1915,14 @@ def test_explicit_wrapper_has_nonwrapper_as_aq_parent():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
TODO aq_parent, aq_chain
as does ``aq_parent``:
>>> Acquisition.aq_parent(x) == y
True
>>> Acquisition.aq_parent(y) is z
True
TODO aq_chain
"""

import unittest
Expand Down

0 comments on commit 8deb579

Please sign in to comment.