py.c 14 KB

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