From: Brendan Miller on
I'm using python 2.5.2.

I have a ctypes function with argtypes like this:

_create_folder.argyptes = [c_void_p, c_int]

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 didn't see this behavior documented when I read through
http://docs.python.org/library/ctypes.html, maybe I'm just missing
it... if that's the case a reference would be appreciated.

Brendan
From: Mark Dickinson on
On Apr 14, 7:09 pm, Brendan Miller <catph...(a)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
From: Brendan Miller on
On Wed, Apr 14, 2010 at 12:12 PM, Mark Dickinson <dickinsm(a)gmail.com> wrote:
> On Apr 14, 7:09 pm, Brendan Miller <catph...(a)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'.  :)

Woops! Well, nice to know that ctypes works as I expect.