Bind C++ program for use with both Python 2.x and 3.x

P

Peter C.

Hello, I am looking at the possibility of making a program in C++. The
catch is it will require the ability to work with binding for use with
scripting in both Python 2.x and 3.x for various tool plugins. Is
there any way to bind a C++ app to work with both Python 2.x and 3.x
using the Python C API? Note if I could I'd just do Python 3, however
I need Python 2 support to allow for the use of this application as a
plugin in apps that use Python 2 as well.
 
M

Martin v. Loewis

Am 11.12.2010 23:41, schrieb Peter C.:
Hello, I am looking at the possibility of making a program in C++. The
catch is it will require the ability to work with binding for use with
scripting in both Python 2.x and 3.x for various tool plugins. Is
there any way to bind a C++ app to work with both Python 2.x and 3.x
using the Python C API? Note if I could I'd just do Python 3, however
I need Python 2 support to allow for the use of this application as a
plugin in apps that use Python 2 as well.

Notice that binding to Python 2 may not be enough: you also need to
specify the Python 2 version (i.e. different bindings for 2.5, 2.6, and
2.7, say). You will have to ship different copies of the binding. Of
course, you can ship them in a single distribution (zip file, or
whatever your distribution format is).

If you are creating different copies of the binding, supporting both
2.x and 3.x simultaneously will be straight-forward.

Regards,
Martin
 
S

Stefan Behnel

Peter C., 11.12.2010 23:41:
Hello, I am looking at the possibility of making a program in C++. The
catch is it will require the ability to work with binding for use with
scripting in both Python 2.x and 3.x for various tool plugins.

Do I read this right that you want your program to be written in C++, and
use an embedded Python interpreter to provide scripting capabilities?

Is there a reason you'd want to write the application itself in C++ in the
first place? If it's new code, it's likely faster to write it Python
directly. Usually, that also leads to a lot less code, which helps in
keeping the code more maintainable.

Is
there any way to bind a C++ app to work with both Python 2.x and 3.x
using the Python C API? Note if I could I'd just do Python 3, however
I need Python 2 support to allow for the use of this application as a
plugin in apps that use Python 2 as well.

The easiest (and fastest etc.) way to do that is to write the glue code in
Cython. It generates portable C/C++ code that supports Python 2.3 to 3.2.
However, if you ship binaries, you will still need to provide them for the
different Python versions (even for different Py2.x versions), as the
runtimes are not binary compatible.

Stefan
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top