python传递指针_使用Python / C API传递C指针
我是Python / C API的新手…我正在尝试为我的C程序添加新功能,其中我可以将python嵌入其中并同时扩展功能,以便嵌入式解释器可以执行将与之交互的脚本作为我的C程序的一部分编写的扩展python模块.我的C程序没有全局变量.我想保持这种方式;同时为了向Cthon公开C功能,看来扩展C函数至少需要访问全局变量来访问程序的状态.我该如何解决这个问题?
例如这是我如何计划嵌入从main调用PYINTERFACE_Initialize的地方
void PYINTERFACE_Initialize(State *ptr, FILE *scriptFile, const char* scriptFileName)
{
Py_Initialize();
PyObject *module = Py_InitModule("CInterface", CInterfaceMethods);
if (PyRun_SimpleFileEx(scriptFile, scriptFileName, 1) != 0)
{
printf("PYINTERFACE script execution failed!\n");
}
**//ADD State *ptr TO module**
}
这是扩展功能:
static PyObject*
CInterface_GetStateInfo(PyObject *self, PyObject *args)
{
const char *printStr;
double stateInfo;
State *ptr;
if(!PyArg_ParseTuple(args, "s", &printStr))
{
return NULL;
}
printf("Received %s\n", printStr);
**//RETRIEVE State *ptr FROM self**
stateInfo = ptr->info;
return Py_BuildValue("d", currentTime);
}
这是通过State * ptr的最干净的方法吗?我当然不认为需要将内部状态暴露给python.我曾考虑使用胶囊,但似乎并不是胶囊的意图支持这种行为.
提前致谢!
V
解决方法:
Capsule基本上是python-opaque void指针,你可以传递或与模块关联.它们是解决问题的“方法”.
这是一个使用不必是静态的实例x的示例.首先将指针指向您的模块,如下所示(删除错误检查)…
// wrap the methods to be exposed to python in a module
// i.e. this is a list of method descriptions for the module
static PyMethodDef InitializeTurkeyMethods[] = {
// this block describes one method.. turkey.do_something()
{"do_something",
turkey_do_something, // fn pointer to wrap (defined below)
METH_VARARGS,
"do something .. return an int."},
{NULL, NULL, 0, NULL} // sentinel.
};
int init(X * x) {
// initialize embedded python scripting ..
// (this method a no-op on second or later calls).
Py_Initialize();
// initialize the turkey python module
PyObject * module = Py_InitModule("turkey", InitializeTurkeyMethods);
// Create a capsule containing the x pointer
PyObject * c_api_object = PyCapsule_New((void *)x, "turkey._X_C_API", NULL);
// and add it to the module
PyModule_AddObject(module, "_X_C_API", c_api_object);
}
然后在你要暴露给python的函数中,为了得到那个X指针,你做了这样的事情(这实际上必须在你开始在上面的代码中引用它之前):
static PyObject* turkey_do_something(PyObject *self, PyObject *args) {
if(!PyArg_ParseTuple(args, ":turkey_do_something"))
return NULL;
// get the x pointer back from the capsule
X * x = (X*)PyCapsule_Import("turkey._X_C_API", 0);
// call some fn on x
return Py_BuildValue("i", x->some_fn_that_returns_an_int());
}
这里“turkey._X_C_API”只是一些添加类型检查的名称 – 在这里为您的应用程序添加一些有意义的名称.土耳其是我刚才编写的演示模块名称.
现在假设,并且根据方式,你在调用Py_InitModule()时导出了turkey_do_something fn,你可以从python脚本中调用它:
import turkey
print turkey.do_something()
标签:python,python-c-api
python传递指针_使用Python / C API传递C指针相关推荐
- python 时间序列预测_使用Python进行动手时间序列预测
python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...
- python 概率分布模型_使用python的概率模型进行公司估值
python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...
- python 参数封装_扩展Python模块系列(三)----参数解析与结果封装
在上一节中,通过一个简单的例子介绍了C语言扩展Python内建模块的整体流程,从本节开始讲开始深入讨论一些细节问题,在细节讨论中从始至终都会涉及[引用计数]的问题.首先讨论C语言封装的Python函数 ...
- 用python读取股票价格_使用Python写一个量化股票提醒系统
大家在没有阅读本文之前先看下python的基本概念, Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开 ...
- python集群_使用Python集群文档
python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...
- python 异常处理模块_扩展Python模块系列(五)----异常和错误处理
在上一节中,讨论了在用C语言扩展Python模块时,应该如何处理无处不在的引用计数问题.重点关注的是在实现一个C Python的函数时,对于一个PyObject对象,何时调用Py_INCREF和Py_ ...
- python 网页编程_通过Python编程检索网页
python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...
- python机器学习预测_使用Python和机器学习预测未来的股市趋势
python机器学习预测 Note from Towards Data Science's editors: While we allow independent authors to publish ...
- python 免费空间_用python做大数据
不学Python迟早会被淘汰?Python真有这么好的前景? 最近几年Python编程语言在国内引起不小的轰动,有超越Java之势,本来在美国这个编程语言就是最火的,应用的非常非常的广泛,而Pytho ...
- python股票接口_股市python接口
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 最近发现一个很有趣的事情,受到全球经济大环境的影响,周围越来越多的人开始关注股市 ...
最新文章
- nginx特定的 404页面利于seo
- 关于字符串计算size的方法比较
- pythongetattribute_对Github上Python开源项目进行分析时遇到的一个AttributeError的解释及其解决方法。...
- Linux之数值运算:let,(()),[ ]
- 最小的linux内核编译,Linux最小内核移植
- 自动布局AutoLayout
- Sentry : Spark-sql 读取hive数据 权限问题
- ModalPopupExtender控件主要有两种使用方式:客户端使用方式和服务器端使用方式
- python练习:科赫小雪花包裹
- 清水河畔论坛二手帖子爬虫
- ie浏览器和谷歌浏览器控件不同_win7系统怎么重置ie浏览器 重置ie浏览器操作方法【详解】...
- 微信小程序 禁止弹框下面的内容滑动
- 用计算机问你叫什么名字,计算器功能有哪些呢
- 嵌入式高速串行并行技术_推荐(张锋)
- pc端客户端爬虫_爬虫微博模拟登陆-从移动端到PC端
- android内存扩展格式,一文看懂:如何为你的安卓手机选择合适的存储卡
- ufs 固态硬盘_东芝首发UFS 3.0闪存:性能媲美高端PC 固态硬盘
- 解决:用MyEclipse启动tomcat 报错cannot not find the main class
- 12864多级菜单实现方法
- 玩大神的抖音小姐姐机器人遇到的问题