[Zodb-checkins] CVS: Zope/lib/Components/ExtensionClass/src - Acquisition.c:1.52.8.1 Acquisition.h:1.1.12.1

Jim Fulton jim@zope.com
Fri, 28 Sep 2001 13:59:25 -0400


Update of /cvs-repository/Zope/lib/Components/ExtensionClass/src
In directory cvs.zope.org:/tmp/cvs-serv7111

Modified Files:
      Tag: ComponentArchitecture-branch
	Acquisition.c Acquisition.h 
Log Message:
Merged in MattK's wrapper-data changes

=== Zope/lib/Components/ExtensionClass/src/Acquisition.c 1.52 => 1.52.8.1 ===
   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);


=== Zope/lib/Components/ExtensionClass/src/Acquisition.h 1.1 => 1.1.12.1 ===
 
 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;