From e2550e6df11558ccd6e8b95f489c0988b34347af Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Fri, 26 Feb 2016 10:19:58 +0100 Subject: 1. pywrap: - fix get_registers_list crash with bank != NULL - set correct python version in cmake install step 2. html_server: - merge set and get value boxes into one box - add registers bank view mode - read registers/properties values in bank/branch view mode 3. xml/test - remove cmosis registers - add multithread safe property example --- pywrap/pcipywrap.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'pywrap/pcipywrap.c') diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index 64e059a..dca5973 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -458,17 +458,22 @@ PyObject* Pcipywrap_set_property(Pcipywrap *self, PyObject* val, const char *pro PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank) { - pcilib_register_info_t *list = pcilib_get_register_list(self->ctx, bank, PCILIB_LIST_FLAGS_DEFAULT); - PyObject* pyList = PyList_New(0); - for(int i = 0; i < ((pcilib_t*)self->ctx)->num_reg; i++) - { - //serialize item attributes - PyObject* pylistItem = pcilib_convert_register_info_to_pyobject(self->ctx, list[i]); - pcilib_pylist_append(pyList, pylistItem); - //Py_DECREF(pylistItem); - } - pcilib_free_register_info(self->ctx, list); - return pyList; + pcilib_register_info_t *list = pcilib_get_register_list(self->ctx, bank, PCILIB_LIST_FLAGS_DEFAULT); + + if(!list) { + set_python_exception("pcilib_get_register_list return NULL"); + return NULL; + } + + PyObject* pyList = PyList_New(0); + for(int i = 0; list[i].name; i++) + { + //serialize item attributes + PyObject* pylistItem = pcilib_convert_register_info_to_pyobject(self->ctx, list[i]); + pcilib_pylist_append(pyList, pylistItem); + } + pcilib_free_register_info(self->ctx, list); + return pyList; } PyObject* Pcipywrap_get_register_info(Pcipywrap *self, const char* reg,const char *bank) @@ -551,7 +556,7 @@ PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) pcilib_lock_t* lock = pcilib_get_lock(self->ctx, PCILIB_LOCK_FLAGS_DEFAULT, lock_id); - if(!lock) + if(!lock) { set_python_exception("Failed pcilib_get_lock"); return NULL; -- cgit v1.2.3 From 5d775d64bdec554b9842823bd1c46263210425fd Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Tue, 1 Mar 2016 10:42:40 +0100 Subject: 1. multithreading: - Enable multiprocessing for api_server - Enable mutrithreading for html_server 2. py: - extract pcilib->py bases from pcilib->py functions - add api for interact directly with pcilib->py without pcilib context. 3. pcipywrap - Add scripts handling. --- pywrap/pcipywrap.c | 333 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 208 insertions(+), 125 deletions(-) (limited to 'pywrap/pcipywrap.c') diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index 391bdf4..cfb4e53 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -1,6 +1,9 @@ #include "pcipywrap.h" #include "locking.h" +#include +#include + char* full_log = NULL; /*! @@ -101,27 +104,6 @@ void __redirect_logs_to_exeption() pcilib_get_logger_context()); } -/*! - * \brief Wrap for PyDict_SetItem, with decrease reference counting after set. - */ -void pcilib_pydict_set_item(PyObject* dict, PyObject* name, PyObject* value) -{ - PyDict_SetItem(dict, - name, - value); - Py_XDECREF(name); - Py_XDECREF(value); -} - -/*! - * \brief Wrap for PyList_Append, with decrease reference counting after append. - */ -void pcilib_pylist_append(PyObject* list, PyObject* value) -{ - PyList_Append(list, value); - Py_XDECREF(value); -} - void add_pcilib_value_to_dict(pcilib_t* ctx, PyObject* dict, pcilib_value_t* val, const char *name) { PyObject *py_val = (PyObject*)pcilib_get_value_as_pyobject(ctx, val, NULL); @@ -215,126 +197,198 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_register_info_t listItem) { - PyObject* pylistItem = PyDict_New(); - - if(listItem.name) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("name"), - PyString_FromString(listItem.name)); - - if(listItem.description) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("description"), - PyString_FromString(listItem.description)); - - if(listItem.bank) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("bank"), - PyString_FromString(listItem.bank)); - - - //serialize modes - PyObject* modes = PyList_New(0); - - if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R) - pcilib_pylist_append(modes, PyString_FromString("R")); - if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W) - pcilib_pylist_append(modes, PyString_FromString("W")); - if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) - pcilib_pylist_append(modes, PyString_FromString("RW")); - if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C) - pcilib_pylist_append(modes, PyString_FromString("W1C")); - if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C) - pcilib_pylist_append(modes, PyString_FromString("RW1C")); - if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I) - pcilib_pylist_append(modes, PyString_FromString("W1I")); - if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I) - pcilib_pylist_append(modes, PyString_FromString("RW1I")); - if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) - pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); - - pcilib_pydict_set_item(pylistItem, - PyString_FromString("mode"), - modes); - - pcilib_value_t defval = {0}; - pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue); - add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue"); - - if(listItem.range) - { - pcilib_value_t minval = {0}; - pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min); - - pcilib_value_t maxval = {0}; - pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max); - - PyObject* range = PyDict_New(); - add_pcilib_value_to_dict(ctx, range, &minval, "min"); - add_pcilib_value_to_dict(ctx, range, &maxval, "max"); - pcilib_pydict_set_item(pylistItem, - PyString_FromString("range"), - range); - } - - if(listItem.values) - { + PyObject* pylistItem = PyDict_New(); + + if(listItem.name) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("name"), + PyString_FromString(listItem.name)); + + if(listItem.description) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("description"), + PyString_FromString(listItem.description)); + + if(listItem.bank) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("bank"), + PyString_FromString(listItem.bank)); + + + //serialize modes + PyObject* modes = PyList_New(0); + + if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R) + pcilib_pylist_append(modes, PyString_FromString("R")); + if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W) + pcilib_pylist_append(modes, PyString_FromString("W")); + if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) + pcilib_pylist_append(modes, PyString_FromString("RW")); + if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C) + pcilib_pylist_append(modes, PyString_FromString("W1C")); + if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C) + pcilib_pylist_append(modes, PyString_FromString("RW1C")); + if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I) + pcilib_pylist_append(modes, PyString_FromString("W1I")); + if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I) + pcilib_pylist_append(modes, PyString_FromString("RW1I")); + if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) + pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); + + pcilib_pydict_set_item(pylistItem, + PyString_FromString("mode"), + modes); + + pcilib_value_t defval = {0}; + pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue); + add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue"); + + if(listItem.range) + { + pcilib_value_t minval = {0}; + pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min); + + pcilib_value_t maxval = {0}; + pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max); + + PyObject* range = PyDict_New(); + add_pcilib_value_to_dict(ctx, range, &minval, "min"); + add_pcilib_value_to_dict(ctx, range, &maxval, "max"); + pcilib_pydict_set_item(pylistItem, + PyString_FromString("range"), + range); + } - PyObject* values = PyList_New(0); + if(listItem.values) + { - for (int j = 0; listItem.values[j].name; j++) - { - PyObject* valuesItem = PyDict_New(); + PyObject* values = PyList_New(0); - pcilib_value_t val = {0}; - pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value); + for (int j = 0; listItem.values[j].name; j++) + { + PyObject* valuesItem = PyDict_New(); - pcilib_value_t min = {0}; - pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min); + pcilib_value_t val = {0}; + pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value); - pcilib_value_t max = {0}; - pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max); + pcilib_value_t min = {0}; + pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min); - add_pcilib_value_to_dict(ctx, valuesItem, &val, "value"); - add_pcilib_value_to_dict(ctx, valuesItem, &min, "min"); - add_pcilib_value_to_dict(ctx, valuesItem, &max, "max"); + pcilib_value_t max = {0}; + pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max); - if(listItem.values[j].name) - pcilib_pydict_set_item(valuesItem, - PyString_FromString("name"), - PyString_FromString(listItem.values[j].name)); - if(listItem.values[j].description) - { - pcilib_pydict_set_item(valuesItem, - PyString_FromString("description"), - PyString_FromString(listItem.values[j].description)); + add_pcilib_value_to_dict(ctx, valuesItem, &val, "value"); + add_pcilib_value_to_dict(ctx, valuesItem, &min, "min"); + add_pcilib_value_to_dict(ctx, valuesItem, &max, "max"); - } - pcilib_pylist_append(values, valuesItem); - } + if(listItem.values[j].name) + pcilib_pydict_set_item(valuesItem, + PyString_FromString("name"), + PyString_FromString(listItem.values[j].name)); + if(listItem.values[j].description) + { + pcilib_pydict_set_item(valuesItem, + PyString_FromString("description"), + PyString_FromString(listItem.values[j].description)); - pcilib_pydict_set_item(pylistItem, - PyString_FromString("values"), - values); - } + } + pcilib_pylist_append(values, valuesItem); + } - return pylistItem; + pcilib_pydict_set_item(pylistItem, + PyString_FromString("values"), + values); + } + return pylistItem; } + Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model) { //opening device pcilib_t* ctx = pcilib_open(fpga_device, model); - if(!ctx) - { + if(!ctx) { set_python_exception("Failed pcilib_open(%s, %s)", fpga_device, model); return NULL; } + Pcipywrap *self; self = (Pcipywrap *) malloc(sizeof(Pcipywrap)); + if(!self) { + pcilib_close(ctx); + return (Pcipywrap *)PyExc_MemoryError; + } self->shared = 0; self->ctx = ctx; + self->py = NULL; + self->names = NULL; + self->names_size = 0; + + + //processing pcilib scrips + const char *scripts_dir = getenv("PCILIB_SCRIPTS_DIR"); + if(scripts_dir) { + int err = 0; + + self->py = pcilib_init_py_ctx(ctx->py, &err); + if(err) { + delete_Pcipywrap(self); + set_python_exception("Failed pcilib_py_s_clone (%i)", err); + return NULL; + } + + //add scripts directory to Python path + err = pcilib_py_ctx_add_script_dir(self->py, scripts_dir); + if(err) { + delete_Pcipywrap(self); + set_python_exception("Failed pcilib_py_add_dir (%i)", err); + return NULL; + } + + //load scripts in PCILIB_SCRIPTS_DIR + self->names = malloc(++(self->names_size) * sizeof(char*)); + self->names[self->names_size - 1] = NULL; + + DIR *dir; + struct dirent *script_path; + dir = opendir(scripts_dir); + if (dir) { + while ((script_path = readdir(dir)) != NULL) { + + char *py = strrchr(script_path->d_name, '.'); + if ((!py)||(strcasecmp(py, ".py"))) { + continue; + } + + char *name = malloc(strlen(script_path->d_name)); + if(!name) { + delete_Pcipywrap(self); + return (Pcipywrap *)PyExc_MemoryError; + } + + strcpy(name, script_path->d_name); + + err = pcilib_py_ctx_load_script(self->py, name); + + if(err) { + delete_Pcipywrap(self); + set_python_exception("pcilib_py_ctx_load_script (%i)", err); + return NULL; + } + + self->names = realloc(self->names, ++(self->names_size)); + if(!self->names) { + delete_Pcipywrap(self); + return (Pcipywrap *)PyExc_MemoryError; + } + self->names[self->names_size - 1] = NULL; + self->names[self->names_size - 2] = name; + } + closedir(dir); + } + } + return self; } @@ -350,14 +404,27 @@ Pcipywrap *create_Pcipywrap(PyObject* ctx) self = (Pcipywrap *) malloc(sizeof(Pcipywrap)); self->shared = 1; self->ctx = PyCObject_AsVoidPtr(ctx); + self->py = NULL; + self->names = NULL; + self->names_size = 0; return self; } void delete_Pcipywrap(Pcipywrap *self) { - if(!self->shared) - pcilib_close(self->ctx); - - free(self); + if(!self->shared) + pcilib_close(self->ctx); + + pcilib_free_py_ctx(self->py); + + if(self->names) { + for(int i = 0; self->names[i]; i++) + free(self->names[i]); + free(self->names); + self->names = NULL; + self->names_size = 0; + } + + free(self); } PyObject* Pcipywrap_read_register(Pcipywrap *self, const char *regname, const char *bank) @@ -464,7 +531,7 @@ PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank) set_python_exception("pcilib_get_register_list return NULL"); return NULL; } - + PyObject* pyList = PyList_New(0); for(int i = 0; list[i].name; i++) { @@ -554,7 +621,7 @@ void Pcipywrap_unlock_global(Pcipywrap *self) PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) { pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, + PCILIB_LOCK_FLAG_PERSISTENT, lock_id); if(!lock) { @@ -576,7 +643,7 @@ PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id) { pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, + PCILIB_LOCK_FLAG_PERSISTENT, lock_id); if(!lock) { @@ -597,7 +664,7 @@ PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id) PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) { pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, + PCILIB_LOCK_FLAG_PERSISTENT, lock_id); if(!lock) { @@ -609,4 +676,20 @@ PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) return PyInt_FromLong((long)1); } +PyObject* Pcipywrap_get_scripts_list(Pcipywrap *self) +{ + return pcilib_py_ctx_get_scripts_info(self->py); +} +PyObject* Pcipywrap_run_script(Pcipywrap *self, const char* script_name, PyObject* value) +{ + int err = 0; + PyObject* value_out = pcilib_py_ctx_eval_func(self->py, script_name, "run", value, &err); + + if(err) { + set_python_exception("Failed pcilib_py_ctx_eval_func (%i)", err); + return NULL; + } + + return value_out; +} -- cgit v1.2.3 From 9b947f32c3db96c3022afad401b1349205d22259 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Tue, 1 Mar 2016 16:42:48 +0100 Subject: 1. api-serer: - change multiprosessing work mechanism - add new pywrap functions handling 2. html-server: - now works through api-server --- pywrap/pcipywrap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pywrap/pcipywrap.c') diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index cfb4e53..9113d40 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -97,7 +97,7 @@ void set_python_exception(const char* msg, ...) } -void __redirect_logs_to_exeption() +void redirect_logs_to_exeption() { pcilib_set_logger(pcilib_get_log_level(), pcilib_print_error_to_py, -- cgit v1.2.3 From 8719b84a95805d109e21c20f05a0164315e1b38a Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Wed, 2 Mar 2016 10:26:13 +0100 Subject: Move scripts handing code from py.c to Python wrap --- pywrap/pcipywrap.c | 327 ++++++++++++++++++++--------------------------------- 1 file changed, 122 insertions(+), 205 deletions(-) (limited to 'pywrap/pcipywrap.c') diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index 9113d40..0f6729e 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -1,9 +1,6 @@ #include "pcipywrap.h" #include "locking.h" -#include -#include - char* full_log = NULL; /*! @@ -104,6 +101,27 @@ void redirect_logs_to_exeption() pcilib_get_logger_context()); } +/*! + * \brief Wrap for PyDict_SetItem, with decrease reference counting after set. + */ +void pcilib_pydict_set_item(PyObject* dict, PyObject* name, PyObject* value) +{ + PyDict_SetItem(dict, + name, + value); + Py_XDECREF(name); + Py_XDECREF(value); +} + +/*! + * \brief Wrap for PyList_Append, with decrease reference counting after append. + */ +void pcilib_pylist_append(PyObject* list, PyObject* value) +{ + PyList_Append(list, value); + Py_XDECREF(value); +} + void add_pcilib_value_to_dict(pcilib_t* ctx, PyObject* dict, pcilib_value_t* val, const char *name) { PyObject *py_val = (PyObject*)pcilib_get_value_as_pyobject(ctx, val, NULL); @@ -197,198 +215,126 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_register_info_t listItem) { - PyObject* pylistItem = PyDict_New(); - - if(listItem.name) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("name"), - PyString_FromString(listItem.name)); - - if(listItem.description) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("description"), - PyString_FromString(listItem.description)); - - if(listItem.bank) - pcilib_pydict_set_item(pylistItem, - PyString_FromString("bank"), - PyString_FromString(listItem.bank)); - - - //serialize modes - PyObject* modes = PyList_New(0); - - if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R) - pcilib_pylist_append(modes, PyString_FromString("R")); - if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W) - pcilib_pylist_append(modes, PyString_FromString("W")); - if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) - pcilib_pylist_append(modes, PyString_FromString("RW")); - if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C) - pcilib_pylist_append(modes, PyString_FromString("W1C")); - if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C) - pcilib_pylist_append(modes, PyString_FromString("RW1C")); - if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I) - pcilib_pylist_append(modes, PyString_FromString("W1I")); - if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I) - pcilib_pylist_append(modes, PyString_FromString("RW1I")); - if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) - pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); - - pcilib_pydict_set_item(pylistItem, - PyString_FromString("mode"), - modes); - - pcilib_value_t defval = {0}; - pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue); - add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue"); - - if(listItem.range) - { - pcilib_value_t minval = {0}; - pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min); - - pcilib_value_t maxval = {0}; - pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max); - - PyObject* range = PyDict_New(); - add_pcilib_value_to_dict(ctx, range, &minval, "min"); - add_pcilib_value_to_dict(ctx, range, &maxval, "max"); - pcilib_pydict_set_item(pylistItem, - PyString_FromString("range"), - range); - } + PyObject* pylistItem = PyDict_New(); - if(listItem.values) - { + if(listItem.name) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("name"), + PyString_FromString(listItem.name)); - PyObject* values = PyList_New(0); + if(listItem.description) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("description"), + PyString_FromString(listItem.description)); - for (int j = 0; listItem.values[j].name; j++) - { - PyObject* valuesItem = PyDict_New(); + if(listItem.bank) + pcilib_pydict_set_item(pylistItem, + PyString_FromString("bank"), + PyString_FromString(listItem.bank)); - pcilib_value_t val = {0}; - pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value); - pcilib_value_t min = {0}; - pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min); + //serialize modes + PyObject* modes = PyList_New(0); - pcilib_value_t max = {0}; - pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max); + if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R) + pcilib_pylist_append(modes, PyString_FromString("R")); + if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W) + pcilib_pylist_append(modes, PyString_FromString("W")); + if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) + pcilib_pylist_append(modes, PyString_FromString("RW")); + if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C) + pcilib_pylist_append(modes, PyString_FromString("W1C")); + if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C) + pcilib_pylist_append(modes, PyString_FromString("RW1C")); + if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I) + pcilib_pylist_append(modes, PyString_FromString("W1I")); + if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I) + pcilib_pylist_append(modes, PyString_FromString("RW1I")); + if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) + pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); - add_pcilib_value_to_dict(ctx, valuesItem, &val, "value"); - add_pcilib_value_to_dict(ctx, valuesItem, &min, "min"); - add_pcilib_value_to_dict(ctx, valuesItem, &max, "max"); + pcilib_pydict_set_item(pylistItem, + PyString_FromString("mode"), + modes); - if(listItem.values[j].name) - pcilib_pydict_set_item(valuesItem, - PyString_FromString("name"), - PyString_FromString(listItem.values[j].name)); - if(listItem.values[j].description) - { - pcilib_pydict_set_item(valuesItem, - PyString_FromString("description"), - PyString_FromString(listItem.values[j].description)); + pcilib_value_t defval = {0}; + pcilib_set_value_from_register_value(ctx, &defval, listItem.defvalue); + add_pcilib_value_to_dict(ctx, pylistItem, &defval, "defvalue"); - } - pcilib_pylist_append(values, valuesItem); - } + if(listItem.range) + { + pcilib_value_t minval = {0}; + pcilib_set_value_from_register_value(ctx, &minval, listItem.range->min); - pcilib_pydict_set_item(pylistItem, - PyString_FromString("values"), - values); - } + pcilib_value_t maxval = {0}; + pcilib_set_value_from_register_value(ctx, &maxval, listItem.range->max); - return pylistItem; -} + PyObject* range = PyDict_New(); + add_pcilib_value_to_dict(ctx, range, &minval, "min"); + add_pcilib_value_to_dict(ctx, range, &maxval, "max"); + pcilib_pydict_set_item(pylistItem, + PyString_FromString("range"), + range); + } + + if(listItem.values) + { + + PyObject* values = PyList_New(0); + + for (int j = 0; listItem.values[j].name; j++) + { + PyObject* valuesItem = PyDict_New(); + + pcilib_value_t val = {0}; + pcilib_set_value_from_register_value(ctx, &val, listItem.values[j].value); + + pcilib_value_t min = {0}; + pcilib_set_value_from_register_value(ctx, &min, listItem.values[j].min); + + pcilib_value_t max = {0}; + pcilib_set_value_from_register_value(ctx, &max, listItem.values[j].max); + + add_pcilib_value_to_dict(ctx, valuesItem, &val, "value"); + add_pcilib_value_to_dict(ctx, valuesItem, &min, "min"); + add_pcilib_value_to_dict(ctx, valuesItem, &max, "max"); + + if(listItem.values[j].name) + pcilib_pydict_set_item(valuesItem, + PyString_FromString("name"), + PyString_FromString(listItem.values[j].name)); + if(listItem.values[j].description) + { + pcilib_pydict_set_item(valuesItem, + PyString_FromString("description"), + PyString_FromString(listItem.values[j].description)); + + } + pcilib_pylist_append(values, valuesItem); + } + + pcilib_pydict_set_item(pylistItem, + PyString_FromString("values"), + values); + } + + return pylistItem; +} Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model) { //opening device pcilib_t* ctx = pcilib_open(fpga_device, model); - if(!ctx) { + if(!ctx) + { set_python_exception("Failed pcilib_open(%s, %s)", fpga_device, model); return NULL; } - Pcipywrap *self; self = (Pcipywrap *) malloc(sizeof(Pcipywrap)); - if(!self) { - pcilib_close(ctx); - return (Pcipywrap *)PyExc_MemoryError; - } self->shared = 0; self->ctx = ctx; - self->py = NULL; - self->names = NULL; - self->names_size = 0; - - - //processing pcilib scrips - const char *scripts_dir = getenv("PCILIB_SCRIPTS_DIR"); - if(scripts_dir) { - int err = 0; - - self->py = pcilib_init_py_ctx(ctx->py, &err); - if(err) { - delete_Pcipywrap(self); - set_python_exception("Failed pcilib_py_s_clone (%i)", err); - return NULL; - } - - //add scripts directory to Python path - err = pcilib_py_ctx_add_script_dir(self->py, scripts_dir); - if(err) { - delete_Pcipywrap(self); - set_python_exception("Failed pcilib_py_add_dir (%i)", err); - return NULL; - } - - //load scripts in PCILIB_SCRIPTS_DIR - self->names = malloc(++(self->names_size) * sizeof(char*)); - self->names[self->names_size - 1] = NULL; - - DIR *dir; - struct dirent *script_path; - dir = opendir(scripts_dir); - if (dir) { - while ((script_path = readdir(dir)) != NULL) { - - char *py = strrchr(script_path->d_name, '.'); - if ((!py)||(strcasecmp(py, ".py"))) { - continue; - } - - char *name = malloc(strlen(script_path->d_name)); - if(!name) { - delete_Pcipywrap(self); - return (Pcipywrap *)PyExc_MemoryError; - } - - strcpy(name, script_path->d_name); - - err = pcilib_py_ctx_load_script(self->py, name); - - if(err) { - delete_Pcipywrap(self); - set_python_exception("pcilib_py_ctx_load_script (%i)", err); - return NULL; - } - - self->names = realloc(self->names, ++(self->names_size)); - if(!self->names) { - delete_Pcipywrap(self); - return (Pcipywrap *)PyExc_MemoryError; - } - self->names[self->names_size - 1] = NULL; - self->names[self->names_size - 2] = name; - } - closedir(dir); - } - } - return self; } @@ -404,27 +350,14 @@ Pcipywrap *create_Pcipywrap(PyObject* ctx) self = (Pcipywrap *) malloc(sizeof(Pcipywrap)); self->shared = 1; self->ctx = PyCObject_AsVoidPtr(ctx); - self->py = NULL; - self->names = NULL; - self->names_size = 0; return self; } void delete_Pcipywrap(Pcipywrap *self) { - if(!self->shared) - pcilib_close(self->ctx); - - pcilib_free_py_ctx(self->py); - - if(self->names) { - for(int i = 0; self->names[i]; i++) - free(self->names[i]); - free(self->names); - self->names = NULL; - self->names_size = 0; - } - - free(self); + if(!self->shared) + pcilib_close(self->ctx); + + free(self); } PyObject* Pcipywrap_read_register(Pcipywrap *self, const char *regname, const char *bank) @@ -531,7 +464,7 @@ PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank) set_python_exception("pcilib_get_register_list return NULL"); return NULL; } - + PyObject* pyList = PyList_New(0); for(int i = 0; list[i].name; i++) { @@ -676,20 +609,4 @@ PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) return PyInt_FromLong((long)1); } -PyObject* Pcipywrap_get_scripts_list(Pcipywrap *self) -{ - return pcilib_py_ctx_get_scripts_info(self->py); -} -PyObject* Pcipywrap_run_script(Pcipywrap *self, const char* script_name, PyObject* value) -{ - int err = 0; - PyObject* value_out = pcilib_py_ctx_eval_func(self->py, script_name, "run", value, &err); - - if(err) { - set_python_exception("Failed pcilib_py_ctx_eval_func (%i)", err); - return NULL; - } - - return value_out; -} -- cgit v1.2.3 From 02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Wed, 2 Mar 2016 14:38:59 +0100 Subject: Add Python3 support --- pywrap/pcipywrap.c | 101 +++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 50 deletions(-) (limited to 'pywrap/pcipywrap.c') diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index 0f6729e..a1092be 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -128,12 +128,12 @@ void add_pcilib_value_to_dict(pcilib_t* ctx, PyObject* dict, pcilib_value_t* val if(py_val) pcilib_pydict_set_item(dict, - PyString_FromString(name), + PyUnicode_FromString(name), py_val); else pcilib_pydict_set_item(dict, - PyString_FromString("defvalue"), - PyString_FromString("invalid")); + PyUnicode_FromString("defvalue"), + PyUnicode_FromString("invalid")); } PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_property_info_t listItem) @@ -142,18 +142,18 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper if(listItem.name) pcilib_pydict_set_item(pylistItem, - PyString_FromString("name"), - PyString_FromString(listItem.name)); + PyUnicode_FromString("name"), + PyUnicode_FromString(listItem.name)); if(listItem.description) pcilib_pydict_set_item(pylistItem, - PyString_FromString("description"), - PyString_FromString(listItem.description)); + PyUnicode_FromString("description"), + PyUnicode_FromString(listItem.description)); if(listItem.path) pcilib_pydict_set_item(pylistItem, - PyString_FromString("path"), - PyString_FromString(listItem.path)); + PyUnicode_FromString("path"), + PyUnicode_FromString(listItem.path)); //serialize types const char* type = "invalid"; @@ -175,40 +175,40 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper break; } pcilib_pydict_set_item(pylistItem, - PyString_FromString("type"), - PyString_FromString(type)); + PyUnicode_FromString("type"), + PyUnicode_FromString(type)); //serialize modes PyObject* modes = PyList_New(0); if((listItem.mode & PCILIB_ACCESS_R ) == PCILIB_REGISTER_R) - pcilib_pylist_append(modes, PyString_FromString("R")); + pcilib_pylist_append(modes, PyUnicode_FromString("R")); if((listItem.mode & PCILIB_ACCESS_W ) == PCILIB_REGISTER_W) - pcilib_pylist_append(modes, PyString_FromString("W")); + pcilib_pylist_append(modes, PyUnicode_FromString("W")); if((listItem.mode & PCILIB_ACCESS_RW ) == PCILIB_REGISTER_RW) - pcilib_pylist_append(modes, PyString_FromString("RW")); + pcilib_pylist_append(modes, PyUnicode_FromString("RW")); if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) - pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); + pcilib_pylist_append(modes, PyUnicode_FromString("NO_CHK")); pcilib_pydict_set_item(pylistItem, - PyString_FromString("mode"), + PyUnicode_FromString("mode"), modes); //serialize flags PyObject* flags = PyList_New(0); if((listItem.flags & PCILIB_LIST_FLAG_CHILDS ) == PCILIB_LIST_FLAG_CHILDS) - pcilib_pylist_append(flags, PyString_FromString("childs")); + pcilib_pylist_append(flags, PyUnicode_FromString("childs")); pcilib_pydict_set_item(pylistItem, - PyString_FromString("flags"), + PyUnicode_FromString("flags"), flags); if(listItem.unit) pcilib_pydict_set_item(pylistItem, - PyString_FromString("unit"), - PyString_FromString(listItem.unit)); + PyUnicode_FromString("unit"), + PyUnicode_FromString(listItem.unit)); return pylistItem; } @@ -219,42 +219,42 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist if(listItem.name) pcilib_pydict_set_item(pylistItem, - PyString_FromString("name"), - PyString_FromString(listItem.name)); + PyUnicode_FromString("name"), + PyUnicode_FromString(listItem.name)); if(listItem.description) pcilib_pydict_set_item(pylistItem, - PyString_FromString("description"), - PyString_FromString(listItem.description)); + PyUnicode_FromString("description"), + PyUnicode_FromString(listItem.description)); if(listItem.bank) pcilib_pydict_set_item(pylistItem, - PyString_FromString("bank"), - PyString_FromString(listItem.bank)); + PyUnicode_FromString("bank"), + PyUnicode_FromString(listItem.bank)); //serialize modes PyObject* modes = PyList_New(0); if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R) - pcilib_pylist_append(modes, PyString_FromString("R")); + pcilib_pylist_append(modes, PyUnicode_FromString("R")); if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W) - pcilib_pylist_append(modes, PyString_FromString("W")); + pcilib_pylist_append(modes, PyUnicode_FromString("W")); if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) - pcilib_pylist_append(modes, PyString_FromString("RW")); + pcilib_pylist_append(modes, PyUnicode_FromString("RW")); if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C) - pcilib_pylist_append(modes, PyString_FromString("W1C")); + pcilib_pylist_append(modes, PyUnicode_FromString("W1C")); if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C) - pcilib_pylist_append(modes, PyString_FromString("RW1C")); + pcilib_pylist_append(modes, PyUnicode_FromString("RW1C")); if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I) - pcilib_pylist_append(modes, PyString_FromString("W1I")); + pcilib_pylist_append(modes, PyUnicode_FromString("W1I")); if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I) - pcilib_pylist_append(modes, PyString_FromString("RW1I")); + pcilib_pylist_append(modes, PyUnicode_FromString("RW1I")); if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT) - pcilib_pylist_append(modes, PyString_FromString("NO_CHK")); + pcilib_pylist_append(modes, PyUnicode_FromString("NO_CHK")); pcilib_pydict_set_item(pylistItem, - PyString_FromString("mode"), + PyUnicode_FromString("mode"), modes); pcilib_value_t defval = {0}; @@ -273,7 +273,7 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist add_pcilib_value_to_dict(ctx, range, &minval, "min"); add_pcilib_value_to_dict(ctx, range, &maxval, "max"); pcilib_pydict_set_item(pylistItem, - PyString_FromString("range"), + PyUnicode_FromString("range"), range); } @@ -301,20 +301,20 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist if(listItem.values[j].name) pcilib_pydict_set_item(valuesItem, - PyString_FromString("name"), - PyString_FromString(listItem.values[j].name)); + PyUnicode_FromString("name"), + PyUnicode_FromString(listItem.values[j].name)); if(listItem.values[j].description) { pcilib_pydict_set_item(valuesItem, - PyString_FromString("description"), - PyString_FromString(listItem.values[j].description)); + PyUnicode_FromString("description"), + PyUnicode_FromString(listItem.values[j].description)); } pcilib_pylist_append(values, valuesItem); } pcilib_pydict_set_item(pylistItem, - PyString_FromString("values"), + PyUnicode_FromString("values"), values); } @@ -340,16 +340,17 @@ Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model) Pcipywrap *create_Pcipywrap(PyObject* ctx) { - if(!PyCObject_Check(ctx)) + if(!PyCapsule_CheckExact(ctx)) { - set_python_exception("Incorrect ctx type. Only PyCObject is allowed"); + set_python_exception("Incorrect ctx type. Only PyCapsule is allowed"); return NULL; } Pcipywrap *self; self = (Pcipywrap *) malloc(sizeof(Pcipywrap)); self->shared = 1; - self->ctx = PyCObject_AsVoidPtr(ctx); + self->ctx = PyCapsule_GetPointer(ctx, PyCapsule_GetName(ctx)); + return self; } @@ -415,7 +416,7 @@ PyObject* Pcipywrap_write_register(Pcipywrap *self, PyObject* val, const char *r return NULL; } - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } PyObject* Pcipywrap_get_property(Pcipywrap *self, const char *prop) @@ -453,7 +454,7 @@ PyObject* Pcipywrap_set_property(Pcipywrap *self, PyObject* val, const char *pro return NULL; } - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank) @@ -542,7 +543,7 @@ PyObject* Pcipywrap_lock_global(Pcipywrap *self) return NULL; } - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } void Pcipywrap_unlock_global(Pcipywrap *self) @@ -570,7 +571,7 @@ PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) return NULL; } - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id) @@ -591,7 +592,7 @@ PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id) return NULL; } - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) @@ -606,7 +607,7 @@ PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) } pcilib_unlock(lock); - return PyInt_FromLong((long)1); + return PyLong_FromLong((long)1); } -- cgit v1.2.3