GNOME Bugzilla – Bug 575652
exceptions raised in get/set_properties are not trapped by try/except
Last modified: 2018-01-10 19:59:19 UTC
Please describe the problem: As the following code shows, a try/except will not trap an error raised by within a get_property Steps to reproduce: import gobject class MyError(Exception): pass class Test(gobject.GObject): __gproperties__ = { 'date' : (gobject.TYPE_PYOBJECT, # type 'Date', # nick name 'The date currently selected', # description gobject.PARAM_READWRITE), # flags } def get_date(self): raise MyError("Gulp!") def do_get_property(self, property): if property.name == 'date': return self.get_date() t = Test() try: t.get_property('date') # this will raise MyError except: # this will not catch it!!!! pass Actual results: Expected results: I'd expect try/except to catch all normal errors. I asked in several list and no one expected this behaviour or answered that this is correct Does this happen every time? yes, also teted it in ubuntu 8.10 (pygobject 2.15.3 Other information:
With current trunk I at least get exception stack trace on stderr. However, that's probably the most that can be done. The problem is, when you call gobject.GObject.get_property (or indirectly with gobject.GObject.props.foo), you invoke C code. In this case, it in turn invokes your Python property getter method. However, there is no (known to me) way to pass the exception generated in the inner Python code through the C layer. Moreover, C layer can be anything (on top of g_object_get_property), because the latter function can be legitemately called from e.g. widget implementation code. In other words, this is probably NOTABUG, even though it does feel very unpythonic.
Works as expected with pygobject 3.2.
It turns out this isn't really fixed but perhaps the given test code was not showing what should be expected properly, here is updated test code: from gi.repository import GObject class Test(GObject.GObject): @GObject.Property(type=int) def date(self): raise ValueError('Gulp!') try: t.date except ValueError: print('this should be printed!') We get a the ValueError printed but it is not actually raised in the calling code. Changing this to a dup of bug 616279 which is still open and essentially describes the same problem. *** This bug has been marked as a duplicate of bug 616279 ***
Re-opening because bug 616279 is not specific enough. Property getter exceptions are a whole different code path.
The following fix has been pushed: b1caef9 tests: Add failing tests which verify exceptions raised in property getters
Created attachment 283021 [details] [review] tests: Add failing tests which verify exceptions raised in property getters
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/pygobject/issues/4.