一、我们都知道python这门语言是有c语言编写而成的,那么今天就用c语言编写一个python模块,python在底层运算中会对每个运算做类型检查, 这就影响了运行的性能,而利用扩展, 可以避免这样的情况, 获得优越的执行性能,利用Python提供的C API,如宏,类型,函数等来编写扩展。
二、环境准备

Centos7.4
Python 2.7.5
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 

说明:需要保证环境中已经安装了python-dev包
三、以下已判断一个数是否为质数为例,py.c:

#include<stdio.h>
#include<python2.7/Python.h> //有的是#include<Python.h>

//判断是否是质数
static PyObject *pr_isprime(PyObject *self, PyObject *args) {
    int n, num;
    //解析参数
    if (!PyArg_ParseTuple(args, "i", &num)) {
        return NULL;
    }

    if (num < 1) {
        return Py_BuildValue("i", 0); //C类型转成python对象
    }

    n = num - 1;
    while (n > 1) {
        if (num % n == 0)
            return Py_BuildValue("i", 0);
        n--;
    }
    return Py_BuildValue("i", 1);
}

static PyMethodDef PrMethods[] = {
    //方法名,导出函数,参数传递方式,方法描述。
    {"isPrime", pr_isprime, METH_VARARGS, "check if an input number is prime or not."},
    {NULL, NULL, 0, NULL}
};

void initpr(void) {
    (void) Py_InitModule("pr", PrMethods);
}

以上代码包含了3个部分:

  • 导出函数:C模块对外暴露的接口函数为pr_isprime,带有self和args两个参数,args包含了python解释器要传给c函数的所有参数,通常使用PyArg_ParseTuple()来获得这些参数值。
  • 初始化函数:一遍python解释器能够对模块进行正确的初始化,初始化要以init开头,如initp。
  • 方法列表:提供给外部的python程序使用函数名称映射表PrMethods,它是一个PyMethodDef结构体,成员依次是方法名,导出函数,参数传递方式,方法描述。
    PyMethodDef原型:
struct PyMethodDef {
    char* ml_name;        #方法名
    PyCFunction ml_meth;  #导出函数
    int ml_flags;         #参数传递方式
    char* ml_doc;         #方法描述
}

四、setup.py脚本

#!/usr/bin/env python
# coding=utf-8

from distutils.core import setup, Extension

module = Extension('pr', sources = ['py.c'])

setup(name = 'Pr test', version = '1.0', ext_modules = [module])

使用python setup.py build进行编译,系统会在当前目录下生产一个build目录,里面包含pr.so和pr.o文件。


五、安装模块
下面三种方法任一种都可以:

  • 将生产的pr.so复制到python的site_packages目录下(我的是/usr/local/lib/python2.7/dist-packages,放到site_packages反而没作用)。
  • 或者将pr.so路径添加到sys.path中。
  • 或者用python setup.py install让python完成安装过程。
  • 或者gcc -I /usr/include/python2.7/ -fpic --shared -o pr.so pr.c(编译和安装一起)
    这里我用python setup.py install 来进行安装

    我们可以看到pip list中出现了Pr-test,也就是我们刚刚安装的模块


    要想更加深入的学习还是要看官方文档
    参考文章

更多推荐

利用c/c++编写python模块