title: combine multiple decorators in cython
body: <p>I found this interesting question on SO to <a href="https://stackoverflow.com/questions/5409450/can-i-combine-two-decorators-into-a-single-one-in-python">combine multiple decorators in python to a single decorator</a>. </p>
  
  <p>I wanted to do the same thing in Cython. Typically, I have Cython codes that look like:</p>
  
  <pre><code>@cython.boundscheck(False)
  @cython.wraparound(False)
  @cython.cdivision(True)
  cdef ar[dtype_t, ndim=2] sma_vec(ar[dtype_t, ndim=2] x, int m):
      cdef int n
      cdef Py_ssize_t i, j
      ...
  </code></pre>
  
  <p>or like</p>
  
  <pre><code>@cython.boundscheck(False)
  @cython.wraparound(False)
  @cython.cdivision(True)
  cpdef ar[dtype_t, ndim=2] sma_vec(ar[dtype_t, ndim=2] x, int m):
      cdef int n
      cdef Py_ssize_t i, j
      ...
  </code></pre>
  
  <p>and I tend to repeat these three decorators <code>@cython.boundscheck(False)</code>, <code>@cython.wraparound(False)</code>, <code>@cython.cdivision(True)</code> pretty much everywhere.</p>
  
  <p>The solution for regular Python given in that page is </p>
  
  <pre><code>def composed(*decs):
      def deco(f):
          for dec in reversed(decs):
              f = dec(f)
          return f
      return deco
  </code></pre>
  
  <p>For Cython, I tried to do the following:</p>
  
  <pre><code>cdef composed_for_cdef(*decs):
       cdef deco(f):
            for dec in reversed(decs):
                f = dec(f)
             return f
        return deco
  </code></pre>
  
  <p>and</p>
  
  <pre><code>cpdef composed_for_cpdef(*decs):
       cpdef deco(f):
            for dec in reversed(decs):
                f = dec(f)
             return f
        return deco
  </code></pre>
  
  <p>but I got an error during compilation:</p>
  
  <pre><code>cdef composed_for_cdef(*decs):
      cdef deco(f):
                 ^
  ------------------------------------------------------------
  
  stat\movavg.pyx:12:16: C function definition not allowed here
  </code></pre>
  
  <p>I even tried the solution for regular Python (given above), but I got an error:</p>
  
  <pre><code>@composed(cython.boundscheck(False), cython.wraparound(False), cython.cdivision(True))
  ^
  ------------------------------------------------------------
  
  stat\movavg.pyx:24:0: Cdef functions/classes cannot take arbitrary decorators.
  </code></pre>