diff options
author | Jean Felder <jfelder@src.gnome.org> | 2020-04-16 23:54:18 +0200 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2020-04-17 13:30:31 +0200 |
commit | 2172d89bccbb2eeaa945da9752558d412c996938 (patch) | |
tree | 743931526198acdc2fbc4b2bb2b1994c56bac606 | |
parent | 1ff66b6b124fbcafa92ea29ef9a15504c7988f29 (diff) | |
download | pygobject-2172d89bccbb2eeaa945da9752558d412c996938.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.
-rw-r--r-- | gi/_gtktemplate.py | 2 | ||||
-rw-r--r-- | tests/test_gtk_template.py | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/gi/_gtktemplate.py b/gi/_gtktemplate.py index efaca339..4b80106c 100644 --- a/gi/_gtktemplate.py +++ b/gi/_gtktemplate.py @@ -99,7 +99,7 @@ def register_template(cls): def init_template(self, cls, base_init_template): - self.init_template = lambda s: None + self.init_template = lambda: None if self.__class__ is not cls: raise TypeError( diff --git a/tests/test_gtk_template.py b/tests/test_gtk_template.py index b5c9adcb..338d1029 100644 --- a/tests/test_gtk_template.py +++ b/tests/test_gtk_template.py @@ -660,3 +660,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 |