summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJean Felder <jfelder@src.gnome.org>2020-04-16 23:54:18 +0200
committerJean Felder <jfelder@src.gnome.org>2020-04-17 01:52:34 +0200
commit94f32d1f0861b66bead712313e8b39f58529f139 (patch)
treef832027c2f9b6fceb48a7b4f25cb22f86901819f /tests
parentb5013bf7e127a87cf9f1ab0aef048bf8ef9f46c5 (diff)
downloadpygobject-94f32d1f0861b66bead712313e8b39f58529f139.tar.gz
gtktemplate: Do not crash on multiple init_template calls
init_template method is automatically called when a widget is created and it is not supposed to be called multiple times. That is why this method is turned into a no-op after its first call via a lambda function. init_template can still be called after a widget creation (and it is supposed to do nothing). However, a second call will result in a crash because the lambda function has a parameter while init_template is not supposed to have any parameter. This issue is fixed by removing the parameter of the lambda function. A new test is also added to assert that a second init_template call does nothing.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_gtk_template.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/test_gtk_template.py b/tests/test_gtk_template.py
index 69911c81..febdf705 100644
--- a/tests/test_gtk_template.py
+++ b/tests/test_gtk_template.py
@@ -656,3 +656,33 @@ def test_template_hierarchy():
win = MyWindow()
assert isinstance(win, MyWindow)
+
+
+def test_multiple_init_template_calls():
+ xml = """
+ <interface>
+ <template class="MyBox" parent="GtkBox">
+ <child>
+ <object class="GtkLabel" id="_label"/>
+ </child>
+ </template>
+ </interface>
+ """
+ @Gtk.Template(string=xml)
+ class MyBox(Gtk.Box):
+
+ __gtype_name__ = "MyBox"
+
+ _label = Gtk.Template.Child()
+
+ def __init__(self):
+ super().__init__()
+ self._label.props.label = "awesome label"
+
+ my_box = MyBox()
+ assert isinstance(my_box, MyBox)
+ assert len(my_box.get_children()) == 1
+
+ my_box.init_template()
+ assert isinstance(my_box, MyBox)
+ assert len(my_box.get_children()) == 1