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 710671 - vfuncs with throws=1 should be able to raise a GError somehow
vfuncs with throws=1 should be able to raise a GError somehow
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: introspection
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on: 729543
Blocks: 616036
 
 
Reported: 2013-10-22 18:26 UTC by Simon McVittie
Modified: 2015-09-22 22:04 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add vfunc exception conversion and passthrough as a GError (3.93 KB, patch)
2014-05-05 08:05 UTC, Simon Feltman
none Details | Review
Support throwing exceptions in closures (9.62 KB, patch)
2015-06-03 14:12 UTC, Garrett Regier
committed Details | Review

Description Simon McVittie 2013-10-22 18:26:13 UTC
I have this virtual method in TpBaseProtocolClass:

  gchar *(*normalize_contact) (TpBaseProtocol *self,
      const gchar *contact,
      GError **error);

which translates to this g-i:

      <virtual-method name="normalize_contact" throws="1">
        <return-value transfer-ownership="full">
          <type name="utf8" c:type="gchar*"/>
        </return-value>
        <parameters>
          <instance-parameter name="self" transfer-ownership="none">
            <type name="BaseProtocol" c:type="TpBaseProtocol*"/>
          </instance-parameter>
          <parameter name="contact" transfer-ownership="none">
            <type name="utf8" c:type="const gchar*"/>
          </parameter>
        </parameters>
      </virtual-method>

I would like to be able to do this:

    def do_normalize_contact(self, contact):
        if contact:
            return contact.lower()
        else:
            raise ValueError('The empty string is not a valid identifier')

or ideally, something more like this:

    def do_normalize_contact(self, contact):
        if contact:
            return contact.lower()
        else:
            raise TelepathyGLib.Error(TelepathyGLib.Error.INVALID_HANDLE,
                'The empty string is not a valid identifier')

This doesn't seem to be possible: if I raise an exception, it just gets printed to stderr:

> Traceback (most recent call last):
>   File "tests/python/cm.py", line 40, in do_normalize_contact
>     raise ValueError('Invalid identifier')
> ValueError: Invalid identifier

and then my Python code returns None from the vfunc without setting the GError correctly:

** (process:19507): CRITICAL **: dbus_g_method_return_error: assertion `error != NULL' failed
Comment 1 Simon Feltman 2013-10-22 23:34:03 UTC
Not exactly a dup of bug 616036 but definitely related. I think the difference is that GErrors are not actually explicit arguments that need to be marshaled but rather we need to check if an exception was raised and the function throws, then translate the exception to a GError:
https://git.gnome.org/browse/pygobject/tree/gi/pygi-closure.c?id=3.10.1#n532
(after the call to PyObject_CallObject)
Comment 2 Simon McVittie 2013-10-23 11:27:31 UTC
This TODO also looks related:

            if (g_type_info_get_tag (&type_info) == GI_TYPE_TAG_ERROR) {
                /* TODO: check if an exception has been set and convert it to a GError */
                out_args[i_out_args].v_pointer = NULL;
                i_out_args++;
                continue;
            }

although I don't know whether that's talking about explicit GError parameters rather than "throws".
Comment 3 Simon Feltman 2014-05-05 08:05:49 UTC
Created attachment 275873 [details] [review]
Add vfunc exception conversion and passthrough as a GError

Add support for converting exceptions raised within Python
vfunc implementations into GErrors.

Notes:
This is working well but depends on the patches in GI bug 729543
to work in practice.
Comment 4 Garrett Regier 2015-06-03 14:12:00 UTC
Created attachment 304519 [details] [review]
Support throwing exceptions in closures

This allows exceptions raised in vfunc implemntations
and callbacks to be turned into GErrors.

NOTE: this requires matchs in https://bugzilla.gnome.org/show_bug.cgi?id=729543
thus we must bump the GI req once they are commited.

----
This needed to be updated after the updating C->Python calls to use the cache.
Comment 5 Garrett Regier 2015-07-06 03:40:53 UTC
ping?
Comment 6 Garrett Regier 2015-09-22 22:04:01 UTC
This problem has been fixed in the unstable development version. The fix will be available in the next major software release. You may need to upgrade your Linux distribution to obtain that newer version.