3

我正在研究现有 C 项目的 Python 克隆。C-Project 连接到自定义 DBus 并在那里提供一个对象来获取回调。

我尝试使用 Python 来复制它,其代码基本上可以归结为:

def vtable_method_call_cb(connection, sender, object_path, interface_name, method_name, parameters, invocation, user_data):
    print('vtable_method_call_cb: %s' % method_name)

connection = Gio.DBusConnection.new_for_address_sync(
    "unix:abstract=gstswitch",
    Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT,
    None,
    None)

node_info = Gio.DBusNodeInfo.new_for_xml(introspection_xml)

vtable = Gio.DBusInterfaceVTable()
vtable.method_call(vtable_method_call_cb)
vtable.get_property(None)
vtable.set_property(None)

connection.register_object(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable,
    None,
    None)

vtable.method_call在调用时创建 vtable 时代码失败(get_property但当我注释一个调用时也失败)以下日志/回溯:

** (process:18062): WARNING **: Field method_call: Interface type 2 should have is_pointer set
Traceback (most recent call last):
  File "moo.py", line 69, in <module>
    vtable.method_call(vtable_method_call_cb)
RuntimeError: unable to get the value

我无法找到register_object()在 python 中使用的代码,所以我不确定 Gio 的这一部分是否应该可用,或者它是否不完整。

4

1 回答 1

3

这当然不是您想听到的,但是您在 GDBus Python 绑定中遇到了一个4 年前的错误,该错误导致无法在总线上注册对象。很久以前就提出了一个补丁,但每次看起来它实际上要登陆时,一些 GNOME 开发人员发现了一些他/她不喜欢的地方,提出了一个新补丁......但什么也没发生明年的大部分时间。这个循环已经发生了3次,我不知道它是否有很大的希望很快就会被打破......

基本上 GNOME 开发人员自己或多或少地建议人们使用 dbus-python 直到这个问题最终得到解决,所以我猜你应该去这里。:-/

顺便说一句:我认为你的源代码是错误的(除了它不会以任何方式工作)。要创建 VTable,您实际上会写成这样的东西,我认为:

vtable = Gio.DBusInterfaceVTable()
vtable.method_call  = vtable_method_call_cb
vtable.get_property = None
vtable.set_property = None

但是由于绑定被破坏了,你只是在abort()这里交易一个例外...... :-(

如果补丁实际上python-gi以当前形式vtable将其完全转储(是的!)并且connection.register_object调用将变为:

connection.register_object_with_closures(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable_method_call_cb, # vtable.method_call
    None,                  # vtable.get_property
    None)                  # vtable.set_property

更新

看来现在终于解决了!您现在可以使用以下方式导出对象g_dbus_connection_register_object_with_closures

def vtable_method_call_cb(connection, sender, object_path, interface_name, method_name, parameters, invocation, user_data):
    print('vtable_method_call_cb: %s' % method_name)

connection = Gio.DBusConnection.new_for_address_sync(
    "unix:abstract=gstswitch",
    Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT,
    None,
    None)

node_info = Gio.DBusNodeInfo.new_for_xml(introspection_xml)

connection.register_object(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable_method_call_cb,
    None,
    None)
于 2015-02-16T23:11:02.077 回答