python - 以 typed-memory-view 作为参数的 Cython 扩展类型方法

标签 python cython typed-memory-views

我想编写一个扩展类型,其方法采用类型化内存 View 作为其参数之一。 MWE:

main.pyx

# cython: language_level = 3

cdef class main:

    cdef void foo(self, double [:] x):

        pass

setup.py

from setuptools import setup
from Cython.Build import cythonize

if __name__ == "__main__":

    setup(ext_modules=cythonize("main.pyx"))

当我运行代码(真正的代码)时,一切似乎都工作正常,但我收到以下编译警告:

running build_ext
building 'main' extension
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -I/usr/local/opt/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1969606d717677592a372829" rel="noreferrer noopener nofollow">[email protected]</a>/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c main.c -o build/temp.macosx-12-x86_64-cpython-310/main.o
main.c:19220:21: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
                    CYTHON_FALLTHROUGH;
                    ^
main.c:353:34: note: expanded from macro 'CYTHON_FALLTHROUGH'
      #define CYTHON_FALLTHROUGH __attribute__((fallthrough))
                                 ^
main.c:19231:21: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
                    CYTHON_FALLTHROUGH;
                    ^
main.c:353:34: note: expanded from macro 'CYTHON_FALLTHROUGH'
      #define CYTHON_FALLTHROUGH __attribute__((fallthrough))
                                 ^
2 warnings generated.
clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk build/temp.macosx-12-x86_64-cpython-310/main.o -o build/lib.macosx-12-x86_64-cpython-310/main.cpython-310-darwin.so
ld: warning: -undefined dynamic_lookup may not work with chained fixups
copying build/lib.macosx-12-x86_64-cpython-310/main.cpython-310-darwin.so ->

我不知道这意味着什么,但只有当扩展类型具有采用类型化内存 View 作为参数的方法时才会发生。

我应该采取什么措施吗?

最佳答案

这几乎肯定不是问题。本质上,C 的 switch 行为有点违反直觉:

switch (x) {
    case 1:
        func1();
    case 2:
        func2();
        break;
    case 3:
        func3();
}

如果您使用 x==1 调用它,它将运行 func1()func2() (但不会运行 func3() 因为 break 告诉它结束跳出开关。这就是调用“falling through”。您通常不希望这样,并且因此,如果您不使用 break 结束 switch case,很多编译器会警告您。

但是,有时您确实想要它,并且可以使用可选属性(非标准且依赖于编译器)来表示“我确实想失败”。在本例中,属性为 __attribute__((fallthrough))

此外,Cython 生成了大量 C 代码,这些代码实际上是为了优化而设计的 - 大多数时候编译器将能够看到只有一个分支可以发生并删除其他分支。这是因为生成代码更容易,并将完整的决定权留给 C 编译器。

在这种情况下,编译器会说“您已将 switch-case 标记为‘fallthrough’,但您甚至永远无法到达它”。这不是一个会给你带来问题的警告 - 如果你自己编写代码,它可能表明你没有仔细考虑,但在像 Cython 这样的代码生成器中,重用通用代码是明智的做法.

但是 - report it as a bug to Cython 。一般来说,Cython 会尽量减少 C 警告的数量(因为它们让人们担心),因此如果有一种简单的方法来避免它,那将是一种改进。

关于python - 以 typed-memory-view 作为参数的 Cython 扩展类型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73815831/

相关文章:

python - 增加一个类别

python - 在 apache 服务器上导入 Pandas 导致超时错误

c++ - 从没有内置类型的 C++ 调用 Cython

python - Cython 中的内存 View 数组

python - FFTshift 导致振荡 - 为什么? ( NumPy 的)

python - 将 AST 节点转换为 python 代码

Python 到 cython - 提高大型数组迭代的性能

python - 将 ndarray 转换为 cv::Mat 的最简单方法是什么?

numpy - Cython Memoryview 中的数组广播

python - 在 bytes 对象上获取指向 python memoryview 的指针