Do locally set Cython compiler directives affect one or all functions?
<p>I am working on speeding up some Python/Numpy code with Cython, and am a bit unclear on the effects of "locally" setting (as defined <a href="http://docs.cython.org/en/latest/src/reference//compilation.html" rel="nofollow">here</a> in the docs) compiler directives. In my case I'd like to use:</p>

<pre><code>@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) #turn off bounds-checking
</code></pre>

<p>I understand that I can globally define this in my <code>setup.py</code> file, but I'm developing for non-Cython users and would like the directives to be obvious from the <code>.pyx</code> file.</p>

<p>If I am writing a <code>.pyx</code> file with several functions defined in it, need I only set these once, or will they only apply to the next function defined? The reason I ask is that the documentation often says things like "turn off <code>boundscheck</code> for this function," making me wonder whether it only applies to the next function defined.</p>

<p>In other words, do I need to do this:</p>

<pre><code>import numpy as np
cimport numpy as np
cimport cython

ctypedef np.float64_t DTYPE_FLOAT_t

@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc1(np.ndarray[DTYPE_FLOAT_t] a):
    do things here

def myfunc2(np.ndarray[DTYPE_FLOAT_t] b):
    do things here
</code></pre>

<p>Or do I need to do this:</p>

<pre><code>import numpy as np
cimport numpy as np
cimport cython

ctypedef np.float64_t DTYPE_FLOAT_t

@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc1(np.ndarray[DTYPE_FLOAT_t] a):
    do things here

@cython.wraparound (False) #turn off negative indexing
@cython.boundscheck(False) # turn off bounds-checking
def myfunc2(np.ndarray[DTYPE_FLOAT_t] b):
    do things here
</code></pre>

<p>Thanks!</p>

<p>The manual does not say explicitly, but these directives are using <a href="https://www.python.org/dev/peps/pep-0318/" rel="nofollow">decorator notation</a>.  In plain Python,</p>

<pre><code>@decorator2
@decorator1
def fn(args):
    body
</code></pre>

<p>is syntactic sugar for</p>

<pre><code>def fn(args):
    body
fn = decorator2(decorator1(fn))
</code></pre>

<p>So my <em>expectation</em> is that the directives work like that, which would mean that they apply only to the next function.</p>

<p>The Cython manual also says that the compiler directives can be used in <code>with</code> statements.  What it <em>doesn't</em> say, unfortunately, is whether those can appear at top level:</p>

<pre><code>with cython.wraparound(False), cython.boundscheck(False):

    def myfunc1(np.ndarray[DTYPE_FLOAT_t] a):
        do things here

    def myfunc2(np.ndarray[DTYPE_FLOAT_t] b):
        do things here
</code></pre>

<p>might be what you're looking for, or then again it might be a syntax error or a no-op.  You're going to have to try it and see.</p>

python numpy cython