py.c 17 KB

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