py.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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_warning("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_warning("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_warning("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_warning("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. if (!ctx->py) return 0;
  146. const char *model_dir = getenv("PCILIB_MODEL_DIR");
  147. if (!model_dir) model_dir = PCILIB_MODEL_DIR;
  148. if (!dir) dir = ctx->model;
  149. if (*dir == '/') {
  150. script_dir = (char*)dir;
  151. } else {
  152. script_dir = alloca(strlen(model_dir) + strlen(dir) + 2);
  153. if (!script_dir) return PCILIB_ERROR_MEMORY;
  154. sprintf(script_dir, "%s/%s", model_dir, dir);
  155. }
  156. pypath = PySys_GetObject("path");
  157. if (!pypath) {
  158. pcilib_python_warning("Can't get python path");
  159. return PCILIB_ERROR_FAILED;
  160. }
  161. pynewdir = PyUnicode_FromString(script_dir);
  162. if (!pynewdir) {
  163. pcilib_python_warning("Can't create python string");
  164. return PCILIB_ERROR_MEMORY;
  165. }
  166. // Checking if the directory already in the path?
  167. pydict = PyDict_New();
  168. if (pydict) {
  169. pystr = PyUnicode_FromString("cur");
  170. if (pystr) {
  171. PyDict_SetItem(pydict, pystr, pynewdir);
  172. Py_DECREF(pystr);
  173. }
  174. pystr = PyUnicode_FromString("path");
  175. if (pystr) {
  176. PyDict_SetItem(pydict, pystr, pypath);
  177. Py_DECREF(pystr);
  178. }
  179. pyret = PyRun_String("cur in path", Py_eval_input, ctx->py->global_dict, pydict);
  180. Py_DECREF(pydict);
  181. }
  182. if ((pyret == Py_False)&&(PyList_Append(pypath, pynewdir) == -1))
  183. err = PCILIB_ERROR_FAILED;
  184. if (pyret) Py_DECREF(pyret);
  185. Py_DECREF(pynewdir);
  186. if (err) {
  187. pcilib_python_warning("Can't add directory (%s) to python path", script_dir);
  188. return err;
  189. }
  190. #endif /* HAVE_PYTHON */
  191. return 0;
  192. }
  193. void pcilib_free_py(pcilib_t *ctx) {
  194. #ifdef HAVE_PYTHON
  195. int finalyze = 0;
  196. if (ctx->py) {
  197. if (ctx->py->finalyze) finalyze = 1;
  198. if (ctx->py->script_hash) {
  199. pcilib_script_t *script, *script_tmp;
  200. HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) {
  201. Py_DECREF(script->module);
  202. HASH_DEL(ctx->py->script_hash, script);
  203. free(script);
  204. }
  205. ctx->py->script_hash = NULL;
  206. }
  207. if (ctx->py->pcilib_pywrap)
  208. Py_DECREF(ctx->py->pcilib_pywrap);
  209. free(ctx->py);
  210. ctx->py = NULL;
  211. }
  212. if (finalyze)
  213. Py_Finalize();
  214. #endif /* HAVE_PYTHON */
  215. }
  216. int pcilib_py_load_script(pcilib_t *ctx, const char *script_name) {
  217. #ifdef HAVE_PYTHON
  218. PyObject* pymodule;
  219. pcilib_script_t *module = NULL;
  220. if (!ctx->py) return 0;
  221. char *module_name = strdupa(script_name);
  222. if (!module_name) return PCILIB_ERROR_MEMORY;
  223. char *py = strrchr(module_name, '.');
  224. if ((!py)||(strcasecmp(py, ".py"))) {
  225. pcilib_error("Invalid script name (%s) is specified", script_name);
  226. return PCILIB_ERROR_INVALID_ARGUMENT;
  227. }
  228. *py = 0;
  229. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  230. if (module) return 0;
  231. pymodule = PyImport_ImportModule(module_name);
  232. if (!pymodule) {
  233. pcilib_python_error("Error importing script (%s)", script_name);
  234. return PCILIB_ERROR_FAILED;
  235. }
  236. module = (pcilib_script_t*)malloc(sizeof(pcilib_script_t));
  237. if (!module) return PCILIB_ERROR_MEMORY;
  238. module->module = pymodule;
  239. module->name = script_name;
  240. HASH_ADD_KEYPTR(hh, ctx->py->script_hash, module->name, strlen(module->name), module);
  241. #endif /* HAVE_PYTHON */
  242. return 0;
  243. }
  244. int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_name, pcilib_access_mode_t *mode_ret) {
  245. pcilib_access_mode_t mode = 0;
  246. #ifdef HAVE_PYTHON
  247. PyObject *dict;
  248. PyObject *pystr;
  249. pcilib_script_t *module;
  250. if (!ctx->py) {
  251. if (mode_ret) *mode_ret = mode;
  252. return 0;
  253. }
  254. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  255. if(!module) {
  256. pcilib_error("Script (%s) is not loaded yet", script_name);
  257. return PCILIB_ERROR_NOTFOUND;
  258. }
  259. dict = PyModule_GetDict(module->module);
  260. if (!dict) {
  261. pcilib_python_error("Error getting dictionary for script (%s)", script_name);
  262. return PCILIB_ERROR_FAILED;
  263. }
  264. pystr = PyUnicode_FromString("read_from_register");
  265. if (pystr) {
  266. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
  267. Py_DECREF(pystr);
  268. }
  269. pystr = PyUnicode_FromString("write_to_register");
  270. if (pystr) {
  271. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W;
  272. Py_DECREF(pystr);
  273. }
  274. #endif /* HAVE_PYTHON */
  275. if (mode_ret) *mode_ret = mode;
  276. return 0;
  277. }
  278. pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val, int *ret) {
  279. #ifdef HAVE_PYTHON
  280. int err = 0;
  281. PyObject *res = NULL;
  282. PyGILState_STATE gstate;
  283. long ival;
  284. double fval;
  285. if (!ctx->py) return NULL;
  286. gstate = PyGILState_Ensure();
  287. switch(val->type) {
  288. case PCILIB_TYPE_LONG:
  289. ival = pcilib_get_value_as_int(ctx, val, &err);
  290. if (!err) res = (PyObject*)PyLong_FromLong(ival);
  291. break;
  292. case PCILIB_TYPE_DOUBLE:
  293. fval = pcilib_get_value_as_float(ctx, val, &err);
  294. if (!err) res = (PyObject*)PyFloat_FromDouble(fval);
  295. break;
  296. default:
  297. PyGILState_Release(gstate);
  298. pcilib_error("Can't convert pcilib value of type (%lu) to PyObject", val->type);
  299. if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
  300. return NULL;
  301. }
  302. PyGILState_Release(gstate);
  303. if (err) {
  304. if (ret) *ret = err;
  305. return NULL;
  306. } else if (!res) {
  307. if (ret) *ret = PCILIB_ERROR_MEMORY;
  308. return res;
  309. }
  310. if (ret) *ret = 0;
  311. return res;
  312. #else /* HAVE_PYTHON */
  313. pcilib_error("Python is not supported");
  314. return NULL;
  315. #endif /* HAVE_PYTHON */
  316. }
  317. int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py_object *pval) {
  318. #ifdef HAVE_PYTHON
  319. int err = 0;
  320. PyObject *pyval = (PyObject*)pval;
  321. PyGILState_STATE gstate;
  322. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  323. gstate = PyGILState_Ensure();
  324. if (PyLong_Check(pyval)) {
  325. err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
  326. } else if (PyFloat_Check(pyval)) {
  327. err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval));
  328. #if PY_MAJOR_VERSION < 3
  329. } else if (PyInt_Check(pyval)) {
  330. err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
  331. } else if (PyString_Check(pyval)) {
  332. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
  333. } else if (PyUnicode_Check(pyval)) {
  334. PyObject *buf = PyUnicode_AsASCIIString(pyval);
  335. if (buf) {
  336. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
  337. Py_DecRef(buf);
  338. } else {
  339. err = PCILIB_ERROR_FAILED;
  340. }
  341. #else /* PY_MAJOR_VERSION < 3 */
  342. } else if (PyUnicode_Check(pyval)) {
  343. err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval));
  344. #endif /* PY_MAJOR_VERSION < 3 */
  345. } else {
  346. PyGILState_Release(gstate);
  347. pcilib_error("Can't convert PyObject to polymorphic pcilib value");
  348. return PCILIB_ERROR_NOTSUPPORTED;
  349. }
  350. PyGILState_Release(gstate);
  351. return err;
  352. #else /* HAVE_PYTHON */
  353. pcilib_error("Python is not supported");
  354. return PCILIB_ERROR_NOTSUPPORTED;
  355. #endif /* HAVE_PYTHON */
  356. }
  357. #ifdef HAVE_PYTHON
  358. static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  359. int i;
  360. int err = 0;
  361. pcilib_value_t val = {0};
  362. pcilib_register_value_t regval;
  363. char save;
  364. char *reg, *cur;
  365. size_t offset = 0;
  366. size_t size;
  367. char *src;
  368. char *dst;
  369. size_t max_repl = 2 + 2 * sizeof(pcilib_value_t); // the text representation of largest integer
  370. if (value) {
  371. err = pcilib_copy_value(ctx, &val, value);
  372. if (err) return NULL;
  373. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  374. if (err) return NULL;
  375. if (strlen(val.sval) > max_repl)
  376. max_repl = strlen(val.sval);
  377. }
  378. 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
  379. src = strdup(codestr);
  380. dst = (char*)malloc(size); // allocating maximum required space
  381. if ((!src)||(!dst)) {
  382. if (src) free(src);
  383. if (dst) free(dst);
  384. pcilib_error("Failed to allocate memory for string formulas");
  385. return NULL;
  386. }
  387. cur = src;
  388. reg = strchr(src, '$');
  389. while (reg) {
  390. strcpy(dst + offset, cur);
  391. offset += reg - cur;
  392. // find the end of the register name
  393. reg++;
  394. if (*reg == '{') {
  395. reg++;
  396. for (i = 0; (reg[i])&&(reg[i] != '}'); i++);
  397. if (!reg[i]) {
  398. pcilib_error("Python formula (%s) contains unterminated variable reference", codestr);
  399. err = PCILIB_ERROR_INVALID_DATA;
  400. break;
  401. }
  402. } else {
  403. for (i = 0; isalnum(reg[i])||(reg[i] == '_'); i++);
  404. }
  405. save = reg[i];
  406. reg[i] = 0;
  407. // determine replacement value
  408. if (!strcasecmp(reg, "value")) {
  409. if (!value) {
  410. pcilib_error("Python formula (%s) relies on the value of register, but it is not provided", codestr);
  411. err = PCILIB_ERROR_INVALID_REQUEST;
  412. break;
  413. }
  414. strcpy(dst + offset, val.sval);
  415. } else {
  416. if (*reg == '/') {
  417. pcilib_value_t val = {0};
  418. err = pcilib_get_property(ctx, reg, &val);
  419. if (err) break;
  420. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  421. if (err) break;
  422. sprintf(dst + offset, "%s", val.sval);
  423. } else {
  424. err = pcilib_read_register(ctx, NULL, reg, &regval);
  425. if (err) break;
  426. sprintf(dst + offset, "0x%lx", regval);
  427. }
  428. }
  429. offset += strlen(dst + offset);
  430. if (save == '}') i++;
  431. else reg[i] = save;
  432. // Advance to the next register if any
  433. cur = reg + i;
  434. reg = strchr(cur, '$');
  435. }
  436. strcpy(dst + offset, cur);
  437. free(src);
  438. if (err) {
  439. free(dst);
  440. return NULL;
  441. }
  442. return dst;
  443. }
  444. #endif /* HAVE_PYTHON */
  445. int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  446. #ifdef HAVE_PYTHON
  447. int err;
  448. PyGILState_STATE gstate;
  449. char *code;
  450. PyObject* obj;
  451. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  452. code = pcilib_py_parse_string(ctx, codestr, value);
  453. if (!code) {
  454. pcilib_error("Failed to parse registers in the code: %s", codestr);
  455. return PCILIB_ERROR_FAILED;
  456. }
  457. gstate = PyGILState_Ensure();
  458. obj = PyRun_String(code, Py_eval_input, ctx->py->global_dict, ctx->py->global_dict);
  459. PyGILState_Release(gstate);
  460. if (!obj) {
  461. pcilib_error("Failed to run the Python code: %s", code);
  462. free(code);
  463. return PCILIB_ERROR_FAILED;
  464. }
  465. pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code);
  466. err = pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
  467. Py_DECREF(obj);
  468. free(code);
  469. return err;
  470. #else /* HAVE_PYTHON */
  471. pcilib_error("Current build not support python.");
  472. return PCILIB_ERROR_NOTAVAILABLE;
  473. #endif /* HAVE_PYTHON */
  474. }
  475. int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func_name, pcilib_value_t *val) {
  476. #ifdef HAVE_PYTHON
  477. int err = 0;
  478. PyObject *pyfunc;
  479. PyObject *pyval = NULL, *pyret;
  480. pcilib_script_t *module = NULL;
  481. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  482. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  483. if (!module) {
  484. pcilib_error("Script (%s) is not loaded", script_name);
  485. return PCILIB_ERROR_NOTFOUND;
  486. }
  487. if (val) {
  488. pyval = pcilib_get_value_as_pyobject(ctx, val, &err);
  489. if (err) return err;
  490. }
  491. PyGILState_STATE gstate = PyGILState_Ensure();
  492. pyfunc = PyUnicode_FromString(func_name);
  493. if (!pyfunc) {
  494. if (pyval) Py_DECREF(pyval);
  495. PyGILState_Release(gstate);
  496. return PCILIB_ERROR_MEMORY;
  497. }
  498. pyret = PyObject_CallMethodObjArgs(module->module, pyfunc, ctx->py->pcilib_pywrap, pyval, NULL);
  499. Py_DECREF(pyfunc);
  500. Py_DECREF(pyval);
  501. if (!pyret) {
  502. PyGILState_Release(gstate);
  503. pcilib_python_error("Error executing function (%s) of python script (%s)", func_name, script_name);
  504. return PCILIB_ERROR_FAILED;
  505. }
  506. if ((val)&&(pyret != Py_None))
  507. err = pcilib_set_value_from_pyobject(ctx, val, pyret);
  508. Py_DECREF(pyret);
  509. PyGILState_Release(gstate);
  510. return err;
  511. #else /* HAVE_PYTHON */
  512. pcilib_error("Python is not supported");
  513. return PCILIB_ERROR_NOTSUPPORTED;
  514. #endif /* HAVE_PYTHON */
  515. }