# Extending and Embedding the Python Interpreter

Guido van Rossum
Corporation for National Research Initiatives (CNRI)
1895 Preston White Drive, Reston, Va 20191, USA
E-mail: `guido@CNRI.Reston.Va.US`, `guido@python.org`

December 31, 1997
Release 1.5

Copyright © 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands.

All Rights Reserved

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.

While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

### Abstract:

Python is an interpreted, object-oriented programming language. This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

This document assumes basic knowledge about Python. For an informal introduction to the language, see the Python Tutorial. The Python Reference Manual gives a more formal definition of the language. The Python Library Reference documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.

For a detailed description of the whole Python/C API, see the separate Python/C API Reference Manual. Note: While that manual is still in a state of flux, it is safe to say that it is much more up to date than the manual you're reading currently (which has been in need for an upgrade for some time now).

- Contents
- Extending Python with C or C++ code
  - 1.1 Introduction
  - 1.2 A Simple Example
  - 1.3 Intermezzo: Errors and Exceptions
  - 1.4 Back to the Example
  - 1.5 The Module's Method Table and Initialization Function
  - 1.6 Compilation and Linkage
  - 1.7 Calling Python Functions From C
  - Format Strings for `PyArg_ParseTuple()`
  - The `Py_BuildValue()` Function
  - 1.10 Reference Counts
    - 1.10.1 Introduction
    - 1.10.2 Reference Counting in Python
    - 1.10.3 Ownership Rules
    - 1.10.4 Thin Ice
    - 1.10.5 NULL Pointers
  - Writing Extensions in C++
- 2 Embedding Python in another application
  - Embedding Python in C++
- 3 Dynamic Loading
  - 3.1 Configuring and Building the Interpreter for Dynamic Loading
    - 3.1.1 Shared Libraries
    - 3.1.2 SGI IRIX 4 Dynamic Loading
    - 3.1.3 GNU Dynamic Loading
  - 3.2 Building a Dynamically Loadable Module
    - 3.2.1 Shared Libraries
    - 3.2.2 SGI IRIX 4 Dynamic Loading
    - 3.2.3 GNU Dynamic Loading
- About this document ...