[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security/secproxy - secproxy.c:1.1.2.1 secproxy.h:1.1.2.1

Guido van Rossum guido@python.org
Wed, 17 Apr 2002 18:14:04 -0400


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

Added Files:
      Tag: SecurityProxy-branch
	secproxy.c secproxy.h 
Log Message:
Interim checkin so Barry can continue with this code.

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

#define Wrapper_Check(wrapper) \
        (((wrapper)->ob_type == &WrapperType) \
         || (PyObject_TypeCheck(wrapper, &WrapperType)))

#define Wrapper_GetObject(wrapper) \
        (((WrapperObject *)wrapper)->wrap_object)

#define Wrapper_GetChecker(wrapper) \
        (((WrapperObject *)wrapper)->wrap_checker)


staticforward PyTypeObject WrapperType;


/*
 *   Slot methods.
 */

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

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

static int
wrap_traverse(PyObject *self, visitproc visit, void *arg)
{
	int err = visit(Wrapper_GetObject(self), arg);
	if (err == 0)
		err = visit(Wrapper_GetChecker(self), arg);
	return err;
}

static PyObject *
wrap_richcompare(PyObject* self, PyObject* other, int op)
{
	while (Wrapper_Check(self)) {
		self = Wrapper_GetObject(self);
	}
	while (Wrapper_Check(other)) {
		other = Wrapper_GetObject(other);
	}
	return PyObject_RichCompare(self, other, op);
}

static PyObject *
wrap_iter(PyObject *self)
{
	return PyObject_GetIter(Wrapper_GetObject(self));
}

static void
wrap_dealloc(PyObject *self)
{
	Py_DECREF(Wrapper_GetObject(self));
	Py_DECREF(Wrapper_GetChecker(self));
	self->ob_type->tp_free(self);
}

static PyObject *
wrap_getattro(PyObject *self, PyObject *name)
{
	PyObject *result;
	PyObject *checker;
	PyObject *object;

	object = Wrapper_GetObject(self);
	checker = Wrapper_GetChecker(self);
	/* Call checker.check_getattr(object, name) */
	result = PyObject_CallMethod(checker, "check_getattr", "OO",
				     object, name);
	if (result == NULL)
		return NULL;
	Py_DECREF(result);
	result = PyObject_GetAttr(object, name);
	return result;
}

static int
wrap_setattro(PyObject *self, PyObject *name, PyObject *value)
{
        return PyObject_SetAttr(Wrapper_GetObject(self), name, value);
}

static PyObject *
wrap_str(PyObject *wrapper) {
	return PyObject_Str(Wrapper_GetObject(wrapper));
}

static PyObject *
wrap_repr(PyObject *wrapper)
{
	return PyObject_Repr(Wrapper_GetObject(wrapper));
}


static int
wrap_setattr(PyObject *wrapper, PyObject *name, PyObject *value)
{
	return PyObject_SetAttr(Wrapper_GetObject(wrapper), name, value);
}

static int
wrap_compare(PyObject *wrapper, PyObject *v)
{
	return PyObject_Compare(Wrapper_GetObject(wrapper), v);
}

static long
wrap_hash(PyObject *self)
{
	return PyObject_Hash(Wrapper_GetObject(self));
}

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

/*
 *   Number methods
 */

static int
wrap_nonzero(PyObject *self)
{
	return PyObject_IsTrue(Wrapper_GetObject(self));
}

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

static int
wrap_length(PyObject *self)
{
	return PyObject_Length(Wrapper_GetObject(self));
}

static PyObject *
wrap_slice(PyObject *self, int start, int end)
{
	return PySequence_GetSlice(Wrapper_GetObject(self), start, end);
}

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

static int
wrap_contains(PyObject *self, PyObject *value)
{
	return PySequence_Contains(Wrapper_GetObject(self), value);
}

/*
 *   Mapping methods
 */

static PyObject *
wrap_getitem(PyObject *wrapper, PyObject *v) {
	return PyObject_GetItem(Wrapper_GetObject(wrapper), v);
}

static int
wrap_setitem(PyObject *self, PyObject *key, PyObject *value)
{
	return PyObject_SetItem(Wrapper_GetObject(self), key, value);
}

/*
 *   Normal methods
 */

static PyNumberMethods
wrap_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 */
	wrap_nonzero, /* nb_nonzero */
};

static PySequenceMethods
wrap_as_sequence = {
	wrap_length,				/* sq_length */
	0,					/* sq_concat */
	0,					/* sq_repeat */
	0,					/* sq_item */
	wrap_slice,				/* sq_slice */
	0,					/* sq_ass_item */
	wrap_ass_slice,				/* sq_ass_slice */
	wrap_contains,				/* sq_contains */
};

static PyMappingMethods
wrap_as_mapping = {
	wrap_length,				/* mp_length */
	wrap_getitem,				/* mp_subscript */
	wrap_setitem,				/* mp_ass_subscript */
};

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

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

statichere PyTypeObject
WrapperType = {
	PyObject_HEAD_INIT(NULL)
	0,
	"secproxy.secproxy",
	sizeof(WrapperObject),
	0,
	wrap_dealloc,				/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	wrap_compare,				/* tp_compare */
	wrap_repr,				/* tp_repr */
	&wrap_as_number,			/* tp_as_number */
	&wrap_as_sequence,			/* tp_as_sequence */
	&wrap_as_mapping,			/* tp_as_mapping */
	wrap_hash,				/* tp_hash */
	wrap_call,				/* tp_call */
	wrap_str,				/* tp_str */
	wrap_getattro,				/* tp_getattro */
	wrap_setattro,				/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
	0,					/* tp_doc */
	wrap_traverse,				/* tp_traverse */
	0,					/* tp_clear */
	wrap_richcompare,			/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	wrap_iter,				/* tp_iter */
	0,					/* tp_iternext */
	wrap_methods,				/* 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 */
	wrap_new,				/* tp_new */
	_PyObject_GC_Del,			/* tp_free */
};

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

static char
module___doc__[] =
"XXX";

void
initsecproxy(void)
{
	PyObject *m = Py_InitModule3("secproxy",
				     module_functions,
				     module___doc__);

	if (m == NULL)
		return;

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

	Py_INCREF(&WrapperType);
	PyModule_AddObject(m, "secproxy", (PyObject *)&WrapperType);
}


=== Added File Zope3/lib/python/Zope/Security/secproxy/secproxy.h ===
/*
 * Based on code by Fred Drake.
 */

#ifndef _wrapper_H_
#define _wrapper_H_

typedef struct {
	PyObject_HEAD
	PyObject *wrap_object;
	PyObject *wrap_checker;
} WrapperObject;

#endif