After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 543056 - gobject.type_register() no longer needed
gobject.type_register() no longer needed
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: codegen
Git master
Other All
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2008-07-15 08:53 UTC by Steve Chaplin
Modified: 2011-01-18 12:12 UTC
See Also:
GNOME target: ---
GNOME version: 2.19/2.20


Attachments
Always register a new GType when a GObject class is subclassed (4.55 KB, patch)
2011-01-17 18:02 UTC, Steve Frécinaux
none Details | Review
Always register a new GType when a GObject class is subclassed (4.44 KB, patch)
2011-01-17 18:11 UTC, Steve Frécinaux
committed Details | Review

Description Steve Chaplin 2008-07-15 08:53:53 UTC
Please describe the problem:
pygtk 2.12.0

http://live.gnome.org/PyGTK/WhatsNew28 says
"Now GObject's are better integrated into python. For one thing, it is no
longer needed to call gobject.type_register(MyClass) on subclasses, ever ..."

Edit examples/gtk/widget.py
to remove the unnecessary call to gobject.type_register(PyGtkWidget)
and run the example. It reports the error:

Traceback (most recent call last):
  • File "widget.py", line 133 in <module>
    sys.exit(main(sys.argv))
  • File "widget.py", line 125 in main
    w = PyGtkWidget(TEXT)
  • File "widget.py", line 25 in __init__
    gtk.Widget.__init__(self)
TypeError: cannot create instance of abstract (non-instantiable) type `GtkWidget'

- it looks like pygtk 2.12 has regressed to the behaviour before version 2.8



Steps to reproduce:
1. 
2. 
3. 


Actual results:


Expected results:


Does this happen every time?


Other information:
Comment 1 Paul Pogonyshev 2008-07-17 20:04:26 UTC
Do you have PyGTK 2.8 around?  In 'gobject/__init__.py' there are these lines:

        if not ('__gproperties__' in namespace or
                '__gsignals__' in namespace or
                '__gtype_name__' in namespace):
            return

Which means that types with neither of those three attributes don't get autoregistered.  Was it different in 2.8?  Or maybe all standard classes just had some of the attributes set, so that subclasses got it automatically?

(Difficult to trace down by sources, since type autoregistering has been moved from C part to Python part.)
Comment 2 Steve Chaplin 2008-07-18 02:52:35 UTC
I dont have pygtk 2.8 installed (just Fedora 8 with pygtk 2.12 and Fedora 7
with pygtk 2.10). pygtk 2.10.4 with pygobject 2.12.3 demonstrated the same
problem.

In 'gobject/__init__.py':

class GObjectMeta(type):
    "Metaclass for automatically registering GObject classes"
    def __init__(cls, name, bases, dict_):
        type.__init__(cls, name, bases, dict_)
        cls._install_properties()
        cls._type_register(cls.__dict__)

    # ...

    def _type_register(cls, namespace):
        ## don't register the class if already registered
        if '__gtype__' in namespace:
            return

        if not ('__gproperties__' in namespace or
                '__gsignals__' in namespace or
                '__gtype_name__' in namespace):
            return

        type_register(cls, namespace.get('__gtype_name__'))


I can understand the check for '__gtype__' - don't register the class if it is
already registered. But why the '__gproperties__', '__gsignals__',
'__gtype_name__' clause - what does it achieve?
type_register() already checks that the class is a GObject subclass, so why
are further checks necessary?

Removing the '__gproperties__', '__gsignals__', '__gtype_name__' clause fixes
the problem.
Comment 3 Paul Pogonyshev 2008-07-18 20:56:59 UTC
See bug 311226 for why it is needed (it seems, at least).  Feel free to find a way to remove '__gproperties__' etc. clause but avoid the problem outlined in that bug report.
Comment 4 Steve Chaplin 2008-07-19 01:28:16 UTC
That's easy - just comment out the 
        if not ('__gproperties__' in namespace or
                '__gsignals__' in namespace or
                '__gtype_name__' in namespace):
            return
clause and run the foodialog.py attached test case and the problem reported in bug 311226 is still avoided. This is because the __gproperties__ clause does *not* fix bug 311226, the bug was fixed by a patch against GTK+.

The __gproperties__ clause was a side issue which arose from the bug's discussion.
It's intention was to "Do not automatically register a type if not needed." However, this bug report demonstrates that the clause avoids registering a type when it *is* needed - so its not working as originally intended.
Comment 5 Paul Pogonyshev 2008-07-19 12:39:05 UTC
Well, it _seems_ the problem is gone because gtk.MessageDialog constructor is now new-style (with GTK+ 2.10 and up).  Also, the problem seems to be gone for everything that uses relatively new function pygobject_register_wrapper() (new-style constructors do use that).

However, while it seems that all class in PyGTK are now safe, I cannot say for sure that doing as you suggest will not break inheriting non-PyGTK classes.  I leave this bug to someone who better understands these low level mechanics (Gustavo?).
Comment 6 Steve Frécinaux 2011-01-17 18:02:39 UTC
Created attachment 178537 [details] [review]
Always register a new GType when a GObject class is subclassed

This patch makes the GType <-> python mapping much more predictible,
and fixes the bug caused by overriding methods without specifying a
__gtype_name__ member in the subclass, and makes type_register useless
for real :-)

It is still possible to provide an explicit __gtype_name__ member in the
subclass as it allows having a predictible GType name, which is handy
for some of our tests. There is also an explicit special case for
overrides because we obviously do not want to register new GTypes for
those ones as it would clearly defeat the purpose of overrides.
Comment 7 Steve Frécinaux 2011-01-17 18:11:50 UTC
Created attachment 178540 [details] [review]
Always register a new GType when a GObject class is subclassed

I forgot to remove a debug line in the previous patch...
Comment 8 Steve Frécinaux 2011-01-18 12:12:08 UTC
Attachment 178540 [details] pushed as 84d6142 - Always register a new GType when a GObject class is subclassed