Python支持C/C++实现的第3方扩展包,在性能要求高的场合,这种特性显得尤其重要。
本文以实例说明定制Python扩展包的基本步骤。
1. 扩展包源码实现
按照Python官网教程Extending Python with C or C++说明的步骤,扩展模块的源文件实现如下:#include <Python.h> // forward declaration void initpyext(void); // self-defined error obj static PyObject * ExtError; int main(int argc, char * argv[]) { // Pass argv[0] to the Python interpreter Py_SetProgramName(argv[0]); // Initialize the Python interpreter. Required. Py_Initialize(); initpyext(); Py_Exit(0); // not reached, warning will be printed when build the extension } static PyObject * py_ext_test_func(PyObject * self, PyObject * args) { const char * command; int sts; if(!PyArg_ParseTuple(args, "s", &command)) { return NULL; } sts = system(command); if(sts < 0) { PyErr_SetString(ExtError, "System command failed"); return NULL; } return PyLong_FromLong(sts); } static PyMethodDef PyextMethods[] = { {"ext_cmd", py_ext_test_func, METH_VARARGS, "Execute a shell command." }, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initpyext(void) { PyObject * m; m = Py_InitModule("pyext", PyextMethods); if(NULL == m) { return; } ExtError = PyErr_NewException("pyext.error", NULL, NULL); Py_INCREF(ExtError); PyModule_AddObject(m, "error", ExtError); }上面的代码展示的是一个典型的针对Python的c扩展源码布局:
2. 扩展文件编译/链接
根据官网文档Building C and C++ Extensions with distutils,可以这样编译扩展并安装至python第三方package目录:$> python setup.py build running build running build_ext building 'pyext' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/slvher/tools/python-2.7.5/include/python2.7 -c pyext.c -o build/temp.linux-x86_64-2.7/pyext.o pyext.c: In function `main': pyext.c:28: warning: control reaches end of non-void function gcc -pthread -shared build/temp.linux-x86_64-2.7/pyext.o -o build/lib.linux-x86_64-2.7/pyext.so注意:这里直接用python调用setup.py是因为在PATH中设置了python的默认环境变量,如果当前系统默认的python解释器不是我们想要用来编译扩展的解释器时,需要用python的绝对路径调用。
$> python setup.py install running install running build running build_ext running install_lib copying build/lib.linux-x86_64-2.7/pyext.so -> /home/slvher/tools/python-2.7.5/lib/python2.7/site-packages running install_egg_info Writing /home/slvher/tools/python-2.7.5/lib/python2.7/site-packages/TestPyExtPackage-1.0-py2.7.egg-info4. 验证是否可用
>>> import pyext >>> pyext.ext_cmd("echo hello world") hello world 0L >>>【参考资料】
2. Building C and C++ Extensions with distutils
====================== EOF ==================
【Python笔记】如何用C语言实现Python第三方扩展包
原文地址:http://blog.csdn.net/slvher/article/details/39233765