-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
ctypes c_long & c_bool have incorrect PEP-3118 type codes #54955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Currently on Python 3.x: >>> import ctypes
>>> memoryview(ctypes.c_long()).format
'<l' This is invalid on 64-bit platforms: the above means 32-bit little-endian float. The '<' endian specification turns on the "standard size" mode (similarly as for the struct module), which makes type character have a platform-independent meaning. Unfortunately, the struct module format syntax does *not* allow specifying native-size non-native-endian items. So just replacing '<' by '^' cannot be in general be done. Suggested fix attached. It adds a converter function that maps the platform-dependent ctypes codes to struct module standard-size codes; handling c_long and c_bool specially.
After this patch (and the one in http://bugs.python.org/issue10744 ): >>> import numpy as np
>>> from ctypes import *
>>> class Point(Structure):
... _fields_ = [("x", c_long), ("y", c_long)]
...
>>> class StructWithArrays(Structure):
... _fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)]
...
>>> x = StructWithArrays()
>>> y = np.asarray(x)
>>> y.dtype
dtype([('x', '<i8', (2, 3)), ('y', [('x', '<i8'), ('y', '<i8')], (4,))])
>>> y['x'] = [[1,2,3],[4,5,6]]
>>> y['y']['x'] = np.arange(4) + 10
>>> y['y']['y'] = np.arange(4) + 20
>>> x.x[0][0]
1
>>> x.x[0][1]
2
>>> x.x[0][2]
3
>>> x.y[0].x
10
>>> x.y[1].x
11
>>> x.y[0].y
20
>>> x.y[1].y
21 |
Could someone do a patch review on this please. |
Converted patch to Github PR + some cleanup. |
Added the names from the experts list to this ticket. |
Pauli, do you want to submit backport PRs? This can be done using the cherry_picker script: https://devguide.python.org/committing/?highlight=cherry_picker#backporting-changes-to-an-older-version |
Created backport PR for 3.6: #3241 |
At this point, 2.7 and 3.6 do. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: