pgi icon indicating copy to clipboard operation
pgi copied to clipboard

Subclassing: what of it?

Open jjl opened this issue 9 years ago • 6 comments

Took a little time to get a minimal test case this time. Subclassing in the normal python way does not work. The objects get reinterpreted as being of the class you are subclassing.

No idea if there is a "proper" way of doing this at present (and it's merely undocumented), but it's getting in the way of one of my projects so I'd appreciate any advice about workarounds.

import pgi
pgi.install_as_gi()
from gi.repository import Gtk

class Failure(Gtk.ListBoxRow):
    def __init__(self):
        Gtk.ListBoxRow.__init__(self)
        self.foo = "bar"

def cb(lb, row):
    assert(row.foo == "bar")


f = Failure()
lb = Gtk.ListBox()
lb.insert(f,-1)
lb.connect('row-selected',cb)
lb.select_row(lb.get_row_at_index(0))

jjl avatar Dec 02 '15 13:12 jjl

Subclassing isn't implemented (it gets lost on the Python -> C boundary)

lazka avatar Dec 02 '15 13:12 lazka

Okay. Well, I need it. Do you have any tips for where I might start looking in order to implement it? I would be loathe to have to switch to PyGObject and lose pypy support.

jjl avatar Dec 02 '15 13:12 jjl

It's not easy. There are two things missing: registering of gtypes for subclasses and adding a reference from the wrapper to the gobject and vice versa using toggle references [0]. No idea where to start... sorry.

[0] https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#g-object-add-toggle-ref

lazka avatar Dec 02 '15 13:12 lazka

Okay. Well I'll have a bash at it and see if I get anywhere.

jjl avatar Dec 02 '15 13:12 jjl

I didn't get anywhere. My knowledge of gobject-related stuff just isn't good enough to do this work :/

jjl avatar Dec 05 '15 15:12 jjl

I don't know how gobject stores it's data, but have built a class structure or two in my time

Here's an example that takes some flat data: class name, methods and base classes names and then builds a set of classes from it.

https://gist.github.com/stuaxo/397789c9c5272fee70fde0e6e73f62ef

It builds the classes from the bottom up. Building constructors isn't included, but is relatively straightforward.

I'm not sure if this example is helpful, but meant to write it here at least two years ago :)

stuaxo avatar May 01 '18 14:05 stuaxo