3030operating system supports this feature.
3131
3232This document assumes basic knowledge about Python. For an informal
33- introduction to the language, see the Python Tutorial. The Python
34- Reference Manual gives a more formal definition of the language. The
35- Python Library Reference documents the existing object types,
33+ introduction to the language, see the Python Tutorial. The \emph { Python
34+ Reference Manual } gives a more formal definition of the language. The
35+ \emph { Python Library Reference } documents the existing object types,
3636functions and modules (both built-in and written in Python) that give
3737the language its wide application range.
3838
3939For a detailed description of the whole Python/\C {} API, see the separate
40- Python/\C {} API Reference Manual. \strong {Note:} While that manual is
41- still in a state of flux, it is safe to say that it is much more up to
42- date than the manual you're reading currently (which has been in need
43- for an upgrade for some time now).
40+ \emph { Python/\C {} API Reference Manual } . \strong {Note:} While that
41+ manual is still in a state of flux, it is safe to say that it is much
42+ more up to date than the manual you're reading currently (which has
43+ been in need for an upgrade for some time now).
4444
4545
4646\end {abstract }
5151\chapter {Extending Python with \C {} or \Cpp {} code }
5252
5353
54- \section {Introduction }
54+ % \section{Introduction}
55+ \label {intro }
5556
5657It is quite easy to add new built-in modules to Python, if you know
5758how to program in \C {}. Such \dfn {extension modules} can do two things
@@ -69,6 +70,7 @@ \section{Introduction}
6970
7071
7172\section {A Simple Example }
73+ \label {simpleExample }
7274
7375Let's create an extension module called \samp {spam} (the favorite food
7476of Monty Python fans...) and let's say we want to create a Python
@@ -141,13 +143,13 @@ \section{A Simple Example}
141143containing the arguments. Each item of the tuple corresponds to an
142144argument in the call's argument list. The arguments are Python
143145objects --- in order to do anything with them in our \C {} function we have
144- to convert them to \C {} values. The function \code {PyArg_ParseTuple()}
146+ to convert them to \C {} values. The function \cfunction {PyArg_ParseTuple()}
145147in the Python API checks the argument types and converts them to \C {}
146148values. It uses a template string to determine the required types of
147149the arguments as well as the types of the \C {} variables into which to
148150store the converted values. More about this later.
149151
150- \code {PyArg_ParseTuple()} returns true (nonzero) if all arguments have
152+ \cfunction {PyArg_ParseTuple()} returns true (nonzero) if all arguments have
151153the right type and its components have been stored in the variables
152154whose addresses are passed. It returns false (zero) if an invalid
153155argument list was passed. In the latter case it also raises an
@@ -156,6 +158,7 @@ \section{A Simple Example}
156158
157159
158160\section {Intermezzo: Errors and Exceptions }
161+ \label {errors }
159162
160163An important convention throughout the Python interpreter is the
161164following: when a function fails, it should set an exception condition
@@ -226,7 +229,7 @@ \section{Intermezzo: Errors and Exceptions}
226229\code {malloc()} directly this note is of importance.
227230
228231Also note that, with the important exception of
229- \code {PyArg_ParseTuple()} and friends, functions that return an
232+ \cfunction {PyArg_ParseTuple()} and friends, functions that return an
230233integer status usually return a positive value or zero for success and
231234\code {-1} for failure, like \UNIX {} system calls.
232235
@@ -240,7 +243,7 @@ \section{Intermezzo: Errors and Exceptions}
240243course, you should choose exceptions wisely --- don't use
241244\code {PyExc_TypeError} to mean that a file couldn't be opened (that
242245should probably be \code {PyExc_IOError}). If something's wrong with
243- the argument list, the \code {PyArg_ParseTuple()} function usually
246+ the argument list, the \cfunction {PyArg_ParseTuple()} function usually
244247raises \code {PyExc_TypeError}. If you have an argument whose value
245248which must be in a particular range or must satisfy other conditions,
246249\code {PyExc_ValueError} is appropriate.
@@ -277,6 +280,7 @@ \section{Intermezzo: Errors and Exceptions}
277280
278281
279282\section {Back to the Example }
283+ \label {backToExample }
280284
281285Going back to our example function, you should now be able to
282286understand this statement:
@@ -288,15 +292,15 @@ \section{Back to the Example}
288292
289293It returns \NULL {} (the error indicator for functions returning
290294object pointers) if an error is detected in the argument list, relying
291- on the exception set by \code {PyArg_ParseTuple()}. Otherwise the
295+ on the exception set by \cfunction {PyArg_ParseTuple()}. Otherwise the
292296string value of the argument has been copied to the local variable
293297\code {command}. This is a pointer assignment and you are not supposed
294298to modify the string to which it points (so in Standard \C {}, the variable
295299\code {command} should properly be declared as \samp {const char
296300*command}).
297301
298302The next statement is a call to the \UNIX {} function \code {system()},
299- passing it the string we just got from \code {PyArg_ParseTuple()}:
303+ passing it the string we just got from \cfunction {PyArg_ParseTuple()}:
300304
301305\begin {verbatim }
302306 sts = system(command);
@@ -305,7 +309,7 @@ \section{Back to the Example}
305309Our \code {spam.system()} function must return the value of \code {sts}
306310as a Python object. This is done using the function
307311\code {Py_BuildValue()}, which is something like the inverse of
308- \code {PyArg_ParseTuple()}: it takes a format string and an arbitrary
312+ \cfunction {PyArg_ParseTuple()}: it takes a format string and an arbitrary
309313number of \C {} values, and returns a new Python object. More info on
310314\code {Py_BuildValue()} is given later.
311315
@@ -331,6 +335,7 @@ \section{Back to the Example}
331335
332336
333337\section {The Module's Method Table and Initialization Function }
338+ \label {methodTable }
334339
335340I promised to show how \code {spam_system()} is called from Python
336341programs. First, we need to list its name and address in a `` method
@@ -349,7 +354,7 @@ \section{The Module's Method Table and Initialization Function}
349354the interpreter the calling convention to be used for the \C {}
350355function. It should normally always be \samp {METH_VARARGS} or
351356\samp {METH_VARARGS | METH_KEYWORDS}; a value of \samp {0} means that an
352- obsolete variant of \code {PyArg_ParseTuple()} is used.
357+ obsolete variant of \cfunction {PyArg_ParseTuple()} is used.
353358
354359When using only \samp {METH_VARARGS}, the function should expect
355360the Python-level parameters to be passed in as a tuple acceptable for
@@ -359,7 +364,7 @@ \section{The Module's Method Table and Initialization Function}
359364The \code {METH_KEYWORDS} bit may be set in the third field if keyword
360365arguments should be passed to the function. In this case, the \C {}
361366function should accept a third \samp {PyObject *} parameter which will
362- be a dictionary of keywords. Use \code {PyArg_ParseTupleAndKeywords()}
367+ be a dictionary of keywords. Use \cfunction {PyArg_ParseTupleAndKeywords()}
363368to parse the arguemts to such a function.
364369
365370The method table must be passed to the interpreter in the module's
@@ -387,6 +392,7 @@ \section{The Module's Method Table and Initialization Function}
387392
388393
389394\section {Compilation and Linkage }
395+ \label {compilation }
390396
391397There are two more things to do before you can use your new extension:
392398compiling and linking it with the Python system. If you use dynamic
@@ -419,6 +425,7 @@ \section{Compilation and Linkage}
419425\end {verbatim }
420426
421427\section {Calling Python Functions From \C {} }
428+ \label {callingPython }
422429
423430So far we have concentrated on making \C {} functions callable from
424431Python. The reverse is also useful: calling Python functions from \C {}.
@@ -543,8 +550,9 @@ \section{Calling Python Functions From \C{}}
543550
544551
545552\section {Format Strings for \sectcode {PyArg_ParseTuple()} }
553+ \label {parseTuple }
546554
547- The \code {PyArg_ParseTuple()} function is declared as follows:
555+ The \cfunction {PyArg_ParseTuple()} function is declared as follows:
548556
549557\begin {verbatim }
550558int PyArg_ParseTuple(PyObject *arg, char *format, ...);
@@ -558,7 +566,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
558566\var {arg} object must match the format and the format must be
559567exhausted.
560568
561- Note that while \code {PyArg_ParseTuple()} checks that the Python
569+ Note that while \cfunction {PyArg_ParseTuple()} checks that the Python
562570arguments have the required types, it cannot check the validity of the
563571addresses of \C {} variables passed to the call: if you make mistakes
564572there, your code will probably crash or at least overwrite random bits
@@ -568,7 +576,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
568576unit describes one Python object; it is usually a single character or
569577a parenthesized sequence of format units. With a few exceptions, a
570578format unit that is not a parenthesized sequence normally corresponds
571- to a single address argument to \code {PyArg_ParseTuple()}. In the
579+ to a single address argument to \cfunction {PyArg_ParseTuple()}. In the
572580following description, the quoted form is the format unit; the entry
573581in (round) parentheses is the Python object type that matches the
574582format unit; and the entry in [square] brackets is the type of the \C {}
@@ -582,7 +590,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
582590must not provide storage for the string itself; a pointer to an
583591existing string is stored into the character pointer variable whose
584592address you pass. The \C {} string is null-terminated. The Python string
585- must not contain embedded null bytes; if it does, a \code {TypeError}
593+ must not contain embedded null bytes; if it does, a \exception {TypeError}
586594exception is raised.
587595
588596\item [\samp {s\# } (string) {[char *, int]}]
@@ -748,6 +756,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
748756
749757
750758\section {Keyword Parsing with \sectcode {PyArg_ParseTupleAndKeywords()} }
759+ \label {parseTupleAndKeywords }
751760
752761The \cfunction {PyArg_ParseTupleAndKeywords()} function is declared as
753762follows:
@@ -817,6 +826,7 @@ \section{Keyword Parsing with \sectcode{PyArg_ParseTupleAndKeywords()}}
817826
818827
819828\section {The \sectcode {Py_BuildValue()} Function }
829+ \label {buildValue }
820830
821831This function is the counterpart to \code {PyArg_ParseTuple()}. It is
822832declared as follows:
@@ -945,8 +955,9 @@ \section{The \sectcode{Py_BuildValue()} Function}
945955\end {verbatim }
946956
947957\section {Reference Counts }
958+ \label {refcounts }
948959
949- \subsection {Introduction }
960+ % \subsection{Introduction}
950961
951962In languages like \C {} or \Cpp {}, the programmer is responsible for
952963dynamic allocation and deallocation of memory on the heap. In \C {}, this
@@ -1007,6 +1018,7 @@ \subsection{Introduction}
10071018reference counts.
10081019
10091020\subsection {Reference Counting in Python }
1021+ \label {refcountsInPython }
10101022
10111023There are two macros, \code {Py_INCREF(x)} and \code {Py_DECREF(x)},
10121024which handle the incrementing and decrementing of the reference count.
@@ -1054,6 +1066,7 @@ \subsection{Reference Counting in Python}
10541066dispose of the reference properly, as well as the previous owner).
10551067
10561068\subsection {Ownership Rules }
1069+ \label {ownershipRules }
10571070
10581071Whenever an object reference is passed into or out of a function, it
10591072is part of the function's interface specification whether ownership is
@@ -1102,6 +1115,7 @@ \subsection{Ownership Rules}
11021115function to its caller.
11031116
11041117\subsection {Thin Ice }
1118+ \label {thinIce }
11051119
11061120There are a few situations where seemingly harmless use of a borrowed
11071121reference can lead to problems. These all have to do with implicit
@@ -1181,6 +1195,7 @@ \subsection{Thin Ice}
11811195\end {verbatim }
11821196
11831197\subsection {NULL Pointers }
1198+ \label {nullPointers }
11841199
11851200In general, functions that take object references as arguments don't
11861201expect you to pass them \NULL {} pointers, and will dump core (or
@@ -1217,6 +1232,7 @@ \subsection{NULL Pointers}
12171232
12181233
12191234\section {Writing Extensions in \Cpp {} }
1235+ \label {cplusplus }
12201236
12211237It is possible to write extension modules in \Cpp {}. Some restrictions
12221238apply. If the main program (the Python interpreter) is compiled and
@@ -1231,6 +1247,7 @@ \section{Writing Extensions in \Cpp{}}
12311247symbol).
12321248
12331249\chapter {Embedding Python in another application }
1250+ \label {embedding }
12341251
12351252Embedding Python is similar to extending it, but not quite. The
12361253difference is that when you extend Python, the main program of the
@@ -1258,6 +1275,7 @@ \chapter{Embedding Python in another application}
12581275
12591276
12601277\section {Embedding Python in \Cpp {} }
1278+ \label {embeddingInCplusplus }
12611279
12621280It is also possible to embed Python in a \Cpp {} program; precisely how this
12631281is done will depend on the details of the \Cpp {} system used; in general you
@@ -1267,6 +1285,7 @@ \section{Embedding Python in \Cpp{}}
12671285
12681286
12691287\chapter {Dynamic Loading }
1288+ \label {dynload }
12701289
12711290On most modern systems it is possible to configure Python to support
12721291dynamic loading of extension modules implemented in \C {}. When shared
@@ -1290,12 +1309,14 @@ \chapter{Dynamic Loading}
12901309
12911310
12921311\section {Configuring and Building the Interpreter for Dynamic Loading }
1312+ \label {dynloadConfig }
12931313
12941314There are three styles of dynamic loading: one using shared libraries,
12951315one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
12961316loading.
12971317
12981318\subsection {Shared Libraries }
1319+ \label {sharedlibs }
12991320
13001321The following systems support dynamic loading using shared libraries:
13011322SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
@@ -1308,6 +1329,7 @@ \subsection{Shared Libraries}
13081329loading.
13091330
13101331\subsection {SGI IRIX 4 Dynamic Loading }
1332+ \label {irixDynload }
13111333
13121334Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
13131335loading. (SGI IRIX 5 might also support it but it is inferior to
@@ -1329,6 +1351,7 @@ \subsection{SGI IRIX 4 Dynamic Loading}
13291351\file {README} file in the toplevel Python directory.)
13301352
13311353\subsection {GNU Dynamic Loading }
1354+ \label {gnuDynload }
13321355
13331356GNU dynamic loading supports (according to its \file {README} file) the
13341357following hardware and software combinations: VAX (Ultrix), Sun 3
@@ -1359,6 +1382,7 @@ \subsection{GNU Dynamic Loading}
13591382
13601383
13611384\section {Building a Dynamically Loadable Module }
1385+ \label {makedynload }
13621386
13631387Since there are three styles of dynamic loading, there are also three
13641388groups of instructions for building a dynamically loadable module.
@@ -1379,6 +1403,7 @@ \section{Building a Dynamically Loadable Module}
13791403
13801404
13811405\subsection {Shared Libraries }
1406+ \label {linking }
13821407
13831408You must link the \file {.o} file to produce a shared library. This is
13841409done using a special invocation of the \UNIX {} loader/linker,
@@ -1413,6 +1438,7 @@ \subsection{Shared Libraries}
14131438
14141439
14151440\subsection {SGI IRIX 4 Dynamic Loading }
1441+ \label {irixLinking }
14161442
14171443\strong {IMPORTANT:} You must compile your extension module with the
14181444additional \C {} flag \samp {-G0} (or \samp {-G 0}). This instruct the
@@ -1441,6 +1467,7 @@ \subsection{SGI IRIX 4 Dynamic Loading}
14411467
14421468
14431469\subsection {GNU Dynamic Loading }
1470+ \label {gnuLinking }
14441471
14451472Just copy \file {spammodule.o} into a directory along the Python module
14461473search path.
0 commit comments