3. 定义扩展类型:分类话题
CPython 的 C 扩展是共享库 (如 .so 文件在 Linux, .pyd 在 Windows),导出 初始化函数 .
.so
.pyd
要可导入,共享库必须可用于 PYTHONPATH , and must be named after the module name, with an appropriate extension. When using setuptools, the correct filename is generated automatically.
PYTHONPATH
初始化函数带有签名:
It returns either a fully initialized module, or a PyModuleDef 实例。见 初始化 C 模块 了解细节。
PyModuleDef
对于采用仅 ASCII 名称的模块,函数必须被命名为 PyInit_<modulename> ,采用 <modulename> 替换模块名称。当使用 多阶段初始化 ,非 ASCII 模块名称是允许的。在这种情况下,初始化函数名称是 PyInitU_<modulename> ,采用 <modulename> 编码使用 Python 的 punycode 编码采用下划线替换连字符。在 Python 中:
PyInit_<modulename>
<modulename>
PyInitU_<modulename>
def initfunc_name(name): try: suffix = b'_' + name.encode('ascii') except UnicodeEncodeError: suffix = b'U_' + name.encode('punycode').replace(b'-', b'_') return b'PyInit' + suffix
从单个共享库导出多个模块是可能的,通过定义多个初始化函数。不管怎样,导入它们要求使用符号链接或自定义导入器,因为默认情况下只能找到对应文件名的函数。见 在一个库中多个模块 章节在 PEP 489 了解细节。
Python 3.12 and newer no longer come with distutils. Please refer to the setuptools documentation at https://setuptools.readthedocs.io/en/latest/setuptools.html to learn more about how build and distribute C/C++ extensions with setuptools.
setuptools
5. 在 Windows 构建 C/C++ 扩展
键入搜索术语或模块、类、函数名称。