指的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