[Zope-dev] Re: proxies

Philipp von Weitershausen philipp at weitershausen.de
Wed Jan 16 03:00:29 EST 2008


Chris Withers wrote:
> Hi All,
> 
> I saw the zope.proxies module and wondered if it might be able to help 
> me with two problems I need to solve:
> 
> - can these proxies be used to keep track of a traversal path in much 
> the same way (although no seperate containment and context chains 
> needed) as the old Zope 2 acquisition wrappers did?

We generally assign a __parent__ attribute to objects that have a 
hierarchical parent object. Objects have to explicitly allow this by 
providing ILocation (or the derived IContained). If they're not ok with 
having __parent__ assigned, we wrap them in a persistent containment 
proxy [1]. It will make the original object behave normally, except for 
the ILocation/IContained API (__parent__, __name__ attributes). It will 
intercept those values and store them inside the pickled proxy. THat way 
the object won't ever see the __parent__ attribute assigned to itself.

> - once a proxy has been created, the the object it's proxying for be 
> replaced?

I don't understand this question.

You'll have to explicitly do the wrapping and then continue to work with 
the wrapped object. Container implementations use the following kind of 
code in __setitem__ [2]:

     if not IContained.providedBy(object):
         if ILocation.providedBy(object):
             zope.interface.alsoProvides(object, IContained)
         else:
             object = ContainedProxy(object)

and then they add 'object' to their datastructure (e.g. a BTree) and 
assign object.__parent__ and object.__name__.


[1] zope.app.container.contained.ContainedProxy
[2] zope.app.container.contained.containedEvent


More information about the Zope-Dev mailing list