C/C++ Python interpreter - c++

What I'm trying to do is write an app in C/C++ which will allow users to enter a Python script, which the app will then interpret & run. The aforementioned script will have a separate API that I'll implement in C, then expose to the user.
Is there a way to do this? I've searched on Google, but I've only found ways to create 'extensions' and not actual interpreters (like Lua allows).

Documentation about Embedding Python in Another Application says:
The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. 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++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.
Look especially into Extending Embedded Python:
Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application.
Just read the docs I referenced and you should be able to implement Python interpreter within your app written in C.

You can do a call from C++ to Python and vice versa using Boost Python

If you're interested I recently implemented embedded Python scripting for C++ from first principles. I created a C++ Python wrapper library called ECS:Python (Embedded C++ Scripting with Python) that allows you to expose object from a C++ application to an embedded Python interpreter for interactive scripting. It's light-weight and very easy to use (and free!).
http://sourceforge.net/projects/ecspython

Related

How to provide Scripting support for Qt-Applications?

I'm looking for a scripting language which can be integrated into my Qt5 Application. The application has a public api, which can be used to extend the application with plugins. Now I want to add a scripting language to the application which provides access to the whole public api.
The scripting language must fulfill the following Requirements:
Script Code can be executed from within the QT-Application.
The user can access the file-system, network and create graphical elements from the scripting language.
The user can access the public api of my QT Application through bindings.
There should be a generator available to automatically generate script-language bindings for my public api.
For classes that are part of the Public Api, it should be possible to pass around objects between the QT-Application and the Scripting Engine.
I evaluated the following Script-Languages:
Qt-Script, together with the QT-Script Generator.
The scripting language is based on ECMAScript/Javascript and can easily be integrated into QT apps. This fulfills all my requirements and works as expected. The generator can be used to generate bindings for the QT-Api itself and to generate bindings for the public api of my application. Sadly the qt-script module is going to be deprecated with qt5.5 and the scriptgenerator is no longer maintained.
Python
There seem to be several python-qt bindings available.
Pyside would probably be ok, but it seems to be inactive as well. Apart from that I would have to embed python into c++, which is not supported by pyside out of the box, but could be done by the python c api.
What scripting-languages and tools do you suggest, that fulfills all my requirements?
SWIG with Python seems to be a good choice. SWIG is still actively maintained.
Although SWIG doesn't fulfill all of my requirements out of the box, it shouldn't be that a big thing to make all of them work:
Script Code can be executed from within the QT-Application.
This is not supported out of the box. You have to embed a python interpreter into your application. https://docs.python.org/2/extending/embedding.html
The user can access the file-system, network and create graphical elements from the scripting language.
Accessing the filesystem and network should not be a problem with python. To create graphical userinterfaces, there are a lot of libraries available:
https://wiki.python.org/moin/GuiProgramming
The user can access the public api of my QT Application through bindings.
There should be a generator available to automatically generate script-language bindings for my public api.
This is done by SWIG. They provide great C++ and c++11 support.
http://www.swig.org/Doc3.0/SWIGPlus.html
http://www.swig.org/Doc3.0/CPlusPlus11.html
For classes that are part of the Public Api, it should be possible to
pass around objects between the QT-Application and the Scripting
Engine.
This is possible using the c++ functions provided by swig:
SWIG_TypeQuery gets you information about C++ types
SWIG_NewPointerObj converts a c++ object to a python (proxy) object
SWIG_ConvertPtr converts a python (proxy) object back to c++ object
More info in the External runtime chapter

Using C compiled code from python GUI

I am writing an application where I am coding all of my front-end and GUI with python library (wxpython specifically). For this application, I would like to write the model class with C and use python to use the compiled C code? How can this be implemented in python?
I know this is little vague question but I am struggling with the starting point.
If you're using CPython (the most popular version of Python), you'll need to learn the CPython C API. Other python implementations may or may not support C calls. You can also use the ctypes library which is easier to learn, but also more rigid and may not support everything you need.
You can try Cython, which is basically Python with embedded support for C types and calls (it goes through the CPython API).
You could strictly seperate design(python part) and code(c++ part) like this:
Write a complete c++ programm that works in the terminal/console and then make the python-application call these c++-terminal programm via os.Popen.
So if your programm is a calculator it does this:
(python gui) 5 + 5 -> my_c_programm.exe "5 + 5" -> (returns) 10 -> (python gui) display
that way you can use your programm with and without gui.
Its easier and faster than embedding Python in your c++ programm or extending Python with C++.
I basically do the same thing on my current project, but like this:
php: my webinterface
python: for structure and logic and easy operation
c++: for heavy calculations and where I need speed
so php -> python -> c++
and it works very well for me :)

Embedded Python Module Configuration

I'm looking at adding Python support to my game engine project, with the intent of using it as a scripting language for much of the game logic and world building. I'd like to expose a lot of the C++ code to Python. I've already gone through the relevant documentation on the Python site, and I'm fairly confident that I understand the basic requirements for embedding Python and communicating back and forth between Python and the core engine components.
I'm going to embedding Python into the engine itself, rather than compiling the engine as a Python module, since one of my target platforms is the iPhone.
One question I haven't been able to answer myself is how to introduce structure to the C++ modules. For example, I'd like to be able to do something along the lines of:
from engine.scene import skyobject
import engine.core.platforminfo
Would you just create a single "engine" module, and then add some sort of sub module for scene, core, etc? Or should there be separate modules for each of the exposed components, i.e. defining the skyobject module with the name "engine.scene.skyobject"?
Any insight on this is appreciated.

Adding python script to c++ project

How would I go about adding a script written in python to a c++ project? Thanks
Edit: Basically all the script does is email some data. I would like to pass the data and maybe the email address to a function written in python. Hope that clears things up..
You could look at Boost.Python which is a "a C++ library which enables seamless interoperability between C++ and the Python programming language."
You have to be more specific, though.
You may be interested in Boost.Python: Embedding the Interpreter, or Python/C API: Embedding the Python Interpreter. You can either use the Python C APIs directly or use the Boost.Python library as you so choose. You might also be interested in reading Embedding Python in Your C Programs which walks you through it.
The most primitve solution would be to use the system command to call you script, but that does limit your control over it to setting environment variables and passing parameters.
system("myscript.py param1 param2")

How does Gedit expose its api to python for plugins?

I'm starting a medium (academic) project in C++ for which I need users to be able to write small scripts, which interact directly with the main program. My first thought as an aproach to this was to make something like Gedit does with it's plugins (in fact I thought about it because it is something very similar to what I need to do.)
I do have some experience writting plugins for geddit, but zero experience in writting a plugin framework.
Would it be really difficult to me to write one similar to gedit's? (i mean, the way it exposes its API to python, and then loads the python plugin and calls its methods). Can anyone point me in the right directions or teach me a little if you have experience with it?
Fortunately, gedit's plugin framework can be used. You could use Ethos, which is the same plugin framework gedit uses, only without gedit.