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:Python31include foo.c C:Python31libspython31.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:Python31include foo.c C:Python31libspython31.lib
原文地址:https://www.cnblogs.com/zhangdewang/p/8685594.html