怎样把Python代码嵌入到C程序

Python023

怎样把Python代码嵌入到C程序,第1张

步骤1:安装Python开发包

由于需要访问Python/C API,首先安装Python开发包。

在Debian,Ubuntu或Linux Mint中:

在CentOS,Fedora或RHEL中:

安装成功后,Python头文件在/usr/include/python2.7。根据Linux发行版的不同,确切的路径可能是不相同的。例如,CentOS 6中是/usr/include/python2.6。

步骤2:初始化解释器并设置路径

C中嵌入Python的第一步是初始化Python解释器,这可以用以下C函数完成。

初始化解释器后,需要设置你的C程序中要导入的Python模块的路径。例如,比如你的Python模块位于/usr/local/modules。然后使用以下C函数调用来设置路径。

步骤3:数据转换

C中嵌入Python最重要的方面之一是数据转换。从C中传递数据到Python函数,需要首先将数据从C数据类型转换到Python数据类型。Python/C API提供各种函数来实现这。例如,转换C字符串到Python字符串,使用PyString_FromString函数。

另外一个类似函数PyInt_FromLong,将C中long数据类型转换为Python int。每个Python/C API函数返回一个PyObject类型的引用。

步骤4:定义一个Python模块

当你想嵌入Python代码到另一种语言如C,该代码需要被写成Python模块,然后用另一种语言“导入”。所以让我们来看看如何在C中导入Python模块。

为了进行说明,我们实现一个简单的Python模块例子如下:

以上的Python函数有一个字符串作为参数并返回两个重复的字符串。例如,如果输入字符串是“cyberpersons”,该函数返回'cyberpersonscyberpersons'。此模块文件命名为“printData.py”并将它放在前面声明的Python模块目录中(/usr/local/modules)。

步骤5:加载一个Python模块

现在你已经定义了Python模块,是时候在C程序中加载它了。导入模块的C代码看起来像这样:

步骤6:构建函数的参数

当加载一个模块时,可以调用模块中定义的Python函数。通常,我们需要传递一个或多个参数到一个Python函数。我们必须构建一个Python元组对象,它包括Python函数中的参数。

在我们的例子中,printData函数定义带一个参数的模块。因此,我们构建一个大小是一的Python元组对象如下。我们可以使用PyTuple_SetItem设置元组对象的每个项。

我们已经成功构建一个参数传递到函数调用,是时候从C程序调用python函数了。

步骤7:调用Python函数

一旦成功创建Python元组对象作为函数参数,我们可以调用一个带参数的Python函数。为此,通过使用PyObject_GetAttrString首先获得模块中定义的函数的引用,然后使用PyObject_CallObject调用该函数。例如:

步骤8:错误检查

避免运行时错误的常见方法是检查函数的返回值并根据返回值采取适当的行动。类似于C程序中的全局变量errno,Python/C API提供一个全局指示符,它报告最后发生的错误。当Python/C API函数失败,全局指示符设置为指示错误,并且PyErr_Print可以用于显示相应的人类可读的trackback。例如:

在你的应用程序中,你可以轻松地将各种错误检查。

这里是完整的C程序,它如本教程描述的嵌入Python代码。

步骤9:编译和执行

保存以上代码到finalCode.c,并且链接Python库(-lpython2.7)编译该代码。根据发行版的不同,可能使用不同的版本(例如,-lpython2.6)。

实现C与python混合编程方法

代码如下:

/* tcpportping.c */

#include <Python.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <sys/time.h>

/* count time functions */

static double mytime(void)

{

struct timeval tv

if (gettimeofday(&tv, NULL) == -1)

return 0.0

return (double)tv.tv_usec + (double)tv.tv_sec * 1000000

}

static PyObject * /* returns object */

tcpping(PyObject *self, PyObject *args )

{

struct sockaddr_in addr

struct hostent *hp

double time

char*host = NULL

int fd

int port, timeout

if (!PyArg_ParseTuple(args, "sii", &host, &port, &timeout)) /* convert Python ->C */

return NULL /* null=raise exception */

if ((fd = socket(AF_INET, SOCK_STREAM, 0)) <0) {

return Py_BuildValue("d", -1.0) /* convert C ->Python */

}

bzero((char *)&addr, sizeof(addr))

if ((hp = gethostbyname(host)) == NULL) {

return Py_BuildValue("d", -2.0) /* convert C ->Python */

}

bcopy(hp->h_addr, &addr.sin_addr, hp->h_length)

addr.sin_family = AF_INET

addr.sin_port = htons(port)

struct timeval tv

tv.tv_sec = 0

tv.tv_usec = timeout * 1000

double stime = mytime()

if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) <0) {

return Py_BuildValue("d", -3.0) /* convert C ->Python */

}

fd_set read, write

FD_ZERO(&read)

FD_ZERO(&write)

FD_SET(fd, &read)

FD_SET(fd, &write)

if (select(fd + 1, &read, &write, NULL, &tv) == 0) {

close(fd)

return Py_BuildValue("d", -4.0) /* convert C ->Python */

}

double etime = mytime()

time = etime - stime

if (!FD_ISSET(fd, &read) &&!FD_ISSET(fd, &write)) {

close(fd)

return Py_BuildValue("d", -4.0) /* convert C ->Python */

}

close(fd)

return Py_BuildValue("d", time/1000) /* convert C ->Python */

}

/* registration table */

static struct PyMethodDef portping_methods[] = {

{"tcpping", tcpping, METH_VARARGS}, /* method name, C func ptr, always-tuple */

{NULL, NULL} /* end of table marker */

}

/* module initializer */

void inittcpportping( ) /* called on first import */

{ /* name matters if loaded dynamically */

(void) Py_InitModule("tcpportping", portping_methods) /* mod name, table ptr */

}

二、Python调用C/C++\x0d\x0a\x0d\x0a\x0d\x0a1、Python调用C动态链接库\x0d\x0a\x0d\x0aPython调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。\x0d\x0a(1)C语言文件:pycall.c\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a/***gcc -o libpycall.so -shared -fPIC pycall.c*/ \x0d\x0a#include \x0d\x0a#include \x0d\x0aint foo(int a, int b) \x0d\x0a{ \x0d\x0a printf("you input %d and %d\n", a, b) \x0d\x0a return a+b \x0d\x0a} \x0d\x0a(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。\x0d\x0a(3)Python调用动态库的文件:pycall.py\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0aimport ctypes \x0d\x0all = ctypes.cdll.LoadLibrary \x0d\x0alib = ll("./libpycall.so")\x0d\x0alib.foo(1, 3) \x0d\x0aprint '***finish***' \x0d\x0a(4)运行结果:\x0d\x0a\x0d\x0a\x0d\x0a2、Python调用C++(类)动态链接库 \x0d\x0a\x0d\x0a 需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。\x0d\x0a(1)C++类文件:pycallclass.cpp\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a#include \x0d\x0ausing namespace std \x0d\x0a \x0d\x0aclass TestLib \x0d\x0a{ \x0d\x0apublic: \x0d\x0avoid display() \x0d\x0avoid display(int a) \x0d\x0a} \x0d\x0avoid TestLib::display() { \x0d\x0acout \x0d\x0ausing namespace std \x0d\x0aint test() \x0d\x0a{ \x0d\x0aint a = 10, b = 5 \x0d\x0areturn a+b \x0d\x0a} \x0d\x0aint main() \x0d\x0a{ \x0d\x0acout \x0d\x0a#include \x0d\x0a#include \x0d\x0a \x0d\x0aint fac(int n) \x0d\x0a{ \x0d\x0aif (n 回答于 2022-11-16