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

Skip to content

Commit 6f0132f

Browse files
committed
* text2latex.py: call main() instead of always processing ext.tex.
* Makefile: added 'ext' to 'all' target * ext.tex: more changes towards a readable text * lib4.tex (posix): added set{uid,gid} * lib2.tex (array): restored doc for typecode and itemsize (which were there but not visible for dir())
1 parent c45611d commit 6f0132f

5 files changed

Lines changed: 449 additions & 105 deletions

File tree

Doc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ LIBDESTDIR=$DESTDIR/lib
33
LIBDEST=$LIBDESTDIR/python
44
DOCDESTDIR=$LIBDEST/doc
55

6-
all: tut ref lib qua
6+
all: tut lib ref ext qua
77

88
tut:
99
latex tut

Doc/ext.tex

Lines changed: 223 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
\begin{abstract}
2222

2323
\noindent
24-
This document describes how you can extend the Python interpreter with
25-
new modules written in C or C++. It also describes how to use the
26-
interpreter as a library package from applications using Python as an
27-
``embedded'' language.
24+
This document describes how to write modules in C or C++ to extend the
25+
Python interpreter. It also describes how to use Python as an
26+
`embedded' language, and how extension modules can be loaded
27+
dynamically (at run time) into the interpreter, if the operating
28+
system supports this feature.
2829

2930
\end{abstract}
3031

@@ -42,26 +43,31 @@
4243

4344
\chapter{Extending Python with C or C++ code}
4445

46+
47+
\section{Introduction}
48+
4549
It is quite easy to add non-standard built-in modules to Python, if
4650
you know how to program in C. A built-in module known to the Python
47-
programmer as \code{foo} is generally implemented in a file called
48-
\file{foomodule.c}. The standard built-in modules also adhere to this
49-
convention, and in fact some of them form excellent examples of how to
50-
create an extension.
51+
programmer as \code{foo} is generally implemented by a file called
52+
\file{foomodule.c}. All but the most essential standard built-in
53+
modules also adhere to this convention, and in fact some of them form
54+
excellent examples of how to create an extension.
5155

5256
Extension modules can do two things that can't be done directly in
53-
Python: implement new data types and provide access to system calls or
54-
C library functions. Since the latter is usually the most important
55-
reason for adding an extension, I'll concentrate on adding "wrappers"
56-
around C library functions; the concrete example uses the wrapper for
57-
\code{system()} in module posix, found in (of course) the file
58-
posixmodule.c.
57+
Python: they can implement new data types, and they can make system
58+
calls or call C library functions. Since the latter is usually the
59+
most important reason for adding an extension, I'll concentrate on
60+
adding `wrappers' around C library functions; the concrete example
61+
uses the wrapper for
62+
\code{system()} in module \code{posix}, found in (of course) the file
63+
\file{posixmodule.c}.
5964

6065
It is important not to be impressed by the size and complexity of
6166
the average extension module; much of this is straightforward
62-
``boilerplate'' code (starting right with the copyright notice!).
67+
`boilerplate' code (starting right with the copyright notice)!
6368

64-
Let's skip the boilerplate and jump right to an interesting function:
69+
Let's skip the boilerplate and have a look at an interesting function
70+
in \file{posixmodule.c} first:
6571

6672
\begin{verbatim}
6773
static object *
@@ -74,7 +80,7 @@ \chapter{Extending Python with C or C++ code}
7480
if (!getargs(args, "s", &command))
7581
return NULL;
7682
sts = system(command);
77-
return newintobject((long)sts);
83+
return mkvalue("i", sts);
7884
}
7985
\end{verbatim}
8086

@@ -88,34 +94,36 @@ \chapter{Extending Python with C or C++ code}
8894
\end{verbatim}
8995

9096
There is a straightforward translation from the arguments to the call
91-
in Python (here the single value 'ls -l') to the arguments that are
92-
passed to the C function. The C function always has two parameters,
93-
conventionally named 'self' and 'args'. In this example, 'self' will
94-
always be a NULL pointer, since this is a function, not a method (this
95-
is done so that the interpreter doesn't have to understand two
96-
different types of C functions).
97-
98-
The 'args' parameter will be a pointer to a Python object, or NULL if
99-
the Python function/method was called without arguments. It is
100-
necessary to do full argument type checking on each call, since
101-
otherwise the Python user could cause a core dump by passing the wrong
102-
arguments (or no arguments at all). Because argument checking and
103-
converting arguments to C is such a common task, there's a general
104-
function in the Python interpreter which combines these tasks:
105-
\code{getargs()}. It uses a template string to determine both the
106-
types of the Python argument and the types of the C variables into
107-
which it should store the converted values.
108-
109-
When getargs returns nonzero, the argument list has the right type and
110-
its components have been stored in the variables whose addresses are
111-
passed. When it returns zero, an error has occurred. In the latter
112-
case it has already raised an appropriate exception by calling
113-
\code{err_setstr()}, so the calling function can just return NULL.
114-
115-
The form of the format string is described at the end of this file.
116-
(There are convenience macros \code{getstrarg()}, \code{getintarg()},
117-
etc., for many common forms of argument lists. These are relics from
118-
the past; it's better to call \code{getargs()} directly.)
97+
in Python (here the single value \code{'ls -l'}) to the arguments that
98+
are passed to the C function. The C function always has two
99+
parameters, conventionally named \var{self} and \var{args}. In this
100+
example, \var{self} will always be a \code{NULL} pointer, since this is a
101+
function, not a method (this is done so that the interpreter doesn't
102+
have to understand two different types of C functions).
103+
104+
The \var{args} parameter will be a pointer to a Python object, or
105+
\code{NULL} if the Python function/method was called without
106+
arguments. It is necessary to do full argument type checking on each
107+
call, since otherwise the Python user would be able to cause the
108+
Python interpreter to `dump core' by passing the wrong arguments to a
109+
function in an extension module (or no arguments at all). Because
110+
argument checking and converting arguments to C is such a common task,
111+
there's a general function in the Python interpreter which combines
112+
these tasks: \code{getargs()}. It uses a template string to determine
113+
both the types of the Python argument and the types of the C variables
114+
into which it should store the converted values. (More about this
115+
later.)\footnote{
116+
There are convenience macros \code{getstrarg()},
117+
\code{getintarg()}, etc., for many common forms of \code{getargs()}
118+
templates. These are relics from the past; it's better to call
119+
\code{getargs()} directly.}
120+
121+
If \code{getargs()} returns nonzero, the argument list has the right
122+
type and its components have been stored in the variables whose
123+
addresses are passed. If it returns zero, an error has occurred. In
124+
the latter case it has already raised an appropriate exception by
125+
calling \code{err_setstr()}, so the calling function can just return
126+
\code{NULL}.
119127

120128

121129
\section{Intermezzo: errors and exceptions}
@@ -124,15 +132,15 @@ \section{Intermezzo: errors and exceptions}
124132
following: when a function fails, it should set an exception condition
125133
and return an error value (often a NULL pointer). Exceptions are set
126134
in a global variable in the file errors.c; if this variable is NULL no
127-
exception has occurred. A second variable is the ``associated value''
135+
exception has occurred. A second variable is the `associated value'
128136
of the exception.
129137

130138
The file errors.h declares a host of err_* functions to set various
131139
types of exceptions. The most common one is \code{err_setstr()} --- its
132140
arguments are an exception object (e.g. RuntimeError --- actually it
133141
can be any string object) and a C string indicating the cause of the
134142
error (this is converted to a string object and stored as the
135-
``associated value'' of the exception). Another useful function is
143+
`associated value' of the exception). Another useful function is
136144
\code{err_errno()}, which only takes an exception argument and
137145
constructs the associated value by inspection of the (UNIX) global
138146
variable errno.
@@ -300,7 +308,7 @@ \section{Calling Python functions from C}
300308
The above concentrates on making C functions accessible to the Python
301309
programmer. The reverse is also often useful: calling Python
302310
functions from C. This is especially the case for libraries that
303-
support so-called ``callback'' functions. If a C interface makes heavy
311+
support so-called `callback' functions. If a C interface makes heavy
304312
use of callbacks, the equivalent Python often needs to provide a
305313
callback mechanism to the Python programmer; the implementation may
306314
require calling the Python callback functions from a C callback.
@@ -351,8 +359,8 @@ \section{Calling Python functions from C}
351359

352360
\code{call_object()} returns a Python object pointer: this is
353361
the return value of the Python function. \code{call_object()} is
354-
``reference-count-neutral'' with respect to its arguments, but the
355-
return value is ``new'': either it is a brand new object, or it is an
362+
`reference-count-neutral' with respect to its arguments, but the
363+
return value is `new': either it is a brand new object, or it is an
356364
existing object whose reference count has been incremented. So, you
357365
should somehow apply DECREF to the result, even (especially!) if you
358366
are not interested in its value.
@@ -734,6 +742,171 @@ \section{Using C++}
734742
compiler to compile and link your program. There is no need to
735743
recompile Python itself with C++.
736744

745+
746+
\chapter{Dynamic Loading}
747+
748+
On some systems (e.g., SunOS, SGI Irix) it is possible to configure
749+
Python to support dynamic loading of modules implemented in C. Once
750+
configured and installed it's trivial to use: if a Python program
751+
executes \code{import foo}, the search for modules tries to find a
752+
file \file{foomodule.o} in the module search path, and if one is
753+
found, it is linked with the executing binary and executed. Once
754+
linked, the module acts just like a built-in module.
755+
756+
The advantages of dynamic loading are twofold: the `core' Python
757+
binary gets smaller, and users can extend Python with their own
758+
modules implemented in C without having to build and maintain their
759+
own copy of the Python interpreter. There are also disadvantages:
760+
dynamic loading isn't available on all systems (this just means that
761+
on some systems you have to use static loading), and dynamically
762+
loading a module that was compiled for a different version of Python
763+
(e.g., with a different representation of objects) may dump core.
764+
765+
{\bf NEW:} Under SunOS, dynamic loading now uses SunOS shared
766+
libraries and is always configured. See at the end of this chapter
767+
for how to create a dynamically loadable module.
768+
769+
770+
\section{Configuring and building the interpreter for dynamic loading}
771+
772+
(Ignore this section for SunOS --- on SunOS dynamic loading is always
773+
configured.)
774+
775+
Dynamic loading is a little complicated to configure, since its
776+
implementation is extremely system dependent, and there are no
777+
really standard libraries or interfaces for it. I'm using an
778+
extremely simple interface, which basically needs only one function:
779+
780+
\begin{verbatim}
781+
funcptr = dl_loadmod(binary, object, function)
782+
\end{verbatim}
783+
784+
where \code{binary} is the pathname of the currently executing program
785+
(not just \code{argv[0]}!), \code{object} is the name of the \samp{.o}
786+
file to be dynamically loaded, and \code{function} is the name of a
787+
function in the module. If the dynamic loading succeeds,
788+
\code{dl_loadmod()} returns a pointer to the named function; if not, it
789+
returns \code{NULL}.
790+
791+
I provide two implementations of \code{dl_loadmod()}: one for SGI machines
792+
running Irix 4.0 (written by my colleague Jack Jansen), and one that
793+
is a thin interface layer for Wilson Ho's (GNU) dynamic loading
794+
package \dfn{dld} (version 3.2.3). Dld implements a much more powerful
795+
version of dynamic loading than needed (including unlinking), but it
796+
does not support System V's COFF object file format. It currently
797+
supports only VAX (Ultrix), Sun 3 (SunOS 3.4 and 4.0), SPARCstation
798+
(SunOS 4.0), Sequent Symmetry (Dynix), and Atari ST (from the dld
799+
3.2.3 README file). Dld is part of the standard Python distribution;
800+
if you didn't get it,many ftp archive sites carry dld these days, so
801+
it won't be hard to get hold of it if you need it (using archie).
802+
803+
(If you don't know where to get dld, try anonymous ftp to
804+
\file{wuarchive.wustl.edu:/mirrors2/gnu/dld-3.2.3.tar.Z}. Jack's dld
805+
can be found at \file{ftp.cwi.nl:/pub/python/dl.tar.Z}.)
806+
807+
To build a Python interpreter capable of dynamic loading, you need to
808+
edit the Makefile. Basically you must uncomment the lines starting
809+
with \samp{\#DL_}, but you must also edit some of the lines to choose
810+
which version of dl_loadmod to use, and fill in the pathname of the dld
811+
library if you use it. And, of course, you must first build
812+
dl_loadmod and dld, if used. (This is now done through the Configure
813+
script. For SunOS, everything is now automatic as long as the
814+
architecture type is \code{sun4}.)
815+
816+
817+
\section{Building a dynamically loadable module}
818+
819+
Building an object file usable by dynamic loading is easy, if you
820+
follow these rules (substitute your module name for \code{foo}
821+
everywhere):
822+
823+
\begin{itemize}
824+
825+
\item
826+
The source filename must be \file{foomodule.c}, so the object
827+
name is \file{foomodule.o}.
828+
829+
\item
830+
The module must be written as a (statically linked) Python extension
831+
module (described in an earlier chapter) except that no line for it
832+
must be added to \file{config.c} and it mustn't be linked with the
833+
main Python interpreter.
834+
835+
\item
836+
The module's initialization function must be called \code{initfoo}; it
837+
must install the module in \code{sys.modules} (generally by calling
838+
\code{initmodule()} as explained earlier.
839+
840+
\item
841+
The module must be compiled with \samp{-c}. The resulting .o file must
842+
not be stripped.
843+
844+
\item
845+
Since the module must include many standard Python include files, it
846+
must be compiled with a \samp{-I} option pointing to the Python source
847+
directory (unless it resides there itself).
848+
849+
\item
850+
On SGI Irix, the compiler flag \samp{-G0} (or \samp{-G 0}) must be passed.
851+
IF THIS IS NOT DONE THE RESULTING CODE WILL NOT WORK.
852+
853+
\item
854+
{\bf NEW:} On SunOS, you must create a shared library from your \samp{.o}
855+
file using the following command (assuming your module is called
856+
\code{foo}):
857+
858+
\begin{verbatim}
859+
ld -o foomodule.so foomodule.o <any other libraries needed>
860+
\end{verbatim}
861+
862+
and place the resulting \samp{.so} file in the Python search path (not
863+
the \samp{.o} file). Note: on Solaris, you need to pass \samp{-G} to
864+
the loader.
865+
866+
\end{itemize}
867+
868+
869+
\section{Using libraries}
870+
871+
If your dynamically loadable module needs to be linked with one or
872+
more libraries that aren't linked with Python (or if it needs a
873+
routine that isn't used by Python from one of the libraries with which
874+
Python is linked), you must specify a list of libraries to search
875+
after loading the module in a file with extension \samp{.libs} (and
876+
otherwise the same as your \samp{.o} file). This file should contain
877+
one or more lines containing whitespace-separated absolute library
878+
pathnames. When using the dl interface, \samp{-l...} flags may also
879+
be used (it is in fact passed as an option list to the system linker
880+
ld(1)), but the dl-dld interface requires absolute pathnames. I
881+
believe it is possible to specify shared libraries here.
882+
883+
(On SunOS, any extra libraries must be specified on the \code{ld}
884+
command that creates the \samp{.so} file.)
885+
886+
887+
\section{Caveats}
888+
889+
Dynamic loading requires that \code{main}'s \code{argv[0]} contains
890+
the pathname or at least filename of the Python interpreter.
891+
Unfortunately, when executing a directly executable Python script (an
892+
executable file with \samp{\#!...} on the first line), the kernel
893+
overwrites \code{argv[0]} with the name of the script. There is no
894+
easy way around this, so executable Python scripts cannot use
895+
dynamically loaded modules. (You can always write a simple shell
896+
script that calls the Python interpreter with the script as its
897+
input.)
898+
899+
When using dl, the overlay is first converted into an `overlay' for
900+
the current process by the system linker (\code{ld}). The overlay is
901+
saved as a file with extension \samp{.ld}, either in the directory
902+
where the \samp{.o} file lives or (if that can't be written) in a
903+
temporary directory. An existing \samp{.ld} file resulting from a
904+
previous run (not from a temporary directory) is used, bypassing the
905+
(costly) linking phase, provided its version matches the \samp{.o}
906+
file and the current binary. (See the \code{dl} man page for more
907+
details.)
908+
909+
737910
\input{ext.ind}
738911

739912
\end{document}

0 commit comments

Comments
 (0)