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

Mark Lawrence report at bugs.python.org
Tue Sep 21 23:58:52 CEST 2010


Mark Lawrence <breamoreboy at 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 <report at bugs.python.org>
To: breamoreboy at 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 <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):
    @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 <report at bugs.python.org>
<http://bugs.python.org/issue1962>
_______________________________________

----------
Added file: http://bugs.python.org/file18958/unnamed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1962>
_______________________________________
-------------- next part --------------
<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>Please ensure tha I'm taken off of the email list as I've been banned from contributing to Python by Raymond Hettinger<br></div><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><br><div style="font-family: arial,helvetica,sans-serif; font-size: 13px;"><font face="Tahoma" size="2"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Amaury Forgeot d'Arc &lt;report at bugs.python.org&gt;<br><b><span style="font-weight: bold;">To:</span></b> breamoreboy at yahoo.co.uk<br><b><span style="font-weight: bold;">Sent:</span></b> Tue, 21 September, 2010 18:56:51<br><b><span style="font-weight: bold;">Subject:</span></b> [issue1962] ctypes feature request: Automatic type conversion of input arguments to C functions<br></font><br><br>Amaury Forgeot d'Arc &lt;<a
 ymailto="mailto:amauryfa at gmail.com" href="mailto:amauryfa at gmail.com">amauryfa at gmail.com</a>&gt; added the comment:<br><br>I don't think this should happen by default.<br>but 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*):<br><br>from ctypes import *<br><br>class AutoStrParam(c_char_p):<br>&nbsp; &nbsp; @classmethod<br>&nbsp; &nbsp; def from_param(cls, value):<br>&nbsp; &nbsp; &nbsp; &nbsp; return str(value)<br><br>strlen = cdll.LoadLibrary('msvcrt').strlen<br>strlen.argtypes = [AutoStrParam]<br><br>print strlen(None)&nbsp; &nbsp;  # "None"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;&nbsp; 4<br>print strlen(type)&nbsp; &nbsp;  # "&lt;type 'type'&gt;" -&gt; 13<br><br>----------<br>nosy: +amaury.forgeotdarc<br><br>_______________________________________<br>Python tracker &lt;<a ymailto="mailto:report at bugs.python.org"
 href="mailto:report at bugs.python.org">report at bugs.python.org</a>&gt;<br>&lt;<a href="http://bugs.python.org/issue1962" target="_blank">http://bugs.python.org/issue1962</a>&gt;<br>_______________________________________<br></div></div>
</div><br>



      </body></html>


More information about the Python-bugs-list mailing list