GNOME Bugzilla – Bug 710671
vfuncs with throws=1 should be able to raise a GError somehow
Last modified: 2015-09-22 22:04:01 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
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)
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".
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.
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.
ping?
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.