8

每当我将单个值分配给多维内存视图的切片时,Cython 似乎使用了错误的跨步,除非切片沿第一个维度。我在下面给出一个完整的例子:

# bug.py
import numpy as np

def bug():
    #cdef int[:, ::1] a
    a = 2*np.ones((2, 2), dtype=np.intc)
    a[:, :1] = 1
    print(np.asarray(a))

如果我们在 Python 中运行它(例如python3 -c 'import bug; bug.bug()'),我们得到

[[1 2]
 [1 2]]

正如预期的那样打印出来。我现在通过将文件重命名为 用 Cython 编译它,bug.pyx将以下内容保存在Makefile同一目录中,

# Makefile
python = python3
python_config = $(python)-config
CC = gcc
CFLAGS  = $(shell $(python_config) --cflags) -fPIC
CFLAGS += $(shell $(python_config) --includes)
python_libdir = $(shell $(python) -c "import sysconfig; \
    print(sysconfig.get_config_var('LIBDIR'));")
LDLIBS  = -L$(python_libdir) -Wl,-rpath=$(python_libdir)
LDLIBS += $(shell $(python_config) --libs)
LDFLAGS = $(shell $(python_config) --ldflags) -shared

bug.so: bug.c; $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o bug.so bug.c
bug.c: bug.pyx; $(python) -m cython -3 $<

并运行make。再次运行python3 -c 'import bug; bug.bug()'它现在拿起已编译bug.so的,再次打印出来

[[1 2]
 [1 2]]

如果我们现在取消注释cdef声明,运行makepython3 -c 'import bug; bug.bug()'再次,我们得到

[[1 1]
 [2 2]]

这是错误的。我不相信int[:, ::1]声明是错误的,因为 Cython 会抱怨。如果我用它替换它就int[:, :]可以了。此外,如果我分配给 , 的第一个维度aa[:1, :] = 1它会起作用。

这是一个已知问题,还是我以某种方式误解了 Cython 内存视图的这种看似基本的用法?

4

1 回答 1

1

我提交了一份错误报告,该问题已得到修复

于 2019-06-02T19:53:37.667 回答