[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security - _Proxy.c:1.1.2.1 setup.py:1.1.2.1

Guido van Rossum guido@python.org
Thu, 18 Apr 2002 11:20:52 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Security
In directory cvs.zope.org:/tmp/cvs-serv21683

Added Files:
      Tag: SecurityProxy-branch
	_Proxy.c setup.py 
Log Message:
Got rid of secproxy subpackage, the C code is now a module _Proxy.c.

=== Added File Zope3/lib/python/Zope/Security/_Proxy.c ===
#include <Python.h>

typedef struct {
	PyObject_HEAD
	PyObject *proxy_object;
	PyObject *proxy_checker;
} ProxyObject;

#define Proxy_Check(proxy) \
        (((proxy)->ob_type == &ProxyType) \
         || (PyObject_TypeCheck(proxy, &ProxyType)))

#define Proxy_GetObject(proxy) \
        (((ProxyObject *)proxy)->proxy_object)

#define Proxy_GetChecker(proxy) \
        (((ProxyObject *)proxy)->proxy_checker)


staticforward PyTypeObject ProxyType;


/*
 *   Slot methods.
 */

static PyObject *
proxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = {"object", "checker", 0};
	ProxyObject *self;
	PyObject *object;
	PyObject *checker;

	if (!PyArg_ParseTupleAndKeywords(args, kwds,
					 "OO:secproxy.__new__", kwlist,
					 &object, &checker))
		return NULL;
	self = (ProxyObject *)type->tp_alloc(type, 0);
	if (self == NULL)
		return NULL;
	Py_INCREF(object);
	Py_INCREF(checker);
	self->proxy_object = object;
	self->proxy_checker = checker;
	return (PyObject *)self;
}

static int
proxy_traverse(PyObject *self, visitproc visit, void *arg)
{
	int err = visit(Proxy_GetObject(self), arg);
	if (err == 0)
		err = visit(Proxy_GetChecker(self), arg);
	return err;
}

static PyObject *
proxy_richcompare(PyObject* self, PyObject* other, int op)
{
	while (Proxy_Check(self)) {
		self = Proxy_GetObject(self);
	}
	while (Proxy_Check(other)) {
		other = Proxy_GetObject(other);
	}
	return PyObject_RichCompare(self, other, op);
}

static PyObject *
proxy_iter(PyObject *self)
{
	return PyObject_GetIter(Proxy_GetObject(self));
}

static void
proxy_dealloc(PyObject *self)
{
	Py_DECREF(Proxy_GetObject(self));
	Py_DECREF(Proxy_GetChecker(self));
	self->ob_type->tp_free(self);
}

static PyObject *
proxy_getattro(PyObject *self, PyObject *name)
{
	PyObject *object, *checker, *checked, *value;
	PyObject *result = NULL;

	object = Proxy_GetObject(self);
	checker = Proxy_GetChecker(self);
	/*
	 * checked = checker.check_getattr(object, name)
	 * value = getattr(object, name)
	 * return checker.checkValue(value, checked)
	 */
	checked = PyObject_CallMethod(
		checker, "check_getattr", "(OO)", object, name);
	if (checked != NULL) {
		value = PyObject_GetAttr(object, name);
		if (value != NULL) {
			result = PyObject_CallMethod(
				checker, "proxy", "(OO)", value, checked);
			Py_DECREF(value);
		}
		Py_DECREF(checked);
	}
	return result;
}

static int
proxy_setattro(PyObject *self, PyObject *name, PyObject *value)
{
        return PyObject_SetAttr(Proxy_GetObject(self), name, value);
}

static PyObject *
proxy_str(PyObject *proxy) {
	return PyObject_Str(Proxy_GetObject(proxy));
}

static PyObject *
proxy_repr(PyObject *proxy)
{
	return PyObject_Repr(Proxy_GetObject(proxy));
}


static int
proxy_setattr(PyObject *proxy, PyObject *name, PyObject *value)
{
	return PyObject_SetAttr(Proxy_GetObject(proxy), name, value);
}

static int
proxy_compare(PyObject *proxy, PyObject *v)
{
	return PyObject_Compare(Proxy_GetObject(proxy), v);
}

static long
proxy_hash(PyObject *self)
{
	return PyObject_Hash(Proxy_GetObject(self));
}

static PyObject *
proxy_call(PyObject *self, PyObject *args, PyObject *kw)
{
	if (kw)
		return PyEval_CallObjectWithKeywords(Proxy_GetObject(self),
						     args, kw);
	else
		return PyObject_CallObject(Proxy_GetObject(self), args);
}

/*
 *   Number methods
 *   XXX need more :-)
 */

static int
proxy_nonzero(PyObject *self)
{
	return PyObject_IsTrue(Proxy_GetObject(self));
}

/*
 *   Sequence methods
 *   (If you have mapping getitem/setitem, sequence getitem/setitem
 *   never get called.)
 */

static int
proxy_length(PyObject *self)
{
	return PyObject_Length(Proxy_GetObject(self));
}

static PyObject *
proxy_slice(PyObject *self, int start, int end)
{
	return PySequence_GetSlice(Proxy_GetObject(self), start, end);
}

static int
proxy_ass_slice(PyObject *self, int i, int j, PyObject *value)
{
	return PySequence_SetSlice(Proxy_GetObject(self), i, j, value);
}

static int
proxy_contains(PyObject *self, PyObject *value)
{
	return PySequence_Contains(Proxy_GetObject(self), value);
}

/*
 *   Mapping methods
 */

static PyObject *
proxy_getitem(PyObject *proxy, PyObject *v) {
	return PyObject_GetItem(Proxy_GetObject(proxy), v);
}

static int
proxy_setitem(PyObject *self, PyObject *key, PyObject *value)
{
	return PyObject_SetItem(Proxy_GetObject(self), key, value);
}

/*
 *   Normal methods
 */

static PyNumberMethods
proxy_as_number = {
	0, /* nb_add */
	0, /* nb_subtract */
	0, /* nb_multiply */
	0, /* nb_divide */
	0, /* nb_remainder */
	0, /* nb_divmod */
	0, /* nb_power */
	0, /* nb_negative */
	0, /* nb_positive */
	0, /* nb_absolute */
	proxy_nonzero, /* nb_nonzero */
};

static PySequenceMethods
proxy_as_sequence = {
	proxy_length,				/* sq_length */
	0,					/* sq_concat */
	0,					/* sq_repeat */
	0,					/* sq_item */
	proxy_slice,				/* sq_slice */
	0,					/* sq_ass_item */
	proxy_ass_slice,				/* sq_ass_slice */
	proxy_contains,				/* sq_contains */
};

static PyMappingMethods
proxy_as_mapping = {
	proxy_length,				/* mp_length */
	proxy_getitem,				/* mp_subscript */
	proxy_setitem,				/* mp_ass_subscript */
};

/*
 * XXX Numeric methods are not yet supported.
 */

statichere PyTypeObject
ProxyType = {
	PyObject_HEAD_INIT(NULL)
	0,
	"Zope.Security._Proxy._Proxy",
	sizeof(ProxyObject),
	0,
	proxy_dealloc,				/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	proxy_compare,				/* tp_compare */
	proxy_repr,				/* tp_repr */
	&proxy_as_number,			/* tp_as_number */
	&proxy_as_sequence,			/* tp_as_sequence */
	&proxy_as_mapping,			/* tp_as_mapping */
	proxy_hash,				/* tp_hash */
	proxy_call,				/* tp_call */
	proxy_str,				/* tp_str */
	proxy_getattro,				/* tp_getattro */
	proxy_setattro,				/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
		Py_TPFLAGS_HAVE_GC,		/* tp_flags */
	0,					/* tp_doc */
	proxy_traverse,				/* tp_traverse */
	0,					/* tp_clear */
	proxy_richcompare,			/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	proxy_iter,				/* tp_iter */
	0,					/* tp_iternext */
	0,					/* tp_methods */
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	0,					/* tp_init */
	PyType_GenericAlloc,			/* tp_alloc */
	proxy_new,				/* tp_new */
	_PyObject_GC_Del,			/* tp_free */
};

static PyMethodDef
module_functions[] = {
	{NULL}
};

static char
module___doc__[] = "XXX";

void
init_Proxy(void)
{
	PyObject *m;

	ProxyType.ob_type = &PyType_Type;
	if (PyType_Ready(&ProxyType) < 0)
		return;

	m = Py_InitModule3("_Proxy", module_functions, module___doc__);
	if (m == NULL)
		return;

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


=== Added File Zope3/lib/python/Zope/Security/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="_Proxy", version = "0.1",
      ext_modules=[Extension("_Proxy", ["_Proxy.c"])])