1

Is there any compiler which will help me convert my python code to proper C code? In my python code I am using the xml.dom.minidom module to parse an xml and process the results obtained by ElementsByTagName. I don't know of any such compiler which will help me convert this kind of python code to C code.

My intention is to convert python code to a readable C code which may later be compile to an .exe file. Cython will not be of much use to me as it is in the end writing c extensions which will later be used in python. However, I wish to use this as a proper standalone readable C code which can later be shared with users - the c source code along with its corresponding .py file.

My main impediment here is writing out the C code manually myself, my C skills are quite poor and it would require a more effort to sharpen my C skills before writing the code myself, I can not afford that luxury of time.

I was surfing and came across shedskin, but that might not just support the xml.dom.minidom module for my purposes

The only similar question to mine on stackoverflow: Is there any free Python to C translator? does not help me out much as Cython will not help me and shedskin does not seem to support xml.dom.minidom module.

Any pointers?

Community
  • 1
  • 1
Vijay
  • 924
  • 7
  • 14
  • 1
    Are you sure `cython --embed` won't help you? – icktoofay Aug 09 '11 at 05:56
  • Why does it need to be turned into C code? Why not use http://www.py2exe.org/ or similar? Or if shedskin doesn't work, how about http://kayhayen24x7.homelinux.org/blog/nuitka-a-python-compiler/? – agf Aug 09 '11 at 05:57
  • @agf py2exe will not help me as my main aim is not generate an .exe file. My main aim is to get a readable C code which may later be used to generate .exe. I need to reword my question. Sorry about that... – Vijay Aug 09 '11 at 06:01
  • In that case, @Mikko's answer is spot-on. – agf Aug 09 '11 at 06:02
  • 1
    Funny that, half the time I *swear* that Python is just an awesome wrapper around C anyway. – cwallenpoole Aug 09 '11 at 06:04
  • Python is written in C, perhaps you can fork cpython and work your way from there? – Dima Tisnek Feb 21 '12 at 10:32

2 Answers2

4

There are no Python to C translators which would produce human-readable, editable, C source code.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 4
    Also source code translations are very, very, advanced topic. I'd recommend you to learn C and rewrite it by hand. No other way. Sorry :( – Mikko Ohtamaa Aug 09 '11 at 06:02
  • Following up Mikko's remark, here is my response on translating Python to PHP, pretty much in the same vein as OP's question. – Ira Baxter Aug 09 '11 at 06:12
  • @Mikko as much as I don't want to agree with you...I know I have to agree with you, just was trying to find the easy way out. Need to get back to C now...sigh! – Vijay Aug 09 '11 at 14:21
2

You are looking in the wrong direction. Instead of looking for a generic Python to C compiler to make xml.dom.minidom more efficient, replace the library with lxml which is already compiled C code. The lxml module uses libxml2 and libxslt.

In general, you get more optomization in Python, with less work, by choosing modules that more efficient.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106