python: Make use of the new structure.is_writable method

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/9027>
This commit is contained in:
Thibault Saunier 2025-05-23 13:09:04 +02:00
parent 78a44afc22
commit 515bf888a9
3 changed files with 32 additions and 10 deletions

View File

@ -423,31 +423,25 @@ class Structure(Gst.Structure):
raise TypeError("wrong arguments when creating GstStructure, first argument"
" must be the structure name.")
struct = Structure.new_empty()
struct._writable = True
return struct
elif len(args) > 1:
raise TypeError("wrong arguments when creating GstStructure object")
elif isinstance(args[0], str):
if not kwargs:
struct = Structure.from_string(args[0])[0]
struct._writable = True
return struct
struct = Structure.new_empty(args[0])
struct._writable = True
for k, v in kwargs.items():
struct[k] = v
struct._writable = True
return struct
elif isinstance(args[0], Structure):
struct = args[0].copy()
struct._writable = True
return struct
raise TypeError("wrong arguments when creating GstStructure object")
def __init__(self, *args, **kwargs):
self._writable = False
pass
def __ptr__(self):
@ -470,8 +464,8 @@ class Structure(Gst.Structure):
return self.set_value(key, value)
def set_value(self, key, value):
if not self._writable:
raise NotWritableStructure("Trying to write to a not writable."
if not _gi_gst.structure_is_writable(self):
raise NotWritableStructure("Trying to write to a not writable structure."
" Make sure to use the right APIs to have access to structure"
" in a writable way.")
@ -855,7 +849,6 @@ def pairwise(iterable):
class StructureWrapper:
def __init__(self, structure, parent, writable):
structure._writable = writable
self.__structure = structure
self.__parent__ = parent
@ -863,7 +856,6 @@ class StructureWrapper:
return self.__structure
def __exit__(self, _type, _value, _tb):
self.__structure._writable = False
self.__parent__ = False
return

View File

@ -1026,6 +1026,30 @@ _gst_mini_object_set_flags (PyObject * self, PyObject * args)
Py_RETURN_NONE;
}
static PyObject *
_gst_structure_is_writable (PyObject * self, PyObject * args)
{
PyObject *py_structure, *res;
GstStructure *structure;
py_structure = PyTuple_GetItem (args, 0);
if (py_structure == NULL) {
PyErr_SetString (PyExc_TypeError, "Expected a PyObject");
return NULL;
}
structure = GST_STRUCTURE (pygobject_get (py_structure));
if (gst_structure_is_writable (structure)) {
Py_INCREF (Py_True);
res = Py_True;
} else {
Py_INCREF (Py_False);
res = Py_False;
}
return res;
}
static PyObject *
_gst_mini_object_is_writable (PyObject * self, PyObject * args)
{
@ -1650,9 +1674,12 @@ static PyMethodDef _gi_gst_functions[] = {
{"memory_override_map", (PyCFunction) _gst_memory_override_map, METH_VARARGS, NULL},
{"memory_override_unmap", (PyCFunction) _gst_memory_override_unmap, METH_VARARGS, NULL},
{"structure_is_writable", (PyCFunction) _gst_structure_is_writable, METH_VARARGS, NULL},
{"caps_get_structure", (PyCFunction) _gst_caps_get_structure, METH_VARARGS, NULL},
{"caps_writable_structure", (PyCFunction) _gst_caps_writable_structure, METH_VARARGS, NULL},
{"mini_object_make_writable", (PyCFunction) _gst_mini_object_make_writable, METH_VARARGS, NULL},
{"mini_object_is_writable", (PyCFunction) _gst_mini_object_is_writable, METH_VARARGS, NULL},
{"mini_object_flags", (PyCFunction) _gst_mini_object_flags, METH_VARARGS, NULL},

View File

@ -174,6 +174,9 @@ class TestCaps(TestCase):
c2 = caps.mini_object
self.assertEqual(c2.refcount, 2)
with caps.get_structure(0) as s:
with self.assertRaises(Gst.NotWritableStructure):
s.set_value("rate", 44100)
caps.make_writable()
with caps.get_structure(0) as s: