Opened 10 years ago
Closed 9 years ago
#2581 closed defect (fixed)
Fix Python ctypes conversion for stat64 struct on GNU/Hurd
Reported by: | Bas Couwenberg | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | 7.0.2 |
Component: | Python ctypes | Version: | svn-releasebranch70 |
Keywords: | python | Cc: | |
CPU: | Unspecified | Platform: | All |
Description
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 ctypesgen doesn't look actively maintained upstream anymore, I'm forwarding this patch for inclusion in GRASS only.
Attachments (2)
Change History (9)
by , 10 years ago
Attachment: | python-ctypes-ternary.patch added |
---|
comment:1 by , 10 years ago
Platform: | Unspecified → Other Unix |
---|
comment:2 by , 10 years ago
by , 10 years ago
Attachment: | python-ctypes-ternary.2.patch added |
---|
Also use if/else instead of and/or
comment:3 by , 10 years ago
Replying to 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.
follow-up: 5 comment:4 by , 9 years ago
CPU: | x86-32 → Unspecified |
---|---|
Keywords: | python added |
Milestone: | 7.0.0 → 7.0.2 |
Platform: | Other Unix → All |
Shall we apply this revised patch?
follow-up: 6 comment:5 by , 9 years ago
follow-up: 7 comment:6 by , 9 years ago
comment:7 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Replying to sebastic:
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).