Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclassing: what of it? #29

Open
jjl opened this issue Dec 2, 2015 · 6 comments
Open

Subclassing: what of it? #29

jjl opened this issue Dec 2, 2015 · 6 comments
Labels

Comments

@jjl
Copy link
Contributor

jjl commented Dec 2, 2015

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
Copy link
Member

lazka commented Dec 2, 2015

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

@jjl
Copy link
Contributor Author

jjl commented Dec 2, 2015

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
Copy link
Member

lazka commented Dec 2, 2015

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
Copy link
Contributor Author

jjl commented Dec 2, 2015

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

@jjl
Copy link
Contributor Author

jjl commented Dec 5, 2015

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

@lazka lazka added the bug label Jan 26, 2016
@stuaxo
Copy link
Contributor

stuaxo commented May 1, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants