Frage

I'm trying to do some exception handling in python3 / pygobject with a property inside one of my custom gobject classes. The code I had was something like this

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

I had noticed that the interpreter never got around to the except block, after trying to figure out the problem I came up with this test case

from gi.repository import Gtk, GObject

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

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

The output

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

As you can see, even though there is an exception the property returns None and the program continues.

I don't get it either.

Anyone knows a workaround or a solution for this?

War es hilfreich?

Lösung

GObject properties don't support exceptions, so it's understandable that exceptions wouldn't work here. The workaround is to use getter/setter methods.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top