I want to use Cython to speed up my Python code in analogy to the Cython's Numpy tutorial. I give you a MWE of what I intended:
Test Function:
import pyximport
pyximport.install()
import CythonModule2 as cm2
print cm2.read_data()
Cython Module CythonModule2.pyx
:
from libc.stdio cimport *
import numpy as np
cimport numpy as np
cdef packed struct CData:
np.float32_t A
np.uint8_t CH1, CH2
np.int64_t D
def read_data():
cdef np.ndarray[CData] coins = np.empty(10, dtype=[('A', 'f4'),
('CH1', '|u1'), ('CH2', '|u1'),
('D', '<i8')])
return coins;
The definition in the function read_data()
produces the following error message (and C++ "terminated in an unusual way"):
ValueError: Buffer dtype mismatch; next field is at offset 6 but 8 expected
I could format all entries in 64bit (eight byte) variables but I want to keep the data as small as possible in order to save space.
By the way: My computers setup is as following:
- Windows 7 64bit
- 32bit Python(x,y) 2.7.6
- MinGW's compiler
mingw32-gcc-4.8.1
. - Cython version 0.23.2
- Numpy version 1.10.0
I think it is not the same error as in the previous question since I updated Cython: Passing a structured numpy array with strings to a cython function
Edit
By compiling the module with distutils
there is the same error displayed. When executing the function, the above error occurs.
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize('CythonModule2.pyx'))
import CythonModule2 as cm2
print cm2.read_data()
Edit 2: Output files
There is no file called CythonModule2.pyd
. The created files (CythonModule2.c
, CythonModule2.def
, CythonModule2.o
) are stored in the following location:
C:\Users\Myself.pyxbld\temp.win32-2.7\Release\pyrex\