Need help with Python to C code compiler

Stefan Behnel stefan_ml at behnel.de
Tue Aug 9 10:18:52 CEST 2011


Vijay Anantha Murthy, 09.08.2011 07:37:
> 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.

It will certainly help much more to migrate your code to ElementTree first. 
See the xml.etree.cElementTree package, and also the external lxml.etree 
package. MiniDOM is known to have severe performance problems, including a 
huge memory overhead.


> My intention is to convert python code to a readable C code which can later
> be compile to an .exe file.

Have a look at py2exe then.


> 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.

So? You can just bundle CPython with it. Cython can directly generate the 
necessary code for embedding the runtime.


> However, I wish to use this as a proper readable C code which can later be
> shared with users - the c source code as well as the exe along with its
> corresponding .py file.

Cython will mostly give you that. It may not produce the most obvious C 
code in some cases, because it is an optimising compiler, but it's readable 
(especially in the annotated HTML version), and it allows you to ship your 
C code with your .py file, thus avoiding a user dependency on Cython.


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

That perfectly hits one of the more important use cases Cython is made for.


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

Shedskin produces very fast code and generates stand-alone modules. 
However, it's not meant to compile Python code at all, rather a statically 
typed subset of the language. That implies that most Python code won't 
compile unchanged with it. Cython is much closer to Python semantics and 
capable of compiling a huge amount of existing Python code. Plus, it 
generates fast code and provides a very straight forward path for 
optimising the compiled code.

There are other compilers listed here, although none of them has actual 
advantages over Cython and Shedskin:

http://wiki.python.org/moin/PythonImplementations#Compilers

Stefan




More information about the Python-list mailing list