将Python列表包装为unsigned char*

4 投票
1 回答
2559 浏览
提问于 2025-04-16 22:53

编辑:大家好!!

我现在正在尝试从Python访问C++函数,但在将Python列表作为参数传递给函数时遇到了问题。

这是我想要访问的C++函数定义(用于向PC/SC读卡器发送命令):

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

我的目标是像下面的例子那样从Python调用这个函数。目标是将列表[1,2,3]转换为一个unsigned char *缓冲区:

SendAPDU(0x01020304, 0, 3, [1, 2, 3])

我参考了SWIG文档中的例子“33.9.1 将Python列表转换为char **” http://www.swig.org/Doc2.0/Python.html#Python_nn57,定义了以下typemap来处理unsigned char *,但不幸的是,似乎这个typemap没有被使用。实际上,我只能在忽略最后一个参数或将其设置为None的情况下使用这个函数。

Python代码:

    >>> SendAPDU(0x02000000, 2, 0)  
    [36864, [30, 240]]                  #This call is working
    >>> SendAPDU(0x02000000, 2, 0, None)
    [36864, [30, 240]]                  #Also working

然后,如果我尝试将一个列表作为命令的数据(替换之前例子中的None),我会收到以下错误:

>>> SendAPDU(0x02000000, 2, 4, [1, 2, 3, 4]) # HERE IT'S NOT WORKING
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      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 &)

我认为我们这里遇到了“参数类型错误”的情况,所以我猜测typemap没有被使用,因为我认为(但不确定)函数没有被调用是因为参数格式不匹配。那么主要的问题是,我该如何传递一个列表,并确保它会被函数接受(因此被typemap捕获)呢?

我觉得我可能漏掉了什么,因为我没有找到任何有效的解决方案,而且这个问题应该相对常见。

以下是SWIG .i文件中的typemap代码:

%typemap(in) unsigned char * {
      // Check if is a list 
      unsigned char *ucBuf = NULL;
      if (PyList_Check($input) && PyList_Size($input) > 0) {
        int size = PyList_Size($input);
        int i = 0;
        ucBuf = (unsigned char *) malloc((size+1)*sizeof(unsigned char));
        for (i = 0; i < 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 = &ucBuf[0];
    }

    %typemap(freearg) unsigned char * {
        if($1 != NULL)
            free( $1);
    }

总结一下,如何设置SWIG的typemap来调用C++函数:

SRpdu * SendAPDU(unsigned int uiAPDU, unsigned int ucLE, unsigned int ucLC, unsigned char * pucDataBuf);

像这样从Python调用:

SendAPDU(0x02000000, 2, 4, [1, 2, 3, 4])

任何帮助都欢迎,非常感谢!

附言:抱歉我的英语不好,这不是我的母语。

1 个回答

1

你应该了解一下数组模块

特别是,你需要使用fromlist这个功能。如果你是因为觉得应该用列表而不是字符串来传递数据,其实有一个字符串的函数fromstring可以用。无论哪种方式,你都可以用格式说明符'B'(注意是大写的B)来初始化数组。

下面是一个使用示例:

array ('B', [1, 2, 1, 0, 3, 6, 0, 6])

撰写回答