00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <FCConfig.h>
00012 #include "PyTools.h"
00013
00014 #include <stdarg.h>
00015 #include <string.h>
00016 #include <assert.h>
00017 #include <compile.h>
00018 #include <eval.h>
00019
00020
00021 #if PY_VERSION_HEX <= 0x02050000
00022 #error "Use Python2.5.x or higher"
00023 #endif
00024
00025
00026
00027
00028
00029
00030
00031
00032 int
00033 PP_Run_Method(PyObject *pobject, const char *method,
00034 const char *resfmt, void *cresult,
00035 const char *argfmt, ... )
00036 {
00037 PyObject *pmeth, *pargs, *presult;
00038 va_list argslist;
00039 va_start(argslist, argfmt);
00040
00041 Py_Initialize();
00042 pmeth = PyObject_GetAttrString(pobject, method);
00043 if (pmeth == NULL)
00044 return -1;
00045
00046
00047
00048
00049 pargs = Py_VaBuildValue(argfmt, argslist);
00050
00051 if (pargs == NULL) {
00052 Py_DECREF(pmeth);
00053 return -1;
00054 }
00055 if (PP_DEBUG)
00056 presult = PP_Debug_Function(pmeth, pargs);
00057 else
00058 presult = PyEval_CallObject(pmeth, pargs);
00059
00060 Py_DECREF(pmeth);
00061 Py_DECREF(pargs);
00062
00063 return PP_Convert_Result(presult, resfmt, cresult);
00064
00065
00066 }
00067
00068
00069 int
00070 PP_Get_Member(PyObject *pobject, const char *attrname,
00071 const char *resfmt, void *cresult)
00072 {
00073 PyObject *pmemb;
00074 Py_Initialize();
00075 pmemb = PyObject_GetAttrString(pobject, attrname);
00076 return PP_Convert_Result(pmemb, resfmt, cresult);
00077 }
00078
00079
00080 int
00081 PP_Set_Member(PyObject *pobject, const char *attrname,
00082 const char *argfmt, ... )
00083 {
00084 int result;
00085 PyObject *pval;
00086 va_list argslist;
00087 va_start(argslist, argfmt);
00088 Py_Initialize();
00089 pval = Py_VaBuildValue(argfmt, argslist);
00090 if (pval == NULL)
00091 return -1;
00092 result = PyObject_SetAttrString(pobject, attrname, pval);
00093 Py_DECREF(pval);
00094 return result;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 int
00109 PP_Run_Function(const char *modname, const char *funcname,
00110 const char *resfmt, void *cresult,
00111 const char *argfmt, ... )
00112 {
00113
00114 PyObject *func, *args, *presult;
00115 va_list argslist;
00116 va_start(argslist, argfmt);
00117
00118 func = PP_Load_Attribute(modname, funcname);
00119 if (func == NULL)
00120 return -1;
00121 args = Py_VaBuildValue(argfmt, argslist);
00122 if (args == NULL) {
00123 Py_DECREF(func);
00124 return -1;
00125 }
00126 if (PP_DEBUG && strcmp(modname, "pdb") != 0)
00127 presult = PP_Debug_Function(func, args);
00128 else
00129 presult = PyEval_CallObject(func, args);
00130
00131 Py_DECREF(func);
00132 Py_DECREF(args);
00133 return PP_Convert_Result(presult, resfmt, cresult);
00134 }
00135
00136
00137 PyObject *
00138 PP_Debug_Function(PyObject *func, PyObject *args)
00139 {
00140 int oops, res;
00141 PyObject *presult;
00142
00143
00144
00145
00146 #if (PY_MAJOR_VERSION==2)&&(PY_MINOR_VERSION>=2)
00147 oops = _PyTuple_Resize(&args, (1 + PyTuple_Size(args)));
00148 #else
00149 oops = _PyTuple_Resize(&args, (1 + PyTuple_Size(args)),1);
00150 #endif
00151 oops |= PyTuple_SetItem(args, 0, func);
00152 if (oops)
00153 return NULL;
00154
00155 res = PP_Run_Function(
00156 "pdb", "runcall",
00157 "O", &presult,
00158 "O", args);
00159 return (res != 0) ? NULL : presult;
00160 }
00161
00162
00163 int
00164 PP_Run_Known_Callable(PyObject *object,
00165 const char *resfmt, void *cresult,
00166 const char *argfmt, ... )
00167 {
00168
00169 PyObject *args, *presult;
00170 va_list argslist;
00171 va_start(argslist, argfmt);
00172
00173 Py_Initialize();
00174 args = Py_VaBuildValue(argfmt, argslist);
00175 if (args == NULL)
00176 return -1;
00177 if (PP_DEBUG)
00178 presult = PP_Debug_Function(object, args);
00179 else
00180 presult = PyEval_CallObject(object, args);
00181
00182 Py_DECREF(args);
00183 return PP_Convert_Result(presult, resfmt, cresult);
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 #define MAX 2024
00204
00205
00206
00207
00208
00209 char PP_last_error_type[MAX];
00210 char PP_last_error_info[MAX];
00211 char PP_last_error_trace[MAX];
00212 PyObject *PP_last_traceback = NULL;
00213
00214
00215 void PP_Fetch_Error_Text()
00216 {
00217
00218
00219
00220 char *tempstr;
00221 PyObject *errobj, *errdata, *errtraceback, *pystring;
00222
00223
00224
00225
00226 PyErr_Fetch(&errobj, &errdata, &errtraceback);
00227
00228
00229
00230
00231
00232 pystring = NULL;
00233 if (errobj != NULL &&
00234 (pystring = PyObject_Str(errobj)) != NULL &&
00235 (PyString_Check(pystring)) )
00236 {
00237 strncpy(PP_last_error_type, PyString_AsString(pystring), MAX);
00238 PP_last_error_type[MAX-1] = '\0';
00239 }
00240 else
00241 strcpy(PP_last_error_type, "<unknown exception type>");
00242 Py_XDECREF(pystring);
00243
00244
00245 pystring = NULL;
00246 if (errdata != NULL &&
00247 (pystring = PyObject_Str(errdata)) != NULL &&
00248 (PyString_Check(pystring)) )
00249 {
00250 strncpy(PP_last_error_info, PyString_AsString(pystring), MAX);
00251 PP_last_error_info[MAX-1] = '\0';
00252 }
00253 else
00254 strcpy(PP_last_error_info, "<unknown exception data>");
00255 Py_XDECREF(pystring);
00256
00257
00258
00259
00260
00261
00262 pystring = NULL;
00263 if (errtraceback != NULL &&
00264 (PP_Run_Function("StringIO", "StringIO", "O", &pystring, "()") == 0) &&
00265 (PyTraceBack_Print(errtraceback, pystring) == 0) &&
00266 (PP_Run_Method(pystring, "getvalue", "s", &tempstr, "()") == 0) )
00267 {
00268 strncpy(PP_last_error_trace, tempstr, MAX);
00269 PP_last_error_trace[MAX-1] = '\0';
00270 free(tempstr);
00271 }
00272 else
00273 strcpy(PP_last_error_trace, "<unknown exception traceback>");
00274 Py_XDECREF(pystring);
00275
00276
00277 Py_XDECREF(errobj);
00278 Py_XDECREF(errdata);
00279 Py_XDECREF(PP_last_traceback);
00280 PP_last_traceback = errtraceback;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 int
00299 PP_Convert_Result(PyObject *presult, const char *resFormat, void *resTarget)
00300 {
00301 if (presult == NULL)
00302 return -1;
00303 else
00304 if (resTarget == NULL) {
00305 Py_DECREF(presult);
00306 return 0;
00307 }
00308 else
00309 if (! PyArg_Parse(presult, resFormat, resTarget)) {
00310 Py_DECREF(presult);
00311 return -1;
00312 }
00313 else {
00314 if (strcmp(resFormat, "O") != 0) {
00315 if (strcmp(resFormat, "s") == 0) {
00316 char **target = (char**) resTarget;
00317 #if defined (__GNUC__)
00318 *target = strdup(*target);
00319 #else
00320 *target = _strdup(*target);
00321 #endif
00322 }
00323 Py_DECREF(presult);
00324 }
00325 return 0;
00326 }
00327 }
00328
00329
00330 int
00331 PP_Get_Global(const char *modname, const char *varname, const char *resfmt, void *cresult)
00332 {
00333 PyObject *var;
00334 var = PP_Load_Attribute(modname, varname);
00335 return PP_Convert_Result(var, resfmt, cresult);
00336 }
00337
00338
00339 int
00340 PP_Set_Global(const char *modname, const char *varname, const char *valfmt, ... )
00341 {
00342 int result;
00343 PyObject *module, *val;
00344 va_list cvals;
00345 va_start(cvals, valfmt);
00346
00347 module = PP_Load_Module(modname);
00348 if (module == NULL)
00349 return -1;
00350 val = Py_VaBuildValue(valfmt, cvals);
00351 va_end(cvals);
00352 if (val == NULL)
00353 return -1;
00354 result = PyObject_SetAttrString(module, varname, val);
00355 Py_DECREF(val);
00356 return result;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 int PP_RELOAD = 0;
00372 int PP_DEBUG = 0;
00373
00374
00375 const char *PP_Init(const char *modname) {
00376 Py_Initialize();
00377
00378 if (modname!=NULL) return modname;
00379 {
00380 char* __main__=(char *)malloc(sizeof("__main__"));
00381 return __main__="__main__";
00382 }
00383
00384
00385
00386 }
00387
00388
00389 int
00390 PP_Make_Dummy_Module(const char *modname)
00391 {
00392 PyObject *module, *dict;
00393 Py_Initialize();
00394 module = PyImport_AddModule(modname);
00395 if (module == NULL)
00396 return -1;
00397 else {
00398 dict = PyModule_GetDict(module);
00399 PyDict_SetItemString(dict, "__dummy__", Py_None);
00400 PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
00401 return 0;
00402 }
00403 }
00404
00405
00406 PyObject *
00407 PP_Load_Module(const char *modname)
00408 {
00409
00410
00411
00412
00413
00414
00415
00416
00417 PyObject *module, *sysmods;
00418 modname = PP_Init(modname);
00419
00420 if (strcmp(modname, "__main__") == 0)
00421 return PyImport_AddModule(modname);
00422
00423 sysmods = PyImport_GetModuleDict();
00424 module = PyDict_GetItemString(sysmods, modname);
00425
00426 if (module != NULL &&
00427 PyModule_Check(module) &&
00428 PyDict_GetItemString(PyModule_GetDict(module), "__dummy__")) {
00429 return module;
00430 }
00431 else
00432 if (PP_RELOAD && module != NULL && PyModule_Check(module)) {
00433 module = PyImport_ReloadModule(module);
00434 Py_XDECREF(module);
00435 return module;
00436 }
00437 else {
00438 module = PyImport_ImportModule(modname);
00439 Py_XDECREF(module);
00440 return module;
00441 }
00442 }
00443
00444
00445 PyObject *
00446 PP_Load_Attribute(const char *modname, const char *attrname)
00447 {
00448 PyObject *module;
00449 modname = PP_Init(modname);
00450 module = PP_Load_Module(modname);
00451 if (module == NULL)
00452 return NULL;
00453 return PyObject_GetAttrString(module, attrname);
00454 }
00455
00456
00457
00458 int
00459 PP_Run_Command_Line(const char *prompt)
00460 {
00461 int res;
00462 Py_Initialize();
00463 if (prompt != NULL)
00464 #if defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX)
00465 printf("[%s <Use Ctrl-D (i.e. EOF) to exit.>]\n", prompt);
00466 #elif defined (FC_OS_WIN32)
00467 printf("[%s <Use Ctrl-Z plus Return to exit.>]\n", prompt);
00468 #endif
00469 res = PyRun_InteractiveLoop(stdin, "<stdin>");
00470 return res;
00471 }
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 int
00484 PP_Run_Codestr(PPStringModes mode, const char *code,
00485 const char *modname,
00486 const char *resfmt, void *cresult)
00487 {
00488
00489 int parse_mode;
00490 PyObject *module, *dict, *presult;
00491
00492 module = PP_Load_Module(modname);
00493 if (module == NULL)
00494 return -1;
00495 dict = PyModule_GetDict(module);
00496 if (dict == NULL)
00497 return -1;
00498
00499 parse_mode = (mode == PP_EXPRESSION ? Py_eval_input : Py_file_input);
00500 if (PP_DEBUG)
00501 presult = PP_Debug_Codestr(mode, code, dict);
00502 else
00503 presult = PyRun_String(code, parse_mode, dict, dict);
00504
00505 if (mode == PP_STATEMENT) {
00506 int result = (presult == NULL? -1 : 0);
00507 Py_XDECREF(presult);
00508 return result;
00509 }
00510 return PP_Convert_Result(presult, resfmt, cresult);
00511 }
00512
00513
00514 PyObject *
00515 PP_Compile_Codestr(PPStringModes mode,
00516 const char *codestr)
00517 {
00518 int start;
00519 Py_Initialize();
00520 switch (mode) {
00521 case PP_STATEMENT:
00522 start = Py_file_input; break;
00523 case PP_EXPRESSION:
00524 start = Py_eval_input; break;
00525 default:
00526 start = Py_single_input;
00527 }
00528 return Py_CompileString(codestr, "<PP_Compile_Codestr>", start);
00529 }
00530
00531
00532 int
00533 PP_Run_Bytecode(PyObject *codeobj,
00534 const char *modname,
00535 const char *resfmt, void *restarget)
00536 {
00537 PyObject *presult, *module, *dict;
00538
00539 if (! PyCode_Check(codeobj))
00540 return -1;
00541 module = PP_Load_Module(modname);
00542 if (module == NULL)
00543 return -1;
00544 dict = PyModule_GetDict(module);
00545 if (dict == NULL)
00546 return -1;
00547 if (PP_DEBUG)
00548 presult = PP_Debug_Bytecode(codeobj, dict);
00549 else
00550 presult = PyEval_EvalCode((PyCodeObject *)codeobj, dict, dict);
00551 return PP_Convert_Result(presult, resfmt, restarget);
00552 }
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584 static void fixPdbRetval(PyObject *moddict)
00585 { if (PyDict_DelItemString(moddict, "__return__")) PyErr_Clear(); }
00586
00587
00588 PyObject *
00589 PP_Debug_Codestr(PPStringModes mode, const char *codestring, PyObject *moddict)
00590 {
00591 int res;
00592 PyObject *presult;
00593 const char *pdbname = (mode == PP_EXPRESSION ? "runeval" : "run");
00594 fixPdbRetval(moddict);
00595
00596 res = PP_Run_Function(
00597 "pdb", pdbname,
00598 "O", &presult,
00599 "(sOO)", codestring, moddict, moddict);
00600 return (res != 0) ? NULL : presult;
00601 }
00602
00603
00604 PyObject *
00605 PP_Debug_Bytecode(PyObject *codeobject, PyObject *moddict)
00606 {
00607 int res;
00608 PyObject *presult;
00609 fixPdbRetval(moddict);
00610 res = PP_Run_Function(
00611 "pdb", "runeval",
00612 "O", &presult,
00613 "(OOO)", codeobject, moddict, moddict);
00614 return (res != 0) ? NULL : presult;
00615 }
00616
00617
00618
00619