views:

46

answers:

1

Due to refactoring/reworking on a controller I've had to embed a Python Interpreter inside a C application. I can now call python functions and pass/get Objects into Python fine.

The python code is a controller for a robot (currently simulated), this now needs make use of some C code for comparisons I'm making.

Previously the Python code created objects, read sensors, ran control code and wrote the outputs to motors. All of this except the control code now needs to be done in C. The problem I have is that Objects which are created in an init function (in python) which, when I come to run the control code no longer exist.

What is the best way to solve this? My idea was to return the controllers from the init function and store references to them in the C, passing the reference to the controller each time it is called.

Thanks for any help.

A: 

This may not be the answer you want, but there are ways of working with C and Python other than embedding an interpreter inside a C application.

Namely, why don't you do the opposite? Create C libraries for Python? You can control the general flow of your application in Python, which is much more comfortable, and call C code whenever you see fit.

Again, I'm not really addressing your actual question, so feel free to ignore me.

Santiago Lezica
Yes, I think that generally python code is written purely in python, then performance bottlenecks are moved to C modules. Unless the author needs some very low level interfaces to the underlying OS, I don't see the need for C.
Novikov
I need to make use of a considerable code base that is written in C. It is another controller Im using in research for comparison. It is bigger and has more libraries, attachments and all sorts while the Python is fairly standalone. Hence I thought it would be easier to call the python bit from C rather than attempt to get the C working from python - the only problem I have is the one of object persistence detailed above.
NBenatar
I'd still say it's easier for the interpreter to call C code, than for C code to leak things into an embedded interpreter. Why don't you wrap the libraries with PyObjects?
Santiago Lezica