用C扩展Python2

时间:2024-04-01 11:37:08

参考

python扩展实现方法--python与c混和编程

编写Python扩展(Extending Python with C or C++)

https://docs.python.org/2.7/extending/embedding.html

环境

主机: ubuntu14.04 64bit

开发板: qemu + aarch64 (参考: http://www.cnblogs.com/pengdonglin137/p/6442583.html

工具链: aarch64-linux-gnu-gcc  (gcc version 4.9.1 20140529)

Python版本: Python-2.7.13

概述

上面参考列表中的文章已经说的很全了,这里仅作一些补充。分为三个:

1、交叉编译扩展模块到aarch64上面

2、编译扩展模块到Qemu模拟的x86_64上面

3、编译扩展模块到PC(x86_64)上面

采用的测试模块是Extend_wrap.c,这个在python扩展实现方法--python与c混和编程中有说明,源码如下:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Python.h> #define BUFSIZE 10 int fac(int n) {
if (n < )
return ;
return n * fac(n - );
} static PyObject * Extest_fac(PyObject *self, PyObject *args) {
int res;//计算结果值
int num;//参数
PyObject* retval;//返回值 //i表示需要传递进来的参数类型为整型,如果是,就赋值给num,如果不是,返回NULL;
res = PyArg_ParseTuple(args, "i", &num);
if (!res) {
//包装函数返回NULL,就会在Python调用中产生一个TypeError的异常
return NULL;
}
res = fac(num);
//需要把c中计算的结果转成python对象,i代表整数对象类型。
retval = (PyObject *)Py_BuildValue("i", res);
return retval;
} char *reverse(char *s) {
register char t;
char *p = s;
char *q = (s + (strlen(s) - ));
while (p < q) {
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
} static PyObject *
Extest_reverse(PyObject *self, PyObject *args) {
char *orignal;
if (!(PyArg_ParseTuple(args, "s", &orignal))) {
return NULL;
}
return (PyObject *)Py_BuildValue("s", reverse(orignal));
} static PyObject *
Extest_doppel(PyObject *self, PyObject *args) {
char *orignal;
char *reversed;
PyObject * retval;
if (!(PyArg_ParseTuple(args, "s", &orignal))) {
return NULL;
}
retval = (PyObject *)Py_BuildValue("ss", orignal, reversed=reverse(strdup(orignal)));
free(reversed);
return retval;
} static PyMethodDef
ExtestMethods[] = {
{"fac", Extest_fac, METH_VARARGS},
{"doppel", Extest_doppel, METH_VARARGS},
{"reverse", Extest_reverse, METH_VARARGS},
{NULL, NULL},
}; void initExtest() {
Py_InitModule("Extest", ExtestMethods);
} int main() {
char s[BUFSIZE];
printf("4! == %d\n", fac());
printf("8! == %d\n", fac());
printf("12! == %d\n", fac());
strcpy(s, "abcdef");
printf("reversing 'abcdef', we get '%s'\n", reverse(s));
strcpy(s, "madam");
printf("reversing 'madam', we get '%s'\n", reverse(s));
return ;
}

关于这段代码的解释,请参考python扩展实现方法--python与c混和编程

正文

1、交叉编译扩展模块到aarch64上面

这里介绍两种方法:

第一种: 将这个文件拷贝到Python2.7.3的Modules目录下面编译

拷贝:

    cp Extest_wrap.c ../../Python-2.7./Modules/

修改Python-2.7.13/setup.py,添加模块:

 diff --git a/setup.py b/setup.py
index 81355c7..5083c3d
--- a/setup.py
+++ b/setup.py
@@ -, +, @@ class PyBuildExt(build_ext):
'-framework', 'Carbon']) ) + exts.append(Extension('Extest', ['Extest_wrap.c']))
self.extensions.extend(exts) # Call the method for detecting whether _tkinter can be compiled

然后执行aarch64/mk2_make.sh,可以看到build/lib.linux2-aarch64-2.7/下面已经有Extest.so了:

 $ls build/lib.linux2-aarch64-2.7/Extest.so -l
-rwxrwxr-x pengdonglin pengdonglin Mar : build/lib.linux2-aarch64-2.7/Extest.so*

然后执行aarch64/mk3_install.sh,就会将Extest.so安装到lib/python2.7/lib-dynload/下面。

最后重新制作ramdisk文件,重启板子,测试Extest.so能否使用:

 [root@aarch64 root]# python
Python 2.7. (default, Mar , ::)
[GCC 4.9. (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Extest
>>> Extest.fac() >>> Extest.reverse("abc")
'cba'
>>> Extest.doppel("abc")
('abc', 'cba')
>>>

第二种: 手动编译

我们需要指定编译用的库以及头文件的搜索路径即可,下面是编译命令:

 #!/bin/bash
export PATH=/home/pengdonglin/src/qemu/aarch64/gcc-linaro-aarch64-linux-gnu-4.9-.07_linux/bin:$PATH CFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/Python2/aarch64/include/python2.7 -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes" LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/Python2/aarch64/lib -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic" aarch64-linux-gnu-gcc -c ../Extest_wrap.c ${CFLAGS} -o Extest.o aarch64-linux-gnu-gcc --shared Extest.o ${LDFLAGS} -o Extest.so

其中CFLAGS和LDFLAGS的值可以用下面的命令获得

 $/usr/local/bin/python2-config --cflags
-I/usr/local/include/python2. -I/usr/local/include/python2. -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes $/usr/local/bin/python2-config --ldflags
-L/usr/local/lib/python2./config -lpython2. -lpthread -ldl -lutil -lm -Xlinker -export-dynamic

然后编译,就会在当前目录下面生成一个Extest.so,然后拷贝到板子的/usr/lib/python2.7/site-packages/下面,这个目录下存放的是一些第三方的扩展模块,而/usr/lib/python2.7/lib-dynload/存放的一般是内建模块。

或者可以写成Makefile:

CFLAGS = -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
CFLAGS += -fPIC -I/home/pengdonglin/qemu/thiry_part/Python2/aarch32/include/python2.
CC = arm-none-linux-gnueabi-gcc all:Extest.so Extest.o: Extest_wrap.c
$(CC) $(CFLAGS) -c $^ -o $@ Extest.so: Extest.o
$(CC) -pthread -shared $^ -o $@
cp $@ /home/pengdonglin/qemu/thiry_part/Python2/aarch32/lib/python2./site-packages/ clean:
$(RM) *.o *.so .PHONY: clean all

将Extest_wrap.c跟Makefile放到统一目录下执行make命令即可

2、编译扩展模块到Qemu模拟的x86_64上面

这里也有三种方法:

第一种:将Extend_wrap.c拷贝到Python源码的Modules目录下,这个前面说过,不再重复

第二种:手动编译,编译命令如下

 #!/bin/bash
CFLAGS="-I/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/include/python2.7 -fPIC" LDFLAGS="-L/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/lib -fPIC" gcc -c ../Extest_wrap.c ${CFLAGS} -o Extest.o gcc --shared Extest.o ${LDFLAGS} -o Extest.so

编译完成后,将Extest.so拷贝到板子上面的相应目录下即可(如/usr/lib/python2.7/site-packages)

第三种:手动编写setup.py

setup.py:

 #!/usr/bin/env python

 from distutils.core import setup, Extension

 MOD = 'Extest_x86_64'
setup(name=MOD, ext_modules=[Extension(MOD, sources=['Extest_wrap.c'])])

这里模块名是Extest_x86_64,同时需要注意的是需要将setup.py跟Extest_wrap.c放到同一个目录下面。

编译:

/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/bin/python ./setup.py build

从log看执行的其实就是下面两条命令:

gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/include/python2. -c Extest_wrap.c -o build/temp.linux-x86_64-2.7/Extest_wrap.o

creating build/lib.linux-x86_64-2.7
gcc -pthread -shared build/temp.linux-x86_64-2.7/Extest_wrap.o -o build/lib.linux-x86_64-2.7/Extest_x86_64.so

安装:

/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/bin/python ./setup.py  install

从log看,Extest_x86_64.so会被安装到/home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/lib/python2.7/site-packages下面

 running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-2.7/Extest_x86_64.so -> /home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/lib/python2./site-packages
running install_egg_info
Writing /home/pengdonglin/src/qemu/python_cross_compile/Python2/x86_64/lib/python2./site-packages/Extest_x86_64-0.0.-py2..egg-info

然后从新制作ramdisk就可以了

3、编译扩展模块到PC(x86_64)上面

在操作之前PC上面应该用Python源码编译安装一次,方法很简单:

#!/bin/bash
../Python-2.7./configure
make -j8
sudo make install

默认会被安装到/usr/local下面

方法一: 将Extend_wrap.c拷贝到Python源码的Modules目录下,这个前面说过,不再重复

方法二: 手动编译,编译命令如下

 #!/bin/bash
CFLAGS="-I/usr/local/include/python2.7 -fPIC"
LDFLAGS="-L/usr/local/lib -fPIC"
gcc -c ../Extest_wrap.c ${CFLAGS} -o Extest.o
gcc --shared Extest.o ${LDFLAGS} -o Extest.so

将生成的Extest.so拷贝到/usr/local/lib/python2.7/site-packages/即可

测试:

 $sudo cp Extest.so /usr/local/lib/python2./site-packages/
$/usr/local/bin/python
Python 2.7. (default, Mar , ::)
[GCC 4.8.] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Extest
>>> Extest.reverse("peng")
'gnep'

方法三: 编写setup.py

setup.py:

 $cat setup.py
#!/usr/bin/env python
from distutils.core import setup, Extension
MOD = 'Extest'
setup(name=MOD, ext_modules=[Extension(MOD, sources=['Extest_wrap.c'])])

编译:

/usr/local/bin/python ./setup.py build

从log看,执行的是下面的命令:

 running build
running build_ext
building 'Extest' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python2. -c Extest_wrap.c -o build/temp.linux-x86_64-2.7/Extest_wrap.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/Extest_wrap.o -o build/lib.linux-x86_64-2.7/Extest.so

安装:

sudo /usr/local/bin/python ./setup.py install
从log看,Extest.so被安装到了/usr/local/lib/python2.7/site-packages下面
 running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-2.7/Extest.so -> /usr/local/lib/python2./site-packages
running install_egg_info
Removing /usr/local/lib/python2./site-packages/Extest-0.0.-py2..egg-info
Writing /usr/local/lib/python2./site-packages/Extest-0.0.-py2..egg-info