[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Changes by Amaury Forgeot d'Arc amaur...@gmail.com: -- resolution: - works for me status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Amaury Forgeot d'Arc amaur...@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): @classmethod 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 rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Mark Lawrence breamore...@yahoo.co.uk added the comment: Please ensure tha I'm taken off of the email list as I've been banned from contributing to Python by Raymond Hettinger From: Amaury Forgeot d'Arc rep...@bugs.python.org To: breamore...@yahoo.co.uk Sent: Tue, 21 September, 2010 18:56:51 Subject: [issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions Amaury Forgeot d'Arc amaur...@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): @classmethod 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 rep...@bugs.python.org http://bugs.python.org/issue1962 ___ -- Added file: http://bugs.python.org/file18958/unnamed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___htmlheadstyle type=text/css!-- DIV {margin:0px;} --/style/headbodydiv style=font-family:times new roman,new york,times,serif;font-size:12ptdivPlease ensure tha I'm taken off of the email list as I've been banned from contributing to Python by Raymond Hettingerbr/divdiv style=font-family: times new roman,new york,times,serif; font-size: 12pt;brdiv style=font-family: arial,helvetica,sans-serif; font-size: 13px;font face=Tahoma size=2hr size=1bspan style=font-weight: bold;From:/span/b Amaury Forgeot d'Arc lt;rep...@bugs.python.orggt;brbspan style=font-weight: bold;To:/span/b breamore...@yahoo.co.ukbrbspan style=font-weight: bold;Sent:/span/b Tue, 21 September, 2010 18:56:51brbspan style=font-weight: bold;Subject:/span/b [issue1962] ctypes feature request: Automatic type conversion of input arguments to C functionsbr/fontbrbrAmaury Forgeot d'Arc lt;a ymailto=mailto:amaur...@gmail.com; href=mailto:amaur...@gmail.com;amaur...@gmail.com/agt; added the comment:brbrI don't think this should happen by default.brbut what the user wants is already possible, by using the from_param() method.nbsp; For example, the AutoStrParam type converts everything to a string (and a char*):brbrfrom ctypes import *brbrclass AutoStrParam(c_char_p):brnbsp; nbsp; @classmethodbrnbsp; nbsp; def from_param(cls, value):brnbsp; nbsp; nbsp; nbsp; return str(value)brbrstrlen = cdll.LoadLibrary('msvcrt').strlenbrstrlen.argtypes = [AutoStrParam]brbrprint strlen(None)nbsp; nbsp; # Nonenbsp; nbsp; nbsp; nbsp; nbsp; -gt;nbsp; 4brprint strlen(type)nbsp; nbsp; # lt;type 'type'gt; -gt; 13brbr--brnosy: +amaury.forgeotdarcbrbr___brPython tracker lt;a ymailto=mailto:rep...@bugs.python.org; href=mailto:rep...@bugs.python.org;rep...@bugs.python.org/agt;brlt;a href=http://bugs.python.org/issue1962; target=_blankhttp://bugs.python.org/issue1962/agt;br___br/div/div /divbr /body/html___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Changes by Amaury Forgeot d'Arc amaur...@gmail.com: Removed file: http://bugs.python.org/file18958/unnamed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Changes by Amaury Forgeot d'Arc amaur...@gmail.com: -- nosy: -BreamoreBoy ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Changes by Amaury Forgeot d'Arc amaur...@gmail.com: -- Removed message: http://bugs.python.org/msg117111 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Mark Lawrence breamore...@yahoo.co.uk added the comment: Is this ever likely to happen, given that there's been 2.75 years since the request without a response? -- nosy: +BreamoreBoy ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
Changes by Terry J. Reedy tjre...@udel.edu: -- versions: +Python 3.2 -Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1962 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions
New submission from mattbaas: This is rather a feature request instead of a bug report. Below is the mail I posted to the ctypes-users mailing list. In short: When ctypes checks input argument types using the argtypes attribute, it would be useful if it would try to convert the input value automatically if it isn't already an appropriate ctypes type (but a compatible Python type). Here is the full mail with some examples: I'm wrapping a couple of C functions from a DLL and I'm using the argtypes attribute to declare the types of the input arguments. I can call the functions just fine, but I was wondering why I have to provide the exact ctypes type as input when using more complex types such as arrays or callbacks (whereas Python floats are automatically converted). Here is an example code snippet (I was using Python 2.5 on WinXP). The library ri.dll contains the functions RiColor() which takes an array of 3 floats as input and a function RiErrorHandler() which takes a pointer to a function as input: # Create the required types... RtColor = 3*c_float RtErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p) # Load the library and declare the input arguments... ri = cdll.LoadLibrary(ri.dll) ri.RiColor.argtypes = [RtColor] ri.RiErrorHandler.argtypes = [RtErrorHandler] Now I can call the color function like this: ri.RiColor(RtColor(1,0,0)) But sometimes it would be more convenient to work with other types like tuples, lists or, in this case, a special vector type (that may come from another module but that behaves like a list of 3 floats). But when I try to pass in just a Python tuple or list I get the following errors: ri.RiColor((1,0,0)) -- ctypes.ArgumentError: argument 1: type 'exceptions.TypeError': Don't know how to convert parameter 1 ri.RiColor([1,0,0]) -- ctypes.ArgumentError: argument 1: type 'exceptions.TypeError': expected c_float_Array_3 instance instead of list Similarly with the error handler function. I have to wrap a Python function with the RtErrorHandler type, otherwise ctypes won't accept it: def errHandler(code, severity, message): pass ri.RiErrorHandler(RtErrorHandler(errHandler)) # works ri.RiErrorHandler(errHandler) # produces a TypeError So whenever an input type doesn't match what was specified in argtypes, couldn't ctypes try to convert the value before issuing an error? (it works with floats, so why not with other types as well?) The conversion could just be done by passing the value to the constructor of the required type. In the color example this also means that array types should also accept sequences as input (i.e. anything that supports iteration and has the right number of elements). I think this modification to ctypes would make the wrapped functions more flexible without having to write additional wrapper functions in Python. -- components: Extension Modules messages: 61815 nosy: mattbaas severity: normal status: open title: ctypes feature request: Automatic type conversion of input arguments to C functions type: behavior versions: Python 2.5 __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1962 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com