classification
Title: ctypes arrays have incorrect buffer information (PEP-3118)
Type: behavior Stage: patch review
Components: ctypes Versions: Python 3.4, Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Tilka, haypo, mark.dickinson, pitrou, pv, ronaldoussoren, skrah, theller
Priority: normal Keywords: patch

Created on 2010-12-20 22:06 by pv, last changed 2013-06-21 14:23 by ronaldoussoren.

Files
File name Uploaded Description Edit
001-ctypes-fix-pep-3118-format-strings-for-arrays.patch pv, 2010-12-20 22:06 Suggested fix (on py3k branch)
Messages (6)
msg124406 - (view) Author: Pauli Virtanen (pv) Date: 2010-12-20 22:06
Ctypes arrays have invalid buffer interface information (on Python 3.1.2):

>>> import ctypes
>>> x = (ctypes.c_double*2)()
>>> y = memoryview(x)
>>> y.shape
(2,)
>>> y.format
'(2)<d'

This implies that the array contains 2 items, each consisting of 2 floats, which is incorrect -- the shape information is duplicated.

A suggested fix is attached.

(From http://projects.scipy.org/numpy/ticket/1699)
msg142081 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-08-14 20:28
Thanks for the patch. I agree with the interpretation of the format string.
One thing is unclear though: Using this interpretation the multi-dimensional 
array notation in format strings only seems useful for pointers to arrays.

The PEP isn't so clear on that, would you agree?


I'm not done reviewing the patch, just a couple of nitpicks:

  - We need a function declaration of _ctypes_alloc_format_string_with_shape()
    in ctypes.h.

  - prefix_len = 32*(ndim+1) + 3: This is surely sufficient, but (ndim+1)
    is not obvious to me. I think we need (20 + 1) * ndim + 3.

  - I'd use "%zd" for Py_ssize_t (I know that in other parts of the
    code "%ld" is used, too).
msg142192 - (view) Author: Pauli Virtanen (pv) Date: 2011-08-16 13:27
The array notation is useful for arrays inside structs, such as "T{(4)i(2,3)f}".
msg191562 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2013-06-21 10:30
@skrah: What is the status of this issue?
msg191575 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-06-21 12:57
Basically someone has to review the patch and commit it. I'm not really
using ctypes, so for me a decent review would take a while.
msg191583 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-06-21 14:23
The correct format string for Py_ssize_t with sprintf is 
"%"PY_FORMAT_SIZE_T"d" (defined and explained in pyport.h).
History
Date User Action Args
2013-06-21 14:23:22ronaldoussorensetnosy: + ronaldoussoren
messages: + msg191583
2013-06-21 12:57:59skrahsetmessages: + msg191575
2013-06-21 10:30:21hayposetmessages: + msg191562
2013-06-21 10:29:53hayposetversions: + Python 3.4, - Python 3.1, Python 3.2
2013-06-21 08:52:59Tilkasetnosy: + Tilka
2011-08-16 13:30:30hayposetnosy: + haypo
2011-08-16 13:27:20pvsetmessages: + msg142192
2011-08-14 20:28:36skrahsetassignee: theller ->
messages: + msg142081
stage: patch review
2011-08-10 08:22:17skrahsetnosy: + skrah
2010-12-21 18:46:51georg.brandlsetnosy: + pitrou
2010-12-21 18:31:24r.david.murraysetnosy: + mark.dickinson
2010-12-20 22:06:47pvcreate