写python的扩展模块(C)

把C语言写的函数作为python的扩展模块。为python创建扩展需要三个步骤:
1. 创建程序代码(C)
2. 写包装代码
3. 编译(写setup.py进行build)

下面先给出一个最简单的例子的: 计算一个数的二分之一,仅包含两个文件half.csetup.py。先给出这两个例子的全部代码,再详细讲解。这个例子参考了 c-info.ufunc-tutorial

half.c

#include <Python.h>
#include <math.h>

double half(double x)
{
    return x/2.;
}

static PyObject* chun_half(PyObject *self, PyObject *args)
{
    double x;

    // parses the Python argument into a double
    if(!PyArg_ParseTuple(args, "d", &x)) {
        return NULL;
    }

    // call function
    double y=half(x);

    /*This builds the answer back into a python object */
    return Py_BuildValue("d", y);
}


static PyMethodDef chunMethods[] = {
    {"half",chun_half,METH_VARARGS, "compute half of a number"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "chun",
    NULL,
    -1,
    chunMethods,
    NULL,NULL,NULL,NULL
};

PyMODINIT_FUNC PyInit_chun(void)
{
    PyObject *m;
    m = PyModule_Create(&moduledef);
    if (!m) {
        return NULL;
    }
    return m;
}

setup.py

'''
    setup.py file for half.c

    $python setup.py install

'''

from distutils.core import setup, Extension

module1 = Extension('chun', sources=['half.c'])

setup(  name = 'chun',
        version='1.0',
        description='This is chun package',
        ext_modules = [module1])

执行wang

解析参数

必要参数和缺省参数,“必要参数|缺省参数”

    const char *file;
    const char *mode = "r";
    int bufsize = 0;
    if(!PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize))
    {
            return NULL;
    }
    /* A string, and optionally another string and an integer */
    /* Possible Python calls:
       f('spam')
       f('spam', 'w')
       f('spam', 'wb', 100000) */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值