#2581: Fix Python ctypes conversion for stat64 struct on GNU/Hurd
---------------------------+------------------------------------------------
Reporter: sebastic | Owner: grass-dev@…
Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Python ctypes | Version: svn-releasebranch70
Keywords: | Platform: Unspecified
Cpu: x86-32 |
---------------------------+------------------------------------------------
The Debian package build of GRASS 7.0.0RC1 on the hurd-i386 architecture
revealed some more issues.
The attached patch encloses the Python ternary expression in parenthesis
to fix an issue with the C to Python conversion of the stat & stat64
structs on GNU/Hurd.
The structs define the final member conditionally, for the {{{stat64}}}
structure this is:
{{{ #define _SPARE_SIZE ((sizeof (__fsid_t) == sizeof (int)) ? 9 : 8)
int st_spare[_SPARE_SIZE]; /* Room for future expansion. */ #undef _SPARE_SIZE
}}}
This gets converted by {{{ctypesgen}}} to:
{{{
('st_spare', c_int * (sizeof(__fsid_t) == sizeof(c_int)) and 9 or 8),
}}}
Which causes a {{{TypeError}}} for every Python script including the
generated gis.py:
{{{
TypeError: second item in _fields_ tuple (index 17) must be a C type
}}}
Enclosing the Python expression in parenthesis fixes the error:
{{{
('st_spare', c_int * ((sizeof(__fsid_t) == sizeof(c_int)) and 9 or 8)),
}}}
Since [https://pypi.python.org/pypi/ctypesgen ctypesgen] doesn't look
actively maintained upstream anymore, I'm forwarding this patch for
inclusion in GRASS only.
#2581: Fix Python ctypes conversion for stat64 struct on GNU/Hurd
---------------------------+------------------------------------------------
Reporter: sebastic | Owner: grass-dev@…
Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Python ctypes | Version: svn-releasebranch70
Keywords: | Platform: Other Unix
Cpu: x86-32 |
---------------------------+------------------------------------------------
Comment(by glynn):
Replying to [ticket:2581 sebastic]:
> The attached patch encloses the Python ternary expression in parenthesis
to fix an issue with the C to Python conversion of the stat & stat64
structs on GNU/Hurd.
It would be better to use Python's own conditional-expression syntax (a if
cond else b). The and-or hack produces the wrong answer if the if-true
expression evaluates to false.
Python's conditional-expression syntax probably didn't exist when
ctypesgen was written (it was added in 2.5), but GRASS doesn't support any
Python version old enough for that to matter (IIRC, we officially require
at least 2.6, and having 2.7-isms slip into the code by accident hasn't
been particularly uncommon).
#2581: Fix Python ctypes conversion for stat64 struct on GNU/Hurd
---------------------------+------------------------------------------------
Reporter: sebastic | Owner: grass-dev@…
Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Python ctypes | Version: svn-releasebranch70
Keywords: | Platform: Other Unix
Cpu: x86-32 |
---------------------------+------------------------------------------------
Comment(by sebastic):
Replying to [comment:2 glynn]:
> It would be better to use Python's own conditional-expression syntax (a
if cond else b). The and-or hack produces the wrong answer if the if-true
expression evaluates to false.
Yes, I'm aware of the recommendation to use if/else instead of and/or in
Python in place of the ?: ternary operator in C.
I kept the patch as minimal as possible to only fix the nesting issue.
I've now updated the patch to also use if/else instead of and/or.