Commit 4222a21a authored by matt@zope.com's avatar matt@zope.com

Added Acquisition C API

parent d158105f
......@@ -33,7 +33,7 @@
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
$Id: Acquisition.c,v 1.51 2001/03/28 14:06:50 jeremy Exp $
$Id: Acquisition.c,v 1.52 2001/07/03 19:38:20 matt Exp $
If you have questions regarding this software,
contact:
......@@ -46,6 +46,11 @@
*/
#include "ExtensionClass.h"
#define _IN_ACQUISITION_C
#include "Acquisition.h"
static ACQUISITIONCAPI AcquisitionCAPI;
static void
PyVar_Assign(PyObject **v, PyObject *e)
{
......@@ -1210,21 +1215,12 @@ static struct PyMethodDef Xaq_methods[] = {
};
static PyObject *
module_aq_acquire(PyObject *ignored, PyObject *args, PyObject *kw)
capi_aq_acquire(PyObject *self, PyObject *name, PyObject *filter,
PyObject *extra, int explicit, PyObject *defalt, int containment)
{
PyObject *self;
PyObject *name, *filter=0, *extra=Py_None;
PyObject *expl=0, *defalt=0;
int explicit=1, containment=0;
UNLESS (PyArg_ParseTupleAndKeywords(
args, kw, "OO|OOOOi", acquire_args,
&self, &name, &filter, &extra, &explicit, &defalt, &containment
))
return NULL;
if (expl) explicit=PyObject_IsTrue(expl);
PyObject *result;
if (filter==Py_None) filter=0;
/* We got a wrapped object, so business as usual */
......@@ -1242,47 +1238,78 @@ module_aq_acquire(PyObject *ignored, PyObject *args, PyObject *kw)
UNLESS (self=newWrapper(self, NULL, (PyTypeObject*)&Wrappertype))
return NULL;
ignored=Wrapper_findattr(WRAPPER(self), name, filter, extra, OBJECT(self),
result=Wrapper_findattr(WRAPPER(self), name, filter, extra, OBJECT(self),
1, 1, explicit, containment);
/* get rid of temp wrapper */
Py_DECREF(self);
return ignored;
return result;
}
static PyObject *
module_aq_get(PyObject *r, PyObject *args)
module_aq_acquire(PyObject *ignored, PyObject *args, PyObject *kw)
{
PyObject *self, *name, *defalt=0;
int containment=0;
UNLESS (PyArg_ParseTuple(args, "OO|Oi",
&self, &name, &defalt, &containment
)) return NULL;
PyObject *self;
PyObject *name, *filter=0, *extra=Py_None;
PyObject *expl=0, *defalt=0;
int explicit=1, containment=0;
UNLESS (PyArg_ParseTupleAndKeywords(
args, kw, "OO|OOOOi", acquire_args,
&self, &name, &filter, &extra, &expl, &defalt, &containment
))
return NULL;
if (expl) explicit=PyObject_IsTrue(expl);
return capi_aq_acquire(self, name, filter, extra, explicit, defalt,
containment);
}
static PyObject *
capi_aq_get(PyObject *self, PyObject *name, PyObject *defalt, int containment)
{
PyObject *result = NULL;
/* We got a wrapped object, so business as usual */
if (isWrapper(self))
r=Wrapper_findattr(WRAPPER(self), name, 0, 0, OBJECT(self), 1, 1, 1,
result=Wrapper_findattr(WRAPPER(self), name, 0, 0, OBJECT(self), 1, 1, 1,
containment);
else
r=PyObject_GetAttr(self, name);
result=PyObject_GetAttr(self, name);
if (! r && defalt)
if (! result && defalt)
{
PyErr_Clear();
r=defalt;
Py_INCREF(r);
result=defalt;
Py_INCREF(result);
}
return r;
return result;
}
static PyObject *
module_aq_base(PyObject *ignored, PyObject *args)
module_aq_get(PyObject *r, PyObject *args)
{
PyObject *self, *r;
UNLESS (PyArg_ParseTuple(args, "O", &self)) return NULL;
PyObject *self, *name, *defalt=0;
int containment=0;
UNLESS (PyArg_ParseTuple(args, "OO|Oi",
&self, &name, &defalt, &containment
)) return NULL;
return capi_aq_get(self, name, defalt, containment);
}
static int
capi_aq_iswrapper(PyObject *self) {
return isWrapper(self);
}
static PyObject *
capi_aq_base(PyObject *self)
{
PyObject *result;
if (! isWrapper(self))
{
Py_INCREF(self);
......@@ -1291,50 +1318,75 @@ module_aq_base(PyObject *ignored, PyObject *args)
if (WRAPPER(self)->obj)
{
r=WRAPPER(self)->obj;
while (isWrapper(r) && WRAPPER(r)->obj) r=WRAPPER(r)->obj;
result=WRAPPER(self)->obj;
while (isWrapper(result) && WRAPPER(result)->obj)
result=WRAPPER(result)->obj;
}
else r=Py_None;
Py_INCREF(r);
return r;
else result=Py_None;
Py_INCREF(result);
return result;
}
static PyObject *
module_aq_parent(PyObject *ignored, PyObject *args)
module_aq_base(PyObject *ignored, PyObject *args)
{
PyObject *self, *r=Py_None;
PyObject *self;
UNLESS (PyArg_ParseTuple(args, "O", &self)) return NULL;
if (isWrapper(self) && WRAPPER(self)->container) r=WRAPPER(self)->container;
return capi_aq_base(self);
}
Py_INCREF(r);
return r;
static PyObject *
capi_aq_parent(PyObject *self)
{
PyObject *result=Py_None;
if (isWrapper(self) && WRAPPER(self)->container)
result=WRAPPER(self)->container;
Py_INCREF(result);
return result;
}
static PyObject *
module_aq_self(PyObject *ignored, PyObject *args)
module_aq_parent(PyObject *ignored, PyObject *args)
{
PyObject *self, *r;
PyObject *self;
UNLESS (PyArg_ParseTuple(args, "O", &self)) return NULL;
return capi_aq_parent(self);
}
static PyObject *
capi_aq_self(PyObject *self)
{
PyObject *result;
if (! isWrapper(self))
{
Py_INCREF(self);
return self;
}
if (WRAPPER(self)->obj) r=WRAPPER(self)->obj;
else r=Py_None;
if (WRAPPER(self)->obj) result=WRAPPER(self)->obj;
else result=Py_None;
Py_INCREF(r);
return r;
Py_INCREF(result);
return result;
}
static PyObject *
module_aq_inner(PyObject *ignored, PyObject *args)
module_aq_self(PyObject *ignored, PyObject *args)
{
PyObject *self, *r;
PyObject *self;
UNLESS (PyArg_ParseTuple(args, "O", &self)) return NULL;
return capi_aq_self(self);
}
static PyObject *
capi_aq_inner(PyObject *self)
{
PyObject *result;
if (! isWrapper(self))
{
Py_INCREF(self);
......@@ -1343,30 +1395,35 @@ module_aq_inner(PyObject *ignored, PyObject *args)
if (WRAPPER(self)->obj)
{
r=WRAPPER(self)->obj;
while (isWrapper(r) && WRAPPER(r)->obj)
result=WRAPPER(self)->obj;
while (isWrapper(result) && WRAPPER(result)->obj)
{
self=r;
r=WRAPPER(r)->obj;
self=result;
result=WRAPPER(result)->obj;
}
r=self;
result=self;
}
else r=Py_None;
else result=Py_None;
Py_INCREF(r);
return r;
Py_INCREF(result);
return result;
}
static PyObject *
module_aq_chain(PyObject *ignored, PyObject *args)
module_aq_inner(PyObject *ignored, PyObject *args)
{
PyObject *self, *r;
int containment=0;
PyObject *self;
UNLESS (PyArg_ParseTuple(args, "O|i", &self, &containment))
return NULL;
UNLESS (PyArg_ParseTuple(args, "O", &self)) return NULL;
return capi_aq_inner(self);
}
UNLESS (r=PyList_New(0)) return NULL;
static PyObject *
capi_aq_chain(PyObject *self, int containment)
{
PyObject *result;
UNLESS (result=PyList_New(0)) return NULL;
while (1)
{
......@@ -1377,7 +1434,7 @@ module_aq_chain(PyObject *ignored, PyObject *args)
if (containment)
while (WRAPPER(self)->obj && isWrapper(WRAPPER(self)->obj))
self=WRAPPER(self)->obj;
if (PyList_Append(r,OBJECT(self)) < 0)
if (PyList_Append(result,OBJECT(self)) < 0)
goto err;
}
if (WRAPPER(self)->container)
......@@ -1387,16 +1444,28 @@ module_aq_chain(PyObject *ignored, PyObject *args)
}
}
else
if (PyList_Append(r, self) < 0)
if (PyList_Append(result, self) < 0)
goto err;
break;
}
return r;
return result;
err:
Py_DECREF(r);
return r;
Py_DECREF(result);
return result;
}
static PyObject *
module_aq_chain(PyObject *ignored, PyObject *args)
{
PyObject *self;
int containment=0;
UNLESS (PyArg_ParseTuple(args, "O|i", &self, &containment))
return NULL;
return capi_aq_chain(self, containment);
}
static struct PyMethodDef methods[] = {
......@@ -1427,7 +1496,8 @@ void
initAcquisition(void)
{
PyObject *m, *d;
char *rev="$Revision: 1.51 $";
PyObject *api;
char *rev="$Revision: 1.52 $";
PURE_MIXIN_CLASS(Acquirer,
"Base class for objects that implicitly"
" acquire attributes from containers\n"
......@@ -1446,7 +1516,7 @@ initAcquisition(void)
/* Create the module and add the functions */
m = Py_InitModule4("Acquisition", methods,
"Provide base classes for acquiring objects\n\n"
"$Id: Acquisition.c,v 1.51 2001/03/28 14:06:50 jeremy Exp $\n",
"$Id: Acquisition.c,v 1.52 2001/07/03 19:38:20 matt Exp $\n",
OBJECT(NULL),PYTHON_API_VERSION);
d = PyModule_GetDict(m);
......@@ -1464,5 +1534,18 @@ initAcquisition(void)
PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
PyDict_SetItemString(d,"Acquired",Acquired);
AcquisitionCAPI.AQ_Acquire = capi_aq_acquire;
AcquisitionCAPI.AQ_Get = capi_aq_get;
AcquisitionCAPI.AQ_IsWrapper = capi_aq_iswrapper;
AcquisitionCAPI.AQ_Base = capi_aq_base;
AcquisitionCAPI.AQ_Parent = capi_aq_parent;
AcquisitionCAPI.AQ_Self = capi_aq_self;
AcquisitionCAPI.AQ_Inner = capi_aq_inner;
AcquisitionCAPI.AQ_Chain = capi_aq_chain;
api = PyCObject_FromVoidPtr(&AcquisitionCAPI, NULL);
PyDict_SetItemString(d, "AcquisitionCAPI", api);
Py_DECREF(api);
CHECK_FOR_ERRORS("can't initialize module Acquisition");
}
/*
Copyright (c) 1996-2001, Digital Creations, Fredericksburg, VA, USA.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions, and the disclaimer that follows.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
o Neither the name of Digital Creations nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
$Id: Acquisition.h,v 1.1 2001/07/03 19:38:20 matt Exp $
If you have questions regarding this software,
contact:
Digital Creations L.C.
info@digicool.com
(540) 371-6909
*/
#ifndef __ACQUISITION_H_
#define __ACQUISITION_H_
typedef struct {
PyObject *(*AQ_Acquire) (PyObject *obj, PyObject *name, PyObject *filter,
PyObject *extra, int explicit, PyObject *deflt,
int containment);
PyObject *(*AQ_Get) (PyObject *obj, PyObject *name, PyObject *deflt,
int containment);
int (*AQ_IsWrapper) (PyObject *obj);
PyObject *(*AQ_Base) (PyObject *obj);
PyObject *(*AQ_Parent) (PyObject *obj);
PyObject *(*AQ_Self) (PyObject *obj);
PyObject *(*AQ_Inner) (PyObject *obj);
PyObject *(*AQ_Chain) (PyObject *obj, int containment);
} ACQUISITIONCAPI;
#ifndef _IN_ACQUISITION_C
#define aq_Acquire(obj, name, filter, extra, explicit, deflt, containment ) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Acquire(obj, name, filter, extra, explicit, deflt, containment)))
#define aq_acquire(obj, name) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Acquire(obj, name, NULL, NULL, 1, NULL, 0)))
#define aq_get(obj, name, deflt, containment) (AcquistionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Get(obj, name, deflt, containment)))
#define aq_isWrapper(obj) (AcquisitionCAPI == NULL ? -1 : (AcquisitionCAPI->AQ_IsWrapper(obj)))
#define aq_base(obj) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Base(obj)))
#define aq_parent(obj) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Parent(obj)))
#define aq_self(obj) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Self(obj)))
#define aq_inner(obj) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Inner(obj)))
#define aq_chain(obj, containment) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_CHain(obj, containment)))
static ACQUISITIONCAPI *AcquisitionCAPI = NULL;
#define aq_init() { \
PyObject *module; \
PyObject *api; \
if ((module = PyImport_ImportModule("Acquisition")) == NULL) \
Py_FatalError("Acquisition CAPI failed to load Acquisition"); \
if ((api = PyObject_GetAttrString(module,"AcquisitionCAPI")) \
== NULL) Py_FatalError("Acquisition CAPI failed to load AcquistionCAPI"); \
Py_DECREF(module); \
AcquisitionCAPI = PyCObject_AsVoidPtr(api); \
Py_DECREF(api); \
}
#endif
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment