From d365954fc0793d3d6edc12a1532682e676a753d0 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Tue, 10 Dec 2019 11:58:01 -0300 Subject: Subclass Exception for mapping and unmapping errors And minor cleanup in the way errors are handled --- examples/plugins/python/exampleTransform.py | 16 +++++++++------ gi/overrides/Gst.py | 25 +++++++++++++---------- gi/overrides/gstmodule.c | 31 ++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/examples/plugins/python/exampleTransform.py b/examples/plugins/python/exampleTransform.py index 3d9bdb3..ed73904 100755 --- a/examples/plugins/python/exampleTransform.py +++ b/examples/plugins/python/exampleTransform.py @@ -38,12 +38,16 @@ class ExampleTransform(GstBase.BaseTransform): return True def do_transform_ip(self, buf): - with buf.map(Gst.MapFlags.READ | Gst.MapFlags.WRITE) as info: - # Create a NumPy ndarray from the memoryview and modify it in place: - A = np.ndarray(shape = (self.height, self.width), dtype = np.uint8, buffer = info.data) - A[:] = np.invert(A) - - return Gst.FlowReturn.OK + try: + with buf.map(Gst.MapFlags.READ | Gst.MapFlags.WRITE) as info: + # Create a NumPy ndarray from the memoryview and modify it in place: + A = np.ndarray(shape = (self.height, self.width), dtype = np.uint8, buffer = info.data) + A[:] = np.invert(A) + + return Gst.FlowReturn.OK + except Gst.MapError as e: + Gst.error("Mapping error: %s" % e) + return Gst.FlowReturn.ERROR GObject.type_register(ExampleTransform) __gstelementfactory__ = ("ExampleTransform", Gst.Rank.NONE, ExampleTransform) diff --git a/gi/overrides/Gst.py b/gi/overrides/Gst.py index 54bdd5d..c62d9e1 100644 --- a/gi/overrides/Gst.py +++ b/gi/overrides/Gst.py @@ -225,6 +225,11 @@ class LinkError(Exception): pass __all__.append('LinkError') +class MapError(Exception): + pass +__all__.append('MapError') + + class Iterator(Gst.Iterator): def __iter__(self): while True: @@ -612,23 +617,22 @@ __all__.append("MapInfo") class Buffer(Gst.Buffer): def map_range(self, idx, length, flags): - mapinfo = MapInfo() + mapinfo = MapInfo() if (_gi_gst.buffer_override_map_range(self, mapinfo, idx, length, int(flags))): mapinfo.__parent__ = self return (mapinfo) - raise Exception('MappingError','Buffer mapping was not successfull') - return None + raise MapError('MappingError','Buffer mapping was not successfull') def map(self, flags): mapinfo = MapInfo() - if (_gi_gst.buffer_override_map(self, mapinfo, int(flags))): + if _gi_gst.buffer_override_map(self, mapinfo, int(flags)): mapinfo.__parent__ = self - return (mapinfo) - raise Exception('MappingError','Buffer mapping was not successfull') - return None + return mapinfo + raise MapError('MappingError','Buffer mapping was not successfull') def unmap(self, mapinfo): - _gi_gst.buffer_override_unmap(self, mapinfo) + if _gi_gst.buffer_override_unmap(self, mapinfo) is not True: + raise MapError('UnmappingError','Buffer unmapping was not successfull') Buffer = override(Buffer) __all__.append('Buffer') @@ -640,11 +644,10 @@ class Memory(Gst.Memory): if (_gi_gst.memory_override_map(self, mapinfo, int(flags))): mapinfo.__parent__ = self return (mapinfo) - raise Exception('MappingError','Memory mapping was not successfull') - return None + raise MapError('MappingError','Memory mapping was not successfull') def unmap(self, mapinfo): - _gi_gst.memory_override_unmap(self, mapinfo) + return _gi_gst.memory_override_unmap(self, mapinfo) Memory = override(Memory) __all__.append('Memory') diff --git a/gi/overrides/gstmodule.c b/gi/overrides/gstmodule.c index 6e65753..b0986b2 100644 --- a/gi/overrides/gstmodule.c +++ b/gi/overrides/gstmodule.c @@ -805,14 +805,18 @@ _gst_memory_override_unmap (PyObject * self, PyObject * args) /* Look up Gst.Buffer and Gst.Mapinfo parameters */ gst_memory_type = pygobject_lookup_class (_gst_memory_type); - if (!PyArg_ParseTuple (args, "O!O", gst_memory_type, &py_memory, &py_mapinfo)) + if (!PyArg_ParseTuple (args, "O!O", gst_memory_type, &py_memory, &py_mapinfo)) { + PyErr_BadArgument (); return NULL; + } /* Extract attributes from Gst.MapInfo */ if (!(mview = PyObject_GetAttrString (py_mapinfo, "data"))) goto err; + if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo")) goto end; + if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo"))) goto err; @@ -835,10 +839,10 @@ _gst_memory_override_unmap (PyObject * self, PyObject * args) g_free (mapinfo); end: Py_DECREF (mview); - Py_RETURN_NONE; + return Py_True; err: - return NULL; + return Py_False; } static PyObject * @@ -856,7 +860,7 @@ _gst_buffer_override_map_range (PyObject * self, PyObject * args) gst_buffer_type = pygobject_lookup_class (_gst_buffer_type); if (!PyArg_ParseTuple (args, "O!OIii", gst_buffer_type, &py_buffer, &py_mapinfo, &idx, &range, &flags)) - return NULL; + return Py_False; /* Since Python does only support r/o or r/w it has to be changed to either */ flags = (flags & GST_MAP_WRITE) ? GST_MAP_READWRITE : GST_MAP_READ; @@ -893,8 +897,10 @@ _gst_buffer_override_map (PyObject * self, PyObject * args) /* Look up Gst.Buffer, Gst.MapInfo, and Gst.MapFlags parameters */ gst_buffer_type = pygobject_lookup_class (_gst_buffer_type); if (!PyArg_ParseTuple (args, "O!Oi", gst_buffer_type, &py_buffer, &py_mapinfo, - &flags)) + &flags)) { + PyErr_BadArgument (); return NULL; + } /* Since Python does only support r/o or r/w it has to be changed to either */ flags = (flags & GST_MAP_WRITE) ? GST_MAP_READWRITE : GST_MAP_READ; @@ -928,14 +934,18 @@ _gst_buffer_override_unmap (PyObject * self, PyObject * args) /* Look up Gst.Buffer and Gst.Mapinfo parameters */ gst_buf_type = pygobject_lookup_class (_gst_buffer_type); - if (!PyArg_ParseTuple (args, "O!O", gst_buf_type, &py_buffer, &py_mapinfo)) + if (!PyArg_ParseTuple (args, "O!O", gst_buf_type, &py_buffer, &py_mapinfo)) { + PyErr_BadArgument (); return NULL; + } /* Extract attributes from Gst.MapInfo */ if (!(mview = PyObject_GetAttrString (py_mapinfo, "data"))) goto err; + if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo")) goto end; + if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo"))) goto err; @@ -946,8 +956,11 @@ _gst_buffer_override_unmap (PyObject * self, PyObject * args) /* Call the memoryview.release() Python method, there is no C API */ PyObject *ret = PyObject_CallMethod (mview, "release", NULL); - if (!ret) + if (!ret) { + GST_ERROR ("Could not call `.release()` on the memoryview."); + goto err; + } Py_DECREF (ret); Py_DECREF (py_cmapinfo); PyObject_SetAttrString (py_mapinfo, "__cmapinfo", NULL); @@ -957,10 +970,10 @@ _gst_buffer_override_unmap (PyObject * self, PyObject * args) g_free (mapinfo); end: Py_DECREF (mview); - Py_RETURN_NONE; + return Py_True; err: - return NULL; + return Py_False; } static PyMethodDef _gi_gst_functions[] = { -- cgit v1.2.3