首页 > 解决方案 > Python 异常在 Glib/DBUS 循环中发生时不会传播/打印

问题描述

当错误/异常(例如 NameError,因为我打错了变量名)发生在notification_filter-- 错误/异常信息似乎没有被传播(或被静默捕获)。

一个简单的例子:

import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib

def notification_filter(bus, message):
    raise Exception("This exception is never propagated/printed")

if __name__ == "__main__":        
    DBusGMainLoop(set_as_default=True)

    bus = dbus.SessionBus()
    bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'")
    bus.add_message_filter(notification_filter)

    mainloop = GLib.MainLoop()
    mainloop.run()

发生错误/异常时如何打印/记录信息?

标签: pythonglibdbuspygobject

解决方案


推荐阅读