[pygobject/bilelmoussaoui/dispose] object: Allow overriding dispose implementation
- From: Bilal Elmoussaoui <bilelmoussaoui src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject/bilelmoussaoui/dispose] object: Allow overriding dispose implementation
- Date: Tue, 15 Feb 2022 14:56:58 +0000 (UTC)
commit 1033f0b86c26e57ce35a773a87b99946e7b212dd
Author: Bilal Elmoussaoui <belmouss redhat com>
Date: Tue Feb 15 15:02:54 2022 +0100
object: Allow overriding dispose implementation
In GTK4 people are expected to unparent their custom GtkWidget implementation
in the object's dispose method which is the main motivation behind this patch
gi/gimodule.c | 37 +++++++++++++++++++++++++++++++++++++
tests/test_gobject.py | 11 +++++++++++
2 files changed, 48 insertions(+)
---
diff --git a/gi/gimodule.c b/gi/gimodule.c
index 02daba85..963c1779 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -937,6 +937,42 @@ pyg_object_set_property (GObject *object, guint property_id,
PyGILState_Release(state);
}
+static void
+py_object_dispose (GObject *object)
+{
+ PyObject *object_wrapper, *retval;
+ PyGILState_STATE state;
+ GObjectClass *parent_class = g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (object)));
+
+ state = PyGILState_Ensure();
+
+ object_wrapper = g_object_get_qdata(object, pygobject_wrapper_key);
+
+ if (object_wrapper)
+ Py_INCREF (object_wrapper);
+ else
+ object_wrapper = pygobject_new(object);
+
+ if (object_wrapper == NULL) {
+ PyGILState_Release(state);
+ return;
+ }
+ // Chain up the dispose call
+ if (parent_class->dispose) {
+ parent_class->dispose(object);
+ }
+ retval = PyObject_CallMethod(object_wrapper, "do_dispose", NULL);
+ if (retval) {
+ Py_DECREF(retval);
+ } else {
+ PyErr_Print();
+ }
+
+ Py_DECREF(object_wrapper);
+
+ PyGILState_Release(state);
+}
+
static void
pyg_object_class_init(GObjectClass *class, PyObject *py_class)
{
@@ -945,6 +981,7 @@ pyg_object_class_init(GObjectClass *class, PyObject *py_class)
class->set_property = pyg_object_set_property;
class->get_property = pyg_object_get_property;
+ class->dispose = py_object_dispose;
/* install signals */
/* we look this up in the instance dictionary, so we don't
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index fbc3bb79..49229e43 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -775,6 +775,17 @@ class TestGValue(unittest.TestCase):
value = GObject.Value(GObject.TYPE_OBJECT, obj)
self.assertEqual(value.get_value(), obj)
+ def test_dispose_object(self):
+ class TestObject(GObject.Object):
+ def __init__(self):
+ self.child = "Some Child"
+ def do_dispose(self):
+ self.child = None
+ obj = TestObject()
+ obj.do_dispose()
+ self.assertEqual(obj.child, None)
+
+
def test_value_array(self):
value = GObject.Value(GObject.ValueArray)
self.assertEqual(value.g_type, GObject.type_from_name('GValueArray'))
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]