[GRASS-dev] [GRASS-SVN] r64850 - in grass-addons/grass7/gui/wxpython: . wx.mwprecip

Glynn Clements glynn at gclements.plus.com
Sat Mar 14 04:41:26 PDT 2015


Vaclav Petras wrote:

> This should be one big try-except. Also, `except:` is a bad practice, this
> will catch everything including perhaps even SyntaxError. You should
> specify the error/exception type.

If you want to catch all errors, use "except StandardError:". That
includes anything which is normally considered an error but avoids
catching SystemExit (generated by sys.exit()) and KeyboardInterrupt
(Ctrl-C etc).

Catching a more specific error is preferable, but not always possible;
the use of duck-typing means that the set of exceptions which a
function can raise depends entirely on the values of the parameters
and variables which it references.

> +        try:
> +            self.database.SetValue(self.settings['database'])

If the intent is simply to handle the case where the key isn't present
(which would normally raise KeyError), it would probably be better to
just use self.settings.get('database') instead (this returns None if
the key isn't present).

> What you should use in case of `eval` that's a questions, more
> checks with `eval` is always a good idea.

Avoiding eval() altogether is usually a good idea. To read literal
values of basic types, use ast.literal_eval(). This will evaluate
literal expressions comprised of strings, numbers, booleans, None,
tuples, lists and dictionaries, but won't invoke arbitrary
function/method calls.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list