如何从 python 调用用 C 编写的 GObject class 上的方法?

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

我正在尝试在 C 中创建一个 GObject class 并以某种方式对其进行注释,以便我可以使用 Python 中的 class - 但我认为缺少一些东西,因为我得到了我无法理解的奇怪错误。任何帮助将不胜感激!

如果我尝试从其他 C 代码中使用它,class 将按预期工作,并且我可以尽可能地生成 .gir.typelib 文件告诉,XML 看起来是正确的。

当我尝试从 Python 创建 class 的实例时,似乎没有调用对象的 _class_init_init 函数,并且如果我尝试调用方法,我会得到异常:

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>

我试图为我的 class (bop_foo_obj_new) 显式创建一个构造函数,如果我从 Python 调用它,我可以看到 _class_init_init get 函数被调用,但是 Python 解释器段错误:

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

这是我要开始工作的代码:

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;
}

生成的.gir文件:

<?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>

事实证明这是由我使用的构建系统 Meson 调用 g-ir-scanner 的方式引起的问题。我注意到它使用的是 --extra-library=GObject-2.0 而我能找到的关于这个主题的一些教程是用 --include=GObject-2.0.

调用 g-ir-scanner

我将 includes: 'GObject-2.0' 添加到 meson.build 文件的 gnome.generate_gir 部分并解决了问题:

最终的 meson.build 文件如下所示:

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