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

用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;

}

2 wrap.c

#include //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);

}

3 编译

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 功能演示截图

示例2:(支持回调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 编译

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

gcc -shared -o mytest.so mytest.o

3 功能演示截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言代码转换为Python语言是不可行的,因为两种语言的语法和特性都有很大的差异。但是,可以使用C语言扩展Python的功能。下面是一个简单的示例: 在C语言中编写一个函数,用于计算两个整数的和: ```c int add(int a, int b) { return a + b; } ``` 然后,使用Python的C扩展接口将该函数添加到Python中: ```c #include <Python.h> int add(int a, int b) { return a + b; } static PyObject* add_wrapper(PyObject* self, PyObject* args) { int a, b; if (!PyArg_ParseTuple(args, "ii", &a, &b)) return NULL; return Py_BuildValue("i", add(a, b)); } static PyMethodDef module_methods[] = { {"add", add_wrapper, METH_VARARGS, "Add two integers."}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "mymodule", NULL, -1, module_methods }; PyMODINIT_FUNC PyInit_mymodule(void) { return PyModule_Create(&moduledef); } ``` 这个例子中,我们定义了一个名为“add”的函数,在Python中以模块的形式使用。我们使用PyArg_ParseTuple函数将传递给Python函数的参数解析为C变量,并使用Py_BuildValue函数将C变量转换为Python对象。 最后,我们使用PyModule_Create函数创建一个名为“mymodule”的模块,该模块包含我们刚刚定义的函数。可以将该模块编译成共享库,并在Python中使用它。 使用方法如下: ```python import mymodule print(mymodule.add(1, 2)) # 输出3 ``` 这就是用C语言扩展Python的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值