用C语言实现python的扩展模块

示例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;
}

wrap.c

#include <Python.h> //python.h中已经包含了常用的头文件
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);
}

2 编译

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

3  主要参考:
1    Python的C语言扩展
http://www.ibm.com/developerworks/cn/linux/l-pythc/
2  浅谈 Python 程序和 C 程序的整合
http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/index.html?ca=drs-cn-0506

示例2

##示例2:(支持回调python中定义的函数)

1  mytest.c

#include <Python.h>  
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  编译

gcc -fpic -c -I /usr/include/python2.6/ -I /usr/lib/python2.6/config   mytest.c
gcc -shared -o mytest.so  mytest.o

3 功能演示截图

 

4 主要参考

4.1 . Calling Python Functions from C
4.2 在windows上扩展python
4.3 [精华] 在windows上扩展python(2)--从c中调用python函数

转载于:https://my.oschina.net/hnxymjj/blog/358776

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值