pycon logo

PyCon 2011 Atlanta

March 9th–17th

Log in or Sign Up

Writing Python extensions in C

log in to bookmark this presentaton

Novice / Tutorial
March 10th 1:20 p.m. – 4:40 p.m.
This is a course that will cover the Python-C API and will describe how to write a wrapper for a C library enabling it to be used from Python. It will not discuss all parts of the API but will focus on what's necessary to actually write a wrapper and do something useful. The tutorial will use [[http://sourceforge.net/projects/libcsv/|libcsv]] as a vehicle to teach the API.

Abstract

While Python comes with batteries attached, it is sometimes necessary to drop down to lower levels for performance reasons or simply because an existing library already does all the low level work. The ability to "wrap" C libraries as Python modules makes Python a very effective "glue language".

There are many ways to wrap such libraries so as to make them accessible from within Python. This presentation will discuss the raw Python-C API and how it can be used to accomplish this.

We will take a simple C library (libcsv) and write a wrapper for it enabling to be imported and used from within the Python interpreter as if it were a regular Python module. A sizeable fraction of the tutorial will be devoted to making the module a well behaved Python object (proper exceptions, string representations, object oriented handling etc.).

The presentation is designed to be practically useful rather than an in-depth technical discussion. The focus will be on getting the module to work rather than on all the details of the Python-C API.

All participants are expected to be comfortable with C and with Python itself. Lack of experience with C will make it hard to follow the tutorial. The presentation will be in a Gnu/Linux environment.

This tutorial will not cover ctypes, swig and any of the other extension writing tools. It will focus purely on the vanilla Python-C API.