【问题标题】:Cython error: Coercion from Python not allowed without the GILCython 错误:没有 GIL 就不允许来自 Python 的强制
【发布时间】:2017-02-15 13:43:21
【问题描述】:

尝试编译以下 MCE:

from libc.math import fabs


cdef inline double fmax(double x, double y) nogil:
    return x if x > y else y


cdef inline double fsign(double x) nogil :
    if x == 0.:
        return 0.
    elif x > 0.:
        return 1.
    else:
        return - 1.


cdef inline double ST(double u, double x) nogil:
    return fsign(x) * fmax(fabs(x) - u, 0.)

除其他错误外,我得到:

Error compiling Cython file:
------------------------------------------------------------
...
    else:
        return - 1.


cdef inline double ST(double u, double x) nogil:
    return fsign(x) * fmax(fabs(x) - u, 0.)
                                  ^
------------------------------------------------------------

test.pyx:18:35: Coercion from Python not allowed without the GIL

我不知道发生了什么,因为从我的角度来看,所有值都是双精度的(可能 0 除外。它可能是浮点数,但可以安全地提升为双精度)

setup.py 是:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("*.pyx"),
)

编辑:实际上,与这一行相关的错误有很多,例如:

test.pyx:18:35: Operation not allowed without gil
test.pyx:18:31: Calling gil-requiring function not allowed without gil
test.pyx:18:31: Accessing Python global or builtin not allowed without gil
test.pyx:18:33: Converting to Python object not allowed without gil
test.pyx:18:31: Constructing Python tuple not allowed without gil

【问题讨论】:

    标签: python cython


    【解决方案1】:

    这“只是一个错字”,但错误消息对我没有多大帮助,所以我将其发布为答案: 我用的是libc.math import fabs,而不是cimport fabs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多