After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 575652 - exceptions raised in get/set_properties are not trapped by try/except
exceptions raised in get/set_properties are not trapped by try/except
Status: RESOLVED OBSOLETE
Product: pygobject
Classification: Bindings
Component: gobject
2.14.x
Other All
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on: 723872
Blocks:
 
 
Reported: 2009-03-17 07:53 UTC by sandro
Modified: 2018-01-10 19:59 UTC
See Also:
GNOME target: ---
GNOME version: 2.21/2.22


Attachments
tests: Add failing tests which verify exceptions raised in property getters (1.42 KB, patch)
2014-08-09 20:11 UTC, Simon Feltman
committed Details | Review

Description sandro 2009-03-17 07:53:23 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:
Comment 1 Paul Pogonyshev 2009-04-14 20:37:57 UTC
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.
Comment 2 Sebastian Pölsterl 2012-04-21 14:20:25 UTC
Works as expected with pygobject 3.2.
Comment 3 Simon Feltman 2012-11-06 09:55:22 UTC
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 ***
Comment 4 Simon Feltman 2014-08-09 09:23:03 UTC
Re-opening because bug 616279 is not specific enough. Property getter exceptions are a whole different code path.
Comment 5 Simon Feltman 2014-08-09 20:11:16 UTC
The following fix has been pushed:
b1caef9 tests: Add failing tests which verify exceptions raised in property getters
Comment 6 Simon Feltman 2014-08-09 20:11:19 UTC
Created attachment 283021 [details] [review]
tests: Add failing tests which verify exceptions raised in property getters
Comment 7 GNOME Infrastructure Team 2018-01-10 19:59:19 UTC
-- 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.