簡體   English   中英

將python列表包裝到unsigned char *

[英]Wrap python list to unsigned char*

編輯:大家好!

我正在嘗試從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文檔http://www.swig.org/Doc2.0/Python.html#Python_nn57中的示例“33.9.1將Python列表轉換為char **”中,我定義了以下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,因為我認為,但不確定,因為參數格式不匹配而未調用該函數。 所以主要的問題是我如何傳遞一個列表,並確保該函數可以接受(並因此被類型圖捕獲)?

我想我錯過了一些東西,因為我沒有找到任何有效的解決方案,它應該經常使用。

這是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);
    }

在Resume中,如何使用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])

歡迎任何幫助提前致謝

ps對不起,如果我的英語不好,那不是我的母語。

你應該研究一下模塊數組。

特別是,你正在尋找調用fromlist 如果您正在使用列表而不是字符串而只是因為您認為這是您應該做的事情,那么可以使用字符串函數fromstring 無論哪種方式,您只需使用格式說明符'B'初始化數組(注意資本)。

用法示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM