简体   繁体   English

将Python模块嵌入C程序时,如何将C结构传递给Python

[英]How do I pass C structure into Python when embedding a Python module in a C program

I have a working C program and now I'm embedding a python script implementing a specific function. 我有一个可以正常工作的C程序,现在我嵌入了一个实现特定功能的python脚本。

The question is, the arguments passed into Python is a complicated(I mean nested) C structure defined in C. And I hope the solution would be able to do the two ways communication easily: 问题是,传递给Python的参数是用C定义的复杂的C结构(我的意思是嵌套的)。我希望该解决方案能够轻松实现两种通信方式:

1.Create structure in C, and pass it into Python. 1.使用C创建结构,并将其传递给Python。 Let the Python do some modification. 让Python进行一些修改。

2.Create the structure in Python. 2.在Python中创建结构。 And pass it back to C. 并将其传递回C。

I was trying "SWIG" to generate some wrappers for the structure, as well as some helper functions using SWIG for Python so that it could return me some nested part of the structure so that I could visit the structure through Python easily. 我尝试使用“ SWIG”为该结构生成一些包装程序,以及使用SWIG for Python生成一些辅助函数,以便它可以返回结构的某些嵌套部分,以便我可以轻松地通过Python访问该结构。

Will this be a good solution or I may miss some very simple way of solving it? 这将是一个好的解决方案,还是我可能会错过一些非常简单的解决方法?

In my experience, SWIG should be able to handle arbitrarily nested structs the way you'd "expect". 以我的经验,SWIG应该能够以您“期望”的方式处理任意嵌套的结构。 – brunobeltran0 – brunobeltran0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM