C++ - Python API

Markus Kraus kr4uzi at googlemail.com
Wed Sep 1 16:14:51 EDT 2010


Hi guys i worked on this for severl days (or even weeks?!) now, but im
nearly finished with it: A complete C++ to Python API which allows you
to use python as a scripting language for your C++ projects. Simple
example:
--- python code ---
def greet( player ):
    print( "Hello player " + player.getName() + " !" )
------
--- c++ code ---
class CPlayer {
    REGISTER_CLASS( CPlayer, CLASS_METHOD("getName", GetName) )
private:
    string m_Name;
public:
    CPlayer( string nName )
    {
        m_Name = nName;
        INITIALIZE("player");
    }

    string GetName( )    { return m_Name; }
};
------
If you call the python function (look into the example in the project
to see how to do this) this results in ( assume you have
CPlayer("myPlayerName") ) "Hello player myPlayerName!".

So the feature overview:
For C++ classes:
- "translating" it into a python object
- complete reflexion (attributes and methods) of the c++ instance
- call c++ methods nearly directly from python
- method-overloading (native python doesnt support it (!))

Modules:
- the API allowes to create hardcoded python modules without having
any knowledge about the python C-API
- Adding attributes to the module (long/char*/PyObject*)

General:
-runs on any platform and doenst need an installed python
-runs in multithreaded environments (requires python > 2.3)
-support for python 3.x
-no need of any python C-API knowledge (maybe for coding modules but
then only 2 or 3 functions)
-the project is a VC2010 one and there is also an example module +
class

If there is any interest in testing this or using this for your own
project, please post; in that case i'll release it now instead of
finishing the inheritance support before releasing it (this may take a
few days though).



More information about the Python-list mailing list