[Zope3-checkins] CVS: Zope3/src/zope/proxy - .cvsignore:1.2 __init__.py:1.2 introspection.py:1.2 proxy.c:1.2 proxy.h:1.2

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:15:46 -0500


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

Added Files:
	.cvsignore __init__.py introspection.py proxy.c proxy.h 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/proxy/.cvsignore 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:15:46 2002
+++ Zope3/src/zope/proxy/.cvsignore	Wed Dec 25 09:15:16 2002
@@ -0,0 +1 @@
+build


=== Zope3/src/zope/proxy/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:15:46 2002
+++ Zope3/src/zope/proxy/__init__.py	Wed Dec 25 09:15:16 2002
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/proxy/introspection.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:15:46 2002
+++ Zope3/src/zope/proxy/introspection.py	Wed Dec 25 09:15:16 2002
@@ -0,0 +1,110 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+# XXX this module should bnecome unnecessary
+
+"""Temporary hack module until there is a generic way to deal with proxies
+
+This module provides some standard machinery to recognize and remove
+proxies. It is hoped that it will be replaced by a cleaner
+implementation based on a common proxy base class.
+
+This module requires that proxy implementations register themselvss
+with the module, by calling defineProxyType, however, it
+short-circuits the definitions for two types, which, hopefully will be
+the only two types that need to get registered. ;)
+
+$Id$
+"""
+from zope.proxy.interfaces import IProxyIntrospection
+
+__implements__ = IProxyIntrospection
+
+
+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.proxy.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


=== Zope3/src/zope/proxy/proxy.c 1.1 => 1.2 === (673/773 lines abridged)
--- /dev/null	Wed Dec 25 09:15:46 2002
+++ Zope3/src/zope/proxy/proxy.c	Wed Dec 25 09:15:16 2002
@@ -0,0 +1,770 @@
+#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)) {

[-=- -=- -=- 673 lines omitted -=- -=- -=-]

+    }
+    else
+        PyErr_Format(PyExc_TypeError,
+		     "expected proxy, got %s", obj->ob_type->tp_name);
+    return result;
+}
+
+static PyMethodDef
+module_functions[] = {
+    {"getobject", wrapper_getobject, METH_O, getobject__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
+initproxy(void)
+{
+    PyObject *m = Py_InitModule3("proxy", module_functions, module___doc__);
+
+    if (m == NULL)
+        return;
+
+    if (empty_tuple == NULL)
+        empty_tuple = PyTuple_New(0);
+
+    ProxyType.ob_type = &PyType_Type;
+    ProxyType.tp_alloc = PyType_GenericAlloc;
+    ProxyType.tp_free = _PyObject_GC_Del;
+    if (PyType_Ready(&ProxyType) < 0)
+        return;
+
+    Py_INCREF(&ProxyType);
+    PyModule_AddObject(m, "proxy", (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/proxy.h 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:15:46 2002
+++ Zope3/src/zope/proxy/proxy.h	Wed Dec 25 09:15:16 2002
@@ -0,0 +1,54 @@
+#ifndef _proxy_H_
+#define _proxy_H_ 1
+
+typedef struct {
+    PyObject_HEAD
+    PyObject *proxy_object;
+} ProxyObject;
+
+#define Proxy_GET_OBJECT(ob)   (((ProxyObject *)(ob))->proxy_object)
+
+typedef struct {
+    PyTypeObject *proxytype;
+    int (*check)(PyObject *obj);
+    PyObject *(*create)(PyObject *obj);
+    PyObject *(*getobject)(PyObject *proxy);
+} ProxyInterface;
+
+
+#ifndef PROXY_MODULE
+
+/* These are only defined in the public interface, and are not
+ * available within the module implementation.  There we use the
+ * classic Python/C API only.
+ */
+
+static ProxyInterface *_proxy_api = NULL;
+
+static int
+Proxy_Import(void)
+{
+    if (_proxy_api == NULL) {
+        PyObject *m = PyImport_ImportModule("zope.proxy.proxy");
+        if (m != NULL) {
+            PyObject *tmp = PyObject_GetAttrString(m, "_CAPI");
+            if (tmp != NULL) {
+                if (PyCObject_Check(tmp))
+                    _proxy_api = (ProxyInterface *)
+                        PyCObject_AsVoidPtr(tmp);
+                Py_DECREF(tmp);
+            }
+        }
+    }
+    return (_proxy_api == NULL) ? -1 : 0;
+}
+
+#define ProxyType               (_proxy_api->proxytype)
+#define Proxy_Check(obj)        (_proxy_api->check((obj)))
+#define Proxy_CheckExact(obj)   ((obj)->ob_type == ProxyType)
+#define Proxy_New(obj)          (_proxy_api->create((obj)))
+#define Proxy_GetObject(proxy)  (_proxy_api->getobject((proxy)))
+
+#endif /* PROXY_MODULE */
+
+#endif /* _proxy_H_ */