2013-08-04 43 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