zope.proxy API

zope.proxy.interfaces

Proxy-related interfaces.

interface zope.proxy.interfaces.IProxyIntrospection

Provides methods for indentifying proxies and extracting proxied objects

isProxy(obj, proxytype=None)

Check whether the given object is a proxy

If proxytype is not None, checkes whether the object is proxied by the given proxytype.

sameProxiedObjects(ob1, ob2)

Check whether ob1 and ob2 are the same or proxies of the same object

getProxiedObject(obj)

Get the proxied Object

If the object isn’t proxied, then just return the object.

setProxiedObject(ob1, ob2)

Set the underlying object for ob1 to ob2, returning the old object.

Raises TypeError if ob1 is not a proxy.

removeAllProxies(obj)

Get the proxied object with no proxies

If obj is not a proxied object, return obj.

The returned object has no proxies.

queryProxy(obj, proxytype, default=None)

Look for a proxy of the given type around the object

If no such proxy can be found, return the default.

queryInnerProxy(obj, proxytype, default=None)

Look for the inner-most proxy of the given type around the object

If no such proxy can be found, return the default.

If there is such a proxy, return the inner-most one.

zope.proxy

More convenience functions for dealing with proxies.

zope.proxy.getProxiedObject()

getProxiedObject(proxy) –> object

Get the underlying object for proxy, or the object itself, if it is not a proxy.

zope.proxy.isProxy()

Check whether the given object is a proxy

If proxytype is not None, checkes whether the object is proxied by the given proxytype.

zope.proxy.queryInnerProxy()

Look for the inner-most proxy of the given type around the object

If no such proxy can be found, return the default.

If there is such a proxy, return the inner-most one.

zope.proxy.queryProxy()

Look for a proxy of the given type around the object

If no such proxy can be found, return the default.

zope.proxy.removeAllProxies()

removeAllProxies(proxy) –> object

Get the proxied object with no proxies

If obj is not a proxied object, return obj.

The returned object has no proxies.

zope.proxy.sameProxiedObjects()

Check whether two objects are the same or proxies of the same object

zope.proxy.setProxiedObject()

setProxiedObject(proxy, object) –> object

Set the underlying object for proxy, returning the old proxied object. Raises TypeError if proxy is not a proxy.

zope.proxy.decorator

Decorator support

Decorators are proxies that are mostly transparent but that may provide additional features.

>>> from zope.interface import Interface
>>> from zope.interface import directlyProvides
>>> from zope.interface import implementer
>>> class I1(Interface):
...     pass
>>> class I2(Interface):
...     pass
>>> class I3(Interface):
...     pass
>>> class I4(Interface):
...     pass
>>> from zope.proxy.decorator import SpecificationDecoratorBase
>>> @implementer(I1)
... class D1(SpecificationDecoratorBase):
...   pass
>>> @implementer(I2)
... class D2(SpecificationDecoratorBase):
...   pass
>>> @implementer(I3)
... class X(object):
...   pass
>>> x = X()
>>> directlyProvides(x, I4)

Interfaces of X are ordered with the directly-provided interfaces first.

>>> from zope.interface import providedBy
>>> [interface.getName() for interface in list(providedBy(x))]
['I4', 'I3']

When we decorate objects, what order should the interfaces come in? One could argue that decorators are less specific, so they should come last.

>>> [interface.getName() for interface in list(providedBy(D1(x)))]
['I4', 'I3', 'I1']

>>> [interface.getName() for interface in list(providedBy(D2(D1(x))))]
['I4', 'I3', 'I1', 'I2']

SpecificationDecorators also work with old-style classes:

>>> @implementer(I3)
... class X:
...   pass

>>> x = X()
>>> directlyProvides(x, I4)

>>> [interface.getName() for interface in list(providedBy(x))]
['I4', 'I3']

>>> [interface.getName() for interface in list(providedBy(D1(x)))]
['I4', 'I3', 'I1']

>>> [interface.getName() for interface in list(providedBy(D2(D1(x))))]
['I4', 'I3', 'I1', 'I2']
class zope.proxy.decorator.DecoratorSpecificationDescriptor

Support for interface declarations on decorators

class zope.proxy.decorator.SpecificationDecoratorBase

Base class for a proxy that provides additional interfaces.