pythoncglibpygobjectgobject-introspection

How can I call methods on a GObject class written in C from python?


I am trying to create a GObject class in C and annotate it in a way, so that I can use the class from Python - but I think am missing something, since I get weird errors that I can't understand. Any help would be much appreciated!

The class works as expected if I try to use it from other C-code, and I can generate both the .gir and .typelib files and as far as I can tell, the XML looks correct.

When I try to create instances of the class from Python, it appears that the _class_init and _init functions for the object isn't called, and I get exceptions if I try to call methods:

from gi.repository import Bop
foo = Bop.FooObj()
foo.get_number_of_tap_dancers()
# TypeError: expected GObject but got <gi.repository.Bop.FooObj object at 0x104670100>

I have tried to explicitly create a constructor for my class (bop_foo_obj_new) and if I call that from Python, I can see both the _class_init and _init get functions gets called, but then the Python interpreter segfaults:

Bop.FooObj.new()
# bop_foo_obj_new() called!!!!
# bop_foo_obj_class_init() called!!!!
# bop_foo_obj_init() called!!!!
#
# ** (process:25468): CRITICAL **: 12:55:19.436: pygobject_register_wrapper: assertion # 'PyObject_TypeCheck(self, &PyGObject_Type)' failed
# ./wat.sh: line 4: 25468 Segmentation fault: 11  GI_TYPELIB_PATH=build LD_LIBRARY_PATH=build python ./wat.py

Here is the code I am trying to get to work:

bop.h:

#pragma once
#include <glib-object.h>

G_BEGIN_DECLS

G_DECLARE_DERIVABLE_TYPE(BopFooObj, bop_foo_obj, BOP, FOO_OBJ, GObject);

struct _BopFooObjClass {
  GObjectClass parent;
};

BopFooObj *bop_foo_obj_new();
guint32 bop_foo_obj_get_number_of_tap_dancers(BopFooObj *self);

G_END_DECLS

bop.c:

#include "bop.h"
#include <stdio.h>

typedef struct {
  guint tap_dancer_cnt;
} BopFooObjPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(BopFooObj, bop_foo_obj, G_TYPE_OBJECT);

static void bop_foo_obj_class_init(BopFooObjClass *klass) {
  fprintf(stderr, "bop_foo_obj_class_init() called!!!!\n");
}

static void bop_foo_obj_init(BopFooObj *self) {
  fprintf(stderr, "bop_foo_obj_init() called!!!!\n");
  BopFooObjPrivate *priv = bop_foo_obj_get_instance_private(self);
  priv->tap_dancer_cnt = 9999;
}

/**
 * bop_foo_obj_new:(constructor):
 * Returns:(transfer full):
 */
BopFooObj *bop_foo_obj_new() {
  fprintf(stderr, "bop_foo_obj_new() called!!!!\n");
  return BOP_FOO_OBJ(g_object_new(bop_foo_obj_get_type(), NULL));
}

/**
 * bop_foo_obj_get_number_of_tap_dancers:
 * @self: the #BopFooObj instance
 */
guint32 bop_foo_obj_get_number_of_tap_dancers(BopFooObj *self) {
  BopFooObjPrivate *priv = bop_foo_obj_get_instance_private(self);
  return priv->tap_dancer_cnt;
}

Generated .gir file:

<?xml version="1.0"?>
<!-- This file was automatically generated from C sources - DO NOT EDIT!
To affect the contents of this file, edit the original C definitions,
and/or use gtk-doc annotations.  -->
<repository version="1.2"
            xmlns="http://www.gtk.org/introspection/core/1.0"
            xmlns:c="http://www.gtk.org/introspection/c/1.0"
            xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
  <namespace name="Bop"
             version="0.1"
             shared-library="libwat.dylib"
             c:identifier-prefixes="Bop"
             c:symbol-prefixes="bop">
    <class name="FooObj"
           c:symbol-prefix="foo_obj"
           c:type="BopFooObj"
           glib:type-name="BopFooObj"
           glib:get-type="bop_foo_obj_get_type"
           glib:type-struct="FooObjClass">
      <source-position filename="../bop.h" line="10"/>
      <constructor name="new" c:identifier="bop_foo_obj_new">
        <source-position filename="../bop.h" line="12"/>
        <return-value transfer-ownership="full">
          <type name="FooObj" c:type="BopFooObj*"/>
        </return-value>
      </constructor>
      <method name="get_number_of_tap_dancers"
              c:identifier="bop_foo_obj_get_number_of_tap_dancers">
        <source-position filename="../bop.h" line="13"/>
        <return-value transfer-ownership="none">
          <type name="guint32" c:type="guint32"/>
        </return-value>
        <parameters>
          <instance-parameter name="self" transfer-ownership="none">
            <doc xml:space="preserve"
                 filename="../bop.c"
                 line="31">the #BopFooObj instance</doc>
            <type name="FooObj" c:type="BopFooObj*"/>
          </instance-parameter>
        </parameters>
      </method>
      <field name="parent_instance" introspectable="0">
        <type c:type="GObject"/>
      </field>
    </class>
    <record name="FooObjClass"
            c:type="BopFooObjClass"
            glib:is-gtype-struct-for="FooObj">
      <source-position filename="../bop.h" line="10"/>
      <field name="parent" introspectable="0">
        <type c:type="GObjectClass"/>
      </field>
    </record>
  </namespace>
</repository>

Solution

  • This turned out to be a problem caused by the way the build system I am using, Meson, was calling g-ir-scanner. I noticed that it was using --extra-library=GObject-2.0 while the few tutorials I could find on this subject was calling g-ir-scanner with --include=GObject-2.0.

    I added includes: 'GObject-2.0' to the gnome.generate_gir section of my meson.build file and that fixed the problem:

    The final meson.build file looks like this:

    project('wat', 'c', default_options : ['c_std=c17'])
    
    gnome = import('gnome')
    deps = [
      dependency('gobject-2.0'),
      dependency('gobject-introspection-1.0')
    ]
    
    headers = [
      'bop.h'
    ]
    sources = [
      'bop.c'
    ]
    
    wat_lib = library('wat',
        sources,
        dependencies: deps,
        install: true,
    )
    
    gnome.generate_gir(
        wat_lib,
        namespace: 'Bop',
        nsversion: '0.1',
        sources: headers + sources,
        dependencies: deps,
        install: true,
        fatal_warnings: true,
        includes: 'GObject-2.0',   # this line fixed the issue.
    )