如何用pythoncapi创建子模块?

2024-09-27 13:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建以下模块和对象结构:

main_module
|
`--sub_module
   |
   +--ObjectOne
   |
   `--sub_sub_module
      |
      `--ObjectTwo

当我只有main_module.sub_module一切正常时,因为我在main_module文件夹中创建了一个空的__init__.py(因为main_module目前没有任何对象),我把sub_module.so放在它旁边。在

但是,当我试图创建两个独立的c模块来使main_module.sub_module.sub_sub_module工作(即sub_module.so和{}),然后我添加了两个{},它们都导入了它们的扩展模块,然后我进入了ImportError(作为从共享库“引发”的undefined symbol错误的包装器),因为sub_sub_module需要sub_module中的一些C级定义。这让我觉得,创建一个共享库来在其中创建“虚拟”模块,而不是不必要的库链接,这会更容易。。在

所以我的问题是:有可能吗?如果是,怎么办?或者,有没有更好的方法来实现我想要的?在


Tags: 模块对象py文件夹soinitmain错误
1条回答
网友
1楼 · 发布于 2024-09-27 13:23:43

我想我现在有你的问题了。要想获得详细而详细的答案,您应该阅读Providing a C API for an Extension Module。在

Portability therefore requires not to make any assumptions about symbol visibility. This means that all symbols in extension modules should be declared static, except for the module’s initialization function, in order to avoid name clashes with other extension modules [...]. And it means that symbols that should be accessible from other extension modules must be exported in a different way.

要实现这一点,您应该使用Capsules。对于这些,您希望在另一个模块中使用的每个函数都应该存储在void*数组中。然后使用数组作为第一个参数,用PyCapsule_New(void*, const char*, PyCapsule_Destructor)创建一个胶囊。此对象必须添加到子模块。然后,您可以调用sub_sub_模块中的PyCapsule_Import(const char*, int),导入数组并访问所需的函数。在

相关问题 更多 >

    热门问题