Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Testing the latest release of Cython (0.29.13) using both Python2 and Python3 (with
import numpy as np cimport numpy as np cdef packed struct a_type: np.float64_t a1 char a2 cpdef double test_func(a_type[::1] x): cdef: int i double res res = 0.0 for i in range(1, x.shape): print(x.a2, x[i].a2, x.a2 == x[i].a2, x[i].a2 == b'abcd') if x.a2 == x[i].a2: res += 1.0 return 0.0
and then running the code:
import numpy as np from mod1.ftest import test_func if __name__ == '__main__': a = np.recarray(4, dtype=[('a1', np.float64), ('a2', 'S5')]) a = (9.0, b'abcd') a = (8.0, b'abcd') a = (8.0, b'xyz') a = (8.0, b'abcd') print(test_func(a))
I get the following output:
Comparing the field to a literal byte string works correctly as demonstrated in the print statement. Calling
I've also pulled up an old environment I had laying around with Cython 0.25.2, and I see the issue there as well.
x.a2 == x[i].a2
so we are comparing char memory addresses NOT string literals.
I could suggest such a workaround
As i can see it adding type conversion call