classification
Title: ctypes c_long & c_bool have incorrect PEP-3118 type codes
Type: behavior Stage:
Components: ctypes Versions: Python 3.3, Python 3.2, Python 3.1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: theller Nosy List: BreamoreBoy, mark.dickinson, pitrou, pv, skrah, theller
Priority: normal Keywords: patch

Created on 2010-12-20 23:37 by pv, last changed 2014-06-17 12:54 by BreamoreBoy.

Files
File name Uploaded Description Edit
001-ctypes-fix-pep-3118-type-codes-for-c-long-and-c-bool.patch pv, 2010-12-20 23:37
Messages (2)
msg124411 - (view) Author: Pauli Virtanen (pv) Date: 2010-12-20 23:37
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
msg220819 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-17 12:54
Could someone do a patch review on this please.
History
Date User Action Args
2014-06-17 12:54:32BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220819
2011-08-10 12:43:43skrahsetnosy: + skrah
2010-12-21 19:25:35pitrousetnosy: + mark.dickinson
2010-12-21 19:20:51georg.brandlsetnosy: + pitrou
2010-12-20 23:37:52pvsetversions: + Python 3.1, Python 3.2, Python 3.3
nosy: + theller

assignee: theller
components: + ctypes
type: behavior
2010-12-20 23:37:20pvcreate