How to cast a python function to a C function pointer? [duplicate] - cython

Hello i've been trying to call a python user-defined callback from c++ using cython for a while. But it looks like it's impossible without changes on the c++ side or a static function buffer.
So, is there only one option for binding a propper callback (ctypes with CFUNCTYPE)?
Cython 0.29.23
A.hpp:
typedef void (*Callback) ();
class A{
Callback callback;
public:
A(){
this->callback = nullptr;
}
void set_callback(Callback callback){
this->callback = callback;
}
void call_callback(){
this->callback();
}
};
A.pxd:
cdef extern from "A.hpp":
ctypedef void (*Callback) ()
cdef cppclass A:
A() except +
void set_callback(Callback callback)
void call_callback()
B.pyx
from A cimport A, Callback
cdef class B:
cdef A *c_self
cdef object callback
def __cinit__(self):
self.c_self = new A()
def __dealloc__(self):
del self.c_self
cdef void callback_func(self) with gil:
print("I'm here")
self.callback()
def set_callback(self, callback):
self.callback = callback
self.c_self.set_callback(<Callback>self.callback_func)
def call_callback(self):
self.c_self.call_callback()
def print_():
print("hello")
b = B()
b.set_callback(print)
b.call_callback()
Output:
I'm here
[segmentation fault]
Looks like ctypes: get the actual address of a c function is a good one work-around, but it uses ctypes.
It scares me, but works:
B.pyx
from A cimport A, Callback
import ctypes
from libc.stdint cimport uintptr_t
cdef class B:
cdef A *c_self
cdef object callback
def __cinit__(self):
self.c_self = new A()
def __dealloc__(self):
del self.c_self
def set_callback(self, callback):
f = ctypes.CFUNCTYPE(None)(callback)
self.callback = f
cdef Callback c_callback = (<Callback*><uintptr_t>ctypes.addressof(f))[0]
self.c_self.set_callback(c_callback)
def call_callback(self):
self.c_self.call_callback()
def hello():
print("hello")
b = B()
b.set_callback(hello)
b.call_callback()

A function pointer does not have any space to store extra information. Therefore it is not possible to convert a Python callable to a function pointer in pure C. Similarly a cdef function of a cdef class must store the address of the instance to be usable and that is impossible too.
You have three options:
Use ctypes as in https://stackoverflow.com/a/34900829/4657412 (the bottom half of the answer shows how to do it). ctypes accomplishes this with runtime code generation (and thus only works on processors that it explicitly supports).
Use a std::function in C++ instead of a function pointer. These can store arbitrary information. You need to write an object wrapper (or re-use one from elsewhere) so it isn't completely pure Cython. See Pass a closure from Cython to C++. What you're trying to do is probably better covered by How to use a Cython cdef class member method in a native callback.
Use the class C scheme where the callback is of type
void (CallbackT)(/* any args */, void* user_data)
and is registered with:
void register_callback(CallbackT func, void* user_data)
In this case user_data would be the address of your B instances (and you'd need to make sure it was Py_INCREFed before setting the address and Py_DECREFed before unsetting the address). Cython callback with class method provides an example.

Related

Should the reference count of PyObject* created by a cdef public function be manually decreased

I want to export a python class to be used in C++:
class Object:
def method(self):
pass
cdef public object create_object() with gil:
return Object()
cdef public void py_method(object obj) with gil:
obj.method() # obj is expected to have method()
Then the generated header file will have the following functions:
__PYX_EXTERN_C PyObject *create_object(void);
__PYX_EXTERN_C void py_method(PyObject *);
My question is: should I also have a function like
from cpython cimport Py_XDECREF, PyObject
cdef public void destroy_object(PyObject* obj) with gil:
Py_XDECREF(obj)
to avoid memory leak?
Then these functions may be used in C++ code like:
auto obj = create_object()
py_method(obj)
// Finally
destroy_object(obj)
Yes you should decrement it when calling it from C++. You could do it the way you propose or you could skip the definition of destroy_object and just use Py_XDECREF in C++.
Other things you should consider:
create_object can return NULL if it's raised a Python exception so check for that
RAII wrappers are always a good idea (to handle the reference counting)

How to call a cpdef method from C

I have a public class with a method defined with cpdef. According to cython documentation, this can be called from C. However the generated header does not have any C/C++ functions/method generated for this.
cdef public class Model[type ModelType, object Model]:
cdef ModelContext *modelContext
cpdef calculate(self):
print 'calculate called on Modell'

cython: how to declare a cdef function that has no return

when I declare a cdef function that returns a double I write cdef double method_name(...). If it does not return something and I just omit it to cdef method_name(...) then cython --annotate marks it yellow. How to declare that the method/function does not return anything?
cdef void method_name(...) crashes with a segmentation fault
cdef None method_name(...) -> 'None' is not a type identifier
--annotate marks it as yellow as cython assumes the return type to be a python object if you omit the return type annotation (Cython Language Basics).
Specifying void as return type works for me. It's also used in quite some of the official examples, Just make sure not to return anything.
For me (cython 0.21.1) defining the c function with void works:
# mymod.pyx file
cdef void mycfunc(int* a):
a[0] = 2
def myfunc():
cdef int a = 1
mycfunc(&a)
print(a)
The c function is not yellow in the annotated html file and
python -c 'from mymod import myfunc; myfunc()'
prints 2 as expected.
A bug in Cython version 0.22. Update to 0.23.4 solved it.

Wrapping a c++ singleton with cython

Using Cython to wrap some parts from a C++ API to python, I'm having misunderstandings that I couldn't fix by search similar questions. I like to access a Factory class that has the constructors private and a public method GetInstance.
namespace cpplibrary
{
class CppFactory
{
public:
static CppFactory& GetInstance();
private:
CppFactory(void);
CppFactory( const CppFactory& );
};
}
I'm trying a cython code like:
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
CppFactory() except +
CppFactory& GetInstance()
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = GetInstance()
I've check with as many variants as I thought. With and without the declaration of the constructor in the extern. With different ways to define the _thisptr from different posted examples. And so on. But I couldn't find a singleton example like this.
Where is the mistake I can't see?
I think you've got two issues:
1) How to wrap static methods: the recommended way is to declare the function outside the class, and use a string to tell Cython what name it should use in C++
2) The assignment to thisptr, where you just need to use & to get a pointer from the reference.
Code:
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
CppFactory() except +
# declare outside the class and use a string to specify name
# unfortunately this doesn't seem to play well with namespace
# so we need to specify it again
cdef CppFactory& CppFactory_GetInstance "cpplibrary::CppFactory::GetInstance"()
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = &CppFactory_GetInstance()
One of the problem is indeed to wrap a static method (here Getinstance()).
You can do that by using the cython #staticmethod decorator (see Cython's doc here):
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
#staticmember
CppFactory& GetInstance()
Then, the .pyx can be:
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = new CppFactory()

c++ class in fused type

I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have:
cdef cppclass FooImpl1:
FooImpl1()
int foo()
cdef cppclass FooImpl2
FooImpl2()
int foo()
I wonder if I can write something like this in pyx python wrapper:
ctypedef fused FooImpl:
FooImpl1*
FooImpl2*
cdef class Foo:
cdef FooImpl impl
def __cinit__(self, int selector):
if selector == 1:
self.impl = new FooImpl1()
else:
self.impl = new FooImpl2()
def func(self):
# depending on the object stored in impl FooImpl2::foo or FooImpl1::foo
# will be called
return self.impl.foo()
Is there a way to accomplish expected behavior? FooImpl1 and FooImpl2 don't share abstract interface, they are template specializations of a class.
As of this version (0.20), Cython doesn't support fused types in classes, only in function parameters and variables. Here are the docs.