Note that there are some explanatory texts on larger screens.

plurals
  1. POWrap python list to unsigned char*
    text
    copied!<p>Edit : Hi all !!</p> <p>I'm currently trying to access to C++ functions from Python and I'm facing to a problem when I try to pass Python list as argument to a function. </p> <p>here is the C++ function definition I'm trying to access (used to send command to PC/SC reader) : </p> <pre><code>SRpdu *SendAPDU(unsigned int uiAPDU, //Command unsigned int ucLE, //Data expected for response unsigned int ucLC, //Size of data buffer unsigned char * pucDataBuf = 0); //data buffer </code></pre> <p>My goal is to call this function as following from python like in example below. The goal is to convert the list [1,2,3] to a unsigned char * buffer : </p> <pre><code>SendAPDU(0x01020304, 0, 3, [1, 2, 3]) </code></pre> <p>Insipired from example "33.9.1 Converting Python list to a char **" from SWIG documentation <a href="http://www.swig.org/Doc2.0/Python.html#Python_nn57" rel="nofollow">http://www.swig.org/Doc2.0/Python.html#Python_nn57</a>, I defined following typemap to handle <code>unsigned char *</code> but unfortunately it seems that the typemap is not used. Actually I can use the function only with last argument ignored or set to None.</p> <p>Python code : </p> <pre><code> &gt;&gt;&gt; SendAPDU(0x02000000, 2, 0) [36864, [30, 240]] #This call is working &gt;&gt;&gt; SendAPDU(0x02000000, 2, 0, None) [36864, [30, 240]] #Also working </code></pre> <p>Then if I try to put a list for data of the command (replacing None in previous example), I receive the following error : </p> <pre><code>&gt;&gt;&gt; SendAPDU(0x02000000, 2, 4, [1, 2, 3, 4]) # HERE IT'S NOT WORKING Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "EMReaderEx.py", line 196, in SendAPDU def SendAPDU(self, *args): return _EMReaderEx.EMReaderEx_SendAPDU(self, *args) NotImplementedError: Wrong number or type of arguments for overloaded function 'EMReaderEx_SendAPDU'. Possible C/C++ prototypes are: EMReaderEx::SendAPDU(unsigned int,unsigned int,unsigned int,unsigned char *) EMReaderEx::SendAPDU(unsigned int,unsigned int,unsigned int) EMReaderEx::SendAPDU(SApdu &amp;) </code></pre> <p>I think we are here in case of "Wrong type of argument", so I guess that the typemap is not used because I think, but not sure, that the function is not called because of the parameter format doesn't match. So the main question is how can I pass a list and be sure that will be accepted by the function (and therefore catched by the typemap) ? </p> <p>I think I missed something because I didn't found any working solution for that, and it should relatively used often. </p> <p>Here is the code of the typemap code in SWIG .i file: </p> <pre><code>%typemap(in) unsigned char * { // Check if is a list unsigned char *ucBuf = NULL; if (PyList_Check($input) &amp;&amp; PyList_Size($input) &gt; 0) { int size = PyList_Size($input); int i = 0; ucBuf = (unsigned char *) malloc((size+1)*sizeof(unsigned char)); for (i = 0; i &lt; size; i++) { PyObject *o = PyList_GetItem($input,i); if (PyLong_Check(o)) ucBuf[i] = (unsigned char) PyLong_AsLong(o); else { PyErr_SetString(PyExc_TypeError,"list must contain integers"); free(ucBuf); return NULL; } } } $1 = &amp;ucBuf[0]; } %typemap(freearg) unsigned char * { if($1 != NULL) free( $1); } </code></pre> <p>In Resume, how to do the SWIG typemap to call C++ function : </p> <pre><code>SRpdu * SendAPDU(unsigned int uiAPDU, unsigned int ucLE, unsigned int ucLC, unsigned char * pucDataBuf); </code></pre> <p>Like that from python: </p> <pre><code>SendAPDU(0x02000000, 2, 4, [1, 2, 3, 4]) </code></pre> <p>Any help is welcome Thanks in advance </p> <p>p.s. Sorry if my english is bad, it's not my native language. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload