4. 构建 C/C++ 扩展 ¶
CPython 的 C 扩展是共享库 (如
.so
文件在 Linux,
.pyd
在 Windows),导出
初始化函数
.
要可导入,共享库必须可用于
PYTHONPATH
, and must be named after the module name, with an appropriate extension. When using setuptools, the correct filename is generated automatically.
初始化函数带有签名:
It returns either a fully initialized module, or a
PyModuleDef
实例。见
初始化 C 模块
了解细节。
对于采用仅 ASCII 名称的模块,函数必须被命名为
PyInit_<modulename>
,采用
<modulename>
替换模块名称。当使用
多阶段初始化
,非 ASCII 模块名称是允许的。在这种情况下,初始化函数名称是
PyInitU_<modulename>
,采用
<modulename>
编码使用 Python 的
punycode
编码采用下划线替换连字符。在 Python 中:
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