Cython compilation error - local variable specified before assignment

I am new to Cython and try to speed up my kivy mobile game using Cython code in a critical AI module. My code is as follows:

import numpy as np
cimport numpy as np

 #not relevant parts

cdef np.ndarray posarr
cdef np.int poslast = 0
cdef np.int posidx = 0

def posarr_init(np.ndarray pawnpos, np.int act):
    poslast = 0
    # not relevant, but referencing poslast

 #not relevant, but including function where poslast is referenced (not assigned)

def consider_pawn(np.int x, np.int y):
    cdef np.int pact, posx, posy, resx, resy
    cdef np.int p
    cdef np.int found = 0
    #not relevant

    #in the code there is this line, posx and posy are local variables
                    posarr[poslast, posx, posy] = posarr[posidx, posx, posy]

Here Cython gives me this error at compile time:

Error compiling Cython file:

------------------------------------------------------------
...
                pact = 1
        if pact == 1:
            #pawn is active, create child position
            for posx in range(11):
                for posy in range(11):
                    posarr[poslast, posx, posy] = posarr[posidx, posx, posy]
                                 ^
------------------------------------------------------------

position.pyx:98:34: local variable 'poslast' referenced before assignment

I see that Cython compilation errors are reported in the same order as in the code. My questions:

Why does Cython consider poslast a local variable?

Why didn't he consider it a local variable in previous functions?

The generated C file is empty, it only has a message that it cannot be used internally. Is there a way to get Cython to leave C code in the file, even if there is a compilation error? Perhaps looking at this file will help me understand error messages (that is, why this variable is considered local) ...

+3
1

global poslast , .

Cython Python: , , , .

, poslast , ( object). .

posarr_init . poslast, .

+3

All Articles