py.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include "config.h"
  2. #ifdef HAVE_PYTHON
  3. # include <Python.h>
  4. #if PY_MAJOR_VERSION >= 3
  5. #include <pthread.h>
  6. #endif /* PY_MAJOR_VERSION >= 3 */
  7. #endif /* HAVE_PYTHON */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <strings.h>
  12. #include <alloca.h>
  13. #include "pci.h"
  14. #include "debug.h"
  15. #include "pcilib.h"
  16. #include "py.h"
  17. #include "error.h"
  18. #ifdef HAVE_PYTHON
  19. #define PCILIB_PYTHON_WRAPPER "pcipywrap"
  20. typedef struct pcilib_script_s pcilib_script_t;
  21. struct pcilib_script_s {
  22. const char *name; /**< Script name */
  23. PyObject *module; /**< PyModule object, contains script enviroment */
  24. UT_hash_handle hh; /**< hash */
  25. };
  26. #if PY_MAJOR_VERSION >= 3
  27. typedef struct pcilib_py_s_thread_control {
  28. pthread_t pth;
  29. pthread_cond_t cond_finished;
  30. pthread_mutex_t cond_finished_lock;
  31. } pcilib_py_s_thread_control;
  32. #endif /* PY_MAJOR_VERSION < 3 */
  33. struct pcilib_py_s {
  34. int finalyze; /**< Indicates, that we are initialized from wrapper and should not destroy Python resources in destructor */
  35. PyObject *main_module; /**< Main interpreter */
  36. PyObject *global_dict; /**< Dictionary of main interpreter */
  37. PyObject *pcilib_pywrap; /**< pcilib wrapper module */
  38. pcilib_script_t *script_hash; /**< Hash with loaded scripts */
  39. #if PY_MAJOR_VERSION >= 3
  40. pcilib_py_s_thread_control *thr_ctl; /**< Controller for Python main loop thread for Python 3 */
  41. #endif /* PY_MAJOR_VERSION < 3 */
  42. };
  43. #endif /* HAVE_PYTHON */
  44. void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, ...) {
  45. va_list va;
  46. const char *type = NULL;
  47. const char *val = NULL;
  48. #ifdef HAVE_PYTHON
  49. PyGILState_STATE gstate;
  50. PyObject *pytype = NULL;
  51. PyObject *pyval = NULL;
  52. PyObject *pystr = NULL;
  53. PyObject *pytraceback = NULL;
  54. gstate = PyGILState_Ensure();
  55. if (PyErr_Occurred()) {
  56. PyErr_Fetch(&pytype, &pyval, &pytraceback);
  57. PyErr_NormalizeException(&pytype, &pyval, &pytraceback);
  58. if (pyval) pystr = PyObject_Str(pyval);
  59. #if PY_MAJOR_VERSION >= 3
  60. if (pytype) {
  61. if (PyUnicode_Check(pytype))
  62. type = PyUnicode_AsUTF8(pytype);
  63. else
  64. type = PyExceptionClass_Name(pytype);
  65. }
  66. if (pystr) {
  67. val = PyUnicode_AsUTF8(pystr);
  68. }
  69. #else /* PY_MAJOR_VERSION >= 3 */
  70. if (pytype) {
  71. if (PyString_Check(pytype))
  72. type = PyString_AsString(pytype);
  73. else
  74. type = PyExceptionClass_Name(pytype);
  75. }
  76. if (pystr) {
  77. val = PyString_AsString(pystr);
  78. }
  79. #endif /*PY_MAJOR_VERSION >= 3*/
  80. }
  81. PyGILState_Release(gstate);
  82. #endif /* HAVE_PYTHON */
  83. va_start(va, msg);
  84. if (type) {
  85. char *str;
  86. size_t len = 32;
  87. if (msg) len += strlen(msg);
  88. if (type) len += strlen(type);
  89. if (val) len += strlen(val);
  90. str = alloca(len * sizeof(char));
  91. if (str) {
  92. if (msg&&val)
  93. sprintf(str, "%s <%s: %s>", msg, type, val);
  94. else if (msg)
  95. sprintf(str, "%s <%s>", msg, type);
  96. else if (val)
  97. sprintf(str, "Python error %s: %s", type, val);
  98. else
  99. sprintf(str, "Python error %s", type);
  100. pcilib_log_vmessage(file, line, flags, prio, str, va);
  101. }
  102. } else {
  103. pcilib_log_vmessage(file, line, flags, prio, msg, va);
  104. }
  105. va_end(va);
  106. #ifdef HAVE_PYTHON
  107. if (pystr) Py_DECREF(pystr);
  108. if (pytype) Py_DECREF(pytype);
  109. if (pyval) Py_DECREF(pyval);
  110. if (pytraceback) Py_DECREF(pytraceback);
  111. #endif /* HAVE_PYTHON */
  112. }
  113. #ifdef HAVE_PYTHON
  114. #if PY_MAJOR_VERSION >= 3
  115. void *pcilib_py_run_side_thread(void *arg) {
  116. pcilib_t *ctx = (pcilib_t*)(arg);
  117. //Initializing python
  118. Py_Initialize();
  119. PyEval_InitThreads();
  120. PyEval_ReleaseLock();
  121. //send initialization finish signal
  122. pthread_cond_signal(&(ctx->py->thr_ctl->cond_finished));
  123. pthread_mutex_unlock(&(ctx->py->thr_ctl->cond_finished_lock));
  124. pthread_mutex_destroy(&(ctx->py->thr_ctl->cond_finished_lock));
  125. //wait untill finish signal
  126. pthread_mutex_lock(&(ctx->py->thr_ctl->cond_finished_lock));
  127. pthread_cond_wait(&(ctx->py->thr_ctl->cond_finished),
  128. &(ctx->py->thr_ctl->cond_finished_lock));
  129. return NULL;
  130. }
  131. #endif /* PY_MAJOR_VERSION < 3 */
  132. #endif /* HAVE_PYTHON */
  133. int pcilib_init_py(pcilib_t *ctx) {
  134. #ifdef HAVE_PYTHON
  135. ctx->py = (pcilib_py_t*)malloc(sizeof(pcilib_py_t));
  136. if (!ctx->py) return PCILIB_ERROR_MEMORY;
  137. memset(ctx->py, 0, sizeof(pcilib_py_t));
  138. if(!Py_IsInitialized()) {
  139. #if PY_MAJOR_VERSION >= 3
  140. //create thread controller
  141. ctx->py->thr_ctl = malloc(sizeof(pcilib_py_s_thread_control));
  142. if(!ctx->py->thr_ctl) return PCILIB_ERROR_MEMORY;
  143. memset(ctx->py->thr_ctl, 0, sizeof(pcilib_py_s_thread_control));
  144. //create side thread with python main loop
  145. pthread_create(&(ctx->py->thr_ctl->pth), NULL, pcilib_py_run_side_thread, ctx);
  146. pthread_mutex_lock(&(ctx->py->thr_ctl->cond_finished_lock));
  147. //wait until Python initializes
  148. pthread_cond_wait(&(ctx->py->thr_ctl->cond_finished),
  149. &(ctx->py->thr_ctl->cond_finished_lock));
  150. #else /* PY_MAJOR_VERSION < 3 */
  151. Py_Initialize();
  152. // Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads
  153. PyEval_InitThreads();
  154. PyEval_ReleaseLock();
  155. #endif /* PY_MAJOR_VERSION < 3 */
  156. ctx->py->finalyze = 1;
  157. }
  158. PyGILState_STATE gstate = PyGILState_Ensure();
  159. ctx->py->main_module = PyImport_AddModule("__parser__");
  160. if (!ctx->py->main_module) {
  161. pcilib_python_warning("Error importing python parser");
  162. PyGILState_Release(gstate);
  163. return PCILIB_ERROR_FAILED;
  164. }
  165. ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module);
  166. if (!ctx->py->global_dict) {
  167. pcilib_python_warning("Error locating global python dictionary");
  168. PyGILState_Release(gstate);
  169. return PCILIB_ERROR_FAILED;
  170. }
  171. PyObject *pywrap = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER);
  172. if (!pywrap) {
  173. pcilib_python_warning("Error importing pcilib python wrapper");
  174. PyGILState_Release(gstate);
  175. return PCILIB_ERROR_FAILED;
  176. }
  177. PyObject *mod_name = PyUnicode_FromString(PCILIB_PYTHON_WRAPPER);
  178. PyObject *pyctx = PyCapsule_New(ctx, "pcilib", NULL);
  179. ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL);
  180. Py_XDECREF(pyctx);
  181. Py_XDECREF(mod_name);
  182. if (!ctx->py->pcilib_pywrap) {
  183. pcilib_python_warning("Error initializing python wrapper");
  184. PyGILState_Release(gstate);
  185. return PCILIB_ERROR_FAILED;
  186. }
  187. PyGILState_Release(gstate);
  188. #endif /* HAVE_PYTHON */
  189. return 0;
  190. }
  191. int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) {
  192. #ifdef HAVE_PYTHON
  193. int err = 0;
  194. PyObject *pypath, *pynewdir;
  195. PyObject *pydict, *pystr, *pyret = NULL;
  196. char *script_dir;
  197. if (!ctx->py) return 0;
  198. const char *model_dir = getenv("PCILIB_MODEL_DIR");
  199. if (!model_dir) model_dir = PCILIB_MODEL_DIR;
  200. if (!dir) dir = ctx->model;
  201. if (*dir == '/') {
  202. script_dir = (char*)dir;
  203. } else {
  204. script_dir = alloca(strlen(model_dir) + strlen(dir) + 2);
  205. if (!script_dir) return PCILIB_ERROR_MEMORY;
  206. sprintf(script_dir, "%s/%s", model_dir, dir);
  207. }
  208. PyGILState_STATE gstate = PyGILState_Ensure();
  209. pypath = PySys_GetObject("path");
  210. if (!pypath) {
  211. pcilib_python_warning("Can't get python path");
  212. PyGILState_Release(gstate);
  213. return PCILIB_ERROR_FAILED;
  214. }
  215. pynewdir = PyUnicode_FromString(script_dir);
  216. if (!pynewdir) {
  217. pcilib_python_warning("Can't create python string");
  218. PyGILState_Release(gstate);
  219. return PCILIB_ERROR_MEMORY;
  220. }
  221. // Checking if the directory already in the path?
  222. pydict = PyDict_New();
  223. if (pydict) {
  224. pystr = PyUnicode_FromString("cur");
  225. if (pystr) {
  226. PyDict_SetItem(pydict, pystr, pynewdir);
  227. Py_DECREF(pystr);
  228. }
  229. pystr = PyUnicode_FromString("path");
  230. if (pystr) {
  231. PyDict_SetItem(pydict, pystr, pypath);
  232. Py_DECREF(pystr);
  233. }
  234. pyret = PyRun_String("cur in path", Py_eval_input, ctx->py->global_dict, pydict);
  235. Py_DECREF(pydict);
  236. }
  237. if ((pyret == Py_False)&&(PyList_Append(pypath, pynewdir) == -1))
  238. err = PCILIB_ERROR_FAILED;
  239. if (pyret) Py_DECREF(pyret);
  240. Py_DECREF(pynewdir);
  241. if (err) {
  242. pcilib_python_warning("Can't add directory (%s) to python path", script_dir);
  243. PyGILState_Release(gstate);
  244. return err;
  245. }
  246. PyGILState_Release(gstate);
  247. #endif /* HAVE_PYTHON */
  248. return 0;
  249. }
  250. void pcilib_free_py(pcilib_t *ctx) {
  251. #ifdef HAVE_PYTHON
  252. int finalyze = 0;
  253. PyGILState_STATE gstate;
  254. #if PY_MAJOR_VERSION >= 3
  255. pcilib_py_s_thread_control *thr_ctl = ctx->py->thr_ctl;
  256. #endif /* PY_MAJOR_VERSION < 3 */
  257. if (ctx->py) {
  258. if (ctx->py->finalyze) finalyze = 1;
  259. if (ctx->py->script_hash) {
  260. pcilib_script_t *script, *script_tmp;
  261. HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) {
  262. Py_DECREF(script->module);
  263. HASH_DEL(ctx->py->script_hash, script);
  264. free(script);
  265. }
  266. ctx->py->script_hash = NULL;
  267. }
  268. if (ctx->py->pcilib_pywrap) {
  269. gstate = PyGILState_Ensure();
  270. Py_DECREF(ctx->py->pcilib_pywrap);
  271. PyGILState_Release(gstate);
  272. }
  273. free(ctx->py);
  274. ctx->py = NULL;
  275. }
  276. if (finalyze) {
  277. #if PY_MAJOR_VERSION >= 3
  278. //stop python side thread
  279. pthread_cond_signal(&(thr_ctl->cond_finished));
  280. pthread_mutex_unlock(&(thr_ctl->cond_finished_lock));
  281. pthread_join(thr_ctl->pth, NULL);
  282. //free python
  283. //must be finalized in main thread to correctly stop python threading
  284. PyGILState_Ensure();
  285. Py_Finalize();
  286. //destroy thread controllers
  287. pthread_mutex_destroy(&(thr_ctl->cond_finished_lock));
  288. pthread_cond_destroy(&(thr_ctl->cond_finished));
  289. free(thr_ctl);
  290. #else /* PY_MAJOR_VERSION < 3 */
  291. Py_Finalize();
  292. #endif /* PY_MAJOR_VERSION < 3 */
  293. }
  294. #endif /* HAVE_PYTHON */
  295. }
  296. int pcilib_py_load_script(pcilib_t *ctx, const char *script_name) {
  297. #ifdef HAVE_PYTHON
  298. PyObject* pymodule;
  299. pcilib_script_t *module = NULL;
  300. PyGILState_STATE gstate;
  301. if (!ctx->py) return 0;
  302. char *module_name = strdupa(script_name);
  303. if (!module_name) return PCILIB_ERROR_MEMORY;
  304. char *py = strrchr(module_name, '.');
  305. if ((!py)||(strcasecmp(py, ".py"))) {
  306. pcilib_error("Invalid script name (%s) is specified", script_name);
  307. return PCILIB_ERROR_INVALID_ARGUMENT;
  308. }
  309. *py = 0;
  310. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  311. if (module) return 0;
  312. gstate = PyGILState_Ensure();
  313. pymodule = PyImport_ImportModule(module_name);
  314. if (!pymodule) {
  315. pcilib_python_error("Error importing script (%s)", script_name);
  316. PyGILState_Release(gstate);
  317. return PCILIB_ERROR_FAILED;
  318. }
  319. PyGILState_Release(gstate);
  320. module = (pcilib_script_t*)malloc(sizeof(pcilib_script_t));
  321. if (!module) return PCILIB_ERROR_MEMORY;
  322. module->module = pymodule;
  323. module->name = script_name;
  324. HASH_ADD_KEYPTR(hh, ctx->py->script_hash, module->name, strlen(module->name), module);
  325. #endif /* HAVE_PYTHON */
  326. return 0;
  327. }
  328. int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_name, pcilib_access_mode_t *mode_ret) {
  329. pcilib_access_mode_t mode = 0;
  330. #ifdef HAVE_PYTHON
  331. PyObject *dict;
  332. PyObject *pystr;
  333. pcilib_script_t *module;
  334. PyGILState_STATE gstate;
  335. if (!ctx->py) {
  336. if (mode_ret) *mode_ret = mode;
  337. return 0;
  338. }
  339. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  340. if(!module) {
  341. pcilib_error("Script (%s) is not loaded yet", script_name);
  342. return PCILIB_ERROR_NOTFOUND;
  343. }
  344. gstate = PyGILState_Ensure();
  345. dict = PyModule_GetDict(module->module);
  346. if (!dict) {
  347. pcilib_python_error("Error getting dictionary for script (%s)", script_name);
  348. PyGILState_Release(gstate);
  349. return PCILIB_ERROR_FAILED;
  350. }
  351. pystr = PyUnicode_FromString("read_from_register");
  352. if (pystr) {
  353. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
  354. Py_DECREF(pystr);
  355. }
  356. pystr = PyUnicode_FromString("write_to_register");
  357. if (pystr) {
  358. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W;
  359. Py_DECREF(pystr);
  360. }
  361. PyGILState_Release(gstate);
  362. #endif /* HAVE_PYTHON */
  363. if (mode_ret) *mode_ret = mode;
  364. return 0;
  365. }
  366. pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val, int *ret) {
  367. #ifdef HAVE_PYTHON
  368. int err = 0;
  369. PyObject *res = NULL;
  370. PyGILState_STATE gstate;
  371. long ival;
  372. double fval;
  373. if (!ctx->py) return NULL;
  374. gstate = PyGILState_Ensure();
  375. switch(val->type) {
  376. case PCILIB_TYPE_LONG:
  377. ival = pcilib_get_value_as_int(ctx, val, &err);
  378. if (!err) res = (PyObject*)PyLong_FromLong(ival);
  379. break;
  380. case PCILIB_TYPE_DOUBLE:
  381. fval = pcilib_get_value_as_float(ctx, val, &err);
  382. if (!err) res = (PyObject*)PyFloat_FromDouble(fval);
  383. break;
  384. default:
  385. PyGILState_Release(gstate);
  386. pcilib_error("Can't convert pcilib value of type (%lu) to PyObject", val->type);
  387. if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
  388. return NULL;
  389. }
  390. PyGILState_Release(gstate);
  391. if (err) {
  392. if (ret) *ret = err;
  393. return NULL;
  394. } else if (!res) {
  395. if (ret) *ret = PCILIB_ERROR_MEMORY;
  396. return res;
  397. }
  398. if (ret) *ret = 0;
  399. return res;
  400. #else /* HAVE_PYTHON */
  401. pcilib_error("Python is not supported");
  402. return NULL;
  403. #endif /* HAVE_PYTHON */
  404. }
  405. int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py_object *pval) {
  406. #ifdef HAVE_PYTHON
  407. int err = 0;
  408. PyObject *pyval = (PyObject*)pval;
  409. PyGILState_STATE gstate;
  410. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  411. gstate = PyGILState_Ensure();
  412. if (PyLong_Check(pyval)) {
  413. err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
  414. } else if (PyFloat_Check(pyval)) {
  415. err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval));
  416. #if PY_MAJOR_VERSION < 3
  417. } else if (PyInt_Check(pyval)) {
  418. err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
  419. } else if (PyString_Check(pyval)) {
  420. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
  421. } else if (PyUnicode_Check(pyval)) {
  422. PyObject *buf = PyUnicode_AsASCIIString(pyval);
  423. if (buf) {
  424. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
  425. Py_DecRef(buf);
  426. } else {
  427. err = PCILIB_ERROR_FAILED;
  428. }
  429. #else /* PY_MAJOR_VERSION < 3 */
  430. } else if (PyUnicode_Check(pyval)) {
  431. err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval));
  432. #endif /* PY_MAJOR_VERSION < 3 */
  433. } else {
  434. PyGILState_Release(gstate);
  435. pcilib_error("Can't convert PyObject to polymorphic pcilib value");
  436. return PCILIB_ERROR_NOTSUPPORTED;
  437. }
  438. PyGILState_Release(gstate);
  439. return err;
  440. #else /* HAVE_PYTHON */
  441. pcilib_error("Python is not supported");
  442. return PCILIB_ERROR_NOTSUPPORTED;
  443. #endif /* HAVE_PYTHON */
  444. }
  445. #ifdef HAVE_PYTHON
  446. static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  447. int i;
  448. int err = 0;
  449. pcilib_value_t val = {0};
  450. pcilib_register_value_t regval;
  451. char save;
  452. char *reg, *cur;
  453. size_t offset = 0;
  454. size_t size;
  455. char *src;
  456. char *dst;
  457. size_t max_repl = 2 + 2 * sizeof(pcilib_value_t); // the text representation of largest integer
  458. if (value) {
  459. err = pcilib_copy_value(ctx, &val, value);
  460. if (err) return NULL;
  461. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  462. if (err) return NULL;
  463. if (strlen(val.sval) > max_repl)
  464. max_repl = strlen(val.sval);
  465. }
  466. size = ((max_repl + 1) / 3 + 1) * strlen(codestr); // minimum register length is 3 symbols ($a + delimiter space), it is replaces with (max_repl+1) symbols
  467. src = strdup(codestr);
  468. dst = (char*)malloc(size); // allocating maximum required space
  469. if ((!src)||(!dst)) {
  470. if (src) free(src);
  471. if (dst) free(dst);
  472. pcilib_error("Failed to allocate memory for string formulas");
  473. return NULL;
  474. }
  475. cur = src;
  476. reg = strchr(src, '$');
  477. while (reg) {
  478. strcpy(dst + offset, cur);
  479. offset += reg - cur;
  480. // find the end of the register name
  481. reg++;
  482. if (*reg == '{') {
  483. reg++;
  484. for (i = 0; (reg[i])&&(reg[i] != '}'); i++);
  485. if (!reg[i]) {
  486. pcilib_error("Python formula (%s) contains unterminated variable reference", codestr);
  487. err = PCILIB_ERROR_INVALID_DATA;
  488. break;
  489. }
  490. } else {
  491. for (i = 0; isalnum(reg[i])||(reg[i] == '_'); i++);
  492. }
  493. save = reg[i];
  494. reg[i] = 0;
  495. // determine replacement value
  496. if (!strcasecmp(reg, "value")) {
  497. if (!value) {
  498. pcilib_error("Python formula (%s) relies on the value of register, but it is not provided", codestr);
  499. err = PCILIB_ERROR_INVALID_REQUEST;
  500. break;
  501. }
  502. strcpy(dst + offset, val.sval);
  503. } else {
  504. if (*reg == '/') {
  505. pcilib_value_t val = {0};
  506. err = pcilib_get_property(ctx, reg, &val);
  507. if (err) break;
  508. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  509. if (err) break;
  510. sprintf(dst + offset, "%s", val.sval);
  511. } else {
  512. err = pcilib_read_register(ctx, NULL, reg, &regval);
  513. if (err) break;
  514. sprintf(dst + offset, "0x%lx", regval);
  515. }
  516. }
  517. offset += strlen(dst + offset);
  518. if (save == '}') i++;
  519. else reg[i] = save;
  520. // Advance to the next register if any
  521. cur = reg + i;
  522. reg = strchr(cur, '$');
  523. }
  524. strcpy(dst + offset, cur);
  525. free(src);
  526. if (err) {
  527. free(dst);
  528. return NULL;
  529. }
  530. return dst;
  531. }
  532. #endif /* HAVE_PYTHON */
  533. int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  534. #ifdef HAVE_PYTHON
  535. int err;
  536. PyGILState_STATE gstate;
  537. char *code;
  538. PyObject* obj;
  539. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  540. code = pcilib_py_parse_string(ctx, codestr, value);
  541. if (!code) {
  542. pcilib_error("Failed to parse registers in the code: %s", codestr);
  543. return PCILIB_ERROR_FAILED;
  544. }
  545. gstate = PyGILState_Ensure();
  546. obj = PyRun_String(code, Py_eval_input, ctx->py->global_dict, ctx->py->global_dict);
  547. PyGILState_Release(gstate);
  548. if (!obj) {
  549. pcilib_error("Failed to run the Python code: %s", code);
  550. free(code);
  551. return PCILIB_ERROR_FAILED;
  552. }
  553. pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code);
  554. err = pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
  555. Py_DECREF(obj);
  556. free(code);
  557. return err;
  558. #else /* HAVE_PYTHON */
  559. pcilib_error("Current build not support python.");
  560. return PCILIB_ERROR_NOTAVAILABLE;
  561. #endif /* HAVE_PYTHON */
  562. }
  563. int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func_name, pcilib_value_t *val) {
  564. #ifdef HAVE_PYTHON
  565. int err = 0;
  566. PyObject *pyfunc;
  567. PyObject *pyval = NULL, *pyret;
  568. pcilib_script_t *module = NULL;
  569. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  570. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  571. if (!module) {
  572. pcilib_error("Script (%s) is not loaded", script_name);
  573. return PCILIB_ERROR_NOTFOUND;
  574. }
  575. if (val) {
  576. pyval = pcilib_get_value_as_pyobject(ctx, val, &err);
  577. if (err) return err;
  578. }
  579. PyGILState_STATE gstate = PyGILState_Ensure();
  580. pyfunc = PyUnicode_FromString(func_name);
  581. if (!pyfunc) {
  582. if (pyval) Py_DECREF(pyval);
  583. PyGILState_Release(gstate);
  584. return PCILIB_ERROR_MEMORY;
  585. }
  586. pyret = PyObject_CallMethodObjArgs(module->module, pyfunc, ctx->py->pcilib_pywrap, pyval, NULL);
  587. Py_DECREF(pyfunc);
  588. Py_DECREF(pyval);
  589. if (!pyret) {
  590. PyGILState_Release(gstate);
  591. pcilib_python_error("Error executing function (%s) of python script (%s)", func_name, script_name);
  592. return PCILIB_ERROR_FAILED;
  593. }
  594. if ((val)&&(pyret != Py_None))
  595. err = pcilib_set_value_from_pyobject(ctx, val, pyret);
  596. Py_DECREF(pyret);
  597. PyGILState_Release(gstate);
  598. return err;
  599. #else /* HAVE_PYTHON */
  600. pcilib_error("Python is not supported");
  601. return PCILIB_ERROR_NOTSUPPORTED;
  602. #endif /* HAVE_PYTHON */
  603. }