gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:51:05 27 4
gpt4 key购买 nike

我正在尝试在 C 中创建一个 GObject 类并以某种方式对其进行注释,以便我可以使用 Python 中的类 - 但我认为缺少一些东西,因为我遇到了我无法理解的奇怪错误。任何帮助将非常感激!
如果我尝试从其他 C 代码中使用它,该类会按预期工作,并且我可以生成 .gir.typelib文件,据我所知,XML 看起来是正确的。
当我尝试从 Python 创建该类的实例时,似乎 _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>
我试图为我的类( 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而我能找到的关于这个主题的少数教程正在调用 g-ir-scanner--include=GObject-2.0 .
我加了 includes: 'GObject-2.0'gnome.generate_gir我的 meson.build 部分文件并解决了问题:
最终的 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.
)

关于python - 如何从 python 调用用 C 编写的 GObject 类上的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69404827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com