Converting Python code to C/C++

Couper, Tim T Tim.Couper at
Wed Jun 24 03:15:06 EDT 2009

Your prof. may find this thread of interest

My experience is that developers who know C and C++ can be productive in
less than 1 week in python, and find it liberating, and educational, to
do so. And at the same time they will have added a second language to
their toolbox. As Kurt points out, learning C/C++ takes considerably
longer (weeks/months to attain a level of competence). 

Python is now used in a number of universities as the language in which
to teach comp sci undergraduate courses (I know of Leeds, & MIT),
biomathematics, and my daughter just finished her PhD in speech and
language processing at Edinburgh .. using python and Matplotlib .. as
the extensive C/C++ libraries in that infomatics world are wrapped in
python - and the MSc Comp Sci course has replaced Java as the language
for teaching with Python.

Dr Tim Couper 

-----Original Message-----
From: at
[ at] On
Behalf Of Kurt Smith
Sent: 23 June 2009 16:50
Cc: python-list at
Subject: Re: Converting Python code to C/C++

On Mon, Jun 22, 2009 at 9:49 PM, Andras
Pikler<Andras.Pikler at> wrote:
> Hi!
> Short: I need to turn a Python program that I (mostly) wrote into C 
> code, and I am at a loss.
> Long: I'm doing research/programming for a professor, and we are 
> working with MIDI files (a type of simple music file). The research 
> deals with generating variations from a musical melody; currently, my 
> Python code uses a Python midi package I found online to read the 
> notes in question from a midi file, about 350 lines of my own code to 
> generate a variation based on these notes and the professor's 
> algorithms, and finally the package again to write the new melody to
another midi file.
> Now, my professor would like to have this exact code in C/C++, as she 
> believes C is more compatible with MATLAB, and wants the code to be 
> available in multiple languages in case a programmer works for her in 
> the future who knows C but not Python. While I know a tiny bit of C 
> (emphasis on the tiny), I would much prefer if there were some sort of

> automatic compiler I could use to turn my Python code into C than 
> taking a week or two or three to learn the minimum I need about C, 
> find a way to access MIDI files in it, and rewrite all of my code.
> After some googling, I found and tried Shedskin, but it doesn't work, 
> as the Python midi package I'm using uses modules which Shedskin does
not support.
> Otherwise, I haven't found much. Is there anything out there to help 
> me do this? If not, from anyone who has experience in this regard, how

> daunting should I expect this to be?

Taking on C from a cold start and being able to handle the ins and outs
of interfacing with Python isn't something that's feasible in 'two or
three weeks'.  Here are a couple of options -- take 'em or leave 'em:

1) Put the code in Cython:  (full disclosure:
I'm doing a GSoC project with Cython).  It will convert pretty much any
python code into C code (even closures are supported in the most recent
version, I think), and the C code can then be compiled into an extension

The only problem with the above is the C code isn't, at first blush,
easy to read.  Nor is it supposed to be changed by the user.  So that
leads us to option...

2) Write the core functionality in C yourself, and then wrap those C
functions in Cython.  You'll want to take a look at the documentation:

and, more specifically on wrapping C code:

I don't think you'll be able to avoid learning C, though.


This communication is sent by the Standard Bank Plc or one of its affiliates
The registered details of Standard Bank Plc are: 
Registered in England No. 2130447, Registered Office 25 Dowgate Hill London EC4R 2SB
Authorised and Regulated by the Financial Services Authority. 

More information on Standard Bank is available at
Everything in this email and any attachments relating to the official business of Standard Bank Group Limited and any or all subsidiaries, the Company, is proprietary to the Company. It is confidential, legally privileged and protected by relevant laws. The Company does not own and endorse any other content. Views and opinions are those of the sender unless clearly stated as being that of the Company.
The person or persons addressed in this email are the sole authorised recipient. Please notify the sender immediately if it has unintentionally, or inadvertently reached you and do not read, disclose or use the content in any way and delete this e-mail from your system.  

The Company cannot ensure that the integrity of this email has beenmaintained nor that it is free of errors, virus, interception or interference. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission. If verification is required please request a hard-copy version. This message is provided for informational purposes and should not be construed as a solicitation or offer to buy or sell any securities or related financial instruments.


More information about the Python-list mailing list