[Zope3-checkins] SVN: Zope3/trunk/src/zope/proxy/ add a setProxiedObject() function, to change the object a proxy refers to

Fred L. Drake, Jr. fdrake at gmail.com
Tue Aug 8 12:33:58 EDT 2006


Log message for revision 69377:
  add a setProxiedObject() function, to change the object a proxy refers to

Changed:
  U   Zope3/trunk/src/zope/proxy/_zope_proxy_proxy.c
  U   Zope3/trunk/src/zope/proxy/interfaces.py
  U   Zope3/trunk/src/zope/proxy/tests/test_proxy.py

-=-
Modified: Zope3/trunk/src/zope/proxy/_zope_proxy_proxy.c
===================================================================
--- Zope3/trunk/src/zope/proxy/_zope_proxy_proxy.c	2006-08-08 16:31:02 UTC (rev 69376)
+++ Zope3/trunk/src/zope/proxy/_zope_proxy_proxy.c	2006-08-08 16:33:57 UTC (rev 69377)
@@ -905,6 +905,28 @@
 }
 
 static char
+setobject__doc__[] =
+"setProxiedObject(proxy, object) --> object\n"
+"\n"
+"Set the underlying object for proxy, returning the old proxied object.\n"
+"Raises TypeError if proxy is not a proxy.\n";
+
+static PyObject *
+wrapper_setobject(PyObject *unused, PyObject *args)
+{
+  PyObject *proxy;
+  PyObject *object;
+  PyObject *result = NULL;
+  if (PyArg_ParseTuple(args, "O!O:setProxiedObject",
+                       &ProxyType, &proxy, &object)) {
+    result = Proxy_GET_OBJECT(proxy);
+    Py_INCREF(object);
+    ((ProxyObject *) proxy)->proxy_object = object;
+  }
+  return result;
+}
+
+static char
 isProxy__doc__[] =
 "Check whether the given object is a proxy\n"
 "\n"
@@ -1065,6 +1087,7 @@
 static PyMethodDef
 module_functions[] = {
     {"getProxiedObject", wrapper_getobject, METH_O, getobject__doc__},
+    {"setProxiedObject", wrapper_setobject, METH_VARARGS, setobject__doc__},
     {"isProxy", wrapper_isProxy, METH_VARARGS, isProxy__doc__},
     {"sameProxiedObjects", wrapper_sameProxiedObjects, METH_VARARGS, 
      sameProxiedObjects__doc__},

Modified: Zope3/trunk/src/zope/proxy/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/proxy/interfaces.py	2006-08-08 16:31:02 UTC (rev 69376)
+++ Zope3/trunk/src/zope/proxy/interfaces.py	2006-08-08 16:33:57 UTC (rev 69377)
@@ -39,6 +39,12 @@
         If the object isn't proxied, then just return the object.
         """
 
+    def setProxiedObject(ob1, ob2):
+        """Set the underlying object for ob1 to ob2, returning the old object.
+
+        Raises TypeError if ob1 is not a proxy.
+        """
+
     def removeAllProxies(obj):
         """Get the proxied object with no proxies
 

Modified: Zope3/trunk/src/zope/proxy/tests/test_proxy.py
===================================================================
--- Zope3/trunk/src/zope/proxy/tests/test_proxy.py	2006-08-08 16:31:02 UTC (rev 69376)
+++ Zope3/trunk/src/zope/proxy/tests/test_proxy.py	2006-08-08 16:33:57 UTC (rev 69377)
@@ -614,6 +614,38 @@
     
     """
 
+def test_setProxiedObject():
+    """
+    >>> from zope.proxy import ProxyBase
+    >>> from zope.proxy import setProxiedObject, getProxiedObject
+
+    >>> class C(object):
+    ...     pass
+
+    >>> c1 = C()
+    >>> c2 = C()
+
+    >>> p = ProxyBase(c1)
+
+    `setProxiedObject()` allows us to change the object a proxy refers to,
+    returning the previous referent:
+
+    >>> old = setProxiedObject(p, c2)
+    >>> old is c1
+    True
+
+    >>> getProxiedObject(p) is c2
+    True
+
+    The first argument  to `setProxiedObject()` must be a proxy; other objects
+    cause it to raise an exception:
+
+    >>> setProxiedObject(c1, None)
+    Traceback (most recent call last):
+    TypeError: setProxiedObject() argument 1 must be zope.proxy.ProxyBase, not C
+
+    """
+
 def test_suite():
     suite = unittest.makeSuite(ProxyTestCase)
     suite.addTest(DocTestSuite())



More information about the Zope3-Checkins mailing list