title: Pass c function as argument to python function in cython
body: <p>I have the following code which works:</p>
  
  <pre><code>%%cython 
  
  cdef int add(int  a, int b):
      return a+b
  
  cdef int mult(int  a, int b):
      return a*b
  
  ctypedef int (*function_type)(int  a, int b)
  
  cdef int lambda_c(function_type func, int c, int d):
      return func(c,d)
  
  print(lambda_c(add, 5, 6))
  print(lambda_c(mult, 5, 6))
  </code></pre>
  
  <p>So I have <code>lambda_c</code> function that takes c function as an argument and I am not able to change it (as it it a wrapper over c library that is supported by another team). </p>
  
  <p>What I want to do is to write a wrapper:</p>
  
  <pre><code>cdef class PyClass:
      def py_wrap(func, e, f):
          return lambda_c(func, e, f)
  
  print(PyClass().py_wrap(add, 5, 5))
  print(PyClass().py_wrap(mult, 6, 6))
  </code></pre>
  
  <p>But this throws an error:</p>
  
  <blockquote>
    <p>Cannot convert Python object to 'function_type'</p>
  </blockquote>
  
  <p>I also tried to cast func(<code>return lambda_c(&lt;function_type&gt;func, e, f)</code>) but got an error:</p>
  
  <blockquote>
    <p>Python objects cannot be cast to pointers of primitive types</p>
  </blockquote>
  
  <p><strong>The idea</strong> behind this is following: any user will be able to write his own function in cython, compile it and then import and pass his function to <code>PyClass().py_wrap</code> method. </p>
  
  <p>Is it even possible to import pure c function and pass it as a parameter with cython?</p>
  
  <p>I also saw <a href="https://stackoverflow.com/questions/46038154/pass-cython-functions-via-python-interface">Pass cython functions via python interface</a> but unlike the solution there I am not able to change <code>lambda_c</code> functon and turn it into a class. Moreover <code>lambda_c</code> takes only functions of certain type (<code>function_type</code> in our example)</p>