- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
全部。下面有一个 Cython 代码示例,其中有一个无符号字符数组,a
填充有无符号整数。当我将此数组传递给 Python def
方法时,包含 0 的索引之后的任何索引的值都会变得困惑。
在此示例中,由于 0 值位于第 6 个索引处,因此传递到 __cinit__()
方法的数组中的所有后续数组索引都有不正确的值。对于 __init__()
方法或使用 Python 声明 def
的任何函数或方法,也会发生此行为。
但是,当数组传递到任何 cdef
或 cpdef
函数或方法时,数组的值是正确的。
所以,我有两个问题(请注意,我使用的是 .pyx 运行程序文件):
__cinit__()
方法中?还有其他方法吗?def __cinit__()
方法?当然,我可以使用解决方法并使用 cdef
或 cpdef
方法,特别是对于我展示的这个简单的小示例,但我想了解是否有换一种方式...代码:
cdef class Classical:
def __cinit__(self, unsigned char *b):
for x in range(0, 12):
print b[x], " init" # This does not work
cdef void bar(self, unsigned char *b):
for x in range(0, 12):
print b[x], " method" # This works fine
def foo(unsigned char *b):
for x in range(0, 12):
print b[x], " function" # This does not work either
cdef unsigned char a[12]
a = [
83,
12,
85,
31,
7,
0,
91,
11,
0,
12,
77,
100
]
Classical(a).bar(a)
foo(a)
输出:
83 init
12 init
85 init
31 init
7 init
0 init
0 init
0 init
0 init
0 init
0 init
0 init
83 method
12 method
85 method
31 method
7 method
0 method
91 method
11 method
0 method
12 method
77 method
100 method
83 function
12 function
85 function
31 function
7 function
0 function
100 function
0 function
0 function
0 function
0 function
0 function
最佳答案
def 函数的所有参数都是 Python 对象。一个char *
( unsigned char *
也是如此)不是 Python 对象,但是可以自动将(某些)Python 对象转换为 char *
。所以
def foo(char *x):
...
对于 Cython 来说意味着:检查传递的 Python 对象是否可以转换为 cdef char *
,执行转换并在函数体中使用此转换的结果。
当使用 char *
调用 def 函数时(另请参阅此有点相关的 SO-post )作为参数:
cdef char a[12]
....
bar(a) # a decays to char *
Cython 执行以下操作:使用 char *
的自动转换假设它是一个以 null 结尾的 c 字符串到一个字节对象,并将这个临时字节对象传递给 def
-功能bar
.
这意味着在你的情况下:
foo(a)
创建一个长度为 5 的临时字节对象(而不是 12,因为第 6 个元素是 0
),前 5 个字符将被复制到其中。foo
该字节对象缓冲区的地址被用作 unsigned char *b
,现在只有 6 个元素(包括尾随 \0
),因此可以通过 b[6]
访问它是未定义的行为,可能会导致段错误。您可以验证a
和b
通过指向不同的地址
print("Address:", <unsigned long long>(&a[0])) # or &b[0]
所以问题实际上是,当你调用 foo
时不是整个数组都转换为临时bytes
-目的。从/到 char *
的转换Cython-documentation 中描述。在您的情况下,调用应该是:
foo(a[:12]) #pass the length explicitly, so cython doesn't have to depend on '\0'
现在打印以下内容:
83 function
12 function
85 function
31 function
7 function
0 function
91 function
11 function
0 function
12 function
77 function
100 function
<小时/>
cdef
的情况有所不同-函数,其中 char *
停留char *
并且不会转换为 Python 对象。然而,__cinit__
必须是def
函数,因此在这种情况下通常是 cdef
- 使用工厂函数,如the answer pointed out by @DavidW ,例如:
cdef class Classical:
...
@staticmethod
cdef Classical create(char* ptr):
obj = <Classical>Classical.__new__(Classical) # __init__ isn't called!
# set up obj while using ptr
...
return obj
显然,Classical.create
只能从 Cython 代码中使用,但另一方面,只有 Cython 代码才有指针!
关于python - Cython - 替换 def __init__() 方法,因为 Cython 的 Python 函数和方法无法处理值为 0 的无符号字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260575/
c 不做边界检查。那么cython是如何检查是否编译成c的呢? %%cython --annotate cimport cython @cython.boundscheck(True) cpdef m
可以直接声明用于 Cython 构造函数? 据我了解,这是可能的: # Cython cdef int[3] li = [1, 2, 3] # C++ int[3] li = {1, 2, 3} 但
所以,如果你有一个头文件。 %%file test.h struct mystruct{ int i; int j; }; 然后你将它包装在 Cython 中: cdef extern fr
我正在构建一个独立于平台的 cython 项目,我想根据正在使用的编译器传递编译器参数。我可以猜测基于平台的编译器,或者假设它与用于 Python 的编译器相同,但不能保证匹配。通常我注入(injec
我使用诗歌构建我的 cython 包。我在所有函数和类中都有 NumPy 风格的文档字符串。我现在要做的是添加 Sphinx 自动文档并发布在 Read the Docs。 我已阅读此主题 How d
赛通 libcpp模块包含 priority_queue 的模板,这很好,除了一件事:我不能通过自定义比较器(或者,至少,我不知道如何)。 我需要这个,因为我需要 priority_queue做一个a
以下代码定义了一个简单的 Cython 函数(为方便起见,使用 Ipython 魔法)。 %load_ext cython %%cython def f(float x, float y=2):
我正在尝试使用 cython 进行复数计算。在示例代码中,我想计算复数的复指数函数。问题是我不知道如何将我的整数乘以虚数单位。python的虚数单位1.0j乘以cython执行时报错。 这是我的代码:
在这里停留在一些基本的 Cython 上 - 在 Cython 中定义字符串数组的规范且有效的方法是什么? 具体来说,我想定义一个定长常量数组char . (请注意,此时我不想引入 NumPy。) 在
是否有可能,如果是,如何确定 Cython 中整数数据类型的大小(以位为单位)? 我正在尝试做这样的事情,以获得整数大小: cdef WORD_BITS = 0 IF sizeof(unsigned
我只是想打印 cython 变量的地址,但我无法绕过错误消息: cdef int myvar print &myvar 抛出 Cannot convert 'int *' to Python obje
我有一个 C 头文件,它在宏中定义了一个函数。我需要从 Cython 调用它。有没有办法在 Cython 中使用宏并使其完全扩展?我已经有了 C 类型的参数。 我尝试像使用函数一样使用 cdef,我认
令人惊讶的是,我似乎找不到通过名称获取结构体元素的单个示例(无论是在网络上还是在 cython 示例中)。 所以我收到了一个指向 C 函数结构体的指针,并且想要一一访问这些元素并将它们重新打包到 py
我尝试围绕 C++ 库编写一个 Cython 包装器 http://primesieve.org/ 它包装了一个函数count。到目前为止,它可以正确安装 python setup.py instal
我正在尝试将 cython 模块 data.pyx 导入另一个 cython 模块 user.pyx。一切都编译得很好,但是当我尝试在 python 模块中调用 user.pyx 时,我收到错误“Im
更新:内存 View 获胜。Cython 使用类型化内存 View :0.0253449 特别感谢 lothario,他指出了几个关键的变化。 荒谬。当然现在的问题是,似乎不能对它们做太多算术(加法和
我有一个使用 memoryview 数组的 cython 模块,即... double[:,:] foo 我想使用多处理并行运行这个模块。但是我得到了错误: PicklingError: Can't
我正在尝试使用 Cython 加速 PEP 484 类型的 python 脚本。我想保持一些语义和可读性。 之前,我有一个 Flags = int def difference(f1: Flags,
这个问题已经有答案了: Collapse multiple submodules to one Cython extension (5 个回答) 已关闭 3 年前。 我在一个包中有多个 .py 文件
我已经能够在我的 .pyx 脚本上使用 cython 在 linux 上创建一个 .so 文件。我也可以成功地在我的 python 解释器上进行导入。 我的问题是如何在不使用 cython 的情况下将
我是一名优秀的程序员,十分优秀!