Template hierarchy issue cont' : init_template is not applied to the right class
In the path submitter by @jfelder templating fails when applied to a class sub-classing a non Gtk builtin class :
(pbgitlab.py:5066): Gtk-CRITICAL **: 15:43:50.216: gtk_widget_init_template: assertion 'template != NULL' failed
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi._gtktemplate import Template
xml="""
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.20"/>
<template class="SubBox" parent="MyBox">
</template>
</interface>
"""
class MyBox(Gtk.Box):
__gtype_name__ = 'MyBox'
pass
@Template (string=xml)
class SubBox(MyBox):
__gtype_name__ = 'SubBox'
pass
s = SubBox()
The problem seems to come from the fact that init_template is called for each (gtk) class from the root widget hierarchy.
Here MyBox.init_template is called before SubBox.init_template, this call fails as there is no template associated to MyBox .
I'm not mastering the Gtk type system enough to provide a definitive patch so i use the following as a temporary workaround (return early from ini_template if the parameter class is not the good one) :
import re
def init_template(self, cls, base_init_template):
# Is it possible to get the current (dynamic) gtype of self with the API ?
# for now : use a regexp to extract this type from self __repr__
current_gtype = re.match(".*\\((.*) at 0x.*\\)>",str(self))[1]
# do not apply init_template body if current gtype of self is not the
# gtype decorated with @Template
if current_gtype != cls.__gtype_name__: return
....
.... #initial init_template body ...
....