Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'd like to convert an open source C application to pure python (not to Cython, IronPython etc).

The documentation and presentations of the original creator of the C application has given me a good understanding of the architecture of the application.

Since I want the first version of the python application to be as identical to the C based one, a manual conversion seems to be the best option. I'd appreciate some tips on how to best approach this code conversion (C to Python), based on your experience. (I am just looking for insights on how others have handled a similar task before.)

(Python version 2.7; The C application is the popular database SQLite. Please don't let that or C vs python distract you from the actual question. My C is rusty but good enough for the task, and my Python is above average.)


  • I was planning a manual rewrite. Appreciate advice on how to analyze the C source and header files and things to keep in mind vis-a-vis python.
share|improve this question

closed as too broad by gnat, gbjbaanb, durron597, MichaelT, GlenH7 Apr 10 at 14:30

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

Personally I would re-write from scratch, by hand in Python. Using the desired functionality as the guideline rather than the C code. I'd dig into the C code as needed for functionality that is hidden from the user experience.

Even if an auto-conversion tool exists I would not trust it. I'm just making this example up for the sake of argument..... Lets say there is a set of boolean flags used by the program. The original programmer decides to pack the flags inside of a floating point. He re-interprets the floating point as pointer to an 8 bit char and uses bit twiddling to set the flags. This is going to screw up just about any auto-conversion tool. (the example is unrealistic I know)

share|improve this answer
+1! I too would use the existing program as a living, breathing spec, nothing more. The ported program might end up being slightly different too. – Job Aug 21 '11 at 3:17
Yes, I had a manual rewrite in mind too. – Sam Aug 21 '11 at 7:57

You probably don't want a line:line conversion.

Python does things differently, so ou want to keep the functionality but probably implement it differently.

The first thing I would do is to make a set of test data, and idealy unit test cases that you can use to confirm what the existing program does in each case and then confirm that your python program does the same.

Another benefit is that it confirms you REALLY understand what the program is supposed to do!

share|improve this answer
It already has a good set of tests, which I do plan to reuse. – Sam Aug 21 '11 at 7:45

It depends which implementation of python you're going for. If you are targeting cpython (the version), I'd recommend spending your time on getting the semantics of the program right. For other implementations you might want to read up on object caching, since you'll need it. For most languages with GCs, you have to be very careful with how you create objects; create them too often, and the garbage collector will be triggered, killing performance. The solution is usually to cache and reuse objects, which in extreme cases begins to resemble manual memory allocation.

Cpython isn't like that; it "amortizes" the collection cost by combining a cycle collector with reference counting. You don't have to worry about limiting object creation, since most of them will be handled by reference counting. The only downside is that this makes object caching much less useful than in other languages.

Here are a few tips to get you started with object caching if you are not using cpython. To start off with, what is an object cache? Basically, it's anything that allows you to store objects you're done with so you can reuse them later. I like to use cache rings myself; an example implementation in python might be:

class ObjectCache:
    def __init__(self, constructor, size=12):
        self.size = size;
        self.current = 0;
        self.list = [];
        for i in range(size):
    def next():
        item = self.list[self.current]
        self.current = (self.current+1) % len(self.list)
        return item

You might use this with, e.g.:

def vector_2():
    return [0, 0]

cache = ObjectCache(vector_2, 32);

#get an object from the cache
vec2 =

#make sure to initialize it! it's value is not guaranteed
vec2[0] = vec2[1] = 0.0;

I'd suggest you only worry about the really fine-grained cases (code that creates and then destroys lots and lots of really tiny objects), and worry about everything else later on.

share|improve this answer
This doesn't adresses any of the questions about reading the C application, though. While IronPython and Jython object creation strategies are interesting, I doubt they have any bearing on this specific scenario; it is almost certain that the OP is using CPython as otherwise they'd have known to mention they don't. – Martijn Pieters Apr 10 at 10:31
To quote the original question: "I'd appreciate some tips on how to best approach this code conversion (C to Python), based on your experience. (I am just looking for insights on how others have handled a similar task before.)". – Joe Apr 10 at 13:57
So how does this apply to the C code that the OP is converting? – Martijn Pieters Apr 10 at 13:59
The majority of Python users are not aware of there being any other implementations. Pure Python usually means: only written in Python, without using any C extensions. The OP even qualifies it as such here (not to Cython, IronPython, etc.). That they lumped IronPython with Cython here is sorta-interesting in that they seem to think the latter is some form of non-pure-python. – Martijn Pieters Apr 10 at 14:30
This issue has a meta question Please direct discussion there. – Snowman Apr 10 at 14:47

protected by gnat Apr 10 at 9:54

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.