Skip to content

Commit

Permalink
Step 4: Make aq_get aware of __parent__ pointers.
Browse files Browse the repository at this point in the history
(Also some comment cosmetics in _Acquisition.c)
  • Loading branch information
philikon committed Nov 21, 2006
1 parent 398463f commit aedd241
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
37 changes: 29 additions & 8 deletions _Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -1391,9 +1391,8 @@ capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter,
explicit ||
WRAPPER(self)->ob_type==(PyTypeObject*)&Wrappertype,
explicit, containment);
/* Not wrapped; check if we have a __parent__ pointer. If that's
the case, we create a wrapper and pretend it's business as
usual */
/* Not wrapped; check if we have a __parent__ pointer. In that
case, create a wrapper and pretend it's business as usual */
else if ((result = PyObject_GetAttr(self, py__parent__)))
{
self = newWrapper(self, result, (PyTypeObject*)&Wrappertype);
Expand All @@ -1408,8 +1407,8 @@ capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter,
/* No filter, and no __parent__, so just getattr */
else
{
/* we need to clean up the AttributeError from the previous
getattr (because it has clearly failed) */
/* clean up the AttributeError from the previous getattr
(because it has clearly failed) */
PyErr_Fetch(&result,&v,&tb);
if (result && (result != PyExc_AttributeError))
{
Expand Down Expand Up @@ -1457,13 +1456,35 @@ module_aq_acquire(PyObject *ignored, PyObject *args, PyObject *kw)
static PyObject *
capi_aq_get(PyObject *self, PyObject *name, PyObject *defalt, int containment)
{
PyObject *result = NULL;
PyObject *result = NULL, *v, *tb;
/* We got a wrapped object, so business as usual */
if (isWrapper(self))
result=Wrapper_findattr(WRAPPER(self), name, 0, 0, OBJECT(self), 1, 1, 1,
containment);
containment);
/* Not wrapped; check if we have a __parent__ pointer. In that
case, create a wrapper and pretend it's business as usual */
else if ((result = PyObject_GetAttr(self, py__parent__)))
{
self=newWrapper(self, result, (PyTypeObject*)&Wrappertype);
Py_DECREF(result); /* don't need __parent__ anymore */
result=Wrapper_findattr(WRAPPER(self), name, 0, 0, OBJECT(self),
1, 1, 1, containment);
Py_DECREF(self); /* get rid of temp wrapper */
}
else
result=PyObject_GetAttr(self, name);
{
/* 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);

result=PyObject_GetAttr(self, name);
}

if (! result && defalt)
{
Expand Down
58 changes: 52 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,8 @@ def test___parent__no_wrappers():
>>> z.foo = 43 # this should not be found
>>> z.bar = 3.145
``aq_acquire`` works we know it from implicit/acquisition wrappers:
``aq_acquire`` works as we know it from implicit/acquisition
wrappers:
>>> Acquisition.aq_acquire(x, 'hello')
'world'
Expand All @@ -1697,7 +1698,16 @@ def test___parent__no_wrappers():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
as does ``aq_get``:
>>> Acquisition.aq_get(x, 'hello')
'world'
>>> Acquisition.aq_get(x, 'foo')
42
>>> Acquisition.aq_get(x, 'bar')
3.145
and ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
Expand Down Expand Up @@ -1741,7 +1751,16 @@ def test_implicit_wrapper_as___parent__():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
as does ``aq_get``:
>>> Acquisition.aq_get(x, 'hello')
'world'
>>> Acquisition.aq_get(x, 'foo')
42
>>> Acquisition.aq_get(x, 'bar')
3.145
and ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
Expand Down Expand Up @@ -1808,7 +1827,16 @@ def test_explicit_wrapper_as___parent__():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
as does ``aq_get``:
>>> Acquisition.aq_get(x, 'hello')
'world'
>>> Acquisition.aq_get(x, 'foo')
42
>>> Acquisition.aq_get(x, 'bar')
3.145
and ``aq_parent``:
>>> Acquisition.aq_parent(x) is y
True
Expand Down Expand Up @@ -1869,7 +1897,16 @@ def test_implicit_wrapper_has_nonwrapper_as_aq_parent():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
as does ``aq_get``:
>>> Acquisition.aq_get(x, 'hello')
'world'
>>> Acquisition.aq_get(x, 'foo')
42
>>> Acquisition.aq_get(x, 'bar')
3.145
and ``aq_parent``:
>>> Acquisition.aq_parent(x) == y
True
Expand Down Expand Up @@ -1915,7 +1952,16 @@ def test_explicit_wrapper_has_nonwrapper_as_aq_parent():
>>> Acquisition.aq_acquire(x, 'bar')
3.145
as does ``aq_parent``:
as does ``aq_get``:
>>> Acquisition.aq_get(x, 'hello')
'world'
>>> Acquisition.aq_get(x, 'foo')
42
>>> Acquisition.aq_get(x, 'bar')
3.145
and ``aq_parent``:
>>> Acquisition.aq_parent(x) == y
True
Expand Down

0 comments on commit aedd241

Please sign in to comment.