[Zodb-checkins] CVS: Zope2/lib/Components/ExtensionClass - Acquisition.c:1.52.2.1 Acquisition.h:1.1.4.1
Matthew T. Kromer
matt@digicool.com
Wed, 11 Jul 2001 17:30:52 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/Components/ExtensionClass/src
In directory korak.digicool.com:/tmp/cvs-serv18144
Modified Files:
Tag: matt-aquisition-ext
Acquisition.c Acquisition.h
Log Message:
Add wrapper-functions aq_getData(name, [default]) aq_setData(name, value)
and aq_unsetData(name), module function aq_getData(ob, name, [default])
which are WRAPPER-specific attribute manipulation methods.
Also added C API hooks for same.
--- Updated File Acquisition.c in package Zope2/lib/Components/ExtensionClass --
--- Acquisition.c 2001/07/03 19:38:20 1.52
+++ Acquisition.c 2001/07/11 21:30:51 1.52.2.1
@@ -139,6 +139,7 @@
PyObject_HEAD
PyObject *obj;
PyObject *container;
+ PyObject *attributes;
} Wrapper;
staticforward PyExtensionClass Wrappertype, XaqWrappertype;
@@ -157,6 +158,7 @@
Py_INCREF(container);
self->obj=obj;
self->container=container;
+ self->attributes=NULL;
Py_INCREF(Py_None);
return Py_None;
}
@@ -223,6 +225,7 @@
Py_XINCREF(container);
self->obj=obj;
self->container=container;
+ self->attributes = NULL;
return OBJECT(self);
}
@@ -232,6 +235,7 @@
{
Py_XDECREF(self->obj);
Py_XDECREF(self->container);
+ Py_XDECREF(self->attributes);
Py_DECREF(self->ob_type);
if (nWrappers < MAX_CACHED_WRAPPERS)
@@ -283,6 +287,10 @@
Py_INCREF(r);
return r;
}
+ if (strcmp(name, "setData") == 0)
+ {
+ return Py_FindAttr(OBJECT(self),oname);
+ }
break;
case 'e':
if (strcmp(name,"explicit")==0)
@@ -346,7 +354,12 @@
return r;
}
break;
-
+ case 'g':
+ if (strcmp(name, "getData") == 0)
+ {
+ return Py_FindAttr(OBJECT(self),oname);
+ }
+ break;
case 'u':
if (strcmp(name,"uncle")==0)
{
@@ -1093,6 +1106,99 @@
return PyInt_FromLong(0);
}
+static PyObject *
+capi_aq_getData(PyObject *self, PyObject *name)
+{
+ PyObject *result;
+
+ if (!isWrapper(self)) {
+ PyErr_SetObject(PyExc_TypeError, self); /* XXX no msg */
+ return NULL;
+ }
+
+ if (WRAPPER(self)->attributes == NULL) {
+ PyErr_SetObject(PyExc_AttributeError, name);
+ return NULL;
+ }
+
+ result = PyDict_GetItem(WRAPPER(self)->attributes, name);
+ if (result == NULL)
+ PyErr_SetObject(PyExc_AttributeError, name);
+ else
+ Py_INCREF(result);
+
+ return result;
+
+}
+
+static int
+capi_aq_setData(PyObject *self, PyObject *name, PyObject *value)
+{
+
+ if (!isWrapper(self)) {
+ PyErr_SetObject(PyExc_TypeError, self); /* XXX no msg */
+ return -1;
+ }
+
+ if (WRAPPER(self)->attributes == NULL)
+ if ((WRAPPER(self)->attributes = PyDict_New()) == NULL) return -1;
+
+ if (value == NULL)
+ return PyDict_DelItem(WRAPPER(self)->attributes, name);
+
+ return PyDict_SetItem(WRAPPER(self)->attributes, name, value);
+}
+
+static PyObject *
+Wrapper_getData(Wrapper *self, PyObject *args)
+{
+ PyObject *name = NULL;
+ PyObject *deflt = NULL;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "O|O", &name, &deflt)) return NULL;
+
+ result = capi_aq_getData(OBJECT(self), name);
+
+ if (result == NULL && deflt != NULL) {
+ PyErr_Clear();
+
+ Py_INCREF(deflt);
+ result = deflt;
+ }
+
+ return result;
+}
+
+static PyObject *
+Wrapper_setData(Wrapper *self, PyObject *args)
+{
+ PyObject *name = NULL;
+ PyObject *value = NULL;
+
+ if (!PyArg_ParseTuple(args, "OO", &name, &value)) return NULL;
+
+ if (capi_aq_setData(OBJECT(self), name, value) < 0) return NULL;
+
+ Py_INCREF(Py_None);
+ return Py_None;
+
+}
+
+static PyObject *
+Wrapper_unsetData(Wrapper *self, PyObject *args)
+{
+ PyObject *name = NULL;
+
+ if (!PyArg_ParseTuple(args, "O", &name)) return NULL;
+
+ if (capi_aq_setData(OBJECT(self), name, NULL) < 0) return NULL;
+
+ Py_INCREF(Py_None);
+ return Py_None;
+
+}
+
static struct PyMethodDef Wrapper_methods[] = {
{"__init__", (PyCFunction)Wrapper__init__, 0,
"Initialize an Acquirer Wrapper"},
@@ -1104,6 +1210,12 @@
"Get an attribute, acquiring it if necessary"},
{"aq_inContextOf", (PyCFunction)Wrapper_inContextOf, METH_VARARGS,
"Test whether the object is currently in the context of the argument"},
+ {"aq_getData", (PyCFunction)Wrapper_getData, METH_VARARGS,
+ "aq_getData(name, [default]) Get wrapper-specific data"},
+ {"aq_setData", (PyCFunction)Wrapper_setData, METH_VARARGS,
+ "aq_setData(name, value) Set wrapper-specific data"},
+ {"aq_unsetData", (PyCFunction)Wrapper_unsetData, METH_VARARGS,
+ "aq_unsetData(name) Unset wrapper-specific data"},
{NULL, NULL} /* sentinel */
};
@@ -1468,6 +1580,27 @@
return capi_aq_chain(self, containment);
}
+static PyObject *
+module_aq_getData(PyObject *ignored, PyObject *args)
+{
+ PyObject *self = NULL;
+ PyObject *name = NULL;
+ PyObject *deflt = NULL;
+ PyObject *result = NULL;
+
+ if (!PyArg_ParseTuple(args, "OO|O", &self, &name, &deflt)) return NULL;
+
+ result = capi_aq_getData(self, name);
+ if (result == NULL && deflt != NULL) {
+ PyErr_Clear();
+ result = deflt;
+ Py_INCREF(result);
+ }
+
+ return result;
+
+}
+
static struct PyMethodDef methods[] = {
{"aq_acquire", (PyCFunction)module_aq_acquire, METH_VARARGS|METH_KEYWORDS,
"aq_acquire(ob, name [, filter, extra, explicit]) -- "
@@ -1489,6 +1622,9 @@
{"aq_chain", (PyCFunction)module_aq_chain, METH_VARARGS,
"aq_chain(ob [, containment]) -- "
"Get a list of objects in the acquisition environment"},
+ {"aq_getData", (PyCFunction)module_aq_getData, METH_VARARGS,
+ "aq_getData(ob, name [, default]) -- get an attribute from the wrapper"
+ "itself"},
{NULL, NULL}
};
@@ -1542,6 +1678,8 @@
AcquisitionCAPI.AQ_Self = capi_aq_self;
AcquisitionCAPI.AQ_Inner = capi_aq_inner;
AcquisitionCAPI.AQ_Chain = capi_aq_chain;
+ AcquisitionCAPI.AQ_GetData = capi_aq_getData;
+ AcquisitionCAPI.AQ_SetData = capi_aq_setData;
api = PyCObject_FromVoidPtr(&AcquisitionCAPI, NULL);
PyDict_SetItemString(d, "AcquisitionCAPI", api);
--- Updated File Acquisition.h in package Zope2/lib/Components/ExtensionClass --
--- Acquisition.h 2001/07/03 19:38:20 1.1
+++ Acquisition.h 2001/07/11 21:30:51 1.1.4.1
@@ -49,9 +49,9 @@
#define __ACQUISITION_H_
typedef struct {
- PyObject *(*AQ_Acquire) (PyObject *obj, PyObject *name, PyObject *filter,
- PyObject *extra, int explicit, PyObject *deflt,
- int containment);
+ 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);
@@ -60,6 +60,8 @@
PyObject *(*AQ_Self) (PyObject *obj);
PyObject *(*AQ_Inner) (PyObject *obj);
PyObject *(*AQ_Chain) (PyObject *obj, int containment);
+ PyObject *(*AQ_GetData) (PyObject *obj, PyObject *name);
+ int (*AQ_SetData) (PyObject *obj, PyObject *name, PyObject *value);
} ACQUISITIONCAPI;
#ifndef _IN_ACQUISITION_C
@@ -72,7 +74,9 @@
#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)))
+#define aq_chain(obj, containment) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_Chain(obj, containment)))
+#define aq_getData(obj, name) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_GetData(obj, name)))
+#define aq_setData(obj, name, value) (AcquisitionCAPI == NULL ? NULL : (AcquisitionCAPI->AQ_SetData(obj, name, value)))
static ACQUISITIONCAPI *AcquisitionCAPI = NULL;