#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
> grass.lib module overwrites ctype.POINTER function in ctypes_preamble
module.
The replacement POINTER function has the following comment:
{{{
# Convert None to a real NULL pointer to work around bugs
# in how ctypes handles None on 64-bit platforms
}}}
Have you tested whether this is still an issue (it might have been fixed
since ctypesgen was last updated)?
Having to use byref() explicitly seems less of an issue than not working
on 64-bit systems.
Actually, if an argument is a pointer, the caller should probably be using
byref() explicitly regardless of whether or not ctypes has a workaround.
What's the problem with needing to use byref()?
Not a real problem, just a bit disappointing: python ctypes documentation
describes an interesting feature but our code presents a different
behavior (it is far more pythonic do not care about byreference/byvalue if
ctypes can do the dirty work).
The odd thing is that happens unnoticed: while ctypes raises exceptions
passing wrong argument type in this case only function return code (or
segmentation fault exception) reveals troubles.
Replying to [comment:1 glynn]:
> Replying to [ticket:2748 artegion]:
>
> > grass.lib module overwrites ctype.POINTER function in ctypes_preamble
module.
>
> The replacement POINTER function has the following comment:
> {{{
> # Convert None to a real NULL pointer to work around bugs
> # in how ctypes handles None on 64-bit platforms
> }}}
> Have you tested whether this is still an issue (it might have been fixed
since ctypesgen was last updated)?
>
> Having to use byref() explicitly seems less of an issue than not working
on 64-bit systems.
>
> Actually, if an argument is a pointer, the caller should probably be
using byref() explicitly regardless of whether or not ctypes has a
workaround. What's the problem with needing to use byref()?
Not a real problem, just a bit disappointing: python ctypes documentation
describes an interesting feature but our code presents a different
behavior (it is far more pythonic do not care about byreference/byvalue if
ctypes can do the dirty work).
The odd thing is that happens unnoticed: while ctypes raises exceptions
passing wrong argument type in this case only function return code (or
segmentation fault exception) reveals troubles.
Replying to [comment:5 glynn]:
> That suggests that it's working around an issue which is specific to
Python 2.5. Do we still support that? If not, the workaround can be
removed.
Replying to [comment:5 glynn]:
> Replying to [comment:2 artegion]:
> > It seems a known bug of ctypesgen
> > [https://github.com/davidjamesca/ctypesgen/issues/26\]
>
> That suggests that it's working around an issue which is specific to
Python 2.5. Do we still support that? If not, the workaround can be
removed.
Since we require at least Python >= 2.6, let's remove the workaround...