据我可以从可用的文档中看出,GLib.io_add_watch()应该在IOChannel上发生条件时注册要调用的函数,并且callback function应该接收所述IOChannel作为其第一个参数。很好,除了它没有。 GLib将一个完全不同的IOChannel对象传递给回调。为什么?
换句话说,为什么这段代码会产生AssertionError?
#!/usr/bin/env python3
import gi
from gi.repository import GLib
_, _, fd, _ = GLib.spawn_async(['/bin/echo', 'hello'], standard_output=True)
channel = GLib.IOChannel.unix_new(fd)
def on_read(callback_channel, condition):
assert callback_channel is channel
GLib.io_add_watch(channel, GLib.PRIORITY_DEFAULT, GLib.IO_IN, on_read)
GLib.MainLoop().run()
IOChannel是GBoxed结构,而不是GObject。它没有身份,并通过副本传递。您在回调中收到的那个等同于您给出的那个,但它们不是同一个对象,也不是它们的所有字段都具有相同的值。