1

Two functions: one which creates a 32-million integer array and another which uses it for a lookup millions of times.

from cpython cimport array as c_array
from array import array

def get_lut(file_location):
    cdef c_array.array lut
    with open(file_location, "rb") as f:
        lut = array("I", f.read())
    return lut

def use_lut(int[:] lut):
    // Do a whole bunch of lookups.

Compiling with the following command:

c:\Users\Michael\Documents\Poker>set DISTUTILS_USE_SDK=1
c:\Users\Michael\Documents\Poker>python setup.py build_ext -i --compiler=msvc

Produces the following output:

Compiling handeval.pyx because it changed.
Cythonizing handeval.pyx
warning: View.MemoryView:288:5: Cannot profile nogil function.
warning: View.MemoryView:763:5: Cannot profile nogil function.
warning: View.MemoryView:899:5: Cannot profile nogil function.
warning: View.MemoryView:1063:5: Cannot profile nogil function.
warning: View.MemoryView:1070:5: Cannot profile nogil function.
warning: View.MemoryView:1124:5: Cannot profile nogil function.
warning: View.MemoryView:1131:5: Cannot profile nogil function.
warning: View.MemoryView:1142:5: Cannot profile nogil function.
warning: View.MemoryView:1163:5: Cannot profile nogil function.
warning: View.MemoryView:1223:5: Cannot profile nogil function.
warning: View.MemoryView:1295:5: Cannot profile nogil function.
warning: View.MemoryView:1317:5: Cannot profile nogil function.
warning: View.MemoryView:1352:5: Cannot profile nogil function.
warning: View.MemoryView:1362:5: Cannot profile nogil function.
running build_ext
building 'handeval' extension
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64\cl.exe /c /nolo
go /Ox /MD /W3 /GS- /DNDEBUG -IC:\Users\Michael\Anaconda3\include -IC:\Users\Mic
hael\Anaconda3\include /Tchandeval.c /Fobuild\temp.win-amd64-3.4\Release\handeva
l.obj
handeval.c
handeval.c(2391) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'long',
 possible loss of data
handeval.c(2420) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(2449) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(2478) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(2507) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(2536) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(2565) : warning C4244: '+=' : conversion from 'Py_ssize_t' to 'int',
possible loss of data
handeval.c(12955) : error C2275: 'PyGILState_STATE' : illegal use of this type a
s an expression
        C:\Users\Michael\Anaconda3\include\pystate.h(191) : see declaration of '
PyGILState_STATE'
handeval.c(12955) : error C2146: syntax error : missing ';' before identifier '_
_pyx_gilstate_save'
handeval.c(12955) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13026) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13052) : error C2275: 'PyGILState_STATE' : illegal use of this type a
s an expression
        C:\Users\Michael\Anaconda3\include\pystate.h(191) : see declaration of '
PyGILState_STATE'
handeval.c(13052) : error C2146: syntax error : missing ';' before identifier '_
_pyx_gilstate_save'
handeval.c(13052) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13125) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13152) : error C2275: 'PyGILState_STATE' : illegal use of this type a
s an expression
        C:\Users\Michael\Anaconda3\include\pystate.h(191) : see declaration of '
PyGILState_STATE'
handeval.c(13152) : error C2146: syntax error : missing ';' before identifier '_
_pyx_gilstate_save'
handeval.c(13152) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13242) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13909) : error C2275: 'PyGILState_STATE' : illegal use of this type a
s an expression
        C:\Users\Michael\Anaconda3\include\pystate.h(191) : see declaration of '
PyGILState_STATE'
handeval.c(13909) : error C2146: syntax error : missing ';' before identifier '_
_pyx_gilstate_save'
handeval.c(13909) : error C2065: '__pyx_gilstate_save' : undeclared identifier
handeval.c(13935) : error C2065: '__pyx_gilstate_save' : undeclared identifier
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\Bin\\
amd64\\cl.exe' failed with exit status 2

Whereas removing the int[:] type declaration from the use_lut signature compiles fine.

What am I doing wrong here?

4
  • Your code is missing following imports: from cpython cimport array as c_array and from array import array - If I replace // Do a whole bunch of lookups. by pass, your code compiles fine on OSX
    – cel
    Apr 6, 2015 at 12:54
  • Apologies, cut those off the code I copied/pasted. They are in there.
    – MikeRand
    Apr 6, 2015 at 12:55
  • It might be worth running cython handeval.pyx (to generate the C file) and then adding line 12955 of handeval.c (and a few of the lines around it for context) to your question. I suspect it will still be a challenge to diagnose though...
    – DavidW
    Apr 6, 2015 at 16:22
  • do you have to use array.array? I've done similar examples with np.array(data, dtype=np.int32) May 3, 2015 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.