Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding Python into C++ application

Tags:

c++

python

boost

Context:

An ongoing problem we have been facing is unit testing our market data applications. These applications sit and observe data being retrieved from feeds and does something. Some critical events which are hard to trigger rarely occur and it is are difficult for the Testers to verify our applications perform correctly under all situations, hence we have to rely on unit tests.

These systems generally work by issuing callbacks (into our application) when an event has occurred, then our task to deal with this.

Solution I envision:

Is it possible to embed Python, or extend (not 100% clear on this), so that a tester could fire up a Python REPL and issue function calls that are akin to callbacks which are then handled by our C++ classes. Some form of dynamic manipulation of our objects at runtime.

like image 793
Mohamed Bana Avatar asked Feb 11 '11 17:02

Mohamed Bana


People also ask

Can you embed Python in C?

It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++.

How do I connect Python to C?

There a number of ways to do this. The rawest, simplest way is to use the Python C API and write a wrapper for your C library which can be called from Python. This ties your module to CPython. The second way is to use ctypes which is an FFI for Python that allows you to load and call functions in C libraries directly.

Can Python run as fast as C?

It Doesn't Do Ahead-Of-Time Compilation Because of the inherent dynamism of Python, it's impossible to compile Python into a standalone binary and reuse it. PyPy is a runtime interpreter that is faster than a fully interpreted language, but it's slower than a fully compiled language such as C.


2 Answers

I do something similar to this in one of my projects by using SWIG to generate python bindings for the relevant parts of the C++ code. Then I embed the interpreter as others have suggested. Having done that I can execute python code at will (e.g. PyRun_SimpleString), which can access C++ code. Normally I end up using something like a Singleton to make accessing specific C++ objects from python easier.

Also worth a mention is directors in swig python modules, which allow virtual functions to be handled much more intuitively. Depending on quite what you're doing you might find these very helpful.

like image 165
Flexo Avatar answered Oct 18 '22 23:10

Flexo


What you want to do is possible, though not trivial to get right. It sounds like you want to embed (rather than extend) Python. Both topics are covered in the tutorial here.

There's quite a lot of work in mapping from C++ classes to Python classes, and there are a number of things that can go wrong in subtle ways, particularly with memory leaks and multithreading (if your existing code is multi-threaded). However, if it's only for use in a testing situation and stability is not mission-critical then it might be less of a problem.

like image 37
Tim Martin Avatar answered Oct 18 '22 23:10

Tim Martin