python C PyObject


#include"Python.h"
//three ways :
/*
PyObject *MyFunction(PyObject *self, PyObject *args);
PyObject *MyFunctionWithKeywords(PyObject *self,
                                 PyObject *args,
                                 PyObject *kw);
PyObject *MyFunctionWithNoArgs(PyObject *self);
*/

//the return Py_RETURN_NONE
static PyObject* foo_bar(PyObject *self, PyObect *args)
{
    //Do something interesting here.

}
/*
struct PyMethodDef {
    char        *ml_name;//function name in python
    PyCfunction   ml_meth;//the address of function
    int           ml_flags;// METH_VARARGS METH_KEYWORDS METH_NOARGS
    char        *ml_doc;
};
*/
static PyMethodDef foo_methods[] = {
    {"bar", (PyCFunction)foo_bar, METH_NOARGS, "My first function."},
    {NULL, NULL, 0, NULL}
};

//init function for the python  module
PyMODINIT_FUNC initfoo(){//init+moduleName Python解釋器規定所有的初始化函數的函數名都必須以init開頭,並加上模塊的名字
    Py_InitModule3("foo", foo_methods, "My first extension module.");
    //Py_InitModule("foo", foo_methods, "My first extension module");
}

//how to build te module
/* //in unix or linux:
gcc -shared -I/usr/include/python3.1 foo.c -o foo.so
   // in windows:
cl /LD /IC:\Python31\include foo.c C:\Python31\libs\python31.lib

=============================================

example2:

#include"Python.h"
//three ways :
/*
PyObject *MyFunction(PyObject *self, PyObject *args);
PyObject *MyFunctionWithKeywords(PyObject *self,
                                 PyObject *args,
                                 PyObject *kw);
PyObject *MyFunctionWithNoArgs(PyObject *self);
*/

//the return Py_RETURN_NONE
static PyObject* foo_bar(PyObject *self, PyObect *args)
{
    //Do something interesting here0.
    Py_RETURN_NONE;

}
static PyObject * foo_baz(PyObject *self, PyObject *args)
{
    int i;
    dobule d;
    char *s;
    if(!PyArg_ParseTuple(args, "ids", &i, &d, &s))
    {
        return NULL;
    }
    //Do something interesting here.
    Py_RETURN_NONE;
}
static PyObject *foo_baz2(PyObject *self, PyObject *args)
{
    int i;
    double d;
    char *s;
    int i2 = 4;
    dobule d2 = 5.0;
    char *s2 = "six";
    if(!PyArg_ParseTuple(args, "ids|ids", &i, &d, &s, &i2, &d2, &s2))
    {
        return NULL;
    }
    //Do something interesting here.
    Py_RETURN_NONE;
}
static PyObject *foo_quux(PyObject *self, PyObject *args, PyObject* kw)
{
    char *kwlist[] = {"i", "d", "s", NULL};
    int i;
    double d = 2.0;
    char *s = "three";
    if(!PyArg_ParseTupleAndKeyWords(args, kw, "i|ds", kwlist, &i, &d, &s))
    {
        return NULL;
    }
    //Do something interesting here.
    Py_RETURN_NONE;
}
static PyObject * foo_add(PyObject *self, PyObect *args)
{
    int a;
    int b;
    if(!PyArg_ParseTuple(args, "ii", &a, &b))
    {
        return NULL;
    }
    return Py_BuildValue("i", a+b);
    /*
    *python function is:
    def add(a,b):
        return a + b
    */
}
static PyObject *foo_add_and_subtract(PyObject *self, PyObject *args)
{
    int a;
    int b;
    if(!PyArg_ParseTuple(args, "ii", &a, &b))
    {
        return NULL;
    }
    return Py_BuildValue("(ii)", a+b, a-b);
    /*
    *python function is:
    def add_and_subtrace(a, b):
        return (a+b, a-b)
    */
}
/*
struct PyMethodDef {
    char        *ml_name;//function name in python
    PyCfunction   ml_meth;//the address the function
    int           ml_flags;// METH_VARARGS METH_KEYWORDS METH_NOARGS
    char        *ml_doc;
};
*/
static PyMethodDef foo_methods[] = {
    {"bar", (PyCFunction)foo_bar, METH_NOARGS, "My first function."},
    {"baz", (PyCFunction)foo_baz, METH_VARAGRS, NULL},
    {"baz2", (PyCFunction)foo_baz2, METH_VARARGS, NULL},
    {"quux", (PyCFunction)foo_quux, METH_VARARGS|METH_KEYWORDS, NULL},
    {"add", (PyCFunction)foo_add, METH_VARARGS, NULL},
    {"add_and_subtract", (PyCFunction)foo_add_and_subtract, METH_VARAGRS, NULL},
    {NULL, NULL, 0, NULL}
};

//init function for the python  module
PyMODINIT_FUNC initfoo(){//init+moduleName Python解釋器規定所有的初始化函數的函數名都必須以init開頭,並加上模塊的名字
    Py_InitModule3("foo", foo_methods, "My first extension module.");
    //Py_InitModule("foo", foo_methods, "My first extension module");
}

//how to build te module
/* //in unix or linux:
gcc -shared -I/usr/include/python3.1 foo.c -o foo.so
   // in windows:
cl /LD /IC:\Python31\include foo.c C:\Python31\libs\python31.lib

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM