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 342948 - Add exception handling to GOption
Add exception handling to GOption
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: gobject
Git master
Other All
: Normal enhancement
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2006-05-25 18:56 UTC by Johannes Hölzl
Modified: 2007-07-08 21:06 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Patch to add Exception handling to GOption (12.19 KB, patch)
2006-05-25 18:57 UTC, Johannes Hölzl
none Details | Review
Fix in pyg_gerror_exception_check and exception handling for goption (9.00 KB, patch)
2007-04-30 15:59 UTC, Johannes Hölzl
committed Details | Review
Adds "message" attribute to the class dictionary of gobject.GError. (1015 bytes, patch)
2007-07-07 19:17 UTC, Johannes Hölzl
needs-work Details | Review
Adds "message" attribute to the class dictionary of gobject.GError. (1.05 KB, patch)
2007-07-08 20:44 UTC, Johannes Hölzl
committed Details | Review

Description Johannes Hölzl 2006-05-25 18:56:31 UTC
When an arguement like "--number=text" is written and "--number" only accepts an
integer a exception traceback is written to the screen and a GError is raised.

gobject.Option should at least handle the BadOptionValue-Exception and translate
this into an GError. This GError should handled by the OptParser and
backtranslated into a BadOptionValue-Exception.
Comment 1 Johannes Hölzl 2006-05-25 18:57:41 UTC
Created attachment 66214 [details] [review]
Patch to add Exception handling to GOption

Adds also the function pyg_set_gerror_from_exception().
Comment 2 Johan (not receiving bugmail) Dahlin 2006-11-18 15:40:27 UTC
This needs to wait until 2.14, since it adds new API.
Comment 3 Johan (not receiving bugmail) Dahlin 2007-04-30 02:53:03 UTC
Johannes: Okay, we're open for API additions on trunk. Can you commit this?
Comment 4 Johannes Hölzl 2007-04-30 15:59:54 UTC
Created attachment 87275 [details] [review]
Fix in pyg_gerror_exception_check and exception handling for goption

Sorry, but i don't have an gnome-svn account.

I updated the patch to use pyg_gerror_exception_check() instead of my own function. I fixed an segmentation fault in it and made it non-static to be usable by goptioncontext.c.
Comment 5 Gustavo Carneiro 2007-07-07 13:07:44 UTC
This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.

Committed the patch; however it triggers a Python 2.6 deprecation warning:

make[2]: warning: -jN forced in submake: disabling jobserver mode.
.../gobject/option.py:187: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
  gerror.message = str(error)
../gobject/option.py:315: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
  largs.extend(context.parse([sys.argv[0]] + rargs))
../gobject/option.py:324: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
  raise OptionValueError(error.message)
../gobject/option.py:326: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
  raise BadOptionError(error.message)
.......................[7317 refs]

I don't know how to fix them, though.
Comment 6 Johannes Hölzl 2007-07-07 19:17:45 UTC
Created attachment 91368 [details] [review]
Adds "message" attribute to the class dictionary of gobject.GError.

I tried the following: Add the attribute "message" to the class dictionary of GError, so the getter and setter for BaseException.message is hidden. When err.message is accessed no deprecation warning is shown.

Problem: We don't see if the user doesn't explicitly see when he forgets to set the message. But can also set message to None, this would also work.
Comment 7 Gustavo Carneiro 2007-07-07 20:19:25 UTC
Comment on attachment 91368 [details] [review]
Adds "message" attribute to the class dictionary of gobject.GError.

>Index: gobject/gobjectmodule.c
>===================================================================
>--- gobject/gobjectmodule.c	(Revision 688)
>+++ gobject/gobjectmodule.c	(Arbeitskopie)
>@@ -3089,6 +3089,20 @@
>     return -2;
> }
> 
>+static PyObject *
>+build_gerror( void )
>+{
>+    PyObject *dict;
>+    
>+    dict = PyDict_New();
>+    /* This is a hack to work around the deprecation warning of
>+     * BaseException.message in Python 2.6+.
>+     * GError has also an "message" attribute.
>+     */
>+    PyDict_SetItemString(dict, "message", PyString_FromString(""));

This leaks a string object reference.

Why not using None as default value here instead of ""?

Otherwise it does fix the deprecation warning.
Comment 8 Johannes Hölzl 2007-07-08 20:44:12 UTC
Created attachment 91447 [details] [review]
Adds "message" attribute to the class dictionary of gobject.GError.

I changed the attribute "message" from the empty string to None.

I also dereference now the class dictionary.
Comment 9 Gustavo Carneiro 2007-07-08 21:02:02 UTC
Looks good.  Thanks.