Skip to content

Commit

Permalink
Allow assignment to a wrapper's __parent__.
Browse files Browse the repository at this point in the history
  • Loading branch information
philikon committed Nov 21, 2006
1 parent f9a846a commit e9c2493
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions _Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v)

/* Allow assignment to parent, to change context. */
if (PyString_Check(oname)) name=PyString_AS_STRING(oname);
if (*name=='a' && name[1]=='q' && name[2]=='_'
&& strcmp(name+3,"parent")==0)
if ((*name=='a' && name[1]=='q' && name[2]=='_'
&& strcmp(name+3,"parent")==0) || (strcmp(name, "__parent__")==0))
{
Py_XINCREF(v);
ASSIGN(self->container, v);
Expand Down
44 changes: 40 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,9 +1713,9 @@ def test_implicit_wrapper_as___parent__():
The intermediate parent is an object that supports implicit
acquisition. We bind it to the root via the __of__ protocol:
>>> class ImplWrapper(Acquisition.Implicit):
>>> class Impl(Acquisition.Implicit):
... foo = 42
>>> y = ImplWrapper().__of__(z)
>>> y = Impl().__of__(z)
The child object is again a simple object with a simple __parent__
pointer:
Expand All @@ -1741,6 +1741,24 @@ def test_implicit_wrapper_as___parent__():
>>> y.__parent__ is z
True
Just as much as you can assign to aq_parent, you can also assign
to __parent__ to change the acquisition context of the wrapper:
>>> newroot = Root()
>>> y.__parent__ = newroot
>>> y.__parent__ is z
False
>>> y.__parent__ is newroot
True
Note that messing with the wrapper won't in any way affect the
wrapped object:
>>> Acquisition.aq_base(y).__parent__
Traceback (most recent call last):
...
AttributeError: __parent__
TODO aq_parent, aq_chain
"""

Expand All @@ -1755,9 +1773,9 @@ def test_explicit_wrapper_as___parent__():
The intermediate parent is an object that supports implicit
acquisition. We bind it to the root via the __of__ protocol:
>>> class ExplWrapper(Acquisition.Explicit):
>>> class Expl(Acquisition.Explicit):
... foo = 42
>>> y = ExplWrapper().__of__(z)
>>> y = Expl().__of__(z)
The child object is again a simple object with a simple __parent__
pointer:
Expand All @@ -1783,6 +1801,24 @@ def test_explicit_wrapper_as___parent__():
>>> y.__parent__ is z
True
Just as much as you can assign to aq_parent, you can also assign
to __parent__ to change the acquisition context of the wrapper:
>>> newroot = Root()
>>> y.__parent__ = newroot
>>> y.__parent__ is z
False
>>> y.__parent__ is newroot
True
Note that messing with the wrapper won't in any way affect the
wrapped object:
>>> Acquisition.aq_base(y).__parent__
Traceback (most recent call last):
...
AttributeError: __parent__
TODO aq_parent, aq_chain
"""

Expand Down

0 comments on commit e9c2493

Please sign in to comment.