Python in C

David Cournapeau cournape at gmail.com
Mon Dec 29 21:52:44 EST 2008


On Tue, Dec 30, 2008 at 10:22 AM,  <thmpsn.m.k at gmail.com> wrote:
> I've just downloaded Python's mainstream implementation (CPython),
> which is written in C. Not to my surprise, I feel like I'm looking at
> unstructured spaghetti, and I'm having trouble figuring out how it all
> works together. (Please bear with me; I'm just going through the usual
> frustration that anyone goes through when trying to see the
> organization of a C program :)
>
> So, I have two queries:
>
> 1. Can anyone explain to me what kind of program structuring technique
> (which paradigm, etc) CPython uses? How do modules interact together?
> What conventions does it use?

I am not sure what you mean by "which paradigm". CPython uses ansi C,
objects visible at the python level are structures, the API is exposed
through array of function pointers, etc... which are all standard C
techniques. All core objects are in Objects, and each of them expose
its API in a function pointer array; for example, the complex object
(in complexobject.c) implements its basic API through the
PyObjectType, which contains functions called by the interpreter. At
the beginning, I was a bit confused by the terminology (like
protocol), but that may just be my poor English. The documentation is
pretty good:

http://docs.python.org/c-api/index.html (for python 2.6)

It may be easier to first see how to code your own extension, maybe -
if only because the related doc is written as a tutorial instead of as
a reference as the above.

>
> 2. Have there been any suggestions in the past to rewrite Python's
> mainstream implementation in C++ (or why wasn't it done this way from
> the beginning)?

Python was started in the early 90, where arguably, C++ was not easy
to deal with: not many good compilers were available, most
'interesting' features of C++ were not portable because implemented
differently. Without going through the dreadful C vs C++, C++ is
arguably more complex and much bigger than C++, to the point where it
is almost impossible for anyone to master the whole language. This
means that people only use a subset of it, and many projects use a
coding style which severly limit what you can do to avoid everyone
using a different subset; this can be managed in constraints
environments, but it does not work well in open source generally. It
is certainly not my experience that C++ is more readable than C in the
projects I have been involved with.

Also, there are some objective facts against C++ for things "at the
bottom" like a programming language: C++ is not easily callable from
other languages, whereas C is (the C ABI is standardized on many
platforms, whereas C++ is not). In particular, calling dynamically
loaded code in C++ is very difficult (practically, a C wrapper has to
be provided). Taking an example of arguably mature and big C++
project, the JDK, its external API is in C, not C++.

cheers,

David



More information about the Python-list mailing list