[GRASS-dev] [GRASS GIS] #3211: Pygrass crash when trying to drop DB table

#3211: Pygrass crash when trying to drop DB table
---------------------+---------------------------------
Reporter: lrntct | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.2.0
Component: PyGRASS | Version: svn-releasebranch72
Keywords: | CPU: x86-64
Platform: Linux |
---------------------+---------------------------------
When trying to drop an existing table, PyGRASS crash.

The table is created as follow:

{{{
l_junc = Link(layer=LAYER_JUNCTION, name=KWD_SECT_JUNCTION,
                 table=vect_map.name + TN_JUNCTION, key='cat')
t_junc = l_junc.table()
t_junc.create(COLS_JUNCTION)
}}}

When trying to drop the table or rewrite it:
{{{
if t_junc.exist():
     t_junc.drop(force=True)
}}}
{{{
t_junc.create(COLS_JUNCTION, overwrite=True)
}}}

It gives the following error:

{{{
Traceback (most recent call last):
   File "./v.in.swmm.py", line 196, in <module>
     sys.exit(main())
   File "./v.in.swmm.py", line 189, in main
     write_vector(output_map, drainage_network)
   File "./v.in.swmm.py", line 75, in write_vector
     t_junc.drop(force=True)
   File "/usr/lib/grass72/etc/python/grass/pygrass/vector/table.py", line
1022, in drop
     used = db_table_in_vector(self.name)
   File "/usr/lib/grass72/etc/python/grass/script/db.py", line 192, in
db_table_in_vector
     for f in vector_db(vect, stderr=nuldev).values():
   File "/usr/lib/grass72/etc/python/grass/script/vector.py", line 49, in
vector_db
     **args)
   File "/usr/lib/grass72/etc/python/grass/script/core.py", line 461, in
read_command
     return handle_errors(returncode, stdout, args, kwargs)
   File "/usr/lib/grass72/etc/python/grass/script/core.py", line 329, in
handle_errors
     returncode=returncode)
grass.exceptions.CalledModuleError: Module run None ['v.db.connect', '--
q', '-g', 'map=drainage_network@kolkata', 'sep=;'] 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/3211&gt;
GRASS GIS <https://grass.osgeo.org>

#3211: Pygrass crash when trying to drop DB table
----------------------+---------------------------------
  Reporter: lrntct | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.0
Component: PyGRASS | Version: svn-releasebranch72
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by lrntct):

Dropping the table with the command line tool works correctly:
{{{
/usr/lib/grass72/scripts/db.droptable -f table=drainage_network_junction
Forcing ...
(Wed Nov 16 15:29:15 2016) Command finished (4 sec)
}}}

Below is a minimum working example:

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.vector.table import Link

cols = [(u'cat', 'INTEGER PRIMARY KEY'),
         (u'name', 'TEXT')]

cols_j = [(u'cat', 'INTEGER PRIMARY KEY'),
           (u'junction', 'TEXT')]

with VectorTopo('drop_table_test', mode='w', tab_cols=cols) as vect_map:
     l_junc = Link(layer=1, name='link_name',
                   table=vect_map.name + 'junc', key='cat')
     t_junc = l_junc.table()
     t_junc.create(cols_j, overwrite=True)
}}}

Just run two times the script to see the error.

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

#3211: Pygrass crash when trying to drop DB table
----------------------+---------------------------------
  Reporter: lrntct | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.3
Component: PyGRASS | Version: svn-releasebranch72
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
----------------------+---------------------------------

Comment (by lucadelu):

I tested the example and it is quite strange because inside the pyGRASS
code is returning error, but if I run the problematic command just after
there is no error...

{{{
## -- End pasted text --
WARNING: Vector map <drop_table_test> already exists and will be
          overwritten
> /home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/script/vector.py(49)vector_db()
-> s = read_command('v.db.connect', quiet=True, flags='g', map=map,
sep=';',
(Pdb) c
WARNING: Coor file of vector map <drop_table_test@user1> is larger than it
          should be (18 bytes excess)
Building topology for vector map <drop_table_test@user1>...
Registering primitives...
---------------------------------------------------------------------------
CalledModuleError Traceback (most recent call
last)
<ipython-input-1-0ba8b438fe5f> in <module>()
      12 table=vect_map.name + 'junc', key='cat')
      13 t_junc = l_junc.table()
---> 14 t_junc.create(cols_j, overwrite=True)

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/vector/table.pyc in create(self, cols, name,
overwrite, cursor)
    1162 except OperationalError: # OperationalError
    1163 if overwrite:
-> 1164 self.drop(force=True)
    1165 cur.execute(sql.CREATE_TAB.format(tname=newname,
    1166 coldef=coldef))

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/pygrass/vector/table.pyc in drop(self, cursor, force)
    1029 cur = cursor if cursor else self.conn.cursor()
    1030 if self.exist(cursor=cur):
-> 1031 used = db_table_in_vector(self.name)
    1032 if used is not None and len(used) > 0 and not force:
    1033 print(_("Deleting table <%s> which is attached"

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/script/db.pyc in db_table_in_vector(table, mapset)
     192 vects = list_strings('vect', mapset=mapset)
     193 for vect in vects:
--> 194 for f in vector_db(vect, stderr=nuldev).values():
     195 if not f:
     196 continue

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/script/vector.pyc in vector_db(map, **args)
      47 """
      48 import pdb; pdb.set_trace()
---> 49 s = read_command('v.db.connect', quiet=True, flags='g',
map=map, sep=';',
      50 **args)
      51 pdb.set_trace()

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/script/core.pyc in read_command(*args, **kwargs)
     474 if _capture_stderr and returncode:
     475 sys.stderr.write(stderr)
--> 476 return handle_errors(returncode, stdout, args, kwargs)
     477
     478

/home/lucadelu/compilati/grass_trunk/dist.x86_64-pc-linux-
gnu/etc/python/grass/script/core.pyc in handle_errors(returncode, result,
args, kwargs)
     330 args = make_command(*args, **kwargs)
     331 raise CalledModuleError(module=None, code=repr(args),
--> 332 returncode=returncode)
     333
     334 def start_command(prog, flags=b"", overwrite=False, quiet=False,

CalledModuleError: Module run None ['v.db.connect', '--q', '-g',
'map=drop_table_test@user1', 'sep=;'] ended with error
Process ended with non-zero return code 1. See errors in the (error)
output.

In [2]: from grass.script import read_command

In [3]: s = read_command('v.db.connect', quiet=True, flags='g',
map='drop_table_test@
    ...: user1', sep=';')
WARNING: Coor file of vector map <drop_table_test@user1> is larger than it
          should be (18 bytes excess)

In [4]: s
Out[4]:
'1/drop_table_test;drop_table_test;cat;/home/lucadelu/grassdata/nc_spm_08_grass7/user1/sqlite/sqlite.db;sqlite\n'
}}}

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