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

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:33:06 -0500


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

Added Files:
      Tag: NameGeddon-branch
	.cvsignore __init__.py introspection.py proxy.c proxy.h 
	setup.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/proxy/.cvsignore ===
build


=== Added File Zope3/src/zope/proxy/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/proxy/introspection.py ===
##############################################################################
#
# 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: introspection.py,v 1.1.2.1 2002/12/23 19:33:04 jim Exp $
"""
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


=== Added File Zope3/src/zope/proxy/proxy.c === (670/770 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,

[-=- -=- -=- 670 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);
}


=== Added File Zope3/src/zope/proxy/proxy.h ===
#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_ */


=== Added File Zope3/src/zope/proxy/setup.py ===
#! /usr/bin/env python
##############################################################################
#
# 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.
# 
##############################################################################

from distutils.core import setup, Extension

setup(name="Zope.Proxy", version = "0.2",
      ext_modules=[Extension("proxy", ["proxy.c"])])