00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: mydict.c,v 1.1 2005-12-15 15:26:12 sven Exp $";
00022 #endif
00023
00024 #include <Python.h>
00025 #include <eggdrop/eggdrop.h>
00026
00027 #include "pythonscript.h"
00028
00029 #define MyDict_Check(op) PyObject_TypeCheck(op, &MyDict_Type)
00030
00031 PyObject *MyDict_New(PyTypeObject *Type, PyObject *args, PyObject *kwds) {
00032 PyObject *O = PyDict_Type.tp_new(Type, args, kwds);
00033 return O;
00034 }
00035
00036 PyObject *GetVar(script_linked_var_t *var) {
00037 script_var_t newvalue = {0};
00038
00039 newvalue.len = -1;
00040 if (var->callbacks && var->callbacks->on_read) {
00041 if (var->callbacks->on_read(var, &newvalue)) {
00042 PyErr_Format(PyExc_LookupError, "internal eggdrop error in 'on_read' function for '%s'", var->name);
00043 return 0;
00044 }
00045 } else {
00046 newvalue.type = var->type & SCRIPT_TYPE_MASK;;
00047 newvalue.value = *(void **)var->value;
00048 }
00049 return c_to_python_var(&newvalue);
00050 }
00051
00052 int SetVar(script_linked_var_t *var, PyObject *Value) {
00053 script_var_t newvalue = {0};
00054
00055 if (python_to_c_var(Value, &newvalue, var->type)) return 1;
00056 if (script_linked_var_on_write(var, &newvalue)) {
00057 PyErr_SetString(PyExc_RuntimeError, "set");
00058 return 1;
00059 }
00060 return 0;
00061 }
00062
00063 static PyObject *GetItem(PyObject *self, PyObject *Key) {
00064 PyObject *Ret;
00065 CallableObject *Cal;
00066
00067 Ret = PyDict_GetItem(self, Key);
00068 Cal = (CallableObject *) Ret;
00069
00070 if (!Ret || !Cal->client_data) {
00071 PyErr_Format(PyExc_KeyError, "'%s'", PyString_AS_STRING(Key));
00072 return 0;
00073 }
00074 if (Ret->ob_type != &Callable_Type || Cal->type == PYTHON_FUNC) {
00075 Py_INCREF(Ret);
00076 return Ret;
00077 }
00078 return GetVar(Cal->client_data);
00079 }
00080
00081
00082
00083
00084
00085
00086 static int SetItem(PyObject *self, PyObject *Key, PyObject *Value) {
00087 PyObject *Ret;
00088 CallableObject *Call;
00089 script_linked_var_t *var;
00090
00091 Ret = PyDict_GetItem(self, Key);
00092 if (!Ret || !Value) {
00093 PyErr_SetString(PyExc_TypeError, "object does not support item assignment");
00094 return 1;
00095 }
00096 if (Ret->ob_type != &Callable_Type) return PyDict_SetItem(self, Key, Value);
00097 Call = (CallableObject *) Ret;
00098 var = Call->client_data;
00099 if (!var || Call->type != PYTHON_VAR || var->type & SCRIPT_READONLY) {
00100 PyErr_SetString(PyExc_TypeError, "object does not support item assignment");
00101 return 1;
00102 }
00103 return SetVar(var, Value);
00104 }
00105
00106 static PyMappingMethods mapstr = {
00107 0,
00108 GetItem,
00109 SetItem
00110 };
00111
00112 PyTypeObject MyDict_Type = {
00113 PyObject_HEAD_INIT(0)
00114 0,
00115 "eggdrop.mapping",
00116 sizeof(MyDictObject),
00117 0,
00118 0,
00119 0,
00120 0,
00121 0,
00122 0,
00123 0,
00124 0,
00125 0,
00126 &mapstr,
00127 0,
00128 0,
00129 0,
00130 0,
00131 0,
00132 0,
00133 Py_TPFLAGS_DEFAULT,
00134 "mapped eggdrop variables",
00135 0,
00136 0,
00137 0,
00138 0,
00139 0,
00140 0,
00141 0,
00142 0,
00143 0,
00144 &PyDict_Type,
00145 0,
00146 0,
00147 0,
00148 0,
00149 0,
00150 0,
00151 MyDict_New
00152 };