无法使用参数名称调用具有单个位置参数的 Cythonized 函数

Cythonized function with a single positional argument is not possible to call using argument name

长话短说,我想用 cythonize 我的 python 代码并构建 .so 文件以对客户隐藏它。 采用这个简单的函数:

def one_positional_argument(a):
    print(a)

和我的 setup.py 构建 .so 文件

from setuptools import setup, find_packages
from Cython.Build import cythonize

setup(
    name='tmp',
    version='1.0.0',
    packages=find_packages(),
    nthreads=3,
    ext_modules=cythonize(
        ["a.py"],
        compiler_directives={'language_level': 3},
        build_dir="output",
    ),
)

当我导入 .so 文件并尝试执行我的函数时,会发生以下情况:

one_positional_argument(1) # this prints 1 and works fine
one_positional_argument(a=1) # throws TypeError: one_positional_argument() takes no keyword arguments

对此有多种解决方法,但我想知道我是否做错了什么

附加信息: 如果我有一个带有 2 个位置参数的函数,或者一个位置参数和一个带有默认值的函数,一切正常。该问题仅存在于 1 个位置参数中。

您需要 always_allow_keywords 编译器指令 (https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives)。

默认情况下不允许它们是一种有意的 compatibility/speed 权衡。然而,在即将发布的 Cython v3(alpha 版本现在可用......)中,这将发生变化:https://github.com/cython/cython/issues/3090