Subclass Exception for mapping and unmapping errors

And minor cleanup in the way errors are handled
This commit is contained in:
Thibault Saunier 2019-12-10 11:58:01 -03:00
parent 6d53d0ae0e
commit d365954fc0
3 changed files with 45 additions and 25 deletions

View file

@ -38,12 +38,16 @@ class ExampleTransform(GstBase.BaseTransform):
return True return True
def do_transform_ip(self, buf): def do_transform_ip(self, buf):
with buf.map(Gst.MapFlags.READ | Gst.MapFlags.WRITE) as info: try:
# Create a NumPy ndarray from the memoryview and modify it in place: with buf.map(Gst.MapFlags.READ | Gst.MapFlags.WRITE) as info:
A = np.ndarray(shape = (self.height, self.width), dtype = np.uint8, buffer = info.data) # Create a NumPy ndarray from the memoryview and modify it in place:
A[:] = np.invert(A) A = np.ndarray(shape = (self.height, self.width), dtype = np.uint8, buffer = info.data)
A[:] = np.invert(A)
return Gst.FlowReturn.OK return Gst.FlowReturn.OK
except Gst.MapError as e:
Gst.error("Mapping error: %s" % e)
return Gst.FlowReturn.ERROR
GObject.type_register(ExampleTransform) GObject.type_register(ExampleTransform)
__gstelementfactory__ = ("ExampleTransform", Gst.Rank.NONE, ExampleTransform) __gstelementfactory__ = ("ExampleTransform", Gst.Rank.NONE, ExampleTransform)

View file

@ -225,6 +225,11 @@ class LinkError(Exception):
pass pass
__all__.append('LinkError') __all__.append('LinkError')
class MapError(Exception):
pass
__all__.append('MapError')
class Iterator(Gst.Iterator): class Iterator(Gst.Iterator):
def __iter__(self): def __iter__(self):
while True: while True:
@ -612,23 +617,22 @@ __all__.append("MapInfo")
class Buffer(Gst.Buffer): class Buffer(Gst.Buffer):
def map_range(self, idx, length, flags): def map_range(self, idx, length, flags):
mapinfo = MapInfo() mapinfo = MapInfo()
if (_gi_gst.buffer_override_map_range(self, mapinfo, idx, length, int(flags))): if (_gi_gst.buffer_override_map_range(self, mapinfo, idx, length, int(flags))):
mapinfo.__parent__ = self mapinfo.__parent__ = self
return (mapinfo) return (mapinfo)
raise Exception('MappingError','Buffer mapping was not successfull') raise MapError('MappingError','Buffer mapping was not successfull')
return None
def map(self, flags): def map(self, flags):
mapinfo = MapInfo() 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 mapinfo.__parent__ = self
return (mapinfo) return mapinfo
raise Exception('MappingError','Buffer mapping was not successfull') raise MapError('MappingError','Buffer mapping was not successfull')
return None
def unmap(self, mapinfo): 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) Buffer = override(Buffer)
__all__.append('Buffer') __all__.append('Buffer')
@ -640,11 +644,10 @@ class Memory(Gst.Memory):
if (_gi_gst.memory_override_map(self, mapinfo, int(flags))): if (_gi_gst.memory_override_map(self, mapinfo, int(flags))):
mapinfo.__parent__ = self mapinfo.__parent__ = self
return (mapinfo) return (mapinfo)
raise Exception('MappingError','Memory mapping was not successfull') raise MapError('MappingError','Memory mapping was not successfull')
return None
def unmap(self, mapinfo): def unmap(self, mapinfo):
_gi_gst.memory_override_unmap(self, mapinfo) return _gi_gst.memory_override_unmap(self, mapinfo)
Memory = override(Memory) Memory = override(Memory)
__all__.append('Memory') __all__.append('Memory')

View file

@ -805,14 +805,18 @@ _gst_memory_override_unmap (PyObject * self, PyObject * args)
/* Look up Gst.Buffer and Gst.Mapinfo parameters */ /* Look up Gst.Buffer and Gst.Mapinfo parameters */
gst_memory_type = pygobject_lookup_class (_gst_memory_type); 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; return NULL;
}
/* Extract attributes from Gst.MapInfo */ /* Extract attributes from Gst.MapInfo */
if (!(mview = PyObject_GetAttrString (py_mapinfo, "data"))) if (!(mview = PyObject_GetAttrString (py_mapinfo, "data")))
goto err; goto err;
if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo")) if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo"))
goto end; goto end;
if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo"))) if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo")))
goto err; goto err;
@ -835,10 +839,10 @@ _gst_memory_override_unmap (PyObject * self, PyObject * args)
g_free (mapinfo); g_free (mapinfo);
end: end:
Py_DECREF (mview); Py_DECREF (mview);
Py_RETURN_NONE; return Py_True;
err: err:
return NULL; return Py_False;
} }
static PyObject * static PyObject *
@ -856,7 +860,7 @@ _gst_buffer_override_map_range (PyObject * self, PyObject * args)
gst_buffer_type = pygobject_lookup_class (_gst_buffer_type); gst_buffer_type = pygobject_lookup_class (_gst_buffer_type);
if (!PyArg_ParseTuple (args, "O!OIii", gst_buffer_type, &py_buffer, if (!PyArg_ParseTuple (args, "O!OIii", gst_buffer_type, &py_buffer,
&py_mapinfo, &idx, &range, &flags)) &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 */ /* 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; 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 */ /* Look up Gst.Buffer, Gst.MapInfo, and Gst.MapFlags parameters */
gst_buffer_type = pygobject_lookup_class (_gst_buffer_type); gst_buffer_type = pygobject_lookup_class (_gst_buffer_type);
if (!PyArg_ParseTuple (args, "O!Oi", gst_buffer_type, &py_buffer, &py_mapinfo, if (!PyArg_ParseTuple (args, "O!Oi", gst_buffer_type, &py_buffer, &py_mapinfo,
&flags)) &flags)) {
PyErr_BadArgument ();
return NULL; return NULL;
}
/* Since Python does only support r/o or r/w it has to be changed to either */ /* 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; 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 */ /* Look up Gst.Buffer and Gst.Mapinfo parameters */
gst_buf_type = pygobject_lookup_class (_gst_buffer_type); 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; return NULL;
}
/* Extract attributes from Gst.MapInfo */ /* Extract attributes from Gst.MapInfo */
if (!(mview = PyObject_GetAttrString (py_mapinfo, "data"))) if (!(mview = PyObject_GetAttrString (py_mapinfo, "data")))
goto err; goto err;
if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo")) if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo"))
goto end; goto end;
if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo"))) if (!(py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo")))
goto err; 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 */ /* Call the memoryview.release() Python method, there is no C API */
PyObject *ret = PyObject_CallMethod (mview, "release", NULL); PyObject *ret = PyObject_CallMethod (mview, "release", NULL);
if (!ret) if (!ret) {
GST_ERROR ("Could not call `.release()` on the memoryview.");
goto err; goto err;
}
Py_DECREF (ret); Py_DECREF (ret);
Py_DECREF (py_cmapinfo); Py_DECREF (py_cmapinfo);
PyObject_SetAttrString (py_mapinfo, "__cmapinfo", NULL); PyObject_SetAttrString (py_mapinfo, "__cmapinfo", NULL);
@ -957,10 +970,10 @@ _gst_buffer_override_unmap (PyObject * self, PyObject * args)
g_free (mapinfo); g_free (mapinfo);
end: end:
Py_DECREF (mview); Py_DECREF (mview);
Py_RETURN_NONE; return Py_True;
err: err:
return NULL; return Py_False;
} }
static PyMethodDef _gi_gst_functions[] = { static PyMethodDef _gi_gst_functions[] = {