我正在尝试在 C 中创建一个 GObject 类并以某种方式对其进行注释,以便我可以使用 Python 中的类 - 但我认为缺少一些东西,因为我遇到了我无法理解的奇怪错误。任何帮助将非常感激!
如果我尝试从其他 C 代码中使用它,该类将按预期工作,并且我可以生成.gir
和.typelib
文件,据我所知,XML 看起来是正确的。
当我尝试从 Python 创建类的实例时,似乎没有调用对象的_class_init
and_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>
我尝试为我的bop_foo_obj_new
类_class_init
(_init
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>