[GRASS-dev] [GRASS GIS] #2748: ctype.POINTER function overloading prevents automatic byref() conversion

GRASS GIS trac at osgeo.org
Fri Sep 18 04:02:08 PDT 2015


#2748: ctype.POINTER function overloading prevents automatic byref() conversion
---------------------------+-------------------------
 Reporter:  artegion       |      Owner:  grass-dev@…
     Type:  defect         |     Status:  new
 Priority:  normal         |  Milestone:
Component:  Python ctypes  |    Version:  7.0.1
 Keywords:                 |        CPU:  Unspecified
 Platform:  Unspecified    |
---------------------------+-------------------------
 From "https://docs.python.org/2/library/ctypes.html#structured-data-types"
 ''"In addition, if a function argument is explicitly declared to be a
 pointer type (such as POINTER(c_int)) in argtypes, an object of the
 pointed type (c_int in this case) can be passed to the function.
 ctypes will apply the required byref() conversion in this case
 automatically."''

 grass.lib module overwrites ctype.POINTER function in ctypes_preamble
 module.
 Consequently function arguments types are not recognized:
  i.e. Vect_list_append.argtypes reports

 {{{
 <class 'grass.lib.ctypes_preamble.LP_struct_ilist'>
 }}}

  instead of

 {{{
 <class 'grass.lib.vector.LP_struct_ilist'>
 }}}

 and the required byref() conversion  does't happen but ctpyes.byref
 explicit call is needed


 example:
 {{{
 >>> from ctypes import byref
 >>> from grass.lib.vector import struct_ilist, Vect_list_append
 >>> a= struct_ilist()
 >>> a.__class__
 <class 'grass.lib.vector.struct_ilist'>
 >>> Vect_list_append.argtypes
 [<class 'grass.lib.ctypes_preamble.LP_struct_ilist'>, <class
 'ctypes.c_long'>]
 >>>
 >>>
 >>> if Vect_list_append(a, 1):
 ...    print "direct call failed"
 ... else:
 ...    print a.n_values, a.value[a.n_values-1]
 ...
 direct call failed
 >>>
 >>>
 >>> if Vect_list_append(byref(a), 2):
 ...    print "byref call failed"
 ... else:
 ...    print a.n_values, a.value[a.n_values-1]
 ...
 1 2
 >>>
 }}}


 Using a modified version of grass.lib.vector (added
 {{{
 from ctypes import POINTER
 }}}

 after
 {{{
 from ctypes_preamble import *
 }}}
 everything seems fine...



 {{{
 >>> from ctypes import byref
 >>> from grassmod.lib.vector import struct_ilist, Vect_list_append
 >>> a= struct_ilist()
 >>> a.__class__
 <class 'grassmod.lib.vector.struct_ilist'>
 >>> Vect_list_append.argtypes
 [<class 'grassmod.lib.vector.LP_struct_ilist'>, <class 'ctypes.c_long'>]
 >>>
 >>>
 >>> if Vect_list_append(a, 1):
 ...    print "direct call failed"
 ... else:
 ...    print a.n_values, a.value[a.n_values-1]
 ...
 1 1
 >>>
 >>>
 >>> if Vect_list_append(byref(a), 2):
 ...    print "byref call failed"
 ... else:
 ...    print a.n_values, a.value[a.n_values-1]
 ...
 2 2
 >>>
 }}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/2748>
GRASS GIS <https://grass.osgeo.org>



More information about the grass-dev mailing list