X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=46336549609cfc3a46cd665fbb97b6b8c1168db3;hb=df979d6dc6949b1d5c3814b177cb71d6c40d03d4;hp=856038eeb37cf01a0929c6db6e04a1eeea023cf3;hpb=ee9953ef12514d833c6719777dfc285d56c0ffc1;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 856038ee..46336549 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -47,6 +47,7 @@ which provides access to the error code and description." %{ #include #include +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include PyObject *PyGObject_lib; @@ -74,7 +75,7 @@ typedef guint pyg_flags_type; if (!GLib) { fprintf(stderr, "Import of gi.repository.GLib failed.\n"); #if PY_VERSION_HEX >= 0x03000000 - return NULL; + return nullptr; #else return; #endif @@ -82,6 +83,8 @@ typedef guint pyg_flags_type; import_array(); %} +%include "../../../swig/templates.i" + /* Map file objects to file descriptors. */ %typecheck(SWIG_TYPECHECK_POINTER) int fd { $1 = (PyObject_AsFileDescriptor($input) != -1); @@ -94,8 +97,7 @@ typedef guint pyg_flags_type; g_value_set_variant(value, $1.gobj()); PyObject *variant = pyg_value_as_pyobject(value, true); $result = PyObject_CallMethod(variant, - const_cast("unpack"), - const_cast(""), NULL); + const_cast("unpack"), nullptr); Py_XDECREF(variant); g_free(value); } @@ -150,6 +152,39 @@ typedef guint pyg_flags_type; Py_XINCREF($input); } +/* Map from callable PyObject to SessionStoppedCallback */ +%typecheck(SWIG_TYPECHECK_POINTER) sigrok::SessionStoppedCallback { + $1 = PyCallable_Check($input); +} + +%typemap(in) sigrok::SessionStoppedCallback { + if (!PyCallable_Check($input)) + SWIG_exception(SWIG_TypeError, "Expected a callable Python object"); + + $1 = [=] () { + const auto gstate = PyGILState_Ensure(); + + const auto result = PyEval_CallObject($input, nullptr); + const bool completed = !PyErr_Occurred(); + const bool valid_result = (completed && result == Py_None); + + if (completed && !valid_result) { + PyErr_SetString(PyExc_TypeError, + "Session stop callback did not return None"); + } + if (!valid_result) + PyErr_Print(); + + Py_XDECREF(result); + PyGILState_Release(gstate); + + if (!valid_result) + throw sigrok::Error(SR_ERR); + }; + + Py_XINCREF($input); +} + /* Map from callable PyObject to DatafeedCallbackFunction */ %typecheck(SWIG_TYPECHECK_POINTER) sigrok::DatafeedCallbackFunction { $1 = PyCallable_Check($input); @@ -349,7 +384,7 @@ std::map dict_to_map_options(PyObject *dict, %ignore sigrok::InputFormat::create_input; %ignore sigrok::OutputFormat::create_output; -%include "doc.i" +%include "doc_start.i" %define %attributevector(Class, Type, Name, Get) %rename(_ ## Get) sigrok::Class::Get; @@ -492,3 +527,5 @@ std::map dict_to_map_options(PyObject *dict, data = property(_data) } } + +%include "doc_end.i"