5

I have been trying to find a way to convert .py source file to .cpp source (as a time saver from doing it manually). I've never used python before and was hoping for a quick way to convert it, and cleanup any code the converter might not do well.

So far, some of the options that I have found while googling seem to be: nuitka, cython, and pypy/rpython.

However, the documentation I have read only seem to produce executables, and not actual source code.

At this point, I have found py2c, but cannot seem to find any documentation on how to use it. Also, judging by the posted roadmap on the wiki, it does not seem to be a finished product, and so I'm doubtful as to its reliability.

If you can provide other sources on how this can be accomplished, or shed some light on something I may have missed on the above-mentioned possibilities, it would be appreciated. Otherwise, I will simply convert it manually.

Community
  • 1
  • 1
quandrei
  • 204
  • 1
  • 3
  • 10
  • 4
    I think you probably have to stick to the "Otherwise, I will simply convert it manually" part. – root Mar 08 '13 at 20:58
  • 1
    can't you convert the python to php, then just apply the facebook thingie to convert the php to c++ :-P – Cheers and hth. - Alf Mar 08 '13 at 21:02
  • @Cheersandhth.-Alf Your method seems simpler. – crayzeewulf Mar 08 '13 at 21:03
  • 1
    How much code? Might be quicker to just learn Python. – qxn Mar 08 '13 at 21:04
  • 3
    What is your motivation in doing the conversion? Does the resulting code need to be human-readable and human-maintainable? – Robᵩ Mar 08 '13 at 21:07
  • 1
    @Cheersandhth.-Alf Not only is the PHP-to-C++ compiler discontinued in favor of a JIT compiling VM, its output is not usable for *anything* except handing it off to a C++ compiler. The same is true for most compilers really. And while Python->PHP seems a bit less "abs-freaking-olutely infeasible" than Python->C++, I'm not aware of any automated tool doing this well (and keep thinking of reasons why it's hard at the rate of hundreds per minute). –  Mar 08 '13 at 21:07
  • @Robᵩ The resulting code does need to be human-readable/maintainable, and added to a VC++ project. The decision was taken to not keep it in its python form. I had some hope for this when I found this: [py2c](https://code.google.com/p/py2c/) but judging by the responses, probably not a good idea. I will just stick to re-writing it. – quandrei Mar 08 '13 at 21:33
  • Would you consider [embedding Python](http://docs.python.org/3/extending/embedding.html) in your application? – Peter Wood Mar 08 '13 at 21:50
  • @Peter Wood That was my first instinct, and the idea was considered but dismissed. There will be some changes to it's functionality, so it will probably be for the best anyway. – quandrei Mar 08 '13 at 22:02
  • 1
    Possible duplicate of [Convert Python program to C/C++ code?](https://stackoverflow.com/q/4650243/608639) – jww Nov 21 '19 at 13:31

2 Answers2

13

Programming languages cannot be easily converted like this. For example, Python has a large standard library, C++ doesn't have the same library. How do you translate those calls?

More fundamentally, the semantics of the language are very different, even a statement as simple as x = 1 means something different in Python and C++.

You are going to have to write C++ while reading the Python.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

Have a look at shedskin, if it won't do the whole job,it still might be helpfull.

dugres
  • 12,613
  • 8
  • 46
  • 51