classification
Title: ctypes callback fails when called in Python with array argument
Type: behavior
Components: Library (Lib), Windows Versions: Python 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: theller Nosy List: kermode, theller
Priority: normal Keywords:

Created on 2008-01-11 23:14 by kermode, last changed 2008-01-12 00:04 by christian.heimes.

Messages
msg59759 (view) Author: Lenard Lindstrom (kermode) Date: 2008-01-11 23:14
When a callback is created with an array argument and then is called
from Python the callback function receives an array full of garbage.

Here is an example:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> A = c_int * 1
>>> A
<class '__main__.c_long_Array_1'>
>>> Foo = CFUNCTYPE(None, A)
>>> def py_foo(a):
...     print a
...     print a[0]
...
>>> foo = Foo(py_foo)
>>> foo(A(42))
<__main__.c_long_Array_1 object at 0x00B54440>
11879448

It works correctly when the callback is declared with a pointer argument
instead:

>>> A = c_int * 1
>>> Foo = CFUNCTYPE(None, POINTER(c_int))
>>> def py_foo(p):
...     print p
...     print p[0]
...
>>> foo = Foo(py_foo)
>>> foo(A(42))
<ctypes.LP_c_long object at 0x00B54440>
42
History
Date User Action Args
2008-01-12 00:04:28christian.heimessetpriority: normal
assignee: theller
nosy: + theller
2008-01-11 23:14:58kermodecreate