py.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. struct pcilib_py_s {
  27. int finalyze; /**< Indicates, that we are initialized from wrapper and should not destroy Python resources in destructor */
  28. PyObject *main_module; /**< Main interpreter */
  29. PyObject *pywrap_module; /**< Pcilib python wrapper */
  30. PyObject *threading_module; /**< Threading module */
  31. PyObject *global_dict; /**< Dictionary of main interpreter */
  32. PyObject *pcilib_pywrap; /**< pcilib wrapper context */
  33. pcilib_script_t *script_hash; /**< Hash with loaded scripts */
  34. # if PY_MAJOR_VERSION >= 3
  35. int status; /**< Indicates if python was initialized successfuly (0) or error have occured */
  36. pthread_t pth; /**< Helper thread for Python initialization */
  37. pthread_cond_t cond; /**< Condition informing about initialization success and request for clean-up */
  38. pthread_mutex_t lock; /**< Condition lock */
  39. // PyInterpreterState *istate;
  40. // PyThreadState *tstate;
  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. static int pcilib_py_load_default_modules(pcilib_t *ctx) {
  115. PyGILState_STATE gstate = PyGILState_Ensure();
  116. ctx->py->main_module = PyImport_AddModule("__parser__");
  117. if (!ctx->py->main_module) {
  118. PyGILState_Release(gstate);
  119. pcilib_python_warning("Error importing python parser");
  120. return PCILIB_ERROR_FAILED;
  121. }
  122. ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module);
  123. if (!ctx->py->global_dict) {
  124. PyGILState_Release(gstate);
  125. pcilib_python_warning("Error locating global python dictionary");
  126. return PCILIB_ERROR_FAILED;
  127. }
  128. ctx->py->pywrap_module = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER);
  129. if (!ctx->py->pywrap_module) {
  130. PyGILState_Release(gstate);
  131. pcilib_python_warning("Error importing pcilib python wrapper");
  132. return PCILIB_ERROR_FAILED;
  133. }
  134. /**
  135. * We need to load threading module here, otherwise if any of the scripts
  136. * will use threading, on cleanup Python3 will complain:
  137. * Exception KeyError: KeyError(140702199305984,) in <module 'threading' from '/usr/lib64/python3.3/threading.py'> ignored
  138. * The idea to load threading module is inspired by
  139. * http://stackoverflow.com/questions/8774958/keyerror-in-module-threading-after-a-successful-py-test-run
  140. */
  141. ctx->py->threading_module = PyImport_ImportModule("threading");
  142. if (!ctx->py->threading_module) {
  143. PyGILState_Release(gstate);
  144. pcilib_python_warning("Error importing threading python module");
  145. return PCILIB_ERROR_FAILED;
  146. }
  147. PyObject *mod_name = PyUnicode_FromString(PCILIB_PYTHON_WRAPPER);
  148. PyObject *pyctx = PyCapsule_New(ctx, "pcilib", NULL);
  149. ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(ctx->py->pywrap_module, mod_name, pyctx, NULL);
  150. Py_XDECREF(pyctx);
  151. Py_XDECREF(mod_name);
  152. if (!ctx->py->pcilib_pywrap) {
  153. PyGILState_Release(gstate);
  154. pcilib_python_warning("Error initializing python wrapper");
  155. return PCILIB_ERROR_FAILED;
  156. }
  157. PyGILState_Release(gstate);
  158. return 0;
  159. }
  160. static void pcilib_py_clean_default_modules(pcilib_t *ctx) {
  161. PyGILState_STATE gstate;
  162. gstate = PyGILState_Ensure();
  163. if (ctx->py->pcilib_pywrap) Py_DECREF(ctx->py->pcilib_pywrap);
  164. if (ctx->py->threading_module) Py_DECREF(ctx->py->threading_module);
  165. if (ctx->py->pywrap_module) Py_DECREF(ctx->py->pywrap_module);
  166. // Crashes Python2
  167. // if (ctx->py->main_module) Py_DECREF(ctx->py->main_module);
  168. PyGILState_Release(gstate);
  169. }
  170. # if PY_MAJOR_VERSION >= 3
  171. /**
  172. * Python3 specially treats the main thread intializing Python. It crashes if
  173. * the Lock is released and any Python code is executed under the GIL compaling
  174. * that GIL is not locked. Python3 assumes that the main thread most of the time
  175. * holds the Lock, only shortly giving it away to other threads and re-obtaining
  176. * it hereafter. This is not possible to do with GILs, but instead (probably)
  177. * PyEval_{Save,Restore}Thread() should be used. On other hand, the other threads
  178. * are working fine with GILs. This makes things complicated as we need to know
  179. * if we are running in main thread or not.
  180. * To simplify matters, during initalization we start a new thread which will
  181. * performa actual initialization of Python and, hence, act as main thread.
  182. * We only intialize here. No python code is executed afterwards. So we don't
  183. * need to care about special locking mechanisms in main thread. Instead all
  184. * our user threads can use GILs normally.
  185. * See more details here:
  186. * http://stackoverflow.com/questions/24499393/cpython-locking-the-gil-in-the-main-thread
  187. * http://stackoverflow.com/questions/15470367/pyeval-initthreads-in-python-3-how-when-to-call-it-the-saga-continues-ad-naus
  188. */
  189. static void *pcilib_py_run_init_thread(void *arg) {
  190. PyThreadState *state;
  191. pcilib_t *ctx = (pcilib_t*)arg;
  192. pcilib_py_t *py = ctx->py;
  193. Py_Initialize();
  194. PyEval_InitThreads();
  195. // state = PyThreadState_Get();
  196. // py->istate = state->interp;
  197. py->status = pcilib_py_load_default_modules(ctx);
  198. state = PyEval_SaveThread();
  199. // Ensure that main thread waiting for our signal
  200. pthread_mutex_lock(&(py->lock));
  201. // Inform the parent thread that initialization is finished
  202. pthread_cond_signal(&(py->cond));
  203. // Wait untill cleanup is requested
  204. pthread_cond_wait(&(py->cond), &(py->lock));
  205. pthread_mutex_unlock(&(py->lock));
  206. PyEval_RestoreThread(state);
  207. pcilib_py_clean_default_modules(ctx);
  208. Py_Finalize();
  209. return NULL;
  210. }
  211. # endif /* PY_MAJOR_VERSION < 3 */
  212. #endif /* HAVE_PYTHON */
  213. int pcilib_init_py(pcilib_t *ctx) {
  214. #ifdef HAVE_PYTHON
  215. int err = 0;
  216. ctx->py = (pcilib_py_t*)malloc(sizeof(pcilib_py_t));
  217. if (!ctx->py) return PCILIB_ERROR_MEMORY;
  218. memset(ctx->py, 0, sizeof(pcilib_py_t));
  219. if (!Py_IsInitialized()) {
  220. # if PY_MAJOR_VERSION < 3
  221. Py_Initialize();
  222. // Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads
  223. PyEval_InitThreads();
  224. PyEval_ReleaseLock();
  225. err = pcilib_py_load_default_modules(ctx);
  226. # else /* PY_MAJOR_VERSION < 3 */
  227. int err = pthread_mutex_init(&(ctx->py->lock), NULL);
  228. if (err) return PCILIB_ERROR_FAILED;
  229. err = pthread_cond_init(&(ctx->py->cond), NULL);
  230. if (err) {
  231. pthread_mutex_destroy(&(ctx->py->lock));
  232. return PCILIB_ERROR_FAILED;
  233. }
  234. err = pthread_mutex_lock(&(ctx->py->lock));
  235. if (err) {
  236. pthread_cond_destroy(&(ctx->py->cond));
  237. pthread_mutex_destroy(&(ctx->py->lock));
  238. return PCILIB_ERROR_FAILED;
  239. }
  240. // Create initalizer thread and wait until it releases the Lock
  241. err = pthread_create(&(ctx->py->pth), NULL, pcilib_py_run_init_thread, (void*)ctx);
  242. if (err) {
  243. pthread_mutex_unlock(&(ctx->py->lock));
  244. pthread_cond_destroy(&(ctx->py->cond));
  245. pthread_mutex_destroy(&(ctx->py->lock));
  246. return PCILIB_ERROR_FAILED;
  247. }
  248. // Wait until initialized and keep the lock afterwards until free executed
  249. pthread_cond_wait(&(ctx->py->cond), &(ctx->py->lock));
  250. err = ctx->py->status;
  251. // ctx->py->tstate = PyThreadState_New(ctx->py->istate);
  252. # endif /* PY_MAJOR_VERSION < 3 */
  253. ctx->py->finalyze = 1;
  254. } else {
  255. err = pcilib_py_load_default_modules(ctx);
  256. }
  257. if (err) {
  258. pcilib_free_py(ctx);
  259. return err;
  260. }
  261. #endif /* HAVE_PYTHON */
  262. return 0;
  263. }
  264. void pcilib_free_py(pcilib_t *ctx) {
  265. #ifdef HAVE_PYTHON
  266. if (ctx->py) {
  267. PyGILState_STATE gstate;
  268. gstate = PyGILState_Ensure();
  269. if (ctx->py->script_hash) {
  270. pcilib_script_t *script, *script_tmp;
  271. HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) {
  272. Py_DECREF(script->module);
  273. HASH_DEL(ctx->py->script_hash, script);
  274. free(script);
  275. }
  276. ctx->py->script_hash = NULL;
  277. }
  278. PyGILState_Release(gstate);
  279. #if PY_MAJOR_VERSION < 3
  280. pcilib_py_clean_default_modules(ctx);
  281. #endif /* PY_MAJOR_VERSION < 3 */
  282. if (ctx->py->finalyze) {
  283. #if PY_MAJOR_VERSION < 3
  284. Py_Finalize();
  285. #else /* PY_MAJOR_VERSION < 3 */
  286. // PyThreadState_Delete(ctx->py->tstate);
  287. // singal python init thread to stop and wait it to finish
  288. pthread_cond_signal(&(ctx->py->cond));
  289. pthread_mutex_unlock(&(ctx->py->lock));
  290. pthread_join(ctx->py->pth, NULL);
  291. // destroy synchronization primitives
  292. pthread_cond_destroy(&(ctx->py->cond));
  293. pthread_mutex_destroy(&(ctx->py->lock));
  294. #endif /* PY_MAJOR_VERSION < 3 */
  295. }
  296. free(ctx->py);
  297. ctx->py = NULL;
  298. }
  299. #endif /* HAVE_PYTHON */
  300. }
  301. int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) {
  302. #ifdef HAVE_PYTHON
  303. int err = 0;
  304. PyObject *pypath, *pynewdir;
  305. PyObject *pydict, *pystr, *pyret = NULL;
  306. char *script_dir;
  307. if (!ctx->py) return 0;
  308. const char *model_dir = getenv("PCILIB_MODEL_DIR");
  309. if (!model_dir) model_dir = PCILIB_MODEL_DIR;
  310. if (!dir) dir = ctx->model;
  311. if (*dir == '/') {
  312. script_dir = (char*)dir;
  313. } else {
  314. script_dir = alloca(strlen(model_dir) + strlen(dir) + 2);
  315. if (!script_dir) return PCILIB_ERROR_MEMORY;
  316. sprintf(script_dir, "%s/%s", model_dir, dir);
  317. }
  318. PyGILState_STATE gstate = PyGILState_Ensure();
  319. pypath = PySys_GetObject("path");
  320. if (!pypath) {
  321. PyGILState_Release(gstate);
  322. pcilib_python_warning("Can't get python path");
  323. return PCILIB_ERROR_FAILED;
  324. }
  325. pynewdir = PyUnicode_FromString(script_dir);
  326. if (!pynewdir) {
  327. PyGILState_Release(gstate);
  328. pcilib_python_warning("Can't create python string");
  329. return PCILIB_ERROR_MEMORY;
  330. }
  331. // Checking if the directory already in the path?
  332. pydict = PyDict_New();
  333. if (pydict) {
  334. pystr = PyUnicode_FromString("cur");
  335. if (pystr) {
  336. PyDict_SetItem(pydict, pystr, pynewdir);
  337. Py_DECREF(pystr);
  338. }
  339. pystr = PyUnicode_FromString("path");
  340. if (pystr) {
  341. PyDict_SetItem(pydict, pystr, pypath);
  342. Py_DECREF(pystr);
  343. }
  344. pyret = PyRun_String("cur in path", Py_eval_input, ctx->py->global_dict, pydict);
  345. Py_DECREF(pydict);
  346. }
  347. if ((pyret == Py_False)&&(PyList_Append(pypath, pynewdir) == -1))
  348. err = PCILIB_ERROR_FAILED;
  349. if (pyret) Py_DECREF(pyret);
  350. Py_DECREF(pynewdir);
  351. PyGILState_Release(gstate);
  352. if (err) {
  353. pcilib_python_warning("Can't add directory (%s) to python path", script_dir);
  354. return err;
  355. }
  356. #endif /* HAVE_PYTHON */
  357. return 0;
  358. }
  359. int pcilib_py_load_script(pcilib_t *ctx, const char *script_name) {
  360. #ifdef HAVE_PYTHON
  361. PyObject* pymodule;
  362. pcilib_script_t *module = NULL;
  363. PyGILState_STATE gstate;
  364. if (!ctx->py) return 0;
  365. char *module_name = strdupa(script_name);
  366. if (!module_name) return PCILIB_ERROR_MEMORY;
  367. char *py = strrchr(module_name, '.');
  368. if ((!py)||(strcasecmp(py, ".py"))) {
  369. pcilib_error("Invalid script name (%s) is specified", script_name);
  370. return PCILIB_ERROR_INVALID_ARGUMENT;
  371. }
  372. *py = 0;
  373. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  374. if (module) return 0;
  375. gstate = PyGILState_Ensure();
  376. pymodule = PyImport_ImportModule(module_name);
  377. if (!pymodule) {
  378. PyGILState_Release(gstate);
  379. pcilib_python_error("Error importing script (%s)", script_name);
  380. return PCILIB_ERROR_FAILED;
  381. }
  382. module = (pcilib_script_t*)malloc(sizeof(pcilib_script_t));
  383. if (!module) {
  384. Py_DECREF(pymodule);
  385. PyGILState_Release(gstate);
  386. return PCILIB_ERROR_MEMORY;
  387. }
  388. PyGILState_Release(gstate);
  389. module->module = pymodule;
  390. module->name = script_name;
  391. HASH_ADD_KEYPTR(hh, ctx->py->script_hash, module->name, strlen(module->name), module);
  392. #endif /* HAVE_PYTHON */
  393. return 0;
  394. }
  395. int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_name, pcilib_access_mode_t *mode_ret) {
  396. pcilib_access_mode_t mode = 0;
  397. #ifdef HAVE_PYTHON
  398. PyObject *dict;
  399. PyObject *pystr;
  400. pcilib_script_t *module;
  401. PyGILState_STATE gstate;
  402. if (!ctx->py) {
  403. if (mode_ret) *mode_ret = mode;
  404. return 0;
  405. }
  406. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  407. if(!module) {
  408. pcilib_error("Script (%s) is not loaded yet", script_name);
  409. return PCILIB_ERROR_NOTFOUND;
  410. }
  411. gstate = PyGILState_Ensure();
  412. dict = PyModule_GetDict(module->module);
  413. if (!dict) {
  414. PyGILState_Release(gstate);
  415. pcilib_python_error("Error getting dictionary for script (%s)", script_name);
  416. return PCILIB_ERROR_FAILED;
  417. }
  418. pystr = PyUnicode_FromString("read_from_register");
  419. if (pystr) {
  420. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
  421. Py_DECREF(pystr);
  422. }
  423. pystr = PyUnicode_FromString("write_to_register");
  424. if (pystr) {
  425. if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W;
  426. Py_DECREF(pystr);
  427. }
  428. PyGILState_Release(gstate);
  429. #endif /* HAVE_PYTHON */
  430. // pcilib_py_load_default_modules(py->ctx);
  431. if (mode_ret) *mode_ret = mode;
  432. return 0;
  433. }
  434. pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val, int *ret) {
  435. #ifdef HAVE_PYTHON
  436. int err = 0;
  437. PyObject *res = NULL;
  438. PyGILState_STATE gstate;
  439. long ival;
  440. double fval;
  441. if (!ctx->py) return NULL;
  442. gstate = PyGILState_Ensure();
  443. switch(val->type) {
  444. case PCILIB_TYPE_LONG:
  445. ival = pcilib_get_value_as_int(ctx, val, &err);
  446. if (!err) res = (PyObject*)PyLong_FromLong(ival);
  447. break;
  448. case PCILIB_TYPE_DOUBLE:
  449. fval = pcilib_get_value_as_float(ctx, val, &err);
  450. if (!err) res = (PyObject*)PyFloat_FromDouble(fval);
  451. break;
  452. default:
  453. PyGILState_Release(gstate);
  454. pcilib_error("Can't convert pcilib value of type (%lu) to PyObject", val->type);
  455. if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
  456. return NULL;
  457. }
  458. PyGILState_Release(gstate);
  459. if (err) {
  460. if (ret) *ret = err;
  461. return NULL;
  462. } else if (!res) {
  463. if (ret) *ret = PCILIB_ERROR_MEMORY;
  464. return res;
  465. }
  466. if (ret) *ret = 0;
  467. return res;
  468. #else /* HAVE_PYTHON */
  469. pcilib_error("Python is not supported");
  470. return NULL;
  471. #endif /* HAVE_PYTHON */
  472. }
  473. int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py_object *pval) {
  474. #ifdef HAVE_PYTHON
  475. int err = 0;
  476. PyObject *pyval = (PyObject*)pval;
  477. PyGILState_STATE gstate;
  478. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  479. gstate = PyGILState_Ensure();
  480. if (PyLong_Check(pyval)) {
  481. err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
  482. } else if (PyFloat_Check(pyval)) {
  483. err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval));
  484. #if PY_MAJOR_VERSION < 3
  485. } else if (PyInt_Check(pyval)) {
  486. err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
  487. } else if (PyString_Check(pyval)) {
  488. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
  489. } else if (PyUnicode_Check(pyval)) {
  490. PyObject *buf = PyUnicode_AsASCIIString(pyval);
  491. if (buf) {
  492. err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
  493. Py_DecRef(buf);
  494. } else {
  495. err = PCILIB_ERROR_FAILED;
  496. }
  497. #else /* PY_MAJOR_VERSION < 3 */
  498. } else if (PyUnicode_Check(pyval)) {
  499. err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval));
  500. #endif /* PY_MAJOR_VERSION < 3 */
  501. } else {
  502. PyGILState_Release(gstate);
  503. pcilib_error("Can't convert PyObject to polymorphic pcilib value");
  504. return PCILIB_ERROR_NOTSUPPORTED;
  505. }
  506. PyGILState_Release(gstate);
  507. return err;
  508. #else /* HAVE_PYTHON */
  509. pcilib_error("Python is not supported");
  510. return PCILIB_ERROR_NOTSUPPORTED;
  511. #endif /* HAVE_PYTHON */
  512. }
  513. #ifdef HAVE_PYTHON
  514. static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  515. int i;
  516. int err = 0;
  517. pcilib_value_t val = {0};
  518. pcilib_register_value_t regval;
  519. char save;
  520. char *reg, *cur;
  521. size_t offset = 0;
  522. size_t size;
  523. char *src;
  524. char *dst;
  525. size_t max_repl = 2 + 2 * sizeof(pcilib_value_t); // the text representation of largest integer
  526. if (value) {
  527. err = pcilib_copy_value(ctx, &val, value);
  528. if (err) return NULL;
  529. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  530. if (err) return NULL;
  531. if (strlen(val.sval) > max_repl)
  532. max_repl = strlen(val.sval);
  533. }
  534. 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
  535. src = strdup(codestr);
  536. dst = (char*)malloc(size); // allocating maximum required space
  537. if ((!src)||(!dst)) {
  538. if (src) free(src);
  539. if (dst) free(dst);
  540. pcilib_error("Failed to allocate memory for string formulas");
  541. return NULL;
  542. }
  543. cur = src;
  544. reg = strchr(src, '$');
  545. while (reg) {
  546. strcpy(dst + offset, cur);
  547. offset += reg - cur;
  548. // find the end of the register name
  549. reg++;
  550. if (*reg == '{') {
  551. reg++;
  552. for (i = 0; (reg[i])&&(reg[i] != '}'); i++);
  553. if (!reg[i]) {
  554. pcilib_error("Python formula (%s) contains unterminated variable reference", codestr);
  555. err = PCILIB_ERROR_INVALID_DATA;
  556. break;
  557. }
  558. } else {
  559. for (i = 0; isalnum(reg[i])||(reg[i] == '_'); i++);
  560. }
  561. save = reg[i];
  562. reg[i] = 0;
  563. // determine replacement value
  564. if (!strcasecmp(reg, "value")) {
  565. if (!value) {
  566. pcilib_error("Python formula (%s) relies on the value of register, but it is not provided", codestr);
  567. err = PCILIB_ERROR_INVALID_REQUEST;
  568. break;
  569. }
  570. strcpy(dst + offset, val.sval);
  571. } else {
  572. if (*reg == '/') {
  573. pcilib_value_t val = {0};
  574. err = pcilib_get_property(ctx, reg, &val);
  575. if (err) break;
  576. err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
  577. if (err) break;
  578. sprintf(dst + offset, "%s", val.sval);
  579. } else {
  580. err = pcilib_read_register(ctx, NULL, reg, &regval);
  581. if (err) break;
  582. sprintf(dst + offset, "0x%lx", regval);
  583. }
  584. }
  585. offset += strlen(dst + offset);
  586. if (save == '}') i++;
  587. else reg[i] = save;
  588. // Advance to the next register if any
  589. cur = reg + i;
  590. reg = strchr(cur, '$');
  591. }
  592. strcpy(dst + offset, cur);
  593. free(src);
  594. if (err) {
  595. free(dst);
  596. return NULL;
  597. }
  598. return dst;
  599. }
  600. #endif /* HAVE_PYTHON */
  601. int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
  602. #ifdef HAVE_PYTHON
  603. int err;
  604. PyGILState_STATE gstate;
  605. char *code;
  606. PyObject* obj;
  607. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  608. code = pcilib_py_parse_string(ctx, codestr, value);
  609. if (!code) {
  610. pcilib_error("Failed to parse registers in the code: %s", codestr);
  611. return PCILIB_ERROR_FAILED;
  612. }
  613. gstate = PyGILState_Ensure();
  614. obj = PyRun_String(code, Py_eval_input, ctx->py->global_dict, ctx->py->global_dict);
  615. PyGILState_Release(gstate);
  616. if (!obj) {
  617. pcilib_error("Failed to run the Python code: %s", code);
  618. free(code);
  619. return PCILIB_ERROR_FAILED;
  620. }
  621. pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code);
  622. err = pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
  623. Py_DECREF(obj);
  624. free(code);
  625. return err;
  626. #else /* HAVE_PYTHON */
  627. pcilib_error("Current build not support python.");
  628. return PCILIB_ERROR_NOTAVAILABLE;
  629. #endif /* HAVE_PYTHON */
  630. }
  631. int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func_name, pcilib_value_t *val) {
  632. #ifdef HAVE_PYTHON
  633. int err = 0;
  634. PyObject *pyfunc;
  635. PyObject *pyval = NULL, *pyret;
  636. pcilib_script_t *module = NULL;
  637. if (!ctx->py) return PCILIB_ERROR_NOTINITIALIZED;
  638. HASH_FIND_STR(ctx->py->script_hash, script_name, module);
  639. if (!module) {
  640. pcilib_error("Script (%s) is not loaded", script_name);
  641. return PCILIB_ERROR_NOTFOUND;
  642. }
  643. if (val) {
  644. pyval = pcilib_get_value_as_pyobject(ctx, val, &err);
  645. if (err) return err;
  646. }
  647. PyGILState_STATE gstate = PyGILState_Ensure();
  648. pyfunc = PyUnicode_FromString(func_name);
  649. if (!pyfunc) {
  650. if (pyval) Py_DECREF(pyval);
  651. PyGILState_Release(gstate);
  652. return PCILIB_ERROR_MEMORY;
  653. }
  654. pyret = PyObject_CallMethodObjArgs(module->module, pyfunc, ctx->py->pcilib_pywrap, pyval, NULL);
  655. Py_DECREF(pyfunc);
  656. Py_DECREF(pyval);
  657. if (!pyret) {
  658. PyGILState_Release(gstate);
  659. pcilib_python_error("Error executing function (%s) of python script (%s)", func_name, script_name);
  660. return PCILIB_ERROR_FAILED;
  661. }
  662. if ((val)&&(pyret != Py_None))
  663. err = pcilib_set_value_from_pyobject(ctx, val, pyret);
  664. Py_DECREF(pyret);
  665. PyGILState_Release(gstate);
  666. return err;
  667. #else /* HAVE_PYTHON */
  668. pcilib_error("Python is not supported");
  669. return PCILIB_ERROR_NOTSUPPORTED;
  670. #endif /* HAVE_PYTHON */
  671. }