[pygobject/pygobject-3-36] gtktemplate: Do not crash on multiple init_template calls
- From: Christoph Reiter <creiter src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject/pygobject-3-36] gtktemplate: Do not crash on multiple init_template calls
- Date: Fri, 17 Apr 2020 13:12:50 +0000 (UTC)
commit 2172d89bccbb2eeaa945da9752558d412c996938
Author: Jean Felder <jfelder src gnome org>
Date: Thu Apr 16 23:54:18 2020 +0200
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.
gi/_gtktemplate.py | 2 +-
tests/test_gtk_template.py | 30 ++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
---
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]