簡體   English   中英

以 typed-memory-view 作為參數的 Cython 擴展類型方法

[英]Cython extension type method with typed-memory-view as argument

我想使用一種將類型化內存視圖作為其 arguments 之一的方法編寫擴展類型。 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/python@3.10/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 ->

我不知道這是什么意思,但它只發生在擴展類型有一個將類型化內存視圖作為參數的方法時。

我應該對此做些什么嗎?

這幾乎肯定不是問題。 本質上 C 的switch行為有點違反直覺:

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

如果你用x==1調用它,它會運行func1()func2() (但不是func3()因為break告訴它結束跳出開關。這叫做“跌倒”。你通常不會不希望這樣,因此如果您不使用break結束 switch case ,許多編譯器會警告您。

但是,有時您確實需要它,並且可以使用可選屬性(非標准且依賴於編譯器)來表示“我真的很想失敗”。 在這種情況下,屬性是__attribute__((fallthrough))

此外,Cython 會生成大量 C 代碼,這些代碼實際上是為了優化而設計的 - 大多數情況下,編譯器將能夠看到只有一個分支可以發生並刪除其他分支。 這是因為更容易生成代碼並將完整的決定留給 C 編譯器。

在這種情況下,編譯器會說“您已將 switch-case 標記為 'fallthrough',但您甚至無法到達它”。 這不是一個會給您帶來問題的警告 - 如果您自己編寫代碼,它可能表明您沒有仔細考慮,但在像 Cython 這樣的代碼生成器中,重用通用代碼是明智之舉.

但是 -將其作為錯誤報告給 Cython 作為一般規則,Cython 會盡量減少 C 警告的數量(因為它們讓人們擔心),所以如果有一種簡單的方法可以避免它,那將是一個改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM