Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Register
  • Sign in
  • pygobject pygobject
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 302
    • Issues 302
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 31
    • Merge requests 31
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GNOMEGNOME
  • pygobjectpygobject
  • Issues
  • #400
Closed
Open
Issue created Apr 18, 2020 by k-jm@k-jm

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 ...
  ....
Assignee
Assign to
Time tracking