New submission from Pauli Virtanen <p...@iki.fi>: 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 ---------- files: 001-ctypes-fix-pep-3118-type-codes-for-c-long-and-c-bool.patch keywords: patch messages: 124411 nosy: pv priority: normal severity: normal status: open title: ctypes c_long & c_bool have incorrect PEP-3118 type codes Added file: http://bugs.python.org/file20124/001-ctypes-fix-pep-3118-type-codes-for-c-long-and-c-bool.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue10746> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com