Cleanup:

* no need to introduce another variable where we check for a __parent__ attribute
* clean up after failed getattr (it throws an AttributeError)
* properly DECREF the __parent__ attribute when it's no longer needed and
  the wrapper that is temporarily created from the __parent__ attribute.
parent 60516263
...@@ -539,7 +539,7 @@ Wrapper_acquire(Wrapper *self, PyObject *oname, ...@@ -539,7 +539,7 @@ Wrapper_acquire(Wrapper *self, PyObject *oname,
PyObject *filter, PyObject *extra, PyObject *orig, PyObject *filter, PyObject *extra, PyObject *orig,
int explicit, int containment) int explicit, int containment)
{ {
PyObject *r, *v, *tb, *__parent__; PyObject *r, *v, *tb;
int sob=1, sco=1; int sob=1, sco=1;
if (self->container) if (self->container)
...@@ -570,13 +570,17 @@ Wrapper_acquire(Wrapper *self, PyObject *oname, ...@@ -570,13 +570,17 @@ Wrapper_acquire(Wrapper *self, PyObject *oname,
acquisition wrapper for it accordingly. Then we can proceed acquisition wrapper for it accordingly. Then we can proceed
with Wrapper_findattr, just as if the container had an with Wrapper_findattr, just as if the container had an
acquisition wrapper in the first place (see above) */ acquisition wrapper in the first place (see above) */
else if ((__parent__ = PyObject_GetAttr(self->container, py__parent__))) else if ((r = PyObject_GetAttr(self->container, py__parent__)))
{ {
ASSIGN(self->container, newWrapper(self->container, __parent__, ASSIGN(self->container, newWrapper(self->container, r,
(PyTypeObject*)&Wrappertype)); (PyTypeObject*)&Wrappertype));
Py_DECREF(r); /* don't need __parent__ anymore */
r=Wrapper_findattr((Wrapper*)self->container, r=Wrapper_findattr((Wrapper*)self->container,
oname, filter, extra, orig, sob, sco, explicit, oname, filter, extra, orig, sob, sco, explicit,
containment); containment);
/* no need to DECREF the wrapper here because it's not
stored away in self->container, hence self owns its
reference now */
return r; return r;
} }
/* the container is the end of the acquisition chain; if we /* the container is the end of the acquisition chain; if we
...@@ -1376,7 +1380,7 @@ static PyObject * ...@@ -1376,7 +1380,7 @@ static PyObject *
capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter, capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter,
PyObject *extra, int explicit, PyObject *defalt, int containment) PyObject *extra, int explicit, PyObject *defalt, int containment)
{ {
PyObject *result, *__parent__; PyObject *result, *v, *tb;
if (filter==Py_None) filter=0; if (filter==Py_None) filter=0;
...@@ -1387,32 +1391,47 @@ capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter, ...@@ -1387,32 +1391,47 @@ capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter,
explicit || explicit ||
WRAPPER(self)->ob_type==(PyTypeObject*)&Wrappertype, WRAPPER(self)->ob_type==(PyTypeObject*)&Wrappertype,
explicit, containment); explicit, containment);
/* Not wrapped; check if we have a __parent__ pointer. If that's /* Not wrapped; check if we have a __parent__ pointer. If that's
the case, we create a wrapper and pretend it's business as the case, we create a wrapper and pretend it's business as
usual */ usual */
if ((__parent__ = PyObject_GetAttr(self, py__parent__))) else if ((result = PyObject_GetAttr(self, py__parent__)))
{ {
self = newWrapper(self, __parent__, (PyTypeObject*)&Wrappertype); self = newWrapper(self, result, (PyTypeObject*)&Wrappertype);
return Wrapper_findattr( Py_DECREF(result); /* don't need __parent__ anymore */
WRAPPER(self), name, filter, extra, OBJECT(self), 1, 1, result = Wrapper_findattr(
explicit, containment); WRAPPER(self), name, filter, extra, OBJECT(self),
1, 1, explicit, containment);
/* get rid of temp wrapper */
Py_DECREF(self);
return result;
} }
/* 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) */
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);
/* no filter, and no __parent__, so just getattr */ if (! filter) return PyObject_GetAttr(self, name);
if (! filter) return PyObject_GetAttr(self, name);
/* Crap, we've got to construct a wrapper so we can use Wrapper_findattr */ /* Crap, we've got to construct a wrapper so we can use
UNLESS (self=newWrapper(self, Py_None, (PyTypeObject*)&Wrappertype)) Wrapper_findattr */
return NULL; UNLESS (self=newWrapper(self, Py_None, (PyTypeObject*)&Wrappertype))
return NULL;
result=Wrapper_findattr(WRAPPER(self), name, filter, extra, OBJECT(self), result=Wrapper_findattr(WRAPPER(self), name, filter, extra, OBJECT(self),
1, 1, explicit, containment); 1, 1, explicit, containment);
/* get rid of temp wrapper */
Py_DECREF(self);
return result; /* get rid of temp wrapper */
Py_DECREF(self);
return result;
}
} }
static PyObject * static PyObject *
...@@ -1516,6 +1535,8 @@ capi_aq_parent(PyObject *self) ...@@ -1516,6 +1535,8 @@ capi_aq_parent(PyObject *self)
return result; return result;
} }
else if ((result=PyObject_GetAttr(self, py__parent__))) else if ((result=PyObject_GetAttr(self, py__parent__)))
/* no need to INCREF here because we're passing on the reference
GetAttr gave us */
return result; return result;
else else
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment