[GRASS-dev] [GRASS GIS] #4018: GRASS ctypes fail with Python 3.7.6+
GRASS GIS
trac at osgeo.org
Sun Jan 12 10:50:12 PST 2020
#4018: GRASS ctypes fail with Python 3.7.6+
----------------------------+---------------------------------
Reporter: mmetz | Owner: grass-dev@…
Type: defect | Status: new
Priority: critical | Milestone: 7.8.3
Component: Python ctypes | Version: git-releasebranch78
Resolution: | Keywords: ctypes
CPU: All | Platform: All
----------------------------+---------------------------------
Comment (by mmetz):
The main change is that for Python 3.7.6+
[https://docs.python.org/3/library/ctypes.html#structures-and-unions
unions and structures with bit-fields should always be passed to functions
by pointer].
The GRASS ctypes generator uses a custom
[https://github.com/OSGeo/grass/blob/master/lib/python/ctypes/preamble.py#L289
class String] which is a union, which needs to be passed to functions as a
pointer for Python 3.7.6+.
Instead of using the custom class String, we could use the
[https://docs.python.org/3/library/ctypes.html#fundamental-data-types
fundamental data type c_char_p]. This works with Python 3.7.6 and Python
3.7.5, but not with Python 3.8.1. In some cases Python 3.8.1 complains
about a `TypeError`, e.g. for C macros like
{{{
#define GRASS_VERSION_STRING "@(#) 7.9.dev (2020)"
}}}
Maybe these C macros must be of ctype `POINTER(c_char)` which does not
make sense because such a pointer would/should return only the first
character pointed to by `POINTER(c_char)`.
The important change in handling ctypes has been introduced with Python
3.7.6. The different behaviour of Python 3.7.6 and Python 3.8.1 regarding
ctypes, particularly pointers to a '\0' terminated array of characters is
right now a mystery.
I have a patch for the GRASS ctypes generator that works with Python 3.7.5
and 3.7.6, i.e. before and after the important change in ctypes handling
has been introduced in Python, and another patch for the GRASS ctypes
generator that works with Python 3.8.1, but not with Python 3.7.x.
Therefore I regard my patches as dirty hacks and am looking forward to any
advice on how to correctly generate fully compatible ctypes. Or even
better, [https://github.com/davidjamesca/ctypesgen ctypesgen] fixes these
problems soon and we can use an updated version of ctypesgen.
--
Ticket URL: <https://trac.osgeo.org/grass/ticket/4018#comment:1>
GRASS GIS <https://grass.osgeo.org>
More information about the grass-dev
mailing list