Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3da06a6

Browse files
committed
Marked references to the other manuals as \emph{} in the abstract.
Added \label{}s for logical addressing.
1 parent 6e73af7 commit 3da06a6

2 files changed

Lines changed: 98 additions & 44 deletions

File tree

Doc/ext.tex

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
operating system supports this feature.
3131

3232
This 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,
3636
functions and modules (both built-in and written in Python) that give
3737
the language its wide application range.
3838

3939
For 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}
@@ -51,7 +51,8 @@
5151
\chapter{Extending Python with \C{} or \Cpp{} code}
5252

5353

54-
\section{Introduction}
54+
%\section{Introduction}
55+
\label{intro}
5556

5657
It is quite easy to add new built-in modules to Python, if you know
5758
how 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

7375
Let's create an extension module called \samp{spam} (the favorite food
7476
of Monty Python fans...) and let's say we want to create a Python
@@ -141,13 +143,13 @@ \section{A Simple Example}
141143
containing the arguments. Each item of the tuple corresponds to an
142144
argument in the call's argument list. The arguments are Python
143145
objects --- 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()}
145147
in the Python API checks the argument types and converts them to \C{}
146148
values. It uses a template string to determine the required types of
147149
the arguments as well as the types of the \C{} variables into which to
148150
store 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
151153
the right type and its components have been stored in the variables
152154
whose addresses are passed. It returns false (zero) if an invalid
153155
argument 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

160163
An important convention throughout the Python interpreter is the
161164
following: 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

228231
Also 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
230233
integer 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}
240243
course, you should choose exceptions wisely --- don't use
241244
\code{PyExc_TypeError} to mean that a file couldn't be opened (that
242245
should 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
244247
raises \code{PyExc_TypeError}. If you have an argument whose value
245248
which 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

281285
Going back to our example function, you should now be able to
282286
understand this statement:
@@ -288,15 +292,15 @@ \section{Back to the Example}
288292

289293
It returns \NULL{} (the error indicator for functions returning
290294
object 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
292296
string value of the argument has been copied to the local variable
293297
\code{command}. This is a pointer assignment and you are not supposed
294298
to 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

298302
The 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}
305309
Our \code{spam.system()} function must return the value of \code{sts}
306310
as 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
309313
number 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

335340
I promised to show how \code{spam_system()} is called from Python
336341
programs. 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}
349354
the interpreter the calling convention to be used for the \C{}
350355
function. 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

354359
When using only \samp{METH_VARARGS}, the function should expect
355360
the 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}
359364
The \code{METH_KEYWORDS} bit may be set in the third field if keyword
360365
arguments should be passed to the function. In this case, the \C{}
361366
function 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()}
363368
to parse the arguemts to such a function.
364369

365370
The 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

391397
There are two more things to do before you can use your new extension:
392398
compiling 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

423430
So far we have concentrated on making \C{} functions callable from
424431
Python. 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}
550558
int 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
559567
exhausted.
560568

561-
Note that while \code{PyArg_ParseTuple()} checks that the Python
569+
Note that while \cfunction{PyArg_ParseTuple()} checks that the Python
562570
arguments have the required types, it cannot check the validity of the
563571
addresses of \C{} variables passed to the call: if you make mistakes
564572
there, your code will probably crash or at least overwrite random bits
@@ -568,7 +576,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
568576
unit describes one Python object; it is usually a single character or
569577
a parenthesized sequence of format units. With a few exceptions, a
570578
format 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
572580
following description, the quoted form is the format unit; the entry
573581
in (round) parentheses is the Python object type that matches the
574582
format unit; and the entry in [square] brackets is the type of the \C{}
@@ -582,7 +590,7 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
582590
must not provide storage for the string itself; a pointer to an
583591
existing string is stored into the character pointer variable whose
584592
address 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}
586594
exception 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

752761
The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as
753762
follows:
@@ -817,6 +826,7 @@ \section{Keyword Parsing with \sectcode{PyArg_ParseTupleAndKeywords()}}
817826

818827

819828
\section{The \sectcode{Py_BuildValue()} Function}
829+
\label{buildValue}
820830

821831
This function is the counterpart to \code{PyArg_ParseTuple()}. It is
822832
declared 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

951962
In languages like \C{} or \Cpp{}, the programmer is responsible for
952963
dynamic allocation and deallocation of memory on the heap. In \C{}, this
@@ -1007,6 +1018,7 @@ \subsection{Introduction}
10071018
reference counts.
10081019

10091020
\subsection{Reference Counting in Python}
1021+
\label{refcountsInPython}
10101022

10111023
There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)},
10121024
which handle the incrementing and decrementing of the reference count.
@@ -1054,6 +1066,7 @@ \subsection{Reference Counting in Python}
10541066
dispose of the reference properly, as well as the previous owner).
10551067

10561068
\subsection{Ownership Rules}
1069+
\label{ownershipRules}
10571070

10581071
Whenever an object reference is passed into or out of a function, it
10591072
is part of the function's interface specification whether ownership is
@@ -1102,6 +1115,7 @@ \subsection{Ownership Rules}
11021115
function to its caller.
11031116

11041117
\subsection{Thin Ice}
1118+
\label{thinIce}
11051119

11061120
There are a few situations where seemingly harmless use of a borrowed
11071121
reference 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

11851200
In general, functions that take object references as arguments don't
11861201
expect 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

12211237
It is possible to write extension modules in \Cpp{}. Some restrictions
12221238
apply. If the main program (the Python interpreter) is compiled and
@@ -1231,6 +1247,7 @@ \section{Writing Extensions in \Cpp{}}
12311247
symbol).
12321248

12331249
\chapter{Embedding Python in another application}
1250+
\label{embedding}
12341251

12351252
Embedding Python is similar to extending it, but not quite. The
12361253
difference 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

12621280
It is also possible to embed Python in a \Cpp{} program; precisely how this
12631281
is 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

12711290
On most modern systems it is possible to configure Python to support
12721291
dynamic 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

12941314
There are three styles of dynamic loading: one using shared libraries,
12951315
one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
12961316
loading.
12971317

12981318
\subsection{Shared Libraries}
1319+
\label{sharedlibs}
12991320

13001321
The following systems support dynamic loading using shared libraries:
13011322
SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
@@ -1308,6 +1329,7 @@ \subsection{Shared Libraries}
13081329
loading.
13091330

13101331
\subsection{SGI IRIX 4 Dynamic Loading}
1332+
\label{irixDynload}
13111333

13121334
Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
13131335
loading. (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

13331356
GNU dynamic loading supports (according to its \file{README} file) the
13341357
following 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

13631387
Since there are three styles of dynamic loading, there are also three
13641388
groups 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

13831408
You must link the \file{.o} file to produce a shared library. This is
13841409
done 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
14181444
additional \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

14451472
Just copy \file{spammodule.o} into a directory along the Python module
14461473
search path.

0 commit comments

Comments
 (0)