[Zope3-checkins] CVS: Zope3/src/zope/proxy - _zope_proxy_proxy.c:1.1 __init__.py:1.6 interfaces.py:1.3 introspection.py:1.6 proxy.h:1.3 proxy.c:NONE

Jim Fulton jim@zope.com
Wed, 28 May 2003 11:49:42 -0400


Update of /cvs-repository/Zope3/src/zope/proxy
In directory cvs.zope.org:/tmp/cvs-serv12301/src/zope/proxy

Modified Files:
	__init__.py interfaces.py introspection.py proxy.h 
Added Files:
	_zope_proxy_proxy.c 
Removed Files:
	proxy.c 
Log Message:
Refactored to take advantage of the fact that all
proxies subclass a common base class, which is now
zope.proxy.ProxyBase.

Moved most of the proxy introspection functionality into the renamed
extension module _zope_proxy_proxy.c, and expose it in zope.proxy.


=== Added File Zope3/src/zope/proxy/_zope_proxy_proxy.c === (801/901 lines abridged)
#include "Python.h"
#include "modsupport.h"

#define PROXY_MODULE
#include "zope/proxy/proxy.h"

static PyTypeObject ProxyType;

#define Proxy_Check(wrapper)   (PyObject_TypeCheck((wrapper), &ProxyType))

static PyObject *
empty_tuple = NULL;


/*
 *   Slot methods.
 */

static PyObject *
wrap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *result = NULL;
    PyObject *object;

    if (PyArg_UnpackTuple(args, "__new__", 1, 1, &object)) {
        if (kwds != NULL && PyDict_Size(kwds) != 0) {
            PyErr_SetString(PyExc_TypeError,
                            "proxy.__new__ does not accept keyword args");
            return NULL;
        }
        result = PyType_GenericNew(type, args, kwds);
        if (result != NULL) {
            ProxyObject *wrapper = (ProxyObject *) result;
            Py_INCREF(object);
            wrapper->proxy_object = object;
        }
    }
    return result;
}

static int
wrap_init(PyObject *self, PyObject *args, PyObject *kwds)
{
    int result = -1;
    PyObject *object;

    if (PyArg_UnpackTuple(args, "__init__", 1, 1, &object)) {
        ProxyObject *wrapper = (ProxyObject *)self;
        if (kwds != NULL && PyDict_Size(kwds) != 0) {
            PyErr_SetString(PyExc_TypeError,

[-=- -=- -=- 801 lines omitted -=- -=- -=-]


static PyMethodDef
module_functions[] = {
    {"getObject", wrapper_getobject, METH_O, getobject__doc__},
    {"isProxy", wrapper_isProxy, METH_VARARGS, isProxy__doc__},
    {"queryProxy", wrapper_queryProxy, METH_VARARGS, queryProxy__doc__},
    {"queryInnerProxy", wrapper_queryInnerProxy, METH_VARARGS, 
     queryInnerProxy__doc__},
    {"removeAllProxies", wrapper_removeAllProxies, METH_O, 
     removeAllProxies__doc__},
    {NULL}
};

static char
module___doc__[] =
"Association between an object, a context object, and a dictionary.\n\
\n\
The context object and dictionary give additional context information\n\
associated with a reference to the basic object.  The wrapper objects\n\
act as proxies for the original object.";


void
init_zope_proxy_proxy(void)
{
    PyObject *m = Py_InitModule3("_zope_proxy_proxy", 
                                 module_functions, module___doc__);

    if (m == NULL)
        return;

    if (empty_tuple == NULL)
        empty_tuple = PyTuple_New(0);

    ProxyType.tp_free = _PyObject_GC_Del;

    if (PyType_Ready(&ProxyType) < 0)
        return;

    Py_INCREF(&ProxyType);
    PyModule_AddObject(m, "ProxyBase", (PyObject *)&ProxyType);

    if (api_object == NULL) {
        api_object = PyCObject_FromVoidPtr(&wrapper_capi, NULL);
        if (api_object == NULL)
            return;
    }
    Py_INCREF(api_object);
    PyModule_AddObject(m, "_CAPI", api_object);
}


=== Zope3/src/zope/proxy/__init__.py 1.5 => 1.6 ===
--- Zope3/src/zope/proxy/__init__.py:1.5	Sat Apr 19 06:34:38 2003
+++ Zope3/src/zope/proxy/__init__.py	Wed May 28 11:49:11 2003
@@ -15,8 +15,16 @@
 
 $Id$
 """
+
+from zope.interface import moduleProvides
+from zope.proxy.interfaces import IProxyIntrospection
 from types import ClassType
-from zope.proxy.introspection import removeAllProxies
+from zope.proxy._zope_proxy_proxy import *
+from zope.proxy._zope_proxy_proxy import _CAPI
+
+moduleProvides(IProxyIntrospection)
+__all__ = tuple(IProxyIntrospection)
+
 
 
 def proxy_compatible_isinstance(obj, cls):


=== Zope3/src/zope/proxy/interfaces.py 1.2 => 1.3 ===
--- Zope3/src/zope/proxy/interfaces.py:1.2	Wed Dec 25 09:15:17 2002
+++ Zope3/src/zope/proxy/interfaces.py	Wed May 28 11:49:11 2003
@@ -23,13 +23,17 @@
     """Provides methods for indentifying proxies and extracting proxied objects
     """
 
-    def removeProxy(obj):
-        """Return the immediately proxied object.
+    def isProxy(obj, proxytype=None):
+        """Check whether the given object is a proxy
 
-        If obj is not a proxied object, return obj.
+        If proxytype is not None, checkes whether the object is
+        proxied by the given proxytype.
+        """
 
-        Note that the object returned may still be a proxy, if there
-        are multiple layers of proxy.
+    def getObject(obj):
+        """Get the proxied Object
+
+        If the object isn't proxied, then just return the object.
         """
 
     def removeAllProxies(obj):
@@ -40,6 +44,16 @@
         The returned object has no proxies.
         """
 
-    def isProxy(obj):
-        """Checks whether the given object is a proxy
+    def 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.
+        """
+
+    def 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.
         """


=== Zope3/src/zope/proxy/introspection.py 1.5 => 1.6 ===
--- Zope3/src/zope/proxy/introspection.py:1.5	Tue May 27 10:18:30 2003
+++ Zope3/src/zope/proxy/introspection.py	Wed May 28 11:49:11 2003
@@ -27,85 +27,10 @@
 
 $Id$
 """
-from zope.interface import moduleProvides
-from zope.proxy.interfaces import IProxyIntrospection
 
-moduleProvides(IProxyIntrospection)
+import warnings
+from zope.proxy._zope_proxy_proxy import removeAllProxies, isProxy
 
-
-from zope.exceptions import DuplicationError
-
-class ProxyRegistry:
-
-    def __init__(self):
-        self._proxy_types = {}
-
-        # register security proxy
-        from zope.security.proxy import Proxy, getObject
-        self._proxy_types[Proxy] = getObject
-
-        # register context wrappers
-        from zope.context import wrapperTypes, getobject
-        for wrapper_type in wrapperTypes:
-            self._proxy_types[wrapper_type] = getobject
-
-    _clear = __init__
-
-    def defineProxyType(self, type_, remover):
-        """Register a proxy type
-
-        A type and a function are provides. The function should take a
-        proxy and return the object proxied.
-        """
-        if type_ in self._proxy_types:
-            raise DuplicationError(type_)
-
-        self._proxy_types[type_] = remover
-
-    def removeProxy(self, obj):
-        """Return the immediately proxied object.
-
-        If obj is not a proxied object, return obj.
-
-        Note that the object returned may still be a proxy, if there
-        are multiple layers of proxy.
-
-        """
-        remover = self._proxy_types.get(type(obj))
-        if remover is None:
-            return obj
-
-        return remover(obj)
-
-
-    def removeAllProxies(self, obj):
-        """Get the proxied oject with no proxies
-
-        If obj is not a proxied object, return obj.
-
-        The returned object has no proxies.
-        """
-
-        i=0
-        get = self._proxy_types.get
-        while i < 100:
-            remover = get(type(obj))
-            if remover is None:
-                return obj
-
-            obj = remover(obj)
-            i=i+1
-
-        raise TypeError('excessive proxy nesting')
-
-    def isProxy(self, obj):
-        """Check whether the given object is a proxy
-        """
-        return type(obj) in self._proxy_types
-
-theProxyRegistry = ProxyRegistry()
-
-isProxy = theProxyRegistry.isProxy
-removeProxy = theProxyRegistry.removeProxy
-removeAllProxies = theProxyRegistry.removeAllProxies
-_clear = theProxyRegistry._clear
+warnings.warn("The zope.proxy.introspection module is deprecated. "
+              "Use zope.proxy instead.",
+              DeprecationWarning, 2)


=== Zope3/src/zope/proxy/proxy.h 1.2 => 1.3 ===
--- Zope3/src/zope/proxy/proxy.h:1.2	Wed Dec 25 09:15:16 2002
+++ Zope3/src/zope/proxy/proxy.h	Wed May 28 11:49:11 2003
@@ -29,7 +29,7 @@
 Proxy_Import(void)
 {
     if (_proxy_api == NULL) {
-        PyObject *m = PyImport_ImportModule("zope.proxy.proxy");
+        PyObject *m = PyImport_ImportModule("zope.proxy");
         if (m != NULL) {
             PyObject *tmp = PyObject_GetAttrString(m, "_CAPI");
             if (tmp != NULL) {

=== Removed File Zope3/src/zope/proxy/proxy.c ===