Go to the source code of this file.
Data Structures | |
struct | CallableObject |
struct | EgguserObject |
struct | MyDictObject |
Defines | |
#define | PYTHON_VAR 1 |
#define | PYTHON_FUNC 2 |
#define | Callable_Check(op) PyObject_TypeCheck(op, &Callable_Type) |
#define | Egguser_Check(op) PyObject_TypeCheck(op, &Egguser_Type) |
#define | FlushAll() |
Functions | |
void | Flush (unsigned Target) |
PyObject * | c_to_python_var (script_var_t *v) |
int | python_to_c_var (PyObject *obj, script_var_t *var, int type) |
int | MyModule_Init (void) |
PyObject * | GetVar (script_linked_var_t *var) |
int | SetVar (script_linked_var_t *var, PyObject *Value) |
PyObject * | MyModule_Add (char *name, char *doc) |
PyObject * | MyDict_New (PyTypeObject *Type, PyObject *args, PyObject *kwds) |
Variables | |
partymember_t * | LogTarget |
script_module_t | my_script_interface |
PyTypeObject | Callable_Type |
PyTypeObject | MyDict_Type |
PyTypeObject | Stdio_Type |
PyTypeObject | Egguser_Type |
PyObject * | EggdropModule |
#define Callable_Check | ( | op | ) | PyObject_TypeCheck(op, &Callable_Type) |
#define Egguser_Check | ( | op | ) | PyObject_TypeCheck(op, &Egguser_Type) |
Definition at line 26 of file pythonscript.h.
Referenced by Compare(), Egguser_New(), and python_to_c_var().
#define FlushAll | ( | ) |
Value:
Definition at line 28 of file pythonscript.h.
Referenced by Help(), my_create_command(), my_link_var(), my_load_script(), my_python_callbacker(), and party_python().
#define PYTHON_FUNC 2 |
Definition at line 23 of file pythonscript.h.
Referenced by GetDoc(), GetItem(), my_command_handler(), my_create_command(), and Repr().
#define PYTHON_VAR 1 |
Definition at line 22 of file pythonscript.h.
Referenced by my_link_var(), SetAttr(), and SetItem().
PyObject* c_to_python_var | ( | script_var_t * | v | ) |
Definition at line 293 of file pythonscript.c.
References byte_array_b::bytes, c_to_python_var(), byte_array_b::do_free, Egguser_Type, byte_array_b::len, script_var_b::len, NULL, SCRIPT_ARRAY, SCRIPT_BYTES, SCRIPT_FREE, SCRIPT_FREE_VAR, SCRIPT_INTEGER, SCRIPT_PARTIER, SCRIPT_POINTER, SCRIPT_STRING, SCRIPT_STRING_LIST, SCRIPT_TYPE_MASK, SCRIPT_UNSIGNED, SCRIPT_USER, SCRIPT_VAR, script_var_b::type, EgguserObject::user, and script_var_b::value.
Referenced by c_to_python_var(), Get(), GetFlags(), GetIrcMasks(), GetVar(), my_command_handler(), and my_python_callbacker().
00293 { 00294 PyObject *result; 00295 00296 result = NULL; 00297 /* If it's an array, we call ourselves recursively. */ 00298 if (v->type & SCRIPT_ARRAY) { 00299 PyObject *element; 00300 int i; 00301 00302 result = PyTuple_New(v->len); 00303 /* If it's an array of script_var_t's, then it's easy. */ 00304 if ((v->type & SCRIPT_TYPE_MASK) == SCRIPT_VAR) { 00305 script_var_t **v_list; 00306 00307 v_list = (script_var_t **)v->value; 00308 for (i = 0; i < v->len; i++) { 00309 element = c_to_python_var(v_list[i]); 00310 if (!element) { 00311 Py_DECREF(result); 00312 return 0; 00313 } 00314 PyTuple_SET_ITEM(result, i, element); 00315 } 00316 } 00317 else { 00318 /* Otherwise, we have to turn them into fake script_var_t's. */ 00319 script_var_t v_sub; 00320 void **values; 00321 00322 values = (void **)v->value; 00323 for (i = 0; i < v->len; i++) { 00324 v_sub.type = v->type & (~SCRIPT_ARRAY); 00325 v_sub.value = values[i]; 00326 v_sub.len = -1; 00327 element = c_to_python_var(&v_sub); 00328 if (!element) { 00329 Py_DECREF(result); 00330 return 0; 00331 } 00332 PyTuple_SET_ITEM(result, i, element); 00333 } 00334 } 00335 /* Whew */ 00336 if (v->type & SCRIPT_FREE) free(v->value); 00337 if (v->type & SCRIPT_FREE_VAR) free(v); 00338 return result; 00339 } 00340 00341 /* Here is where we handle the basic types. */ 00342 switch (v->type & SCRIPT_TYPE_MASK) { 00343 case SCRIPT_INTEGER: 00344 result = PyInt_FromLong((int) v->value); 00345 break; 00346 case SCRIPT_UNSIGNED: 00347 /* Python has no "unsigned" type. A Python "int" is a C "long". 00348 * So, if sizeof(int) == sizeof(long) like on IA32 a Python "int" 00349 * might be too short to store a C "unsigned". 00350 * Use a Python "long" in that case */ 00351 if ((unsigned) v->value <= PyInt_GetMax()) { 00352 result = PyInt_FromLong((unsigned) v->value); 00353 } else { 00354 result = PyLong_FromUnsignedLong((unsigned) v->value); 00355 } 00356 break; 00357 case SCRIPT_STRING: { 00358 char *str = v->value; 00359 00360 if (!str) str = ""; 00361 if (v->len == -1) v->len = strlen(str); 00362 result = PyString_FromStringAndSize(str, v->len); 00363 if (v->value && v->type & SCRIPT_FREE) free(v->value); 00364 break; 00365 } 00366 case SCRIPT_STRING_LIST: { 00367 char **str = v->value; 00368 PyObject *element; 00369 00370 result = PyList_New(0); 00371 for (str = v->value; str && *str; ++str) { 00372 element = PyString_FromString(*str); 00373 PyList_Append(result, element); 00374 } 00375 break; 00376 } 00377 case SCRIPT_BYTES: { 00378 byte_array_t *bytes = v->value; 00379 result = PyString_FromStringAndSize(bytes->bytes, bytes->len); 00380 if (bytes->do_free) free(bytes->bytes); 00381 if (v->type & SCRIPT_FREE) free(bytes); 00382 break; 00383 } 00384 #if 0 00385 case SCRIPT_POINTER: { 00386 char str[32]; 00387 00388 sprintf(str, "#%u", (unsigned int) v->value); 00389 result = Tcl_NewStringObj(str, -1); 00390 break; 00391 } 00392 case SCRIPT_PARTIER: { 00393 partymember_t *p = v->value; 00394 int pid; 00395 00396 if (p) pid = p->pid; 00397 else pid = -1; 00398 result = Tcl_NewIntObj(pid); 00399 break; 00400 } 00401 #endif 00402 case SCRIPT_USER: { 00403 /* An eggdrop user record (struct userrec *). */ 00404 user_t *u = v->value; 00405 00406 if (u) { 00407 EgguserObject *O = (EgguserObject *) Egguser_Type.tp_new(&Egguser_Type, 0, 0); 00408 O->user = u; 00409 result = (PyObject *) O; 00410 } else { 00411 Py_INCREF(Py_None); 00412 result = Py_None; 00413 } 00414 break; 00415 } 00416 default: 00417 /* Default: just "None". */ 00418 Py_INCREF(Py_None); 00419 result = Py_None; 00420 } 00421 if (v->type & SCRIPT_FREE_VAR) free(v); 00422 return result; 00423 }
void Flush | ( | unsigned | Target | ) |
PyObject* GetVar | ( | script_linked_var_t * | var | ) |
Definition at line 36 of file mydict.c.
References c_to_python_var(), script_linked_var_b::callbacks, script_var_b::len, script_linked_var_b::name, script_var_callbacks_b::on_read, SCRIPT_TYPE_MASK, script_linked_var_b::type, script_var_b::type, script_linked_var_b::value, and script_var_b::value.
Referenced by GetItem(), and Repr().
00036 { 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 }
PyObject* MyDict_New | ( | PyTypeObject * | Type, | |
PyObject * | args, | |||
PyObject * | kwds | |||
) |
PyObject* MyModule_Add | ( | char * | name, | |
char * | doc | |||
) |
Definition at line 63 of file mymodule.c.
References MyModule_New().
Referenced by pythonscript_LTX_start().
00063 { 00064 PyObject *Modules, *ModuleObj; 00065 00066 Modules = PyImport_GetModuleDict(); /* Warning: Borrowed reference */ 00067 if ((ModuleObj = PyDict_GetItemString(Modules, name)) && PyModule_Check(ModuleObj)) return ModuleObj; 00068 ModuleObj = MyModule_New(name); /* new reference */ 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); /* new reference */ 00080 if (DocObj) PyDict_SetItemString(*DictPtr, "__doc__", DocObj); 00081 Py_XDECREF(DocObj); 00082 PyErr_Clear(); 00083 } 00084 00085 Py_DECREF(ModuleObj); /* Yes, it still exists, in modules! */ 00086 return ModuleObj; 00087 }
int MyModule_Init | ( | void | ) |
Definition at line 174 of file mymodule.c.
References DictString, and MyModule_Type.
Referenced by pythonscript_LTX_start().
00174 { 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 }
int python_to_c_var | ( | PyObject * | obj, | |
script_var_t * | var, | |||
int | type | |||
) |
Definition at line 429 of file pythonscript.c.
References byte_array_b::bytes, script_callback_b::callback, script_callback_b::callback_data, script_callback_b::delete_data, Egguser_Check, user::flags, script_callback_b::flags, GetContext(), byte_array_b::len, script_var_b::len, MaxInt, MaxUInt, MinInt, MinUInt, my_python_callbacker(), script_callback_b::name, NULL, script_callback_b::owner, SCRIPT_BYTES, SCRIPT_CALLBACK, SCRIPT_FREE, SCRIPT_INTEGER, SCRIPT_PARTIER, SCRIPT_STRING, SCRIPT_TYPE_MASK, SCRIPT_UNSIGNED, SCRIPT_USER, script_callback_b::syntax, script_var_b::type, EgguserObject::user, USER_DELETED, and script_var_b::value.
Referenced by my_get_arg(), and SetVar().
00429 { 00430 var->type = type; 00431 var->len = -1; 00432 var->value = 0; 00433 00434 switch (type & SCRIPT_TYPE_MASK) { 00435 case SCRIPT_STRING: { 00436 char *Str; 00437 00438 Str = PyString_AsString(obj); 00439 if (!Str) return 1; 00440 var->value = strdup(Str); 00441 var->len = strlen(var->value); 00442 return 0; 00443 } 00444 case SCRIPT_CALLBACK: { 00445 script_callback_t *cback; /* Callback struct */ 00446 PyObject *FunctionName, *ModuleName; 00447 char *CFuncName = 0, *CModName = 0; 00448 00449 if (!PyCallable_Check(obj)) { 00450 PyErr_Format(PyExc_TypeError, "Object of type %s is not callable.", obj->ob_type->tp_name); 00451 return 1; 00452 } 00453 cback = malloc(sizeof(*cback)); 00454 cback->callback = (Function) my_python_callbacker; 00455 cback->callback_data = obj; 00456 Py_INCREF(obj); 00457 00458 ModuleName = PyObject_GetAttrString(obj, "__module__"); 00459 FunctionName = PyObject_GetAttrString(obj, "__name__"); 00460 if (ModuleName) CModName = PyString_AsString(ModuleName); 00461 if (FunctionName) CFuncName = PyString_AsString(FunctionName); 00462 PyErr_Clear(); 00463 if (!CModName) CModName = "python"; 00464 if (CFuncName) { 00465 cback->name = malloc(strlen(CModName) + strlen(CFuncName) + 2); 00466 sprintf(cback->name, "%s.%s", CModName, CFuncName); 00467 } else { 00468 cback->name = malloc(strlen(CModName) + 12); 00469 sprintf(cback->name, "%s.0x%08x", CModName, ((int) obj) & 0xFFFFFFFF); 00470 } 00471 00472 Py_XDECREF(ModuleName); 00473 Py_XDECREF(FunctionName); 00474 00475 cback->delete_data = 0; 00476 cback->syntax = 0; 00477 cback->flags = 0; 00478 cback->owner = GetContext(); 00479 // putlog(LOG_MISC, "*", "Created Callback %s %p %s %p", cback->owner->name, cback->owner->module, cback->owner->script, cback->owner->client_data); 00480 00481 var->value = cback; 00482 return 0; 00483 } 00484 case SCRIPT_BYTES: { 00485 byte_array_t *byte_array; 00486 00487 byte_array = malloc(sizeof(*byte_array)); 00488 00489 var->value = byte_array; 00490 var->type |= SCRIPT_FREE; 00491 00492 return PyString_AsStringAndSize(obj, (char **) &byte_array->bytes, &byte_array->len); 00493 } 00494 case SCRIPT_UNSIGNED: 00495 case SCRIPT_INTEGER: { 00496 if (!PyInt_Check(obj) && !PyLong_Check(obj)) { 00497 PyErr_SetString(PyExc_TypeError, "an integer is required"); 00498 return 1; 00499 } 00500 PyErr_Clear(); 00501 if ((((type & SCRIPT_TYPE_MASK) == SCRIPT_UNSIGNED) && (PyObject_Compare(obj, MinUInt) < 0 || 00502 PyObject_Compare(obj, MaxUInt) > 0)) || 00503 (((type & SCRIPT_TYPE_MASK) == SCRIPT_INTEGER) && (PyObject_Compare(obj, MinInt) < 0 || 00504 PyObject_Compare(obj, MaxInt) > 0))) { 00505 PyErr_SetString(PyExc_OverflowError, "Python value does not fit in C type"); 00506 return 1; 00507 } 00508 00509 if (PyInt_Check(obj)) var->value = (void *) PyInt_AsLong(obj); 00510 else var->value = (void *) PyLong_AsUnsignedLong(obj); 00511 return 0; 00512 } 00513 #if 0 00514 case SCRIPT_PARTIER: { 00515 int pid = -1; 00516 00517 err = Tcl_GetIntFromObj(myinterp, obj, &pid); 00518 if (!err) var->value = partymember_lookup_pid(pid); 00519 else var->value = NULL; 00520 break; 00521 } 00522 #endif 00523 case SCRIPT_USER: { 00524 EgguserObject *O = (EgguserObject *) obj; 00525 00526 if (!Egguser_Check(obj)) { 00527 PyErr_Format(PyExc_TypeError, "Expected eggdrop.egguser, got %s", obj->ob_type->tp_name); 00528 return 1; 00529 } 00530 if (!O->user || (O->user->flags & USER_DELETED)) { 00531 PyErr_SetString(PyExc_RuntimeError, "This user has been deleted."); 00532 return 1; 00533 } 00534 var->value = O->user; 00535 return 0; 00536 } 00537 } 00538 return 1; 00539 }
int SetVar | ( | script_linked_var_t * | var, | |
PyObject * | Value | |||
) |
Definition at line 52 of file mydict.c.
References python_to_c_var(), script_linked_var_on_write(), and script_linked_var_b::type.
Referenced by SetAttr(), and SetItem().
00052 { 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 }
PyTypeObject Callable_Type |
Definition at line 61 of file pythonscript.h.
PyObject* EggdropModule |
Definition at line 62 of file pythonscript.h.
Referenced by my_create_command(), my_delete_command(), my_link_var(), my_unlink_var(), and pythonscript_LTX_start().
PyTypeObject Egguser_Type |
Definition at line 61 of file pythonscript.h.
Definition at line 48 of file pythonscript.h.
Definition at line 50 of file pythonscript.h.
PyTypeObject MyDict_Type |
Definition at line 61 of file pythonscript.h.
PyTypeObject Stdio_Type |
Definition at line 61 of file pythonscript.h.