CFFI 1.5 Embedding. A python class

77 views
Skip to first unread message

Artem Zhukov

unread,
Mar 11, 2016, 5:11:49 AM3/11/16
to python-cffi
Hello everyone,

I need to share a python class with a C++ application by the means of DLL. I found an example:


import cffi

ffi = cffi.FFI()

with open('plugin.h') as f:
ffi.embedding_api(f.read())

ffi.set_source("my_plugin", '''
#include "plugin.h"
''')

ffi.embedding_init_code("""
from my_plugin import ffi

@ffi.def_extern()
def do_stuff(p):
print("adding %d and %d" % (p.x, p.y))
return p.x + p.y
""")

ffi.compile(target="plugin-1.5.*", verbose=True)

But I don't know how to adopt it:
1. Instead of function I want to have a class
2. It would be better to refer to the separate *.py file, which contains the class instead of repeating it in the:
ffi.embedding_init_code("""
from my_plugin import ffi

@ffi.def_extern()
def do_stuff(p):
print("adding %d and %d" % (p.x, p.y))
return p.x + p.y
""")
3. I don't know what to write in plugin.h file

Armin Rigo

unread,
Mar 12, 2016, 4:12:18 AM3/12/16
to pytho...@googlegroups.com
Hi Artem,

On 11 March 2016 at 11:11, Artem Zhukov <green....@gmail.com> wrote:
> I need to share a python class with a C++ application by the means of DLL.

You'll need to give more examples about what you really want before we
can help. The CFFI interface is about exchanging C data and functions
with Python; it contains no built-in way to translate between C++
classes and instances and Python classes and instances. It is still
possible to do it, but you need to go via C.

So the first step is to design a C API for your C++ classes. This C
API goes into the .h file and possibly a .c file. This must be done
upfront without thinking about Python at all. Only then, you can
access this C API with CFFI. You need ffi.new_handle() to attach a
Python object to "void *" data, and store this "void *" data somewhere
inside the C API (maybe inside a new member in the C++ instances).
How to do it exactly depend entirely of the details of your use case.

If you only know C++, and not reasonably well C and Python, then it
can be a bit difficult. Maybe look at alternatives, like for example
PyPy's "cppyy" (there are others), that are direct C++-Python bridges.
The trade-off: CFFI is lower-level but lets you do anything; these
other C++-Python bridges are typically higher-level but assume some
programming model---which are often respected in C++.


A bientôt,

Armin.

Artem Zhukov

unread,
Mar 12, 2016, 5:53:06 AM3/12/16
to pytho...@googlegroups.com
Hello Armin,

Thank you for your help. We have a software written in C++. We need to integrate a new functionality into it. 
This functionality is presented as a python class (https://drive.google.com/open?id=0ByU11YTag4Hqbk1aaExBX1VLSms). So I need to somehow adopt this python class in a way that allow it easy integrate into our c++ software.
I was thinking to use a CFFI for that. I only know Python.
--
-- python-cffi: To unsubscribe from this group, send email to python-cffi...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/python-cffi?hl=en
---
You received this message because you are subscribed to the Google Groups "python-cffi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-cffi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Armin Rigo

unread,
Mar 12, 2016, 6:05:38 AM3/12/16
to pytho...@googlegroups.com
Hi,

On 12 March 2016 at 11:53, Artem Zhukov <green....@gmail.com> wrote:
> Thank you for your help. We have a software written in C++. We need to
> integrate a new functionality into it.
> This functionality is presented as a python class
> (https://drive.google.com/open?id=0ByU11YTag4Hqbk1aaExBX1VLSms). So I need
> to somehow adopt this python class in a way that allow it easy integrate
> into our c++ software.
> I was thinking to use a CFFI for that. I only know Python.

Ok. You have to know a bit of C in order to use CFFI.

Based on your class, what you want may be simply to pass some array of
floats (or "double" in C) to C++.

The basic idea of CFFI is not to expose Python classes to C/C++ code.
It's the other way around. The basic idea is to expose C to Python
code. So that's why I suggest to think about what you want from the
C++ side, in terms of C only, like a C function with an array of
"double" as argument, or a struct, etc.; and once that is decided, you
write some Python code using CFFI to export the data in the correct C
format and do the call to that function.

If you are looking for a way to expose a complete Python class to C++,
look for example at "cppyy", or other projects like Boost.


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages