diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2016-02-26 02:14:55 +0100 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2016-02-26 02:14:55 +0100 |
commit | 30d740cc6accc3bd477e6bc924d5b74dad71ee6d (patch) | |
tree | 2e197791d23958258b34a3bc53cc5b0584ac7060 /pcilib | |
parent | 3ea1907f3169e0233d3a32a7d470af3c34b6f967 (diff) | |
parent | ed7a7ff66c64cc2e164070865306e7d79652063a (diff) | |
download | pcitool-30d740cc6accc3bd477e6bc924d5b74dad71ee6d.tar.gz pcitool-30d740cc6accc3bd477e6bc924d5b74dad71ee6d.tar.bz2 pcitool-30d740cc6accc3bd477e6bc924d5b74dad71ee6d.tar.xz pcitool-30d740cc6accc3bd477e6bc924d5b74dad71ee6d.zip |
Merge of further work on Python scripting from Vasiliy Chernov
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/CMakeLists.txt | 4 | ||||
-rw-r--r-- | pcilib/py.c | 77 | ||||
-rw-r--r-- | pcilib/xml.c | 6 |
3 files changed, 63 insertions, 24 deletions
diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index a7557b7..27543e4 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -8,7 +8,7 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h version.h config.h build.h) +set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h config.h version.h build.h) add_library(pcilib SHARED pci.c datacpy.c memcpy.c pagecpy.c cpu.c timing.c export.c value.c bar.c fifo.c model.c bank.c register.c view.c unit.c property.c xml.c py.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c) target_link_libraries(pcilib dma protocols views ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES} ${PYTHON_LIBRARIES}) add_dependencies(pcilib build dma protocols views) @@ -23,4 +23,4 @@ install(FILES pcilib.h install(FILES bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h timing.h cpu.h datacpy.h pagecpy.h memcpy.h export.h version.h view.h unit.h DESTINATION include/pcilib -)
\ No newline at end of file +) diff --git a/pcilib/py.c b/pcilib/py.c index a5118fb..3e1f38d 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -83,9 +83,9 @@ void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flag va_end(va); #ifdef HAVE_PYTHON - if (pytype) Py_XDECREF(pytype); - if (pyval) Py_XDECREF(pyval); - if (pytraceback) Py_XDECREF(pytraceback); + if (pytype) Py_DECREF(pytype); + if (pyval) Py_DECREF(pyval); + if (pytraceback) Py_DECREF(pytraceback); #endif /* HAVE_PYTHON */ } @@ -98,14 +98,13 @@ int pcilib_init_py(pcilib_t *ctx) { memset(ctx->py, 0, sizeof(pcilib_py_t)); - if(Py_IsInitialized()) - ctx->py->finalyze = 1; - else { + if(!Py_IsInitialized()) { Py_Initialize(); // Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads PyEval_InitThreads(); PyEval_ReleaseLock(); + ctx->py->finalyze = 1; } ctx->py->main_module = PyImport_AddModule("__parser__"); @@ -127,7 +126,9 @@ int pcilib_init_py(pcilib_t *ctx) { } PyObject *mod_name = PyString_FromString("Pcipywrap"); - ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, PyCObject_FromVoidPtr(ctx, NULL), NULL); + PyObject *pyctx = PyCObject_FromVoidPtr(ctx, NULL); + ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL); + Py_XDECREF(pyctx); Py_XDECREF(mod_name); if (!ctx->py->pcilib_pywrap) { @@ -141,7 +142,9 @@ int pcilib_init_py(pcilib_t *ctx) { int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { #ifdef HAVE_PYTHON - PyObject* pypath; + int err = 0; + PyObject *pypath, *pynewdir; + PyObject *pydict, *pystr, *pyret = NULL; char *script_dir; const char *model_dir = getenv("PCILIB_MODEL_DIR"); @@ -163,10 +166,40 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { return PCILIB_ERROR_FAILED; } - // Shall we check if the directory already in the path? - if(PyList_Append(pypath, PyString_FromString(script_dir)) == -1) { + pynewdir = PyString_FromString(script_dir); + if (!pynewdir) { + pcilib_python_error("Can't create python string"); + return PCILIB_ERROR_MEMORY; + } + + // Checking if the directory already in the path? + pydict = PyDict_New(); + if (pydict) { + pystr = PyString_FromString("cur"); + if (pystr) { + PyDict_SetItem(pydict, pystr, pynewdir); + Py_DECREF(pystr); + } + + pystr = PyString_FromString("path"); + if (pystr) { + PyDict_SetItem(pydict, pystr, pypath); + Py_DECREF(pystr); + } + + pyret = PyRun_String("cur in path", Py_eval_input, ctx->py->global_dict, pydict); + Py_DECREF(pydict); + } + + if ((pyret == Py_False)&&(PyList_Append(pypath, pynewdir) == -1)) + err = PCILIB_ERROR_FAILED; + + if (pyret) Py_DECREF(pyret); + Py_DECREF(pynewdir); + + if (err) { pcilib_python_error("Can't add directory (%s) to python path", script_dir); - return PCILIB_ERROR_FAILED; + return err; } #endif /* HAVE_PYTHON */ @@ -178,18 +211,22 @@ void pcilib_free_py(pcilib_t *ctx) { int finalyze = 0; if (ctx->py) { - if(ctx->py->finalyze) finalyze = 1; + if (ctx->py->finalyze) finalyze = 1; if (ctx->py->script_hash) { pcilib_script_t *script, *script_tmp; HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) { + Py_DECREF(script->module); HASH_DEL(ctx->py->script_hash, script); free(script); } ctx->py->script_hash = NULL; } + if (ctx->py->pcilib_pywrap) + Py_DECREF(ctx->py->pcilib_pywrap); + free(ctx->py); ctx->py = NULL; } @@ -258,13 +295,13 @@ int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_ pystr = PyString_FromString("read_from_register"); if (pystr) { if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R; - Py_XDECREF(pystr); + Py_DECREF(pystr); } pystr = PyString_FromString("write_to_register"); if (pystr) { if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W; - Py_XDECREF(pystr); + Py_DECREF(pystr); } #endif /* HAVE_PYTHON */ @@ -470,12 +507,16 @@ int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *va if (!obj) { pcilib_error("Failed to run the Python code: %s", code); + free(code); return PCILIB_ERROR_FAILED; } pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code); err = pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj)); + Py_DECREF(obj); + free(code); + return err; #else /* HAVE_PYTHON */ pcilib_error("Current build not support python."); @@ -506,15 +547,15 @@ int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func pyfunc = PyUnicode_FromString(func_name); if (!pyfunc) { - if (pyval) Py_XDECREF(pyval); + if (pyval) Py_DECREF(pyval); PyGILState_Release(gstate); return PCILIB_ERROR_MEMORY; } pyret = PyObject_CallMethodObjArgs(module->module, pyfunc, ctx->py->pcilib_pywrap, pyval, NULL); - Py_XDECREF(pyfunc); - Py_XDECREF(pyval); + Py_DECREF(pyfunc); + Py_DECREF(pyval); if (!pyret) { PyGILState_Release(gstate); @@ -525,7 +566,7 @@ int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func if ((val)&&(pyret != Py_None)) err = pcilib_set_value_from_pyobject(ctx, val, pyret); - Py_XDECREF(pyret); + Py_DECREF(pyret); PyGILState_Release(gstate); return err; diff --git a/pcilib/xml.c b/pcilib/xml.c index fd12636..b980a83 100644 --- a/pcilib/xml.c +++ b/pcilib/xml.c @@ -615,7 +615,7 @@ static int pcilib_xml_parse_value_name(pcilib_t *ctx, xmlXPathContextPtr xpath, int min_set = 0, max_set = 0; pcilib_register_value_t val; - + for (cur = node->properties; cur != NULL; cur = cur->next) { if(!cur->children) continue; if(!xmlNodeIsText(cur->children)) continue; @@ -713,7 +713,7 @@ static int pcilib_xml_create_enum_view(pcilib_t *ctx, xmlXPathContextPtr xpath, pcilib_error("No names is defined for enum view (%s)", desc.base.name); return PCILIB_ERROR_INVALID_DATA; } - + memset(desc.names, 0, (nodeset->nodeNr + 1) * sizeof(pcilib_register_value_name_t)); for (i = 0; i < nodeset->nodeNr; i++) { err = pcilib_xml_parse_value_name(ctx, xpath, doc, nodeset->nodeTab[i], &desc.names[i]); @@ -723,11 +723,9 @@ static int pcilib_xml_create_enum_view(pcilib_t *ctx, xmlXPathContextPtr xpath, return err; } } - memset(&desc.names[nodeset->nodeNr], 0, sizeof(pcilib_register_value_name_t)); xmlXPathFreeObject(nodes); - err = pcilib_add_views_custom(ctx, 1, (pcilib_view_description_t*)&desc, &view_ctx); if (err) { free(desc.names); |