C++ - Python API

M

Markus Kraus

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).
 
T

Thomas Jollans

So the feature overview:

First, the obligatory things you don't want to hear: Have you had a look at
similar efforts? A while ago, Aahz posted something very similar on this very
list. You should be able to find it in any of the archives without too much
trouble.
The most prominent example of this is obviously Boost.Python.
For C++ classes:
- "translating" it into a python object

How do you handle memory management ?
- 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*)

char*...
Unicode? Somewhere? wchar_t* maybe, or std::wstring? No? Also -- double? (I'm
just being pedantic now, at least double should be trivial to add)
General:
-runs on any platform and doenst need an installed python

Which platforms did you test it on? Which compilers did you test? Are you sure
your C++ is portable?
-runs in multithreaded environments (requires python > 2.3)

How do you deal with the GIL?
How do you handle calling to Python from multiple C++ threads?
-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

Again, have you tested other compilers?
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).

Just publish a bitbucket or github repository ;-)
 
M

Markus Kraus

Thanks for the answer

First, the obligatory things you don't want to hear: Have you had a look at
similar efforts? A while ago, Aahz posted something very similar on this very
list. You should be able to find it in any of the archives without too much
trouble.
The most prominent example of this is obviously Boost.Python.

I searched in Aahz posts but i didn't find anything related.
About Boost.Python: I worked with it but (for me) it seems more like
if it's meant to create pyd modules.

How do you handle memory management ?

As long as the c++ instanze itself exists, the python object is
existing too. If you delete the c++ instanze the python one is also
deleted (in a multithreaded environment you'll get a "This object has
already been deleted" error).
char*...
Unicode? Somewhere? wchar_t* maybe, or std::wstring? No? Also -- double? (I'm
just being pedantic now, at least double should be trivial to add)

I haven't worked too much on this yet but ill add support for all
common c++ types.

Which platforms did you test it on? Which compilers did you test? Are you sure
your C++ is portable?

My C++ code is not platform dependent so it should (haven't tested it
yet) be portable.

How do you deal with the GIL?
How do you handle calling to Python from multiple C++ threads?

Since python 2.3 there are the function PyGILState_Ensure and
PyGILState_Release functions which do the whole GIL stuff for you :).
Again, have you tested other compilers?

Dont have the ability for it (could need a linux guy who knows how to
create a makefile).
Just publish a bitbucket or github repository ;-)

Ill set up a googlecode site :p
 
A

Aahz

First, the obligatory things you don't want to hear: Have you had
a look at similar efforts? A while ago, Aahz posted something very
similar on this very list. You should be able to find it in any of the
archives without too much trouble.

You almost certainly have me confused with someone else -- I wouldn't
touch C++ with a ten-meter pole if I could possibly help it. (The last
time I dealt with C++ code, about a decade ago, I just rewrote it in C.)
 
T

Thomas Jollans

You almost certainly have me confused with someone else -- I wouldn't
touch C++ with a ten-meter pole if I could possibly help it. (The last
time I dealt with C++ code, about a decade ago, I just rewrote it in C.)

Sorry about that.
 
Joined
Sep 20, 2010
Messages
5
Reaction score
0
@Markus Kraus
Could you please upload your code somewhere I would like to test it and look at it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top