Extending Python with C or C++

Terry Reedy tjreedy at udel.edu
Mon Jan 5 23:37:43 CET 2009

Ryan wrote:
> I've been using Python for many years now. It's a wonderful language
> that I enjoy using everyday. I'm now interested in getting to know
> more about the guts

The 'guts' of Python the language include the object model, namespaces 
(including modules), and the statement and infix-expression syntax.

> (C/C++) and extending it.

Now you are asking about CPython, the leading computer implementation.

> But, extending python still seems like a black art to me.
 > Is there anymore docs or info on
> extending it besides the standard sparse ones (http://www.python.org/
> doc/2.5.2/ext/intro.html) that may give me more insight? Is there a
> class available?

If you want to connect CPython to Python-oblivious code written in C, 
Swig (with C code) and Ctypes (with Python code) are the main choices. 
If you want to write new Python-aware (and specific) code, you can use 
the CPython C-API functions.  Extensions in C are written as importable 
modules.  The interface for such is not difficult; existing examples 
should be a good guide.

 > How can I learn more about the guts of python?

The 'guts' of an implementation follow from the 'guts' of the language. 
  There must be a syntax parser and compiler to internal form, 
evaluation loop, and implemenations of built-in constants, functions, 
classes, and modules.  CPython's source tree begins as
You might actually want to start at
Note: if you click a filename, such as 'setup.py', you get the entire 
revision history with checkin messages.
If you click the displayed revision number, such as '67978', you get the 
latest checkin message and the current version of the file.

> How would one go about following an interest in contributing to the
> development of python.

and start following the pydev list, mirrored to gmane.comp.python.devel 
at news.gmane.org.

Terry Jan Reedy

More information about the Python-list mailing list