Implementation of Python's extension module in C language
Example 1:
1 example.c
int add (int a,int b)
{
return a+b;
}
int sub (int a,int b)
{
return a -b;
}
int mul (int a,int b)
{
return a*b;
}
int div1 (int a,int b)
{
if(0 == b) { return b; } return a/b;
}
2 wrap.c
The include//python.h already contains the usual header files
Pyobject Wrap_add (pyobject Self, pyobject* args)
{
int n1,n2, result;
if (! Pyarg_parsetuple (args, "i|i", &n1,&n2))
return NULL;
result = Add (N1,N2);
Return Py_buildvalue ("I", result);
}
Pyobject wrap_sub (pyobject Self, pyobject* args)
{
int n1,n2, result;
if (! Pyarg_parsetuple (args, "i|i", &n1,&n2))
return NULL;
result = Sub (N1,N2);
Return Py_buildvalue ("I", result);
}
Pyobject Wrap_mul (pyobject Self, pyobject* args)
{
int n1,n2, result;
if (! Pyarg_parsetuple (args, "i|i", &n1,&n2))
return NULL;
result = Mul (N1,N2);
Return Py_buildvalue ("I", result);
}
Pyobject wrap_div1 (pyobject Self, pyobject* args)
{
int n1,n2, result;
if (! Pyarg_parsetuple (args, "i|i", &n1,&n2))
return NULL;
result = Div1 (N1,N2);
Return Py_buildvalue ("I", result);
}
Static Pymethoddef examplemethods[] =
{
{"Add", Wrap_add, Meth_varargs, "Caculate 1!"},
{"Sub", Wrap_sub, Meth_varargs, "Caculate 2!"},
{"Mul", Wrap_mul, Meth_varargs, "Caculate 3!"},
{"Div1", Wrap_div1, Meth_varargs, "Caculate 4!"},
{NULL, null,0,null}
};
void Initexample ()
{
pyobject* m;
m = Py_initmodule ("Example", examplemethods);
}
3 compiling
Gcc-fpic-c-i/usr/include/python2.6/-i/usr/lib/python2.6/config example.c wrap.c
Gcc-shared-o wrap.so WRAP.O
4 function Demo
5 Main references:
5. 1 Python's C language extension
http://www.ibm.com/developerworks/cn/linux/l-pythc/
5. 2 on the integration of Python program and C program
http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/index.html?ca=drs-cn-0506
Example 2: (Supports functions defined in callback Python)
1 mytest.c
Include
Static Pyobject my_callback = NULL;
Static Pyobject My_strlen (pyobject self , pyobject args)
{
Char *string;
int Len;
if (! Pyarg_parsetuple (args, "s", &string))
return NULL;
Len = strlen (string);
Return Py_buildvalue ("I", Len);
}
Static Pyobject my_strcat (pyobject Self, pyobject *args)
{
char* string1;char* string2;char* newstring;if (!PyArg_ParseTuple(args, "s|s", &string1, &string2)) return NULL;newstring = strcat(string1, string2);return Py_BuildValue("s", newstring);
}
Static Pyobject my_set_callback (pyobject Self, pyobject *args)
{
PyObject *result = NULL;PyObject *temp;if (PyArg_ParseTuple(args, "O", &temp)){ if (!PyCallable_Check(temp)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } Py_XINCREF(temp); Py_XDECREF(my_callback); my_callback = temp; Py_INCREF(Py_None); result = Py_None;
}
return result;
}
Static Pyobject my_test_callback (pyobject Self, pyobject *args)
{
PyObject * arglist;PyObject * result = NULL;result = PyEval_CallObject(my_callback, args);if (result == NULL){ return NULL;}Py_DECREF(result);Py_INCREF(Py_None);return Py_None;
}
Static Pymethoddef mytestmethods[] =
{
{"mystrlen", my_strlen, METH_VARARGS, "We test strlen of C"}, {"mystrcat", my_strcat, METH_VARARGS, "We test strcat of C"}, {"mysetcallback", my_set_callback, METH_VARARGS, "we set a call back function so that call it in C"}, {"mytestcallback", my_test_callback, METH_VARARGS, "we use this function to test call back function"}, {NULL, NULL, 0, NULL}
};
void Initmytest ()
{
(void) Py_InitModule("mytest", mytestMethods);
}
2 compiling
Gcc-fpic-c-i/usr/include/python2.6/-i/usr/lib/python2.6/config mytest.c
Gcc-shared-o mytest.so MYTEST.O
3 Function Demo
4 main references
4.1. Calling Python Functions from C
4.2 Extending Python on Windows
http://blog.chinaunix.net/space.php?uid=46552&do=blog&id=2116527
4.3 [essence] extending Python on Windows (2) – Calling Python functions from C
Http://www.linuxforum.net/forum/gshowthreaded.php? Cat=&board=python&number=485550&page=3&view=collapsed&sb=5&o=all&vc=1
Implementation of Python's extension module in C language