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/py.c | |
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/py.c')
-rw-r--r-- | pcilib/py.c | 73 |
1 files changed, 72 insertions, 1 deletions
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"); |