ctypes question

Mark Dickinson dickinsm at gmail.com
Wed Apr 14 15:12:30 EDT 2010


On Apr 14, 7:09 pm, Brendan Miller <catph... at catphive.net> wrote:
> I'm using python 2.5.2.
>
> I have a ctypes function with argtypes like this:
>
> _create_folder.argyptes = [c_void_p, c_int]

Is that line a cut-and-paste?  If so, try 'argtypes' instead of
'argyptes'.  :)

> The issue I am having is that I can call it like this
>
> _create_folder(some_pointer, "asdf")
>
> and it won't raise a TypeError. Why would it accept a string for an
> integer argument?

I get the expected TypeError in 2.5.4 (OS X) (using printf, for lack
of anything better):

Python 2.5.4 (r254:67916, Feb 11 2010, 00:50:55)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> libc = CDLL("libc.dylib")
>>> libc
<CDLL 'libc.dylib', handle 8fe46768 at 1a2330>
>>> printf = libc.printf
>>> printf.argtypes = [c_char_p, c_int]
>>> printf("%d\n", 53)
53
3
>>> printf("%d\n", "asdf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong
type

--
Mark



More information about the Python-list mailing list