pygobject / pgi

[Unmaintained: Use PyGObject instead] GTK+ / GObject Introspection Bindings for PyPy.
GNU Lesser General Public License v2.1
74 stars 16 forks source link

Subclassing: what of it? #29

Open jjl opened 8 years ago

jjl commented 8 years ago

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))
lazka commented 8 years ago

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

jjl commented 8 years ago

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.

lazka commented 8 years ago

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

jjl commented 8 years ago

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

jjl commented 8 years ago

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

stuaxo commented 6 years ago

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 :)