0

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?

OdraEncoded
  • 3,064
  • 3
  • 20
  • 31

1 Answers1

2

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

nemequ
  • 16,623
  • 1
  • 43
  • 62
  • PyGObject is horribly incomplete. I would expect a language binding to take care of these things. :( – Bachsau Apr 24 '18 at 21:51
  • It can't. The API at the C level simply doesn't support it; there is nothing PyGObject can do. – nemequ Apr 25 '18 at 00:53