指的http://mail.python.org/pipermail/python-dev/2009-June/090210.html 和http://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表达,是它解析
- 改变串
- 整数
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,正如我在文档中找到的,并不完全是要做的事情的教程。
明白了。但它不是12.这个数字可能会有所不同,那么怎么样呢? – user2290820
PyArg_ParseTuple(args,“OOOO”,&s1,&i1,&s2,&i2)以及名称和我作为PyObj? – user2290820