
static PyObject * foo_bar(PyObject * self, PyObject * args) {
Py_RETURN_NONE;
}
static PyObject * foo_bar2(PyObject * self, PyObject * args) {
int iNum;
double fNum;
char * str;
if ( ! PyArg_ParseTuple(args, " ids " , & iNum, & fNum, & str)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyMethodDef foo_methods[] = {
{ " bar " ,(PyCFunction)foo_bar,METH_NOARGS,NULL},
{ " bar2 " , (PyCFunction)foo_bar2,METH_VARARGS,NULL},
{NULL,NULL, 0 ,NULL}
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3( " foo " , foo_methods, " My first extension module. " );
}
你还可以指定可选的参数,只需要通过在格式字符串中包含一个"|"字符即可,如下所示:

int iNum;
double fNum;
char * str;
int iNum2 = 4 ;
double fNum2 = 5.0 ;
char * str2 = " hello " ;
if ( ! PyArg_ParseTuple(args, " ids|ids " , & iNum, & fNum, & str, & iNum2, & fNum2, & str2)) {
return NULL;
}
Py_RETURN_NONE;
}
你在调用此函数时,前面三个参数是必须要传递的,而后面的则是可选的。
另一种情况是当你的函数接受关键字参数,那么m_flags可设置为METH_VARARGS|METH_KEYWORDS,相应的使用PyArg_ParseTupleAndKeywords来进行参数解析。
函数 PyArg_ParseTupleAndKeywords() 声明如下:
参数arg和format定义同 PyArg_ParseTuple() 。参数 kwdict 是关键字字典,用于接受运行时传来的关键字参数。参数 kwlist 是一个NULL结尾的字符串,定义了可以接受的参数名,并从左到右与format中各个变量对应。如果执行成功 PyArg_ParseTupleAndKeywords() 会返回true,否则返回false并抛出异常。
注:嵌套的tuple在使用关键字参数时无法生效,不在kwlist中的关键字参数会导致 TypeError 异常
代码
static PyObject * foo_bar3(PyObject * self, PyObject * args, PyObject * kw) {
static char * kwlist[] = { " i " , " d " , " s " ,NULL};
int iNum = 0 ;
double fNum = 2.0f ;
char * str = " thing " ;
if ( ! PyArg_ParseTupleAndKeywords(args,kw, " i|ds " ,kwlist, & iNum, & fNum, & str)) {
printf( " ERROR " );
return NULL;
}
printf( " num is: %d,%f,%s/n " ,iNum,fNum,str);
Py_RETURN_NONE;
}
static PyMethodDef foo_methods[] = {
{ " bar3 " , (PyCFunction)foo_bar3, METH_VARARGS | METH_KEYWORDS, NULL},
{NULL,NULL, 0 ,NULL}
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3( " foo " , foo_methods, " My first extension module. " );
}
相应的在函数表里记录如下:
这样你在python代码中调用时可以传递关键字参数,其中只有i表示的整数是必需的,因此下述调用都是合法的:
foo.bar3( 1 )
foo.bar3( 1 ,d = 2.0 )
foo.bar33(i = 1 ,d = 2.0 )
而如果你传递了其他关键参数,则会报TypeError,比如foo.bar3(i=1,dd=3.0,s="fda")
下面来看第二个问题:上面说的PyArg_ParseTuple和PyArg_ParseTupleAndKeywords这两个函数是将传递进C模块的Python对象转变为C里的数据类型,那么相反的情况如何呢?即如何从C模块返回值到Python程序中。要完成这件事,我们所需要的函数是Py_BuildValue,示例如下:

static PyObject * foo_add_sub(PyObject * self, PyObject * args) {
int num1,num2;
if ( ! PyArg_ParseTuple(args, " ii " , & num1, & num2)) {
return NULL;
}
return Py_BuildValue( " ii " , num1 + num2, num1 - num2);
}
static PyMethodDef foo_methods[] = {
{ " add_sub " , (PyCFunction)foo_add_sub, METH_VARARGS, NULL},
{NULL,NULL, 0 ,NULL}
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3( " foo " , foo_methods, " My first extension module. " );
}
这样在Python代码中调用如下:
(sum,sub) = foo.add_sub( 9 , 3 )
好了,现在从Python代码传递参数进C模块,以及C模块返回值到Python代码都已经清楚了,下一篇我们将利用这些技术来完成一个实际的C扩展模块