00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef lint
00025 static const char rcsid[] = "$Id: mymodule.c,v 1.1 2005-12-15 15:26:12 sven Exp $";
00026 #endif
00027
00028 #include <Python.h>
00029 #include <structmember.h>
00030 #include <eggdrop/eggdrop.h>
00031
00032 #include "pythonscript.h"
00033
00034 static PyTypeObject MyModule_Type;
00035
00036 static PyObject *MyModule_New(char *name) {
00037 int set1, set2;
00038 PyObject *NameObj, *ModuleObj, **DictPtr;
00039
00040 ModuleObj = PyObject_GC_New(PyObject, &MyModule_Type);
00041 if (!ModuleObj) return 0;
00042 NameObj = PyString_FromString(name);
00043 DictPtr = _PyObject_GetDictPtr(ModuleObj);
00044 *DictPtr = MyDict_New(&MyDict_Type, 0, 0);
00045 if (!*DictPtr || !NameObj) {
00046 Py_DECREF(ModuleObj);
00047 Py_XDECREF(NameObj);
00048 return 0;
00049 }
00050 set1 = PyDict_SetItemString(*DictPtr, "__name__", NameObj);
00051 Py_DECREF(NameObj);
00052 set2 = PyDict_SetItemString(*DictPtr, "__doc__", Py_None);
00053 if (set1 || set2) {
00054 Py_DECREF(ModuleObj);
00055 return 0;
00056 }
00057 PyObject_GC_Track(ModuleObj);
00058 return ModuleObj;
00059 }
00060
00061
00062
00063 PyObject *MyModule_Add(char *name, char *doc) {
00064 PyObject *Modules, *ModuleObj;
00065
00066 Modules = PyImport_GetModuleDict();
00067 if ((ModuleObj = PyDict_GetItemString(Modules, name)) && PyModule_Check(ModuleObj)) return ModuleObj;
00068 ModuleObj = MyModule_New(name);
00069 if (!ModuleObj) return 0;
00070 if (PyDict_SetItemString(Modules, name, ModuleObj)) {
00071 Py_DECREF(ModuleObj);
00072 return 0;
00073 }
00074
00075 if (doc) {
00076 PyObject **DictPtr, *DocObj;
00077
00078 DictPtr = _PyObject_GetDictPtr(ModuleObj);
00079 DocObj = PyString_FromString(doc);
00080 if (DocObj) PyDict_SetItemString(*DictPtr, "__doc__", DocObj);
00081 Py_XDECREF(DocObj);
00082 PyErr_Clear();
00083 }
00084
00085 Py_DECREF(ModuleObj);
00086 return ModuleObj;
00087 }
00088
00089 static PyObject *DictString;
00090
00091 static PyObject *GetAttr(PyObject *self, PyObject *Key) {
00092 PyObject **DictPtr, *Ret;
00093
00094 DictPtr = _PyObject_GetDictPtr(self);
00095 if (!PyObject_Compare(DictString, Key)) {
00096 Py_INCREF(*DictPtr);
00097 return *DictPtr;
00098 }
00099 Ret = (*DictPtr)->ob_type->tp_as_mapping->mp_subscript(*DictPtr, Key);
00100 if (!Ret && PyErr_ExceptionMatches(PyExc_KeyError)) {
00101 PyErr_Format(PyExc_AttributeError, "'%.50s' object has no attribute '%.400s'", self->ob_type->tp_name, PyString_AS_STRING(Key));
00102 }
00103 return Ret;
00104 }
00105
00106
00107
00108
00109
00110
00111 static int SetAttr(PyObject *self, PyObject *Key, PyObject *Value) {
00112 PyObject **DictPtr, *Cur;
00113 CallableObject *Call;
00114 script_linked_var_t *var;
00115
00116 DictPtr = _PyObject_GetDictPtr(self);
00117 Cur = PyDict_GetItem(*DictPtr, Key);
00118 if (!Cur) {
00119 PyErr_Format(PyExc_AttributeError, "'%s' object has no attribute '%s'", self->ob_type->tp_name, PyString_AsString(Key));
00120 return 1;
00121 }
00122 if (Cur->ob_type != &Callable_Type) return PyDict_SetItem(*DictPtr, Key, Value);
00123 Call = (CallableObject *) Cur;
00124 var = Call->client_data;
00125 if (!Value || Call->type != PYTHON_VAR || var->type & SCRIPT_READONLY) {
00126 PyErr_SetString(PyExc_TypeError, "readonly attribute");
00127 return 1;
00128 }
00129 return SetVar(var, Value);
00130 }
00131
00132 static PyTypeObject MyModule_Type = {
00133 PyObject_HEAD_INIT(0)
00134 0,
00135 "eggdropmodule",
00136 0,
00137 0,
00138 0,
00139 0,
00140 0,
00141 0,
00142 0,
00143 0,
00144 0,
00145 0,
00146 0,
00147 0,
00148 0,
00149 0,
00150 GetAttr,
00151 SetAttr,
00152 0,
00153 Py_TPFLAGS_DEFAULT,
00154 "module containing special mapped eggdrop variables",
00155 0,
00156 0,
00157 0,
00158 0,
00159 0,
00160 0,
00161 0,
00162 0,
00163 0,
00164 &PyModule_Type,
00165 0,
00166 0,
00167 0,
00168 0,
00169 0,
00170 0,
00171 0
00172 };
00173
00174 int MyModule_Init() {
00175 MyModule_Type.tp_basicsize = PyModule_Type.tp_basicsize;
00176 MyModule_Type.tp_dictoffset = PyModule_Type.tp_dictoffset;
00177 DictString = PyString_FromString("__dict__");
00178 return PyType_Ready(&MyModule_Type);
00179 }