C语言实现的Python扩展模块(兼容Python2 & 3)
来源:互联网 发布:大连理工大学 知乎 编辑:程序博客网 时间:2024/05/22 20:53
首先,一个完整的python2的C语言扩展模块开发及运行过程请参考文章:http://blog.csdn.net/zxygww/article/details/49097975。
#include <Python.h>//C functionint add(int arg1, int arg2){ return arg1 + arg2;}//add wrapped functionstatic PyObject* wrap_add(PyObject *self, PyObject *args){ //Convert the python input objects into C objects int arg1, arg2; if(!PyArg_ParseTuple(args, "ii", &arg1, &arg2)) return NULL; //Call c function int result = add(arg1,arg2); //wrap the result from c function to python object. return (PyObject*)Py_BuildValue("i", result);}//define the PyMethodDef methodsstatic PyMethodDef wrap_methods[] ={ {"add", wrap_add, METH_VARARGS}, {NULL, NULL}};#if PY_MAJOR_VERSION >= 3// The method table must be referenced in the module definition structure.static struct PyModuleDef sampleModule = { PyModuleDef_HEAD_INIT, "sample", /* name of module */ NULL, /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ wrap_methods};// The initialization function must be named PyInit_name()PyMODINIT_FUNC PyInit_sample(void){ return PyModule_Create(&sampleModule);}#else//initilization function named init<modulename>PyMODINIT_FUNC initsample(void){ Py_InitModule("sample", wrap_methods);}#endif
0 0
- C语言实现的Python扩展模块(兼容Python2 & 3)
- C语言实现的Python扩展模块
- 用C语言实现python的扩展模块
- 使用vs2008实现C语言对Python2的扩展
- 写python的扩展模块(C)
- 用C语言编写Python扩展模块
- 在python中扩展c语言模块
- 使用c语言来扩展python模块
- 快速实现python c扩展模块
- 快速实现python c扩展模块
- 快速实现python c扩展模块
- 快速实现python c扩展模块
- 快速实现python c扩展模块
- Python 的C语言扩展
- 扩展Python__在python脚本中使用其他语言(c/c++/java/c#)编写的模块
- 【Python】使用C语言来扩展Python模块
- 在python中使用c语言扩展模块的简单示例
- C语言扩展Python
- weblogic 多线程用相同的数据库连接导致的错误
- 安装多个mysql服务,mysql5.7.6版本为例
- 解决ecshop换服务器出现PHP Fatal error: Cannot redeclare get_linked_goods()
- Linux内核中的红黑树
- Android的发布与更新
- C语言实现的Python扩展模块(兼容Python2 & 3)
- jxl实现excel 导出
- ANDROID2.3 Launcher源码分析-MVC
- 正则表达式。。好久前写的。搬过来
- csdn-markdown 技巧
- java悄无声息的吞掉异常
- try catch finally的用法
- 设置Hadoop+Hbase集群pid文件存储位置
- sourceinsight宏的编写-----if等语句的简练