[pygobject/wip/jfelder/template-hierarchy-fix: 3/3] overrides: template hierarchy issue



commit e3a532b33914fce479de13c0666f81a986e19e13
Author: Jean Felder <jfelder src gnome org>
Date:   Thu Mar 19 22:20:37 2020 +0100

    overrides: template hierarchy issue

 gi/gimodule.c              |  9 ------
 gi/overrides/Gtk.py        |  7 +++++
 tests/test_gtk_template.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 9 deletions(-)
---
diff --git a/gi/gimodule.c b/gi/gimodule.c
index 1b997617..76e557f8 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -1103,15 +1103,6 @@ pygobject__g_instance_init(GTypeInstance   *instance,
             Py_DECREF (result);
     }
 
-    /* XXX: used for Gtk.Template */
-    if (PyObject_HasAttrString ((PyObject*) Py_TYPE (wrapper), "__dontuse_ginstance_init__")) {
-        result = PyObject_CallMethod (wrapper, "__dontuse_ginstance_init__", NULL);
-        if (result == NULL)
-            PyErr_Print ();
-        else
-            Py_DECREF (result);
-    }
-
     PyGILState_Release(state);
 }
 
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index d1ed83da..418b3319 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -137,6 +137,13 @@ class Widget(Gtk.Widget):
 
     translate_coordinates = strip_boolean_result(Gtk.Widget.translate_coordinates)
 
+    def __init__(self):
+        super().__init__()
+
+        # if the widget has a widget, initialize it
+        if hasattr(self, "__dontuse_ginstance_init__"):
+            self.__dontuse_ginstance_init__()
+
     if Gtk._version != "4.0":
         def freeze_child_notify(self):
             super(Widget, self).freeze_child_notify()
diff --git a/tests/test_gtk_template.py b/tests/test_gtk_template.py
index 3a1c1f02..d769190d 100644
--- a/tests/test_gtk_template.py
+++ b/tests/test_gtk_template.py
@@ -590,3 +590,75 @@ def test_internal_child():
     child = child.get_children()[0]
     assert isinstance(child, Gtk.Label)
     assert child.props.label == "foo"
+
+
+def test_template_hierarchy():
+    testlabel = """
+    <interface>
+      <template class="TestLabel" parent="GtkLabel">
+        <property name="visible">True</property>
+       </template>
+     </interface>
+    """
+    @Gtk.Template(string=testlabel)
+    class TestLabel(Gtk.Label):
+
+        __gtype_name__ = 'TestLabel'
+
+        def __init__(self):
+            super().__init__()
+            self.props.label = "TestLabel"
+
+    testbox = """
+    <interface>
+      <template class="TestBox" parent="GtkBox">
+        <property name="visible">True</property>
+        <child>
+          <object class="TestLabel" id="_testlabel"/>
+        </child>
+      </template>
+    </interface>
+    """
+    @Gtk.Template(string=testbox)
+    class TestBox(Gtk.Box):
+
+        __gtype_name__ = 'TestBox'
+
+        _testlabel = Gtk.Template.Child()
+
+    def __init__(self):
+        super().__init__()
+
+        assert isinstance(self._testlabel, TestLabel)
+
+    window = """
+    <interface>
+      <template class="MyWindow" parent="GtkWindow">
+        <property name="title">"Hellow World"</property>
+        <property name="visible">True</property>
+        <child>
+          <object class="TestBox" id="_testbox">
+            <child>
+              <object class="TestLabel" id="_testlabel"/>
+            </child>
+          </object>
+        </child>
+      </template>
+    </interface>
+    """
+    @Gtk.Template(string=window)
+    class MyWindow(Gtk.Window):
+
+        __gtype_name__ = "MyWindow"
+
+        _testbox = Gtk.Template.Child()
+        _testlabel = Gtk.Template.Child()
+
+        def __init__(self):
+            super().__init__()
+
+            assert isinstance(self._testbox, TestBox)
+            assert isinstance(self._testlabel, TestLabel)
+
+    win = MyWindow()
+    assert isinstance(win, MyWindow)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]