SWIG - 在导入时运行 python 代码
我有一个使用动态链接的 SWIG 包装的 C++ 模块。由于 python 处理导入函数范围的方式,我必须在导入后直接运行命令 dl.open(library, dl.RLTD_NOW, dl.RTLD_GLOBAL) 。这是为了确保 C++ 库函数可供其导入的其他库使用。
当然,这意味着为了导入模块,需要三行而不是一行。然而,其他线是恒定的并且不依赖于任何东西。也就是说,我想将以下几行:转换
import dl
import module
dl.open(library, dl.RTLD_NOW, dl.RTLD_GLOBAL)
为简单的:
import module
我尝试浏览 SWIG 文档以了解如何使其在导入模块时运行代码,但我找不到任何内容。这可以吗?
谢谢。
I have a C++ module that I'm wrapping with SWIG that uses dynamic linking. Because of the way that python deals with scope of imported functions I've had to run the command dl.open(library, dl.RLTD_NOW, dl.RTLD_GLOBAL)
directly after import. This is to make sure that the C++ libraries functions are available to the other libraries that it imports.
Of course what this means is that in order to import the module three lines are needed instead of one. However the other lines are constant and depend on nothing. That is I want to convert the lines:
import dl
import module
dl.open(library, dl.RTLD_NOW, dl.RTLD_GLOBAL)
into simply:
import module
I have tried looking through the SWIG documentation as to how to make it run code upon the import of the module, but I can't find anything. Is this possible to do?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试包装你的模块。将您的 C++ 代码构建到“私有”模块中,并将其命名为
module_
或其他名称,以明确您不应导入它。然后,在 module.py (包装模块)中:Try wrapping your module. Build your C++ code into a "private" module, and call it
module_
or something, to make it clear that you shouldn't import it. Then, inmodule.py
(the wrapper module):