扩展python 用C语言编写python扩展代码

来源:互联网 发布:樱井知香迅雷种子全集 编辑:程序博客网 时间:2024/05/21 06:46

环境:centos7

一、首先简单的说明下什么情况下需要扩展python

1.需要python没有的额外功能时。

2.改善瓶颈性能。中所周知,由于解释型语言的代码在运行时即时转换,因此执行起来比编译语言慢。

3.隐藏专有代码。

二、编写python扩展主要涉及的三个步骤

1.创建应用代码(一定要保证应用代码的准确性)

2.根据样板编写封装代码

  样板代码主要含有四个部分:

  (1)包含python头文件(一定要安装python需要的库,centos:yum -y install python-devel)。

  (2)为每一个模块函数添加形如PyObject*Module_func()的封装函数。

  (3)为每一个模块函数添加一个PyMethoDef Module Methods[]数组/表。

  (4)添加模块初始化函数void initModule()。

三、python和C/C++之间转换数据

从Python到C int Pyarg_ParseTuple()将位于元组中的一些列参数从python转化为C

从C到Python PyObject * Py_BuildValue()将C数值转化为Python返回对象,要么时单个对象,要么时一个含有多个对象的元组

四、代码示例

扩展写的C文件封装代码:
#include <stdio.h>#include <stdlib.h>#include <string.h>int fac(int n){    if (n<2) return 1;    return n*fac(n-1);}char *reverse(char *s){    register char t,*p = s,*q = (s + (strlen(s)-1));    while(s && (p<q))    {           t = *p;         *p++ = *q;        *q-- = t;    }       return s;}int test(){    char s[100];    printf("4!=%d\n",fac(4));    strcpy(s,"abcdef");    printf("reversing 'abcdef',we get '%s'\n",reverse(s));    return 0;}#include "Python.h"static PyObject *Extest_fac(PyObject *self,PyObject *args){    int num;    if(!PyArg_ParseTuple(args,"i",&num))        return NULL;    return (PyObject*)Py_BuildValue("i",fac(num));}static PyObject *Extest_doopel(PyObject *self,PyObject *args){    char *orig_str;    char *dupe_str;    PyObject * retval;    if(!PyArg_ParseTuple(args,"s",&orig_str))        return NULL;    retval = (PyObject *)Py_BuildValue("ss",orig_str,dupe_str=reverse(strdup(orig_str)));    free(dupe_str);    return retval;}static PyObject *Extest_test(PyObject *self,PyObject *args){    test();    return (PyObject*)Py_BuildValue("");}static PyMethodDefExtestMethods[] ={    {"fac",Extest_fac,METH_VARARGS},    {"doopel",Extest_doopel,METH_VARARGS},    {"test",Extest_test,METH_VARARGS},    {NULL,NULL},};void initExtest(){    Py_InitModule("Extest",ExtestMethods);}
构建脚本(setup.py):
#!/usr/bin/ebv pythonfrom distutils.core import setup,ExtensionMOD = 'Extest'setup(name=MOD,ext_modules=[Extension(MOD,sources=['Extest2.c'])])

五、现在就可以编译安装python模块那样使用自己的扩展了







0 0
原创粉丝点击