[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions

Amaury Forgeot d'Arc report at bugs.python.org
Tue Sep 21 19:56:51 CEST 2010

Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

I don't think this should happen by default.
but what the user wants is already possible, by using the from_param() method.  For example, the AutoStrParam type converts everything to a string (and a char*):

from ctypes import *

class AutoStrParam(c_char_p):
    def from_param(cls, value):
        return str(value)

strlen = cdll.LoadLibrary('msvcrt').strlen
strlen.argtypes = [AutoStrParam]

print strlen(None)     # "None"          ->  4
print strlen(type)     # "<type 'type'>" -> 13

nosy: +amaury.forgeotdarc

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list