How to port Python code into C++ code automatically?

P

Paddy

I want to port a Python project (about 10,000 line python code) to C+
+. Is there any automatically tool to do this kind of things? e.g.,
SWIG(http://www.swig.org/)?

Any comment is welcome!

Thanks!

There isn't a magic porting tool available.
If you said more about the why and the resources available then you
might get a better answer.

- Paddy.
 
J

Jeff Schwab

Bo said:
I want to port a Python project (about 10,000 line python code) to C+
+. Is there any automatically tool to do this kind of things? e.g.,

That's not trivial. Python is very heavily oriented toward run-time
processing, whereas C++ favors compile-time processing.

Swig isn't so much a translator as a way of linking C or C++ code to
multiple different scripting languages, using shared configuration (.i)
files. If you're only connecting two languages (Python and C++), Swig
is probably not going to help you much unless you happen to already be a
master of Swig-foo.

If the Python code works, then it's probably best to keep it in Python.
To access it from C++, try Boost.Python. If you really do need to
translate the code from Python to C++, try to do it incrementally. If
the Python code is so monolithic that you can't find individual pieces
to move one-at-a-time, consider refactoring the code in Python so that
you'll at least have some modularity to work with. (In the process of
refactoring, you may also find that you don't have to move the code out
of Python, after all, e.g. because the performance improves along with
the design.)
 
M

Marc 'BlackJack' Rintsch

I want to port a Python project (about 10,000 line python code) to C+
+. Is there any automatically tool to do this kind of things? e.g.,
SWIG(http://www.swig.org/)?

Any comment is welcome!

Have a look at the ShedSkin Python-to-C++ compiler:

http://shed-skin.blogspot.com/

It has some restrictions, i.e. the code has to be written in a more or
less statically typed way.

And me too is interested in why you want to port the entire project instead
just the time critical parts? ShedSkin might help here too. As Cython_
or Pyrex_ do. Both compile a subset of Python with optional static typing
to C extension modules.

... _Cython: http://www.cython.org/
... _Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/

Ciao,
Marc 'BlackJack' Rintsch
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top