4

编辑:大家好!!

我目前正在尝试从 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 * buffer :

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

受 SWIG 文档http://www.swig.org/Doc2.0/Python.html#Python_nn57中的示例“33.9.1 Converting Python list to a char **”的启发,我定义了以下类型映射来处理unsigned char *,但不幸的是,似乎不使用类型映射。实际上,我只能在忽略最后一个参数或设置为 None 的情况下使用该函数。

蟒蛇代码:

    >>> 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 &)

我认为我们在这里是为了防止“参数类型错误”,所以我猜想没有使用类型映射,因为我认为但不确定,由于参数格式不匹配而没有调用函数。所以主要问题是我如何传递一个列表并确保它会被函数接受(因此被类型映射捕获)?

我想我错过了一些东西,因为我没有找到任何可行的解决方案,而且它应该相对经常使用。

这是 SWIG .i 文件中类型映射代码的代码:

%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 类型映射来调用 C++ 函数:

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

就像来自python的那样:

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

欢迎任何帮助提前谢谢

ps 对不起,如果我的英语不好,那不是我的母语。

4

1 回答 1

1

您应该查看模块数组。

特别是,您希望致电fromlist. 如果您使用列表而不是字符串来传递只是因为您认为这是您应该做的,那么有一个可用的字符串函数,fromstring. 无论哪种方式,您都可以简单地使用格式说明符初始化数组'B'(注意大写)。

示例用法:

array ('B', [1, 2, 1, 0, 3, 6, 0, 6])
于 2011-08-08T14:29:48.697 回答