c扩展和嵌入python

上传人:cunz****g666 文档编号:33443343 上传时间:2021-10-17 格式:DOC 页数:16 大小:60KB
收藏 版权申诉 举报 下载
c扩展和嵌入python_第1页
第1页 / 共16页
c扩展和嵌入python_第2页
第2页 / 共16页
c扩展和嵌入python_第3页
第3页 / 共16页
资源描述:

《c扩展和嵌入python》由会员分享,可在线阅读,更多相关《c扩展和嵌入python(16页珍藏版)》请在装配图网上搜索。

1、c 扩展和嵌入 python C+ 扩展和嵌入 Python 作者:胡金山 下载源代码 Python 简介 Python 是一种简单易学,功能强大 的解释型编程语言,它有简洁明了的语法,高效率的高层数 据结构,能够简单而有效地实现面向对象编程,特别适用于 快速应用程序开发,也可以用来开发大规模的重要的商业应 用。 Python 是一个理想的脚本语言。 Python 免费开源,可移植到多种操作系统,只要避免使用依 赖于特定操作系统的特性, Python 程序无需修改就可以在各 种平台上面运行。 Python 拥有现代编程语言所具有的一切强大功能, Python 标 准库十分庞大,可以帮助开发者处

2、理各种工作,如:图形用 户界面、文件处理、多媒 体、正则表达式、文档生成、单元测试、线程、数据库、网 络通讯、网页浏览器、CGI、FTP、电子邮件、XML、HTML、 WAV 文件、密码系统、 Tk 和 其他与系统有关的操作。只要安装了 Python,这些功能都是 可用的除了标准库以外,还有许多其他高质量的库,如 wxPython 、 Twisted 和 Python 图形库等等数不胜数 Python 容易扩展和嵌入。 Python 提供的许多标准模块支持 C 或者C+接口。Python 和 C 可以一起工作,它可以嵌入到 C 或者 C+的应用 程序当中,因此可用 Python 语言为应用程序

3、提供脚本接口, 由于支持跨语言开发, 可用 Python 设计概念化应用程序, 并 逐步移植到 C,使用前不必用 C 重写应用程序。 (Jython 使 Python 可以和 Java 一起工作,使 开发者可以在 Python 里面调 Java 的包,也可以在 Java 里面 使用 Python 的对象。还有更妙的 ,由于 Jython 的解释器完全用 Java 编写,因此可以在支持 Java 的任何平台上部署 Python 程序, 甚至 WEB 浏 览器也可以直接运行 Python 脚本。)提出问题在某个 C+应 用程序中,我们用一组插件来实现一些具有统一接口的功 能,我们使用 Python

4、 来代替动态链接库形式的插件, 这样可 以方便地根据需求的 变化改写脚本代码, 而不是必须重新编译链接二进制的动态 链接库。 Python 强大的功能足以胜任,但是有一些操作系统 特定的功能需要用 C+来实现,再由 Python 调用。所以,最基础地,我们需要做到: 1. 把 Python 嵌入到 C+应用程序中,在 C+程序中调用 Python 函数和获得变量的值; 2. 用 C+为 Python 编写扩展模块(动态链接库),在 Python 程序中调用 C+开发的扩展功能函数。 常用的 Python/C API 介绍下面是例子中用到的几个 Python/C API 的简要介绍及示例代码。注

5、意,这并不是这些函数的详 细介绍,而仅仅是我们所用到的功能简介,更详细内容请参 考文档 1 、2、3 、4 。 打开 Microsoft Visual Studio .NET 2003 ,新建一个控制台程 序, #include ,并在 main 函数里加入示例代码。 /先定义一些变量 char *cstr; PyObject *pstr, *pmod, *pdict; PyObject *pfunc, *pargs; 1. void Py_Initialize( ) 初始化 Python 解释器,在 C+程序中使用其它 Python/C API 之前,必须调用此函数,如果调用失败,将产生一个

6、致命的 错误。例: Py_Initialize(); 2. int PyRun_SimpleString( const char *command) 执行一段 Python 代码,就好象是在 _main_ 函数里面执行 一样。例: PyRun_SimpleString(from time import time,ctimen print Today is,ctime(time()n); 3. PyObject* PyImport_ImportModule( char *name) 导入一个 Python 模块,参数 name 可以是*.py 文件的文件名。 相当于 Python 内建函数 _i

7、mport_() 。例: pmod = PyImport_ImportModule(mymod); /mymod.py 4. PyObject* PyModule_GetDict( PyObject *module) 相当于 Python 模块对象的 _dict_ 属性,得到模块名称空间 下的字典对象。例: pdict = PyModule_GetDict(pmod); 5. PyObject* PyRun_String( const char *str, int start, PyObject *globals, PyObject *locals) 执行一段 Python 代码。 pstr

8、= PyRun_String(message, Py_eval_input, pdict, pdict); 6. int PyArg_Parse( PyObject *args, char *format, .) 解构 Python 数据为 C 的类型,这样 C 程序中才可以使用 Python 里的数据。例: /* convert to C and print it*/ PyArg_Parse(pstr, s, &cstr); printf(%sn, cstr); 7. PyObject* PyObject_GetAttrString( PyObject *o, char *attr_name

9、) 返回模块对象 o 中的 attr_name 属性或函数,相当于 Python 中表达式语句: o.attr_name。例: /* to call mymod.transform(mymod.message) */ pfunc = PyObject_GetAttrString(pmod, transform); 8. PyObject* Py_BuildValue( char *format, .) 构建一个参数列表, 把 C 类型转换为 Python 对象,使 Python 可以使用 C 类型数据,例: cstr=this is hjss test, to uppercase; pargs

10、 = Py_BuildValue(s), cstr); 9. PyEval_CallObject(PyObject* pfunc, PyObject* pargs) 此函数有两个参数,都指向 Python 对象指针, pfunc 是 要调用的 Python 函数,通常可用 PyObject_GetAttrString() 获得; pargs 是函数的参数列表,通常可用 Py_BuildValue() 构建。例: pstr = PyEval_CallObject(pfunc, pargs); PyArg_Parse(pstr, s, &cstr); printf(%sn, cstr); 10.

11、void Py_Finalize( ) 关闭 Python 解释器,释放解释器所占用的资源。例: Py_Finalize(); Python2.4 环境没有提供调试版本的 Python24d.lib ,所以上述 示例在 release 模式下编译。编译完成后,把可行文件和附 2 给出的 mymod.py 文件放在一起,再点击即可运行。为了简 化编程,附 3 给出了 simplepy.h 。这样, 调用 mymod.transform 变成如下形 式: /#include ” simplepy.h ” CSimplepy py; py.ImportModule(mymod); std:strin

12、g str=py.CallObject(transform, this is hjss test, to uppercase); printf(%sn, str.c_str(); 接下来,我们来用 C+ 为 Python 编写扩展模块 (动态链 接库),并在 Python 程序中调用 C+开发的扩展功能函数。 生成一个取名为 pyUtil 的 Win32 DLL 工程,除了 pyUtil.cpp 文件以外,从工程中移除所有其 它文件,并填入如下的代码: / pyUtil.cpp #ifdef PYUTIL_EXPORTS #define PYUTIL_API _declspec(dllexpo

13、rt) #else #define PYUTIL_API _declspec(dllimport) #endif #include #include #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ?) switch (ul_reason_for_call) case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: brea

14、k; return TRUE; std:string Recognise_Img(const std:string url) /返回结果 return 从 dll 中返回的数据 . : +url; static PyObject* Recognise(PyObject *self, PyObject *args) const char *url; std:string sts; if (!PyArg_ParseTuple(args, s, &url) return NULL; sts = Recognise_Img(url); return Py_BuildValue(s, sts.c_str

15、() ); static PyMethodDef AllMyMethods = Recognise, Recognise, METH_V ARARGS,/ 暴露给 Python 的函数 NULL, NULL /* Sentinel */ ; extern C PYUTIL_API void initpyUtil() PyObject *m, *d; m = Py_InitModule(pyUtil, AllMyMethods); / 初始化本模 块,并暴露函数 d = PyModule_GetDict(m); 在 Python 代码中调用这个动态链接库: import pyUtil resul

16、t = pyUtil.Recognise(input url of specific data) print the result is: + result 用 C+ 为 Python 写扩展时, 如果您愿意使用 Boost.Python 库 的话,开发过程会变得更开心 J 要编写一个与上述 pyUtil 同样功能的动态 链接库,只需把文件内容替换为下面的代码。当然,编译需 要 boost_python.lib 支持,运行需要 boost_python.dll 支持。 #include #include using namespace boost:python; #pragma comment

17、(lib, boost_python.lib) std:string strtmp; char const* Recognise(const char* url) strtmp =从 dll 中返回的数据 . : ; strtmp+=url; return strtmp.c_str(); BOOST_PYTHON_MODULE(pyUtil) def(Recognise, Recognise); 所有示例都在 Microsoft Windows XP Professional + Microsoft Visual Studio .NET 2003 + Python2.4 环境下测试通过,本文

18、所用的 Boost 库为 1.33 版本。参考资料 1 Python Documentation Release 2.4.1. 2005.3.30 ,如果您以 默认方式安装了 Python2.4 ,那么该文档的位置在 C:Program FilesPython24DocPython24.chm ; 2 Michael Dawson. Python Programming for the Absolute Beginner. Premier Press. 2003; 3 Mark Lutz. Programming Python, 2nd Edition. OReilly. 2001.3 4 M

19、ark Hammond, Andy Robinson. Python Programming on Win32. OReilly. 2000.1 Python 主页: http:/www.python.org ; Boost 库主面: www.boost.org ; 附 1 text.txt this is test text in text.txt. 附 2 mymod.py import string message = original string message =message+message msg_error= try: text_file = open(text.txt, r

20、) whole_thing = text_file.read() print whole_thing text_file.close() except IOError, (errno, strerror): print I/O error(%s): %s % (errno, strerror) def transform(input): #input = string.replace(input, life, Python) return string.upper(input) def change_msg(nul): global message #如果没有此行,message 是函数里头的

21、局部 变量 message=string changed return message def r_file(nul): return whole_thing def get_msg(nul): return message 附 3 simplepy.h #ifndef _SIMPLEPY_H_ #define _SIMPLEPY_H_ / simplepy.h v1.0 / Purpose: facilities for Embedded Python. / by hujinshan 2005 年 9 月 2 日 9:13:02 #include using std:string; #inc

22、lude/ - / Purpose: ease the job to embed Python into C+ applications / by hujinshan 2005 年 9 月 2 日 9:13:18 / - class CSimplepy / : private noncopyable public: /constructor CSimplepy() Py_Initialize(); pstr=NULL, pmod=NULL, pdict=NULL; pfunc=NULL, pargs=NULL; /destructor virtual CSimplepy() Py_Finali

23、ze(); /import the user module bool ImportModule(const char* mod_name) try pmod = PyImport_ImportModule(const_cast(mod_name); if(pmod=NULL) return false; pdict = PyModule_GetDict(pmod); catch(.) return false; if(pmod!=NULL & pdict!=NULL) return true; else return false; /Executes the Python source cod

24、e from command in the _main_ module. /If _main_ does not already exist, it is created. /Returns 0 on success or -1 if an exception was raised. /If there was an error, there is no way to get the exception information. int Run_SimpleString(const char* str) return PyRun_SimpleString(const_cast(str) );

25、/PyRun_String(message, Py_eval_input, pdict, pdict); /Execute Python source code from str in the context specified by the dictionaries globals. /The parameter start specifies the start token that should be used to parse the source code. /Returns the result of executing the code as a Python object, o

26、r NULL if an exception was raised. string Run_String(const char* str) char *cstr; pstr = PyRun_String(str, Py_eval_input, pdict, pdict); if(pstr=NULL) throw (when Run_String, there is an exception was raised by Python environment.); PyArg_Parse(pstr, s, &cstr); return string(cstr); /support olny one

27、 parameter for python function, I think its just enough. string CallObject(const char* func_name, const char* parameter) pfunc=NULL; pfunc = PyObject_GetAttrString(pmod, const_cast(func_name); if(pfunc=NULL) throw (string(do not found in Python module for: ) +func_name).c_str(); char* cstr; pargs =

28、Py_BuildValue(s), const_cast(parameter); pstr = PyEval_CallObject(pfunc, pargs); if(pstr=NULL) throw (when PyEval_CallObject, there is an exception was raised by Python environment); PyArg_Parse(pstr, s, &cstr); return string(cstr); /PyObject *args; /args = Py_BuildValue(si), label, count); /* make arg-list */ /pres = PyEval_CallObject(Handler, args); protected: PyObject *pstr, *pmod, *pdict; PyObject *pfunc, *pargs; ; #endif / _SIMPLEPY_H_ / end of file

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!