#include #include #include void print_object(PyObject* object) { PyObject_Print(object, stdout, 0); } PyObject* getObject(const char* string_name) { PyObject *evalModule; PyObject *evalDict; PyObject *evalVal; evalModule = PyImport_AddModule( (char*)"__main__" ); evalDict = PyModule_GetDict( evalModule ); evalVal = PyDict_GetItemString( evalDict, string_name); if ( PyErr_Occurred() ) { PyErr_Print();PyErr_Clear(); return NULL; } else { return evalVal; } } PyObject* getObjectInModule(const char* objectName, const char* moduleName) { PyObject *evalModule; PyObject *evalDict; PyObject *evalVal; evalModule = PyImport_AddModule( moduleName ); evalDict = PyModule_GetDict( evalModule ); evalVal = PyDict_GetItemString( evalDict, objectName ); if ( PyErr_Occurred() ) { PyErr_Print();PyErr_Clear(); return evalVal; } else { return evalVal; } } char* checkError() { if ( PyErr_Occurred() ) { //PyErr_Print();//PyErr_Clear(); /* PyRun_SimpleString("traceback.format_exc()"); */ PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL; PyErr_Fetch( &exc_typ, &exc_val, &exc_tb); /* print_object(exc_val); */ /* printf("\n"); */ /* print_object(exc_tb); */ PyObject *exc_typ_string = PyObject_Str(exc_typ); PyObject *exc_val_string = PyObject_Str(exc_val); char *exc_typ_tmp = PyString_AsString(exc_typ_string); char *exc_val_tmp = PyString_AsString(exc_val_string); char *exc = malloc(strlen(exc_val_tmp) + strlen(exc_typ_tmp) + 1); strcpy(exc, exc_typ_tmp); strcat(exc, ","); strcat(exc, exc_val_tmp); Py_DECREF(exc_val_string); Py_DECREF(exc_typ_string); Py_XDECREF(exc_val); Py_XDECREF(exc_typ); Py_XDECREF(exc_tb); return exc; } else { return NULL; } } void execInModule(const char* payload, const char* moduleName) { PyObject *evalModule; PyObject *evalDict; PyObject *evalVal; evalModule = PyImport_AddModule(moduleName); PyObject *globals = PyModule_GetDict(evalModule); PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()); PyObject *locals = Py_BuildValue("{}"); PyObject *result = PyRun_StringFlags(payload, Py_file_input, globals, globals, NULL); if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();} /* if ( PyErr_Occurred() ) {PyErr_Clear();} */ return; } void finalizer(PyObject* p) { Py_DecRef(p); } typedef void funcType(PyObject*); typedef funcType * pFuncType; pFuncType gimmeFunc(int dummy) { return &finalizer; } /*int main() {*/ /*Py_Initialize();*/ /*[> PyRun_SimpleString("x = 3"); <]*/ /*execInModule("def x(y):\n print(y)\n import traceback\n return 0", "foo");*/ /*PyObject *x = getObjectInModule("x", "foo");*/ /*PyObject *result = PyObject_CallObject(x, Py_BuildValue("(i)", 5));*/ /*print_object(result);*/ /*printf("\n");*/ /*}*/