[GRASS-dev] PyGRASS Module error handling
Vaclav Petras
wenzeslaus at gmail.com
Mon Jun 30 18:10:41 PDT 2014
On Mon, Jun 30, 2014 at 8:33 PM, Sören Gebbert <soerengebbert at googlemail.com
> wrote:
> Hi Vaclav,
>
> 2014-06-30 17:43 GMT+02:00 Vaclav Petras <wenzeslaus at gmail.com>:
> > Hi,
> >
> > when writing tests which involve PyGRASS Module class I found that at
> some
> > occasions the error messages are quite confusing.
> >
> > First when you try to run one module twice, you get AttributeError:
> >
> >>>> from grass.pygrass.modules import Module
> >>>> from subprocess import PIPE
> >>>> m = Module('r.info', map='elevation', run_=False, stdout_=PIPE)
> >>>> m.run()
> >>>> m.outputs['stdout'].value
> > ...
> >>>> # running for the second time
> >>>> m.run()
> > Traceback (most recent call last)
> > ...
> > AttributeError: 'str' object has no attribute 'fileno'
>
> That should be fixed in r61083. Reason was the wrong handling of
> stderr and stdout ,
> the PIPE was overwritten when the module finished.
>
> Thanks.
> >
> > Second, when you confuse m.inputs['map'].value with m.inputs['map']
> (which
> > is pretty easy error to do) you get "TypeError: deletions are implemented
> > differently for unicode":
> >
> >>>> # this is OK
> >>>> m.inputs['map'].value = 5
> > Traceback (most recent call last)
> > ...
> > TypeError: The Parameter <map>, require: raster, get: <type 'int'>
> instead
> >>>> # this is not OK
> >>>> # confusing parameter object and parameter value
> >>>> m.inputs['map'] = 'aspect'
> > Traceback (most recent call last)
> > ...
> > TypeError: deletions are implemented differently for unicode
>
> This is indeed a misleading error report. I do not have a solution right
> away,
> but i would suggest to use always the __call__ function when
> specifying an option:
>
> color = Module("r.colors", run_=False)
> color(map="test")
> color(color="gyr")
> color.run()
> color(map="test2", color="ryb")
> color.run()
>
> I must get used to this one.
> > With the former it depends what is actually allowed. Than it is probably
> > easy to fix. With the later, no easy solutions come to my mind.
> >
> > With my tests I was actually asserting m.outputs['stdout'] for true
> which is
> > always true instead of asserting m.inputs['stdout'].value. I don't know
> how
> > to avoid this mistake in general.
>
> We should avoid in general to set the option values directly, instead
> the __call__ function should be used
> which is much safer.
>
> > Does this make sense to you or do you think that I and PyGRASS users
> should
> > be just be more careful?
>
> The error report must be modified to be more meaningful. I would
> suggest to overwrite the assignment function "=" (if this is
> possible???) of the Parameter class to catch such errors. The
> assignment method will work in case of a Parameter object and raise a
> meaningful error in case anything else. Does this sounds reasonable?
>
> I must admit that I'm not sure whose assignment operator is called.
Parameters, or the function for adding item to the dictionary? Assignment
operator cannot be overloaded in Python. So, it seems that it must the the
later. So, the only solution is TypeDict which is already used. I'm
confused. Now focusing on something else.
Thanks,
Vaclav
> Best regards
> Soeren
>
> >
> > Vaclav
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20140630/0fdc69df/attachment.html>
More information about the grass-dev
mailing list