diff options
author | Vasilii Chernov <vchernov@inr.ru> | 2016-02-11 12:00:54 +0100 |
---|---|---|
committer | Vasilii Chernov <vchernov@inr.ru> | 2016-02-11 12:00:54 +0100 |
commit | f526c4a5a3e4f35bb1c4b03af1f3244a8c0db48a (patch) | |
tree | a818ed42d630e807e9886767e326c8970a77c78d /pcilib | |
parent | d9a31945a92a76e590657dc2e65bb20b6ea95d90 (diff) | |
download | pcitool-f526c4a5a3e4f35bb1c4b03af1f3244a8c0db48a.tar.gz pcitool-f526c4a5a3e4f35bb1c4b03af1f3244a8c0db48a.tar.bz2 pcitool-f526c4a5a3e4f35bb1c4b03af1f3244a8c0db48a.tar.xz pcitool-f526c4a5a3e4f35bb1c4b03af1f3244a8c0db48a.zip |
Change error logging method in Python wrap. Move functions, that converts values between PyObject and pcilib_value_t to py.c
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | pcilib/error.c | 11 | ||||
-rw-r--r-- | pcilib/pcilib.h | 15 | ||||
-rw-r--r-- | pcilib/pcipywrap.c | 177 | ||||
-rw-r--r-- | pcilib/pcipywrap.h | 25 | ||||
-rw-r--r-- | pcilib/pcipywrap.i | 3 | ||||
-rw-r--r-- | pcilib/py.c | 73 | ||||
-rw-r--r-- | pcilib/py.h | 20 |
8 files changed, 187 insertions, 139 deletions
diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index b1f5251..faebcab 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -8,7 +8,7 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS pcilib.h pci.h pcipywrap.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) +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) add_library(pcilib SHARED pci.c pcipywrap.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 dma protocols views) diff --git a/pcilib/error.c b/pcilib/error.c index 06af292..87380e7 100644 --- a/pcilib/error.c +++ b/pcilib/error.c @@ -76,3 +76,14 @@ int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, vo return 0; } + +pcilib_logger_t pcilib_get_logger() { + return pcilib_logger; +} +pcilib_log_priority_t pcilib_get_logger_min_prio() { + return pcilib_logger_min_prio; +} +void* pcilib_get_logger_argument() { + return pcilib_logger_argument; +} + diff --git a/pcilib/pcilib.h b/pcilib/pcilib.h index 4d2b7e9..23fe9be 100644 --- a/pcilib/pcilib.h +++ b/pcilib/pcilib.h @@ -263,6 +263,21 @@ extern "C" { */ int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg); +/** + * Gets current logger function. + */ +pcilib_logger_t pcilib_get_logger(); + +/** + * Gets current logger min priority. + */ +pcilib_log_priority_t pcilib_get_logger_min_prio(); + +/** + * Gets current logger argument. + */ +void* pcilib_get_logger_argument(); + /** public_api_global * @} */ diff --git a/pcilib/pcipywrap.c b/pcilib/pcipywrap.c index d950de3..db0e412 100644 --- a/pcilib/pcipywrap.c +++ b/pcilib/pcipywrap.c @@ -1,6 +1,6 @@ #include "pci.h" #include "error.h" -#include "pcipywrap.h" +#include <Python.h> /*! * \brief Global pointer to pcilib_t context. @@ -9,18 +9,15 @@ pcilib_t* __ctx = 0; /*! - * \brief Sets python exeption text. Function interface same as printf. + * \brief Wraping for vsnprintf function, that saves string to char* + * \return saved from vsnprintf string */ -void __setPyExeptionText(const char* msg, ...) +char* vmake_str(const char* msg, va_list vl) { - if(!Py_IsInitialized()) - Py_Initialize(); - char *buf; size_t sz; - va_list vl, vl_copy; - va_start(vl, msg); + va_list vl_copy; va_copy(vl_copy, vl); sz = vsnprintf(NULL, 0, msg, vl); @@ -28,16 +25,51 @@ void __setPyExeptionText(const char* msg, ...) if(!buf) { - PyErr_SetString(PyExc_Exception, "Memory error"); - return; + return NULL; } vsnprintf(buf, sz+1, msg, vl_copy); va_end(vl_copy); - va_end(vl); - PyErr_SetString(PyExc_Exception, buf); - free(buf); + return buf; +} + +/*! + * \brief Wraping for vsnprintf function, that saves string to char* + * \return saved from vsnprintf string + */ +char* make_str(const char* msg, ...) +{ + va_list vl; + va_start(vl, msg); + char *buf = vmake_str(msg, vl); + va_end(vl); +} + +/*! + * \brief Version of pcilib_logger_t, that saves error text to Python exeption + */ +void pcilib_print_error_to_py(void *arg, const char *file, int line, + pcilib_log_priority_t prio, const char *msg, + va_list va) { + char* buf_raw_msg = vmake_str(msg, va); + char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line); + + printf("%s", buf_wrapped_message); + PyErr_SetString(PyExc_Exception, buf_wrapped_message); + + free(buf_wrapped_message); + free(buf_raw_msg); +} + +/*! + * \brief Inits pcipywrap module at importing + */ +void init_pcipywrap_module() +{ + pcilib_set_logger(pcilib_get_logger_min_prio(), + pcilib_print_error_to_py, + pcilib_get_logger_argument()); } /*! @@ -51,10 +83,7 @@ PyObject* createPcilibInstance(const char *fpga_device, const char *model) //opening device pcilib_t* ctx = pcilib_open(fpga_device, model); if(!ctx) - { - __setPyExeptionText("Failed pcilib_open(%s, %s)", fpga_device, model); return NULL; - } //serializing object return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)); @@ -90,7 +119,7 @@ PyObject* setPcilib(PyObject* addr) { if(!PyByteArray_Check(addr)) { - __setPyExeptionText("Incorrect addr type. Only bytearray is allowed"); + pcilib_error("Incorrect addr type. Only bytearray is allowed"); return NULL; } @@ -104,82 +133,6 @@ PyObject* setPcilib(PyObject* addr) return PyInt_FromLong((long)1); } -PyObject* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val, void (*errstream)(const char* msg, ...)) -{ - int err; - - switch(val->type) - { - case PCILIB_TYPE_INVALID: - if(errstream) - errstream("Invalid register output type (PCILIB_TYPE_INVALID)"); - return NULL; - - case PCILIB_TYPE_STRING: - if(errstream) - errstream("Invalid register output type (PCILIB_TYPE_STRING)"); - return NULL; - - case PCILIB_TYPE_LONG: - { - long ret; - ret = pcilib_get_value_as_int(__ctx, val, &err); - - if(err) - { - if(errstream) - errstream("Failed: pcilib_get_value_as_int (%i)", err); - return NULL; - } - return PyInt_FromLong((long) ret); - } - - case PCILIB_TYPE_DOUBLE: - { - double ret; - ret = pcilib_get_value_as_float(__ctx, val, &err); - - if(err) - { - if(errstream) - errstream("Failed: pcilib_get_value_as_int (%i)", err); - return NULL; - } - return PyFloat_FromDouble((double) ret); - } - - default: - if(errstream) - errstream("Invalid register output type (unknown)"); - return NULL; - } -} - -int pcilib_convert_pyobject_to_val(pcilib_t* ctx, PyObject* pyVal, pcilib_value_t *val) -{ - int err; - - if(PyInt_Check(pyVal)) - { - err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal)); - } - else - if(PyFloat_Check(pyVal)) - err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal)); - else - if(PyString_Check(pyVal)) - err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal)); - else - { - pcilib_error("Invalid input. Input type should be int, float or string."); - return PCILIB_ERROR_NOTSUPPORTED; - } - if(err) - return err; - - return 0; -} - /*! * \brief Reads register value. Wrap for pcilib_read_register function. @@ -191,7 +144,7 @@ PyObject* read_register(const char *regname, const char *bank) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -203,18 +156,18 @@ PyObject* read_register(const char *regname, const char *bank) err = pcilib_read_register(__ctx, bank, regname, ®_value); if(err) { - __setPyExeptionText("Failed: pcilib_read_register (%i)", err); + pcilib_error("Failed: pcilib_read_register (%i)", err); return NULL; } err = pcilib_set_value_from_register_value(__ctx, &val, reg_value); if(err) { - __setPyExeptionText("Failed: pcilib_set_value_from_register_value (%i)", err); + pcilib_error("Failed: pcilib_set_value_from_register_value (%i)", err); return NULL; } - return pcilib_convert_val_to_pyobject(__ctx, &val, __setPyExeptionText); + return pcilib_convert_val_to_pyobject(__ctx, &val); } /*! @@ -228,7 +181,7 @@ PyObject* write_register(PyObject* val, const char *regname, const char *bank) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -239,21 +192,21 @@ PyObject* write_register(PyObject* val, const char *regname, const char *bank) err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal); if(err) { - __setPyExeptionText("Failed pcilib_convert_pyobject_to_val (%i)", err); + pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err); return NULL; } reg_value = pcilib_get_value_as_register_value(__ctx, &val_internal, &err); if(err) { - __setPyExeptionText("Failed: pcilib_get_value_as_register_value (%i)", err); + pcilib_error("Failed: pcilib_get_value_as_register_value (%i)", err); return NULL; } err = pcilib_write_register(__ctx, bank, regname, reg_value); if(err) { - __setPyExeptionText("Failed: pcilib_write_register (%i)", err); + pcilib_error("Failed: pcilib_write_register (%i)", err); return NULL; } return PyInt_FromLong((long)1); @@ -268,7 +221,7 @@ PyObject* get_property(const char *prop) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -279,11 +232,11 @@ PyObject* get_property(const char *prop) if(err) { - __setPyExeptionText("Failed pcilib_get_property (%i)", err); + pcilib_error("Failed pcilib_get_property (%i)", err); return NULL; } - return pcilib_convert_val_to_pyobject(__ctx, &val, __setPyExeptionText); + return pcilib_convert_val_to_pyobject(__ctx, &val); } /*! @@ -298,7 +251,7 @@ PyObject* set_property(const char *prop, PyObject* val) if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -306,7 +259,7 @@ PyObject* set_property(const char *prop, PyObject* val) err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal); if(err) { - __setPyExeptionText("Failed pcilib_convert_pyobject_to_val (%i)", err); + pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err); return NULL; } @@ -314,7 +267,7 @@ PyObject* set_property(const char *prop, PyObject* val) if(err) { - __setPyExeptionText("Failed pcilib_get_property (%i)", err); + pcilib_error("Failed pcilib_get_property (%i)", err); return NULL; } @@ -323,7 +276,7 @@ PyObject* set_property(const char *prop, PyObject* val) void add_pcilib_value_to_dict(PyObject* dict, pcilib_value_t* val, const char *name) { - PyObject *py_val = pcilib_convert_val_to_pyobject(__ctx, val, NULL); + PyObject *py_val = pcilib_convert_val_to_pyobject(__ctx, val); if(py_val) PyDict_SetItem(dict, @@ -503,7 +456,7 @@ PyObject* get_register_list(const char *bank) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -526,7 +479,7 @@ PyObject* get_register_info(const char* reg,const char *bank) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } @@ -534,7 +487,7 @@ PyObject* get_register_info(const char* reg,const char *bank) if(!info) { - __setPyExeptionText("Failed pcilib_get_register_info"); + pcilib_error("Failed pcilib_get_register_info"); return NULL; } @@ -549,7 +502,7 @@ PyObject* get_property_info(const char* branch) { if(!__ctx) { - __setPyExeptionText("pcilib_t handler not initialized"); + pcilib_error("pcilib_t handler not initialized"); return NULL; } diff --git a/pcilib/pcipywrap.h b/pcilib/pcipywrap.h deleted file mode 100644 index c2bb749..0000000 --- a/pcilib/pcipywrap.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _PCITOOL_PCIPYWRAP_H -#define _PCITOOL_PCIPYWRAP_H -#include <Python.h> -#include "pcilib.h" - -/*! - * \brief Converts pcilib_value_t to PyObject. - * \param ctx pointer to pcilib_t context - * \param val pointer to pcilib_value_t to convert - * \return PyObject, containing value. NULL with error message, sended to errstream. - */ -PyObject* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val, void (*errstream)(const char* msg, ...)); - - -/*! - * \brief Converts PyObject to pcilib_value_t. - * \param ctx pcilib context - * \param pyVal python object, containing value - * \param val initialized polymorphic value - * \return 0 on success or memory error - */ -int pcilib_convert_pyobject_to_val(pcilib_t* ctx, PyObject* pyVal, pcilib_value_t *val); - - -#endif /* _PCITOOL_PCIPYWRAP_H */ diff --git a/pcilib/pcipywrap.i b/pcilib/pcipywrap.i index 10a95a7..2da8654 100644 --- a/pcilib/pcipywrap.i +++ b/pcilib/pcipywrap.i @@ -1,5 +1,8 @@ %module pcipywrap +%init %{ + init_pcipywrap_module(); +%} extern PyObject* createPcilibInstance(const char *fpga_device, const char *model = NULL); extern PyObject* setPcilib(PyObject* addr); diff --git a/pcilib/py.c b/pcilib/py.c index 20b85a1..acc18df 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -196,6 +196,77 @@ int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *va return pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj)); } +void* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val) +{ + int err; + + switch(val->type) + { + case PCILIB_TYPE_INVALID: + pcilib_error("Invalid register output type (PCILIB_TYPE_INVALID)"); + return NULL; + + case PCILIB_TYPE_STRING: + pcilib_error("Invalid register output type (PCILIB_TYPE_STRING)"); + return NULL; + + case PCILIB_TYPE_LONG: + { + long ret; + ret = pcilib_get_value_as_int(ctx, val, &err); + + if(err) + { + pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); + return NULL; + } + return (PyObject*)PyInt_FromLong((long) ret); + } + + case PCILIB_TYPE_DOUBLE: + { + double ret; + ret = pcilib_get_value_as_float(ctx, val, &err); + + if(err) + { + pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); + return NULL; + } + return (PyObject*)PyFloat_FromDouble((double) ret); + } + + default: + pcilib_error("Invalid register output type (unknown)"); + return NULL; + } +} + +int pcilib_convert_pyobject_to_val(pcilib_t* ctx, void* pyObjVal, pcilib_value_t *val) +{ + PyObject* pyVal = pyObjVal; + int err; + + if(PyInt_Check(pyVal)) + { + err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal)); + } + else + if(PyFloat_Check(pyVal)) + err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal)); + else + if(PyString_Check(pyVal)) + err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal)); + else + { + pcilib_error("Invalid input. Input type should be int, float or string."); + return PCILIB_ERROR_NOTSUPPORTED; + } + if(err) + return err; + + return 0; +} int pcilib_init_py_script(pcilib_t *ctx, char* module_name, pcilib_script_t **module, pcilib_access_mode_t *mode) { @@ -347,7 +418,7 @@ int pcilib_script_write(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t * { int err; - PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf); + PyObject *input = pcilib_convert_val_to_pyobject(ctx, val); if(!input) { printf("Failed to convert input value to Python object"); diff --git a/pcilib/py.h b/pcilib/py.h index 8fd749c..d0f1328 100644 --- a/pcilib/py.h +++ b/pcilib/py.h @@ -20,6 +20,26 @@ int pcilib_free_py_script(pcilib_script_t *module); int pcilib_script_read(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val); int pcilib_script_write(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val); + +/*! + * \brief Converts pcilib_value_t to PyObject. + * \param ctx pointer to pcilib_t context + * \param val pointer to pcilib_value_t to convert + * \return PyObject, containing value. NULL with error message, sended to errstream. + */ +void* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val); + + +/*! + * \brief Converts PyObject to pcilib_value_t. + * \param ctx pcilib context + * \param pyVal python object, containing value + * \param val initialized polymorphic value + * \return 0 on success or memory error + */ +int pcilib_convert_pyobject_to_val(pcilib_t* ctx, void* pyVal, pcilib_value_t *val); + + #ifdef __cplusplus } #endif |