views:

225

answers:

3

I'm interested in experimenting with embedding Python in my application, to let the user run Python scripts within the application environment, accessing internal (C++-implemented) objects, etc. I'm quite new to this so don't know exactly what I'm doing.

I have read Embedding Python in Another Application, though this seems to talk only about a C API and flat C functions, not classes or objects (unless I've missed something) and its "Embedding Python in C++" section is only two sentences long. However, I also came across how to use boost::python and this looks excellent.

There's one problem: boost::python is not supported by C++ Builder 2010.

So, given this, what is the best approach for embedding Python in a C++ application compiled with C++ Builder 2010, and, using whichever technique is best, how do you expose / integrate classes and objects to give the Python coder access to the object-oriented internals of a program? Have I missed a standard approach? Is exposing internal classes or instantiated objects to Python as objects easy, or is the API truly C-style or flat / non-OO, and if so what's the best approach to mimic an underlying OO layer through such an API?

Note: I actually use RAD Studio, which includes both C++ Builder and Delphi. It may be possible to make use of some sort of Delphi-specific binding, but the ones I've encountered are six or seven years old, or are new-ish (Python 2.6) but don't seem to have any documentation and have comments in the issue list like "Anyone reads thiese [sic] comments anyway? Anyone working on this project?" which is not encouraging. But please feel free to include Delphi-specific answers especially if you think it's likely they'll work in a combined D+CB app. I appreciate all answers even if they aren't quite perfect - I can research, I just need pointers on where to go. A native C++ solution would probably be ideal, though, since using VCL-derived objects has its own limitations.

Thanks for your input!

+1  A: 

Is exposing internal classes or instantiated objects to Python as objects easy, or is the API truly C-style or flat / non-OO, and if so what's the best approach to mimic an underlying OO layer through such an API?

You have already answered yourself. The latter part of the sentence is correct.

Objects and classes do not exist in C++ as soon as you compile, only a few structures (vtables), and also another ones explaining some OO data, provided that RTTI is activated. That's why it is not possible to bridge the gap between Python and C++ using classes and objects.

You can build that surely by yourself, creating a set of C functions along with some data structures, and then an OO-layer. But you cannot do that out of the box.

For instance, class Car:

class Car {
public:
  int getDoors()
      { return this->doors; }
protected:
  int doors;
};

Is translated to:

struct Car {
    int doors;
};

int Car_getDoors(Car * this)
{
    return this->doors;
}

And a call to getDoors:

 c->getDoors()

Is translated as:

Car_getDoors( c )
Baltasarq
Thanks... I do know that's roughly how OO is usually implemented :) So you're basically saying to recreate an OO-like interface by passing this/self everywhere? Seems horribly clumsy :(
David M
Yes it is clumsy, though it is how is implemented. And yes, you should deal with all of that if you plan to build your own OO interface. There is no other choice apart of using another existing interface.
Baltasarq
Well, using another interface is what I'm asking about. I don't want to recreate Python / CPP hooks if at all possible - if there's an existing framework I can use with my compiler, that's what I'm after. If I have to recreate everything I'll simply use another language for which there's better support!
David M
+4  A: 

You should not be afraid of the P4D project at google groups. It seems inactive because, in part, it is very stable and full-featured already. Those components are used in the much more active PyScripter application which is one of the best python development editors currently available. PyScripter is written in Delphi and uses the P4D components. As such, it also presents a very comprehensive example of how to use the P4D components, although the examples provided with the P4D source checkout are already good enough to get started.

cjrh
Ok, thanks. I didn't realise from its website it's as stable as that.
David M
A: 

You can generate C++ to $SCRIPTLANG wrappers with swig.

Rudi