【问题标题】:No exceptions from GObject properties in pygobject?pygobject 中的 GObject 属性没有例外?
【发布时间】:2013-11-07 11:29:22
【问题描述】:

我正在尝试在 python3 / pygobject 中使用我的自定义 gobject 类之一中的属性进行一些异常处理。我的代码是这样的

try:
    label = foo.label # This is a GObject.Property
except Exception:
    label = "fallback"

我注意到解释器在尝试找出问题后从未找到 except 块,我想出了这个测试用例

from gi.repository import Gtk, GObject

class foo(GObject.Object):
    @GObject.Property
    def bar(self):
        raise NotImplementedError

fish = foo()
print("Bar: ", fish.bar)

输出

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gi/_gobject/propertyhelper.py", line 403, in obj_get_property
    return prop.fget(self)
  File "test.py", line 6, in bar
    raise NotImplementedError
NotImplementedError
Bar: None

如您所见,即使出现异常,该属性也会返回 None 并且程序会继续运行。

我也不明白。

有人知道解决方法或解决方案吗?

【问题讨论】:

    标签: python exception python-3.x pygobject gobject


    【解决方案1】:

    GObject 属性不支持异常,因此异常在这里不起作用是可以理解的。解决方法是使用 getter/setter 方法。

    【讨论】:

    • PyGObject 非常不完整。我希望语言绑定能够处理这些事情。 :(
    • 不能。 C 级别的 API 根本不支持它; PyGObject 无能为力。
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2017-07-22
    • 2021-08-31
    • 1970-01-01
    • 2016-01-30
    • 2023-04-02
    相关资源
    最近更新 更多