diff options
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/error.c | 13 | ||||
-rw-r--r-- | pcilib/py.c | 69 |
2 files changed, 66 insertions, 16 deletions
diff --git a/pcilib/error.c b/pcilib/error.c index ae8bacb..8d25c82 100644 --- a/pcilib/error.c +++ b/pcilib/error.c @@ -12,8 +12,17 @@ #define PCILIB_LOGGER_HISTORY 16 void pcilib_print_error(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va) { - vprintf(msg, va); - printf(" [%s:%d]\n", file, line); + size_t size = strlen(msg) + strlen(file) + 64; + char *stmp = alloca(size * sizeof(char*)); + + if (stmp) { + sprintf(stmp, "%s [%s:%d]\n", msg, file, line); + vprintf(stmp, va); + } else { + // Bad for multithreading... + vprintf(msg, va); + printf(" [%s:%d]\n", file, line); + } } static void *pcilib_logger_argument = NULL; diff --git a/pcilib/py.c b/pcilib/py.c index 3e1f38d..9e4ca90 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -17,6 +17,8 @@ #include "error.h" #ifdef HAVE_PYTHON +#define PCILIB_PYTHON_WRAPPER "pcipywrap" + typedef struct pcilib_script_s pcilib_script_t; struct pcilib_script_s { @@ -43,18 +45,41 @@ void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flag PyGILState_STATE gstate; PyObject *pytype = NULL; PyObject *pyval = NULL; + PyObject *pystr = NULL; PyObject *pytraceback = NULL; gstate = PyGILState_Ensure(); if (PyErr_Occurred()) { PyErr_Fetch(&pytype, &pyval, &pytraceback); - type = PyString_AsString(pytype); - val = PyString_AsString(pyval); + PyErr_NormalizeException(&pytype, &pyval, &pytraceback); + if (pyval) pystr = PyObject_Str(pyval); + +# if PY_MAJOR_VERSION >= 3 + if (pytype) { + if (PyUnicode_Check(pytype)) + type = PyUnicode_AsUTF8(pytype); + else + type = PyExceptionClass_Name(pytype); + } + if (pystr) { + val = PyUnicode_AsUTF8(pystr); + } +# else /* PY_MAJOR_VERSION >= 3 */ + if (pytype) { + if (PyString_Check(pytype)) + type = PyString_AsString(pytype); + else + type = PyExceptionClass_Name(pytype); + } + if (pystr) { + val = PyString_AsString(pystr); + } +# endif /*PY_MAJOR_VERSION >= 3*/ } PyGILState_Release(gstate); #endif /* HAVE_PYTHON */ - + va_start(va, msg); if (type) { char *str; @@ -83,6 +108,7 @@ void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flag va_end(va); #ifdef HAVE_PYTHON + if (pystr) Py_DECREF(pystr); if (pytype) Py_DECREF(pytype); if (pyval) Py_DECREF(pyval); if (pytraceback) Py_DECREF(pytraceback); @@ -119,14 +145,14 @@ int pcilib_init_py(pcilib_t *ctx) { return PCILIB_ERROR_FAILED; } - PyObject *pywrap = PyImport_ImportModule("pcipywrap"); + PyObject *pywrap = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER); if (!pywrap) { pcilib_python_error("Error importing pcilib python wrapper"); return PCILIB_ERROR_FAILED; } - PyObject *mod_name = PyString_FromString("Pcipywrap"); - PyObject *pyctx = PyCObject_FromVoidPtr(ctx, NULL); + PyObject *mod_name = PyUnicode_FromString(PCILIB_PYTHON_WRAPPER); + PyObject *pyctx = PyCapsule_New(ctx, "pcilib", NULL); ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL); Py_XDECREF(pyctx); Py_XDECREF(mod_name); @@ -166,7 +192,7 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { return PCILIB_ERROR_FAILED; } - pynewdir = PyString_FromString(script_dir); + pynewdir = PyUnicode_FromString(script_dir); if (!pynewdir) { pcilib_python_error("Can't create python string"); return PCILIB_ERROR_MEMORY; @@ -175,13 +201,13 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { // Checking if the directory already in the path? pydict = PyDict_New(); if (pydict) { - pystr = PyString_FromString("cur"); + pystr = PyUnicode_FromString("cur"); if (pystr) { PyDict_SetItem(pydict, pystr, pynewdir); Py_DECREF(pystr); } - pystr = PyString_FromString("path"); + pystr = PyUnicode_FromString("path"); if (pystr) { PyDict_SetItem(pydict, pystr, pypath); Py_DECREF(pystr); @@ -292,13 +318,13 @@ int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_ return PCILIB_ERROR_FAILED; } - pystr = PyString_FromString("read_from_register"); + pystr = PyUnicode_FromString("read_from_register"); if (pystr) { if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R; Py_DECREF(pystr); } - pystr = PyString_FromString("write_to_register"); + pystr = PyUnicode_FromString("write_to_register"); if (pystr) { if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W; Py_DECREF(pystr); @@ -322,7 +348,7 @@ pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *va switch(val->type) { case PCILIB_TYPE_LONG: ival = pcilib_get_value_as_int(ctx, val, &err); - if (!err) res = (PyObject*)PyInt_FromLong(ival); + if (!err) res = (PyObject*)PyLong_FromLong(ival); break; case PCILIB_TYPE_DOUBLE: fval = pcilib_get_value_as_float(ctx, val, &err); @@ -359,12 +385,27 @@ int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py PyGILState_STATE gstate; gstate = PyGILState_Ensure(); - if (PyInt_Check(pyval)) { - err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval)); + if (PyLong_Check(pyval)) { + err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval)); } else if (PyFloat_Check(pyval)) { err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval)); +#if PY_MAJOR_VERSION < 3 + } else if (PyInt_Check(pyval)) { + err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval)); } else if (PyString_Check(pyval)) { err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval)); + } else if (PyUnicode_Check(pyval)) { + PyObject *buf = PyUnicode_AsASCIIString(pyval); + if (buf) { + err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf)); + Py_DecRef(buf); + } else { + err = PCILIB_ERROR_FAILED; + } +#else /* PY_MAJOR_VERSION < 3 */ + } else if (PyUnicode_Check(pyval)) { + err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval)); +#endif /* PY_MAJOR_VERSION < 3 */ } else { PyGILState_Release(gstate); pcilib_error("Can't convert PyObject to polymorphic pcilib value"); |