2013-08-04 42 views
1

指的http://mail.python.org/pipermail/python-dev/2009-June/090210.htmlhttp://dan.iel.fm/posts/python-c-extensions/Python的C API:解析字符串和整數ARGS用C

,這裏是其他地方我搜索關於我的問題: http://article.gmane.org/gmane.comp.python.general/424736 http://joyrex.spc.uchicago.edu/bookshelves/python/cookbook/pythoncook-CHP-16-SECT-3.html http://docs.python.org/2/c-api/sequence.html#PySequence_Check Python extension module with variable number of arguments

我對Python/C API沒有經驗。

我有以下代碼:

sm_int_list = (1,20,3) 
c_int_array = (ctypes.c_int * len(sm_int_list))(*sm_int_list) 
sm_str_tuple = ('some','text', 'here') 

在C擴展方面,我已經做了這樣的事情:

static PyObject* stuff_here(PyObject *self, PyObject *args) 
{ 
    char* input; 
    int *i1, *i2; 
    char *s1, *s2; 
    // args = (('some','text', 'here'), [1,20,3], ('some','text', 'here'), [1,20,3]) 
    **PyArg_ParseTuple(args, "(s#:):#(i:)#(s#:):#(i:)#", &s1, &i1, &s2, &i2)**; 
/*stuff*/ 
} 

使得: stuff.here(( '有些', 'text','here'),[1,20,3],('some','text','here'),[1,20,3])

返回數據的格式與經過一些計算後發現。 我想知道PyArg_ParseTuple表達,是它解析

  1. 改變串
  2. 整數

UPDATE NEW

的陣列的陣列的正確方式

這是正確的方法嗎?:

static PyObject* stuff_here(PyObject *self, PyObject *args) 
    unsigned int tint[], cint[]; 
    ttotal=0, ctotal=0; 
    char *tstr, *cstr; 
    int *t_counts, *c_counts; 
    Py_ssize_t size; 
    PyObject *t_str1, *t_int1, *c_str2, *c_int2; //the C var that takes in the py variable value 
    PyObject *tseq, cseq; 
    int t_seqlen=0, c_seqlen=0; 

if (!PyArg_ParseTuple(args, "OOiOOi", &t_str1, &t_int1, &ttotal, &c_str2, &c_int2, &ctotal)) 
    { 
     return NULL; 
    } 

if (!PySequence_Check(tag_str1) && !PySequence_Check(cat_str2)) return NULL; 

    else: 
    { 
     //All things t 
     tseq = PySequence_Fast(t_str1, "iterable"); 
     t_seqlen = PySequence_Fast_GET_SIZE(tseq); 
     t_counts = PySequence_Fast(t_int1); 

     //All things c 
     cseq = PySequence_Fast(c_str2); 
     c_seqlen = PySequence_Fast_GET_SIZE(cseq); 
     c_counts = PySequence_Fast(c_int2); 

     //Make c arrays of all things tag and cat 
     for (i=0; i<t_seqlen; i++) 
     { 
      tstr[i] = PySequence_Fast_GET_ITEM(tseq, i); 
      tcounts[i] = PySequence_Fast_GET_ITEM(t_counts, i); 
     } 

     for (i=0; i<c_seqlen; i++) 
     { 
      cstr[i] = PySequence_Fast_GET_ITEM(cseq, i); 
      ccounts[i] = PySequence_Fast_GET_ITEM(c_counts, i); 
     } 


    } 

OR

PyArg_ParseTuple(args, "(s:)(i:)(s:)(i:)", &s1, &i1, &s2, &i2) 

然後再返回時,

Py_BuildValue("sisi", arr_str1,arr_int1,arr_str2,arr_int2)

事實上,如果有人可以詳細闡明各種PyArg_ParseTuple函數,將是非常有益的。 Python C API,正如我在文檔中找到的,並不完全是要做的事情的教程。

回答

1

你可以使用PyArg_ParseTuple來解析一個具有固定結構的真實元組。特別是子元件中的項目數量不能改變。

正如2.7.5文檔所述,您的格式"(s#:):#(i:)#(s#:):#(i:)#"是錯誤的,因爲:不能出現在嵌套圓括號中。格式"(sss)(iii)(sss)(iii)"以及總共12個指針參數應該與您的參數匹配。同樣,對於Py_BuildValue,您可以使用相同的格式字符串(在1元組內創建4個元組),或者如果類型很重要(這會使整數位於列表中而不是元組中),則可以使用"(sss)[iii](sss)[iii]"

+0

明白了。但它不是12.這個數字可能會有所不同,那麼怎麼樣呢? – user2290820

+0

PyArg_ParseTuple(args,「OOOO」,&s1,&i1,&s2,&i2)以及名稱和我作爲PyObj? – user2290820