<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"ext.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"ext.html" title='Extending and Embedding the Python Interpreter'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"defining-new-types.html" />
<link rel=
"prev" href=
"contents.html" />
<link rel=
"parent" href=
"ext.html" />
<link rel=
"next" href=
"simpleExample.html" />
<meta name='aesop' content='information'
/>
<title>1. Extending Python with C or C++
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"Contents"
href=
"contents.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"Extending and Embedding the"
href=
"ext.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"1.1 A Simple Example"
href=
"simpleExample.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"contents.html">Contents
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"ext.html">Extending and Embedding the
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"simpleExample.html">1.1 A Simple Example
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION003000000000000000000"></A><A NAME=
"intro"></A>
1. Extending Python with C or C++
It is quite easy to add new built-in modules to Python, if you know
how to program in C. Such
<i class=
"dfn">extension modules
</i> can do two things
that can't be done directly in Python: they can implement new built-in
object types, and they can call C library functions and system calls.
To support extensions, the Python API (Application Programmers
Interface) defines a set of functions, macros and variables that
provide access to most aspects of the Python run-time system. The
Python API is incorporated in a C source file by including the header
The compilation of an extension module depends on its intended use as
well as on your system setup; details are given in later chapters.
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"simpleExample.html">1.1 A Simple Example
</a>
<LI><A href=
"errors.html">1.2 Intermezzo: Errors and Exceptions
</a>
<LI><A href=
"backToExample.html">1.3 Back to the Example
</a>
<LI><A href=
"methodTable.html">1.4 The Module's Method Table and Initialization Function
</a>
<LI><A href=
"compilation.html">1.5 Compilation and Linkage
</a>
<LI><A href=
"callingPython.html">1.6 Calling Python Functions from C
</a>
<LI><A href=
"parseTuple.html">1.7 Extracting Parameters in Extension Functions
</a>
<LI><A href=
"parseTupleAndKeywords.html">1.8 Keyword Parameters for Extension Functions
</a>
<LI><A href=
"buildValue.html">1.9 Building Arbitrary Values
</a>
<LI><A href=
"refcounts.html">1.10 Reference Counts
</a>
<LI><A href=
"refcountsInPython.html">1.10.1 Reference Counting in Python
</a>
<LI><A href=
"ownershipRules.html">1.10.2 Ownership Rules
</a>
<LI><A href=
"thinIce.html">1.10.3 Thin Ice
</a>
<LI><A href=
"nullPointers.html">1.10.4 NULL Pointers
</a>
<LI><A href=
"cplusplus.html">1.11 Writing Extensions in C++
</a>
<LI><A href=
"using-cobjects.html">1.12 Providing a C API for an Extension Module
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"Contents"
href=
"contents.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"Extending and Embedding the"
href=
"ext.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"1.1 A Simple Example"
href=
"simpleExample.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"contents.html">Contents
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"ext.html">Extending and Embedding the
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"simpleExample.html">1.1 A Simple Example
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.