From 3077bbea252c62e8abf825ad11bf5118d79a2ba3 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 1 Mar 2022 10:34:03 +0100 Subject: Add Python error handling to video_images_get If for any reason the data stream from the camera is no longer available, without these checks the entire python interpreter crashes with a nice segmentation fault. These checks allow to handle the error from within Python with something like: try: o3000.video_images_get() except RuntimeError: ... restart o3000 driver Also, there is a change in the compilation flags to show more (useful) warnings, i.e. -Wall and hide the "unused variable" warning. --- c_extension.c | 21 +++++++++++++++++---- setup_c_extension.py | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/c_extension.c b/c_extension.c index 7aebced..d2e7e4e 100644 --- a/c_extension.c +++ b/c_extension.c @@ -328,16 +328,28 @@ _abort_video_main1: static PyObject* video_images_get(PyObject* self, PyObject* args) { - npy_intp dims[3]; + if (!recthread_data_recent) { + // fprintf(stderr, "%s: no data from video thread", __func__); + PyErr_SetString(PyExc_RuntimeError, "No data from video thread"); + return NULL; + } struct color_pipe_t *cp = recthread_data_recent->color_pipe; - + + if (!cp) { + // fprintf(stderr, "%s: cannot read color pipeline\n", __func__); + PyErr_SetString(PyExc_RuntimeError, "Cannot read color pipeline"); + return NULL; + } if (!is_color) { - fprintf(stderr, "%s: not implemented for gray images\n", __func__); + // fprintf(stderr, "%s: not implemented for gray images\n", __func__); + PyErr_SetString(PyExc_RuntimeError, "Not implemented for gray images"); return NULL; } + npy_intp dims[3]; + dims[0] = cp->height; dims[1] = cp->width; dims[2] = 3; @@ -388,7 +400,7 @@ static PyObject* video_deinit(PyObject* self, PyObject* args) { static PyObject* video_xml_send(PyObject* self, PyObject* args) { Py_ssize_t count; - const uint8_t* str; + const char* str; if (!PyArg_ParseTuple(args, "s#", &str, &count)) { return NULL; } @@ -421,3 +433,4 @@ PyMODINIT_FUNC PyInit_o3000(void) { _import_array(); return PyModule_Create(&o3000); } + diff --git a/setup_c_extension.py b/setup_c_extension.py index e01837f..f1f3e08 100644 --- a/setup_c_extension.py +++ b/setup_c_extension.py @@ -12,5 +12,5 @@ setup(name = 'o3000', version = '1.0', sources = ['c_extension.c', 'helpers.c'], libraries = ['o3000', 'o3000_imgpipe', 'tiff'], include_dirs = [str(numpy_headers_path)], - # extra_compile_args = ['-Wextra'] + extra_compile_args = ['-Wextra', '-Wall', '-Wno-unused-parameter'] )]) -- cgit v1.2.1