[New-bugs-announce] [issue1800] ctypes callback fails when called in Python with array argument

Lenard Lindstrom report at bugs.python.org
Sat Jan 12 00:14:59 CET 2008


New submission from Lenard Lindstrom:

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

----------
components: Library (Lib), Windows
messages: 59759
nosy: kermode
severity: normal
status: open
title: ctypes callback fails when called in Python with array argument
type: behavior
versions: Python 2.5

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1800>
__________________________________


More information about the New-bugs-announce mailing list