all. I have this Cython code example down below where I have an unsigned char array, a filled with unsigned integers. When I pass in this array into a Python def method, the value of any index after the index containing 0 becomes messed up.
In this example, since the value of 0 was at the 6th index, all succeeding array indices from the array passed into the __cinit__() method have incorrect values. This behavior also happens for the __init__() method or any function or method using the Python declaration def.
However, when the array is passed into any cdef or cpdef function or method, the values of the array is correct.
So, I have two questions (and note that I am using a .pyx runner file):
- Am I passing in the array into the
__cinit__()method incorrectly? Is there another way to do it? - Alternatively, is there a Cythonic way of replacing the
def __cinit__()method? Of course, I could use a workaround and usecdeforcpdefmethods, especially for this simple little example that I'm showing, but I would like to learn whether there is a different way...
Code:
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)
Output:
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