天天看點

用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> //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)

  result = sub(n1,n2);

pyobject* wrap_mul(pyobject* self, pyobject* args)

  result = mul(n1,n2);

pyobject* wrap_div1(pyobject* self, pyobject* args)

  result = div1(n1,n2);

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 功能示範截圖

5  主要參考:

5.1    python的c語言擴充

http://www.ibm.com/developerworks/cn/linux/l-pythc/

5.2  淺談 python 程式和 c 程式的整合

http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/index.html?ca=drs-cn-0506

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

http://blog.chinaunix.net/space.php?uid=46552&do=blog&id=2116527

4.3 [精華] 在windows上擴充python(2)--從c中調用python函數

http://www.linuxforum.net/forum/gshowthreaded.php?cat=&board=python&number=485550&page=3&view=collapsed&sb=5&o=all&vc=1