From 515bf888a9040e46e619b2e40f5018f45e249867 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Fri, 23 May 2025 13:09:04 +0200 Subject: [PATCH] python: Make use of the new structure.is_writable method Part-of: --- subprojects/gst-python/gi/overrides/Gst.py | 12 ++------- .../gst-python/gi/overrides/gstmodule.c | 27 +++++++++++++++++++ subprojects/gst-python/testsuite/test_gst.py | 3 +++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/subprojects/gst-python/gi/overrides/Gst.py b/subprojects/gst-python/gi/overrides/Gst.py index e4a5c7d6bf..c0ea46c3dc 100644 --- a/subprojects/gst-python/gi/overrides/Gst.py +++ b/subprojects/gst-python/gi/overrides/Gst.py @@ -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 diff --git a/subprojects/gst-python/gi/overrides/gstmodule.c b/subprojects/gst-python/gi/overrides/gstmodule.c index 7d71425619..27538fd333 100644 --- a/subprojects/gst-python/gi/overrides/gstmodule.c +++ b/subprojects/gst-python/gi/overrides/gstmodule.c @@ -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}, diff --git a/subprojects/gst-python/testsuite/test_gst.py b/subprojects/gst-python/testsuite/test_gst.py index a5d9fa5942..584bb6ff8a 100644 --- a/subprojects/gst-python/testsuite/test_gst.py +++ b/subprojects/gst-python/testsuite/test_gst.py @@ -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: