GNOME Bugzilla – Bug 342948
Add exception handling to GOption
Last modified: 2007-07-08 21:06:15 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.
Created attachment 66214 [details] [review] Patch to add Exception handling to GOption Adds also the function pyg_set_gerror_from_exception().
This needs to wait until 2.14, since it adds new API.
Johannes: Okay, we're open for API additions on trunk. Can you commit this?
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.
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.
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 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.
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.
Looks good. Thanks.