前言
cython编译后,名字会带上系统信息,该行为已经在distutils包中定义。distutils使用sysconfig和"EXT_SUFFIX"配置变量:
# Lib\distutils\command\build_ext.py
def get_ext_filename(self, ext_name):
r"""Convert the name of an extension (eg. "foo.bar") into the name
of the file from which it will be loaded (eg. "foo/bar.so", or
"foo\bar.pyd").
"""
from distutils.sysconfig import get_config_var
ext_path = ext_name.split('.')
ext_suffix = get_config_var('EXT_SUFFIX')
return os.path.join(*ext_path) + ext_suffix
python版本是3.5的话
python版本是3.5的话, “EXT_SUFFIX"变量包含平台的信息,如”.cp35-win_amd64"。
我们可以写一个函数:
def get_ext_filename_without_platform_suffix(filename):
name, ext = os.path.splitext(filename)
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
if ext_suffix == ext:
return filename
ext_suffix = ext_suffix.replace(ext, '')
idx = name.find(ext_suffix)
if idx == -1:
return filename
else:
return name[:idx] + ext
自定义build_ext 命令:
from Cython.Distutils import build_ext
class BuildExtWithoutPlatformSuffix(build_ext):
def get_ext_filename(self, ext_name):
filename = super().get_ext_filename(ext_name)
return get_ext_filename_without_platform_suffix(filename)
用法
setup(
...
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
...
)