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 80915 - deprecating gobject.type_register()
deprecating gobject.type_register()
Status: RESOLVED DUPLICATE of bug 129843
Product: pygtk
Classification: Bindings
Component: general
1.99.x/2.0.x
Other All
: Normal enhancement
: ---
Assigned To: Python bindings maintainers
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2002-05-06 08:13 UTC by Arjan J. Molenaar
Modified: 2004-12-22 21:47 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Do GType initialization at construction time. (36.00 KB, patch)
2002-05-06 08:14 UTC, Arjan J. Molenaar
none Details | Review

Description Arjan J. Molenaar 2002-05-06 08:13:53 UTC
If you inherit from a GObject, you have to call GObject.__gobject_init__()
in the constructor and register the class after creating it with
gobject.type_register(). This is not intuitive and in most cases not
needed. A patch is applied that does initialization of the type in the
GObject.__gobject_init__() function. 

Drawback is that __gproperties__ and __gsignals__ are initialized the first
time an instance is created. This approach, however, opens the door for
custom default handlers for methods defined in the <class>Class struct and
the ability to add interfaces to a new GObject deriviate.

The patch is pretty big since a bunch of functions have been moved from
gobjectmodule.c to pygobject.c.
Comment 1 Arjan J. Molenaar 2002-05-06 08:14:52 UTC
Created attachment 8221 [details] [review]
Do GType initialization at construction time.
Comment 2 James Henstridge 2002-05-06 08:41:19 UTC
I don't know if I like this approach.  It could easily have
unpredictable results with multiple levels of subclassing:
  class MyWidget(gtk.Widget):
      ...
  class MyWidget2(MyWidget):
      ...

What happens if you instantiate MyWidget2 first?

The correct way to handle this is probably to define a special
metatype for GtkWidget derivatives (which is luckily a lot easier and
intuitive with 2.2 than previous versions).  By overriding __call__ on
the metatype, we get control over subclassing of GObjects.

Also in future, could you send your patches in unified diff format? 
It is a lot easier to read than -c format.  The easiest way to
remember this is to add the following line to your ~/.cvsrc file
(create it if you don't have one already):
  diff -up
Comment 3 Arjan J. Molenaar 2002-05-06 10:11:07 UTC
Hmm... Yes, if you create an MyWidget2 object first you probably have
a problem with my approach. I "need" a simple structure to inherit
from GObject for DiaCanvas2. I've been reimplementing the Python
wrapper and it should be possible to create new canvas items by means
of inheritance. I do not like the idea of creating a special class for
every canvas item just to make it possible to create a python
deriviate... Also I have created an interface for grouping canvas
items it would be nice if you can assign the interface only if the
canvas item needs it. (it should be possible to create a TreeModel too
by using 'class MyClass(gobject.GObject, gtk.TreeModel)', that would
be nice IMHO)

What exactly do you mean by "metatype"?

I'll take a look at __call__.

Thanks for the cvs diff hint.

Arjan
Comment 4 James Henstridge 2002-05-06 10:33:18 UTC
(reopenning so I don't lose it.  We have enough information to know
what the enhancement request is).
Comment 5 James Henstridge 2002-05-07 11:32:22 UTC
As for examples of metaclass usage, I actually meant overriding
__init__ rather than __call__.  Here is a simple example of creating a
metatype in pure python code:

  class MyType(type):
      def __init__(self, name, bases, members):
          print "Create type:", (name, bases, members)
          type.__init__(self, name, bases, members)

  class MyBase:
      __metaclass__ = MyType

  class MySubclass(MyBase): pass

If you run this code, it should output the following:

  Create type: ('MyBase', (), {'__metaclass__': <class
'__main__.MyType'>})
  Create type: ('MySubclass', (<class '__main__.MyBase'>,), {})

By using a custom metaclass, we get full control over how people
subclass GObjects.
Comment 6 Arjan J. Molenaar 2002-05-07 14:30:06 UTC
Indeed this is what I call elegance. This makes it trivial to add
(g)interfaces to a Python class and we can easely check for
inheritance of multiple GObjects. Great!
Comment 7 Arjan J. Molenaar 2002-05-08 13:24:23 UTC
Do you know how to write such a thing in C? I don't have a clue, but
can give it a try anyway...
Comment 8 James Henstridge 2002-05-09 15:30:48 UTC
Need to work out when we want to create a C level GObject subclass. 
It isn't obvious that you always want to create one.  How would we
decide when to create a subtype?
Comment 9 Johan (not receiving bugmail) Dahlin 2002-10-17 03:19:07 UTC
Is this still being worked on or is it dropped?

/me would also like to get type_register moved away.
Comment 10 Arjan J. Molenaar 2002-11-06 10:55:49 UTC
I still want it moved away...

Maybe we should add a function ala:

class A:
    def fun():
        pass
    fun = staticmethod(fun)

In pyton2.2 they do the same thing for properties and some other 
stuff.

We then do something like:

def A(gobject.GObject):
    __gsignals__ = { ... }
    __gproperties__ = { ... }
    isGObjectClass()
    
    def __init__(self):
        gobject.GObject.__init__(self)

I know this is basically moving some functionallity around, but we 
can do the __gobject_init__ and type_register() stuff in that one 
function on class level, which makes it look more python-ish.

... just a though...
Comment 11 James Henstridge 2002-11-20 13:41:33 UTC
mass reassign of open pygtk and gnome-python bugs.
Comment 12 Erik Grinaker 2004-02-24 12:40:49 UTC

*** This bug has been marked as a duplicate of 129843 ***