您的位置:首页 > 编程语言 > Python开发

用脚本C调用Python脚本文件里的函数

2013-01-01 21:35 621 查看

C调用Python脚本文件里的函数

#Python脚本,存为pytest.py

def add(a,b):

print "in python function add"

print "a = " + str(a)

print "b = " + str(b)

print "ret = " + str(a+b)

return a + b

// C代码调,用上面的add函数

#include <stdio.h>

#include <stdlib.h>

#include "C:/Python26/include/python.h"

#pragma comment(lib, "C:\\Python26\\libs\\python26.lib")

int main(int argc, char** argv)

{

// 初始化Python

//在使用Python系统前,必须使用Py_Initialize对其

//进行初始化。它会载入Python的内建模块并添加系统路

//径到模块搜索路径中。这个函数没有返回值,检查系统

//是否初始化成功需要使用Py_IsInitialized。

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;

Py_Initialize();

// 检查初始化是否成功

if ( !Py_IsInitialized() )

{

return -1;

}

// 载入名为pytest的脚本(注意:不是pytest.py)

pName = PyString_FromString("pytest");

pModule = PyImport_Import(pName);

if ( !pModule )

{

printf("can't find pytest.py");

getchar();

return -1;

}

pDict = PyModule_GetDict(pModule);

if ( !pDict )

{

return -1;

}

// 找出函数名为add的函数

pFunc = PyDict_GetItemString(pDict, "add");

if ( !pFunc || !PyCallable_Check(pFunc) )

{

printf("can't find function [add]");

getchar();

return -1;

}

// 参数进栈

pArgs = PyTuple_New(2);

// PyObject* Py_BuildValue(char *format, ...)

// 把C++的变量转换成一个Python对象。当需要从

// C++传递变量到Python时,就会使用这个函数。此函数

// 有点类似C的printf,但格式不同。常用的格式有

// s 表示字符串,

// i 表示整型变量,

// f 表示浮点数,

// O 表示一个Python对象。

PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));

PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));

// 调用Python函数

pRetVal = PyObject_CallObject(pFunc, pArgs);

printf("function return value : %ld\r\n", PyInt_AsLong(pRetVal));

Py_DECREF(pName);

Py_DECREF(pArgs);

Py_DECREF(pModule);

Py_DECREF(pRetVal);

// 关闭Python

Py_Finalize();

return 0;

}

//一下为个人实践的另一套方法



#include <Python.h>

#include <conio.h>

int main()

{

Py_Initialize();

if (!Py_IsInitialized())

{

printf("初始化错误\n");

return -1;

}

PyObject* pModule = NULL;

PyObject* pFunc = NULL;

PyObject* pArg = NULL;

PyObject* pRetVal = NULL;

pModule = PyImport_ImportModule("hello");

pFunc = PyObject_GetAttrString(pModule,"hello");

pArg = Py_BuildValue("(i,i)",33,44);

pRetVal = PyObject_CallObject(pFunc,pArg);

printf("%d\n",PyInt_AsLong(pRetVal));

Py_Finalize();

_getch();

return 0;

}

用脚本语言开发网游 – C整合Python

Python
Views: 5885 |Add Comments
像因特网的Web系统一样, 几乎所有的大规模软件系统, 都会使用脚本语言. 在网络游戏服务器的开发中, 也会应用脚本语言. 游戏开发常用的脚本语言主要有: Python, Lua. 本文介绍在C语言中嵌入Python支持, C语言和Python语言通过API进行交互.

任何两种语言要整合, 首先要解决通信问题. C要和Python进行通信, 要进行数据结构转换, 把C的数据结构转为Python的对象, 和把Python对象转换为C数据结构.

下面对代码实例进行讲解, 代码忽略和错误处理.

#include <python2.5/Python.h>

void test1();
void test2();

PyObject *globals

int main(int argc, char **argv){
PyObject *m, *obj1, *pobj;
PyObject *request, *response;
int i;

Py_Initialize();

变量声明, 以及调用初始化函数 Py_Initialize().

m = PyImport_AddModule("__main__");
globals = PyModule_GetDict(m);

获取全局变量的字典表(哈希表). 在 Python 脚本中, 该字典表的键名就是全局变量的的名字. 例如, 全局变量字典表中有一项记录 ‘a’ => ‘123′, 那么, 在 Python 脚本中, 变量 a 的值是 ‘123′. 该字典表就是 C 语言和 Python 语言进行通信的关键.

request = PyDict_New();
response = PyDict_New();

PyDict_SetItemString(globals, "request", request);
PyDict_SetItemString(globals, "response", response);

obj1 = PyInt_FromLong(1);
PyDict_SetItemString(request, "count", obj1);

在本代码例子中, 字典表 request 用于 C 向 Python 传递数据, response 用于 Python 脚本向 C 程序传递数据. 使用两个字典并不是必须的, 可以只用一个. request 中有一项键名为 ‘count’ 的记录, 相当于向 Python 传递了一个名为 ‘count’ 的参数.

/* http://www.benegg.com */
for(i = 0; i < 2; i++){
test1();
pobj = PyDict_GetItemString(response, "code");
printf("code: %s\n", PyString_AsString(pobj));

test2();
pobj = PyDict_GetItemString(response, "code");
printf("code: %s\n", PyString_AsString(pobj));

printf("---\n");
}

Python 脚本中返回名为 ‘code’ 的响应参数, 在 C 程序中将该参数的值打印出来.

Py_DECREF(obj1);
Py_DECREF(request);
Py_DECREF(response);

out:
Py_Finalize();
return 0;
}

使用宏 Py_DECREF 减少对象的引用计数, 以便 Python 进行垃圾收集. 最后调用 Py_Finalize() 释放资源.

void test1(){
FILE *fp;
fp = fopen("benegg.com.py", "r");
PyRun_SimpleFile(fp, filename);
fclose(fp);
}

PyRun_SimpleFile() 函数执行一个 Python 脚本文件.

void test2(){
FILE *fp;
PyObject *co;
char *buf;
long size;

fp = fopen("benegg.com.py", "r");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);

buf = malloc(size + 1);
fread(buf, 1, size, fp);
buf[size] = '\0';

PyRun_SimpleString(buf);

因为 PyRun_SimpleFile() 每执行一次都要读取一次文件, 所以可以把文件内容读取到字符缓冲中, 然后多次执行 PyRun_SimpleString(), 避免磁盘 IO 造成性能消耗.

co = Py_CompileString(buf, "benegg.com", Py_file_input);
PyEval_EvalCode((PyCodeObject *)co, globals, globals);

不过, 虽然 PyRun_SimpleString() 避免了磁盘 IO, 然后还是会在每一次执行的时候解析文本代码. 这时, 可以把代码编译成一个 PyCodeObject 对象, 然后调用 PyEval_EvalCode() 执行, 避免了多次解析造成的性能消耗.

Py_DECREF(co);
out:
free(buf);
fclose(fp);
}

Python 脚本代码如下:

print "test1 - %d" % request['count']
request['count'] += 1
response['code'] = "test" + str(request['count'])

编译 C 代码:

# http://www.benegg.com gcc -g -Wall -o c test.c -L/usr/lib/python2.5/config -lpython2.5

注意, 将路径改为你机器上的实际路径. 执行 ./c, 可以看到, 执行的结果是:

test1 - 1
code: test2
test1 - 2
test1 - 3
code: test4
---
test1 - 4
code: test5
test1 - 5
test1 - 6
code: test7

原发在: http://www.benegg.com/game/archives/51.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: