[GRASS-dev] [GRASS GIS] #2709: pyGRASS Module runs twice from interactive Python prompt

#2709: pyGRASS Module runs twice from interactive Python prompt
--------------------------+--------------------------------
  Reporter: martinl | Owner:
      Type: defect | Status: new
  Priority: major | Milestone: 7.0.4
Component: PyGRASS | Version: unspecified
Resolution: | Keywords: interactive prompt
       CPU: Unspecified | Platform: Linux
--------------------------+--------------------------------

Comment (by zarch):

Hi Markus,

Replying to [comment:6 neteler]:
> {{{
> GRASS 7.1.svn (eu_laea):~ > python
> Python 2.7.10 (default, Sep 8 2015, 17:20:17)
> [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from grass.pygrass.modules import Module
> >>> Module('g.extension', flags='a')
> List of installed extensions (modules):
> r.skyview
> i.segment.hierarchical
> Module('g.extension') <<== ???
> }}}

This is right, things are clearer using ipython:

{{{
In [1]: from grass.pygrass.modules import Module

In [2]: Module('g.extension', flags='a')
No extension (module) installed
Out[2]: Module('g.extension')
}}}

the python shell does not differentiate between print in stdout/stderr
(generate by the execution of module) and the representation of the
object.
The line Out[2] is returning the instance of the Module class.

If you are bothering by this just save the instance into a variable:

{{{
In [3]: gext = Module('g.extension', flags='a')
No extension (module) installed
}}}

> Anyway, now entering the next command from the original report:
>
> {{{
> >>> Module('r.mask', flags='r')
> ERROR: No existing MASK to remove
>
> [cut]
>
> msg = _("Module run %s %s ended with error") % (module, code)
> File "/home/neteler/software/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.py", line 583, in
__call__
> return self.run()
> File "/home/neteler/software/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.py", line 706, in
run
> stdout, stderr = self.popen.communicate(input=self.stdin)
> File "/usr/lib64/python2.7/subprocess.py", line 796, in communicate
> self.wait()
> File "/usr/lib64/python2.7/subprocess.py", line 1384, in wait
> pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
> File "/usr/lib64/python2.7/subprocess.py", line 476, in
_eintr_retry_call
> return func(*args)
> KeyboardInterrupt
> }}}

Ipython does not soffer by this strange behavior and it works well.

{{{
In [4]: Module('r.mask', flags='r')
ERROR: No existing MASK to remove
---------------------------------------------------------------------------
CalledModuleError Traceback (most recent call
last)
<ipython-input-4-217f55a3bd88> in <module>()
----> 1 rmsk = Module('r.mask', flags='r')

/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.pyc in
__init__(self, cmd, *args, **kargs)
     524
     525 if args or kargs:
--> 526 self.__call__(*args, **kargs)
     527 self.__call__.__func__.__doc__ = self.__doc__
     528

/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.pyc in
__call__(self, *args, **kargs)
     581 if self.check_:
     582 self.check()
--> 583 return self.run()
     584 return self
     585

/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.pyc in run(self)
     711 raise
CalledModuleError(returncode=self.popen.returncode,
     712 code=self.get_bash(),
--> 713 module=self.name,
errors=stderr)
     714 return self
     715

CalledModuleError: Module run r.mask r.mask maskcats=* layer=1 -r ended
with error
Process ended with non-zero return code 1. See errors in the (error)
output.
}}}

No idea on why is not working properly in the standard python shell.

Just for the record it is working under python3...

{{{
Python 3.5.1 (default, Dec 7 2015, 12:58:09)
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from grass.pygrass.modules import Module
>>> Module('r.mask', flags='r')
ERROR: No existing MASK to remove
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.py", line 526, in
__init__
     self.__call__(*args, **kargs)
   File "/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.py", line 583, in
__call__
     return self.run()
   File "/home/pietro/docdat/src/gis/grass71/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/modules/interface/module.py", line 713, in
run
     module=self.name, errors=stderr)
grass.exceptions.CalledModuleError: Module run r.mask r.mask maskcats=*
layer=1 -r ended with error
Process ended with non-zero return code 1. See errors in the (error)
output.
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/2709#comment:7&gt;
GRASS GIS <https://grass.osgeo.org>