1994-08-01 09:22:53 -03:00
|
|
|
\documentstyle[twoside,11pt,myformat]{report}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
\title{Extending and Embedding the Python Interpreter}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-10-06 07:29:26 -03:00
|
|
|
\input{boilerplate}
|
1993-11-23 12:28:45 -04:00
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
% Tell \index to actually write the .idx file
|
|
|
|
\makeindex
|
|
|
|
|
|
|
|
\begin{document}
|
|
|
|
|
|
|
|
\pagenumbering{roman}
|
|
|
|
|
|
|
|
\maketitle
|
|
|
|
|
1994-10-06 07:29:26 -03:00
|
|
|
\input{copyright}
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
\begin{abstract}
|
|
|
|
|
|
|
|
\noindent
|
1994-08-08 09:30:22 -03:00
|
|
|
This document describes how to write modules in C or \Cpp{} to extend the
|
1993-11-19 09:13:22 -04:00
|
|
|
Python interpreter. It also describes how to use Python as an
|
|
|
|
`embedded' language, and how extension modules can be loaded
|
|
|
|
dynamically (at run time) into the interpreter, if the operating
|
|
|
|
system supports this feature.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\end{abstract}
|
|
|
|
|
|
|
|
\pagebreak
|
|
|
|
|
|
|
|
{
|
|
|
|
\parskip = 0mm
|
|
|
|
\tableofcontents
|
|
|
|
}
|
|
|
|
|
|
|
|
\pagebreak
|
|
|
|
|
|
|
|
\pagenumbering{arabic}
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
\chapter{Extending Python with C or \Cpp{} code}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
\section{Introduction}
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
It is quite easy to add non-standard built-in modules to Python, if
|
|
|
|
you know how to program in C. A built-in module known to the Python
|
1993-11-19 09:13:22 -04:00
|
|
|
programmer as \code{foo} is generally implemented by a file called
|
1994-08-01 09:22:53 -03:00
|
|
|
\file{foomodule.c}. All but the two most essential standard built-in
|
1993-11-19 09:13:22 -04:00
|
|
|
modules also adhere to this convention, and in fact some of them form
|
|
|
|
excellent examples of how to create an extension.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Extension modules can do two things that can't be done directly in
|
1994-08-01 09:22:53 -03:00
|
|
|
Python: they can implement new data types (which are different from
|
1994-08-08 09:30:22 -03:00
|
|
|
classes, by the way), and they can make system calls or call C library
|
|
|
|
functions. We'll see how both types of extension are implemented by
|
|
|
|
examining the code for a Python curses interface.
|
1994-08-01 09:22:53 -03:00
|
|
|
|
|
|
|
Note: unless otherwise mentioned, all file references in this
|
|
|
|
document are relative to the toplevel directory of the Python
|
|
|
|
distribution --- i.e. the directory that contains the \file{configure}
|
|
|
|
script.
|
|
|
|
|
|
|
|
The compilation of an extension module depends on your system setup
|
|
|
|
and the intended use of the module; details are given in a later
|
|
|
|
section.
|
|
|
|
|
|
|
|
|
|
|
|
\section{A first look at the code}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
It is important not to be impressed by the size and complexity of
|
|
|
|
the average extension module; much of this is straightforward
|
1993-11-19 09:13:22 -04:00
|
|
|
`boilerplate' code (starting right with the copyright notice)!
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-19 09:13:22 -04:00
|
|
|
Let's skip the boilerplate and have a look at an interesting function
|
|
|
|
in \file{posixmodule.c} first:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
static object *
|
|
|
|
posix_system(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *command;
|
|
|
|
int sts;
|
|
|
|
if (!getargs(args, "s", &command))
|
|
|
|
return NULL;
|
|
|
|
sts = system(command);
|
1993-11-19 09:13:22 -04:00
|
|
|
return mkvalue("i", sts);
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
This is the prototypical top-level function in an extension module.
|
1994-08-01 09:22:53 -03:00
|
|
|
It will be called (we'll see later how) when the Python program
|
|
|
|
executes statements like
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
>>> import posix
|
|
|
|
>>> sts = posix.system('ls -l')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
There is a straightforward translation from the arguments to the call
|
1994-08-01 09:22:53 -03:00
|
|
|
in Python (here the single expression \code{'ls -l'}) to the arguments that
|
1993-11-19 09:13:22 -04:00
|
|
|
are passed to the C function. The C function always has two
|
1994-08-01 09:22:53 -03:00
|
|
|
parameters, conventionally named \var{self} and \var{args}. The
|
|
|
|
\var{self} argument is used when the C function implements a builtin
|
1994-08-08 09:30:22 -03:00
|
|
|
method---this will be discussed later.
|
1994-08-01 09:22:53 -03:00
|
|
|
In the example, \var{self} will always be a \code{NULL} pointer, since
|
|
|
|
we are defining a function, not a method (this is done so that the
|
|
|
|
interpreter doesn't have to understand two different types of C
|
|
|
|
functions).
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
The \var{args} parameter will be a pointer to a Python object, or
|
|
|
|
\code{NULL} if the Python function/method was called without
|
|
|
|
arguments. It is necessary to do full argument type checking on each
|
|
|
|
call, since otherwise the Python user would be able to cause the
|
1994-08-01 09:22:53 -03:00
|
|
|
Python interpreter to `dump core' by passing invalid arguments to a
|
|
|
|
function in an extension module. Because argument checking and
|
|
|
|
converting arguments to C are such common tasks, there's a general
|
|
|
|
function in the Python interpreter that combines them:
|
|
|
|
\code{getargs()}. It uses a template string to determine both the
|
|
|
|
types of the Python argument and the types of the C variables into
|
|
|
|
which it should store the converted values.\footnote{There are
|
|
|
|
convenience macros \code{getnoarg()}, \code{getstrarg()},
|
1993-11-19 09:13:22 -04:00
|
|
|
\code{getintarg()}, etc., for many common forms of \code{getargs()}
|
1994-08-01 09:22:53 -03:00
|
|
|
templates. These are relics from the past; the recommended practice
|
|
|
|
is to call \code{getargs()} directly.} (More about this later.)
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
If \code{getargs()} returns nonzero, the argument list has the right
|
|
|
|
type and its components have been stored in the variables whose
|
|
|
|
addresses are passed. If it returns zero, an error has occurred. In
|
1994-08-01 09:22:53 -03:00
|
|
|
the latter case it has already raised an appropriate exception by so
|
|
|
|
the calling function should return \code{NULL} immediately --- see the
|
|
|
|
next section.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
\section{Intermezzo: errors and exceptions}
|
|
|
|
|
|
|
|
An important convention throughout the Python interpreter is the
|
|
|
|
following: when a function fails, it should set an exception condition
|
1994-08-01 09:22:53 -03:00
|
|
|
and return an error value (often a \code{NULL} pointer). Exceptions
|
|
|
|
are stored in a static global variable in \file{Python/errors.c}; if
|
|
|
|
this variable is \code{NULL} no exception has occurred. A second
|
|
|
|
static global variable stores the `associated value' of the exception
|
|
|
|
--- the second argument to \code{raise}.
|
|
|
|
|
|
|
|
The file \file{errors.h} declares a host of functions to set various
|
|
|
|
types of exceptions. The most common one is \code{err_setstr()} ---
|
|
|
|
its arguments are an exception object (e.g. \code{RuntimeError} ---
|
|
|
|
actually it can be any string object) and a C string indicating the
|
|
|
|
cause of the error (this is converted to a string object and stored as
|
|
|
|
the `associated value' of the exception). Another useful function is
|
1993-11-05 13:11:16 -04:00
|
|
|
\code{err_errno()}, which only takes an exception argument and
|
|
|
|
constructs the associated value by inspection of the (UNIX) global
|
1994-08-01 09:22:53 -03:00
|
|
|
variable errno. The most general function is \code{err_set()}, which
|
|
|
|
takes two object arguments, the exception and its associated value.
|
|
|
|
You don't need to \code{INCREF()} the objects passed to any of these
|
|
|
|
functions.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
You can test non-destructively whether an exception has been set with
|
1993-11-05 13:11:16 -04:00
|
|
|
\code{err_occurred()}. However, most code never calls
|
|
|
|
\code{err_occurred()} to see whether an error occurred or not, but
|
1994-08-01 09:22:53 -03:00
|
|
|
relies on error return values from the functions it calls instead.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
When a function that calls another function detects that the called
|
1994-08-01 09:22:53 -03:00
|
|
|
function fails, it should return an error value (e.g. \code{NULL} or
|
|
|
|
\code{-1}) but not call one of the \code{err_*} functions --- one has
|
|
|
|
already been called. The caller is then supposed to also return an
|
|
|
|
error indication to {\em its} caller, again {\em without} calling
|
|
|
|
\code{err_*()}, and so on --- the most detailed cause of the error was
|
|
|
|
already reported by the function that first detected it. Once the
|
|
|
|
error has reached Python's interpreter main loop, this aborts the
|
|
|
|
currently executing Python code and tries to find an exception handler
|
|
|
|
specified by the Python programmer.
|
|
|
|
|
|
|
|
(There are situations where a module can actually give a more detailed
|
|
|
|
error message by calling another \code{err_*} function, and in such
|
|
|
|
cases it is fine to do so. As a general rule, however, this is not
|
|
|
|
necessary, and can cause information about the cause of the error to
|
|
|
|
be lost: most operations can fail for a variety of reasons.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
To ignore an exception set by a function call that failed, the
|
1993-11-05 13:11:16 -04:00
|
|
|
exception condition must be cleared explicitly by calling
|
|
|
|
\code{err_clear()}. The only time C code should call
|
|
|
|
\code{err_clear()} is if it doesn't want to pass the error on to the
|
|
|
|
interpreter but wants to handle it completely by itself (e.g. by
|
|
|
|
trying something else or pretending nothing happened).
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
Finally, the function \code{err_get()} gives you both error variables
|
1994-08-01 09:22:53 -03:00
|
|
|
{\em and clears them}. Note that even if an error occurred the second
|
|
|
|
one may be \code{NULL}. You have to \code{XDECREF()} both when you
|
|
|
|
are finished with them. I doubt you will need to use this function.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
Note that a failing \code{malloc()} call must also be turned into an
|
|
|
|
exception --- the direct caller of \code{malloc()} (or
|
|
|
|
\code{realloc()}) must call \code{err_nomem()} and return a failure
|
|
|
|
indicator itself. All the object-creating functions
|
|
|
|
(\code{newintobject()} etc.) already do this, so only if you call
|
|
|
|
\code{malloc()} directly this note is of importance.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Also note that, with the important exception of \code{getargs()},
|
|
|
|
functions that return an integer status usually return \code{0} or a
|
|
|
|
positive value for success and \code{-1} for failure.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Finally, be careful about cleaning up garbage (making \code{XDECREF()}
|
|
|
|
or \code{DECREF()} calls for objects you have already created) when
|
|
|
|
you return an error!
|
|
|
|
|
|
|
|
The choice of which exception to raise is entirely yours. There are
|
|
|
|
predeclared C objects corresponding to all built-in Python exceptions,
|
|
|
|
e.g. \code{ZeroDevisionError} which you can use directly. Of course,
|
|
|
|
you should chose exceptions wisely --- don't use \code{TypeError} to
|
|
|
|
mean that a file couldn't be opened (that should probably be
|
|
|
|
\code{IOError}). If anything's wrong with the argument list the
|
|
|
|
\code{getargs()} function raises \code{TypeError}. If you have an
|
|
|
|
argument whose value which must be in a particular range or must
|
|
|
|
satisfy other conditions, \code{ValueError} is appropriate.
|
|
|
|
|
|
|
|
You can also define a new exception that is unique to your module.
|
|
|
|
For this, you usually declare a static object variable at the
|
|
|
|
beginning of your file, e.g.
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
static object *FooError;
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
and initialize it in your module's initialization function
|
|
|
|
(\code{initfoo()}) with a string object, e.g. (leaving out the error
|
|
|
|
checking for simplicity):
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
void
|
|
|
|
initfoo()
|
|
|
|
{
|
|
|
|
object *m, *d;
|
|
|
|
m = initmodule("foo", foo_methods);
|
|
|
|
d = getmoduledict(m);
|
|
|
|
FooError = newstringobject("foo.error");
|
|
|
|
dictinsert(d, "error", FooError);
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
\section{Back to the example}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Going back to \code{posix_system()}, you should now be able to
|
|
|
|
understand this bit:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
if (!getargs(args, "s", &command))
|
|
|
|
return NULL;
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
It returns \code{NULL} (the error indicator for functions of this
|
|
|
|
kind) if an error is detected in the argument list, relying on the
|
|
|
|
exception set by \code{getargs()}. Otherwise the string value of the
|
|
|
|
argument has been copied to the local variable \code{command} --- this
|
|
|
|
is in fact just a pointer assignment and you are not supposed to
|
|
|
|
modify the string to which it points.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
If a function is called with multiple arguments, the argument list
|
|
|
|
(the argument \code{args}) is turned into a tuple. If it is called
|
|
|
|
without arguments, \code{args} is \code{NULL}. \code{getargs()} knows
|
|
|
|
about this; see later.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
The next statement in \code{posix_system()} is a call to the C library
|
|
|
|
function \code{system()}, passing it the string we just got from
|
|
|
|
\code{getargs()}:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
sts = system(command);
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Finally, \code{posix.system()} must return a value: the integer status
|
|
|
|
returned by the C library \code{system()} function. This is done
|
|
|
|
using the function \code{mkvalue()}, which is something like the
|
|
|
|
inverse of \code{getargs()}: it takes a format string and a variable
|
|
|
|
number of C values and returns a new Python object.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
return mkvalue("i", sts);
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
In this case, it returns an integer object (yes, even integers are
|
|
|
|
objects on the heap in Python!). More info on \code{mkvalue()} is
|
|
|
|
given later.
|
|
|
|
|
|
|
|
If you had a function that returned no useful argument (a.k.a. a
|
|
|
|
procedure), you would need this idiom:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{None} is a unique Python object representing `no value'. It
|
|
|
|
differs from \code{NULL}, which means `error' in most contexts.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
\section{The module's function table}
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
I promised to show how I made the function \code{posix_system()}
|
1994-08-01 09:22:53 -03:00
|
|
|
callable from Python programs. This is shown later in
|
|
|
|
\file{Modules/posixmodule.c}:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
static struct methodlist posix_methods[] = {
|
|
|
|
...
|
|
|
|
{"system", posix_system},
|
|
|
|
...
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
initposix()
|
|
|
|
{
|
|
|
|
(void) initmodule("posix", posix_methods);
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
(The actual \code{initposix()} is somewhat more complicated, but many
|
|
|
|
extension modules can be as simple as shown here.) When the Python
|
|
|
|
program first imports module \code{posix}, \code{initposix()} is
|
|
|
|
called, which calls \code{initmodule()} with specific parameters.
|
|
|
|
This creates a `module object' (which is inserted in the table
|
|
|
|
\code{sys.modules} under the key \code{'posix'}), and adds
|
|
|
|
built-in-function objects to the newly created module based upon the
|
|
|
|
table (of type struct methodlist) that was passed as its second
|
|
|
|
parameter. The function \code{initmodule()} returns a pointer to the
|
|
|
|
module object that it creates (which is unused here). It aborts with
|
|
|
|
a fatal error if the module could not be initialized satisfactorily,
|
|
|
|
so you don't need to check for errors.
|
|
|
|
|
|
|
|
|
|
|
|
\section{Compilation and linkage}
|
|
|
|
|
|
|
|
There are two more things to do before you can use your new extension
|
|
|
|
module: compiling and linking it with the Python system. If you use
|
|
|
|
dynamic loading, the details depend on the style of dynamic loading
|
|
|
|
your system uses; see the chapter on Dynamic Loading for more info
|
|
|
|
about this.
|
|
|
|
|
|
|
|
If you can't use dynamic loading, or if you want to make your module a
|
|
|
|
permanent part of the Python interpreter, you will have to change the
|
|
|
|
configuration setup and rebuild the interpreter. Luckily, in the 1.0
|
|
|
|
release this is very simple: just place your file (named
|
|
|
|
\file{foomodule.c} for example) in the \file{Modules} directory, add a
|
|
|
|
line to the file \file{Modules/Setup} describing your file:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
foo foomodule.o
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
and rebuild the interpreter by running \code{make} in the toplevel
|
|
|
|
directory. You can also run \code{make} in the \file{Modules}
|
|
|
|
subdirectory, but then you must first rebuilt the \file{Makefile}
|
|
|
|
there by running \code{make Makefile}. (This is necessary each time
|
|
|
|
you change the \file{Setup} file.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
\section{Calling Python functions from C}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
So far we have concentrated on making C functions callable from
|
|
|
|
Python. The reverse is also useful: calling Python functions from C.
|
|
|
|
This is especially the case for libraries that support so-called
|
|
|
|
`callback' functions. If a C interface makes use of callbacks, the
|
|
|
|
equivalent Python often needs to provide a callback mechanism to the
|
|
|
|
Python programmer; the implementation will require calling the Python
|
|
|
|
callback functions from a C callback. Other uses are also imaginable.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Fortunately, the Python interpreter is easily called recursively, and
|
1994-08-01 09:22:53 -03:00
|
|
|
there is a standard interface to call a Python function. (I won't
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
dwell on how to call the Python parser with a particular string as
|
1993-11-05 13:11:16 -04:00
|
|
|
input --- if you're interested, have a look at the implementation of
|
1994-08-01 09:22:53 -03:00
|
|
|
the \samp{-c} command line option in \file{Python/pythonmain.c}.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Calling a Python function is easy. First, the Python program must
|
|
|
|
somehow pass you the Python function object. You should provide a
|
|
|
|
function (or some other interface) to do this. When this function is
|
|
|
|
called, save a pointer to the Python function object (be careful to
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{INCREF()} it!) in a global variable --- or whereever you see fit.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
For example, the following function might be part of a module
|
|
|
|
definition:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
static object *my_callback = NULL;
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
static object *
|
|
|
|
my_set_callback(dummy, arg)
|
|
|
|
object *dummy, *arg;
|
|
|
|
{
|
|
|
|
XDECREF(my_callback); /* Dispose of previous callback */
|
|
|
|
my_callback = arg;
|
|
|
|
XINCREF(my_callback); /* Remember new callback */
|
|
|
|
/* Boilerplate for "void" return */
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
This particular function doesn't do any typechecking on its argument
|
|
|
|
--- that will be done by \code{call_object()}, which is a bit late but
|
|
|
|
at least protects the Python interpreter from shooting itself in its
|
|
|
|
foot. (The problem with typechecking functions is that there are at
|
|
|
|
least five different Python object types that can be called, so the
|
|
|
|
test would be somewhat cumbersome.)
|
|
|
|
|
|
|
|
The macros \code{XINCREF()} and \code{XDECREF()} increment/decrement
|
|
|
|
the reference count of an object and are safe in the presence of
|
|
|
|
\code{NULL} pointers. More info on them in the section on Reference
|
|
|
|
Counts below.
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
Later, when it is time to call the function, you call the C function
|
1993-11-05 13:11:16 -04:00
|
|
|
\code{call_object()}. This function has two arguments, both pointers
|
1994-08-01 09:22:53 -03:00
|
|
|
to arbitrary Python objects: the Python function, and the argument
|
|
|
|
list. The argument list must always be a tuple object, whose length
|
|
|
|
is the number of arguments. To call the Python function with no
|
|
|
|
arguments, you must pass an empty tuple. For example:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
object *arglist;
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
object *result;
|
|
|
|
...
|
|
|
|
/* Time to call the callback */
|
1994-10-06 07:29:26 -03:00
|
|
|
arglist = newtupleobject(0);
|
1994-08-01 09:22:53 -03:00
|
|
|
result = call_object(my_callback, arglist);
|
|
|
|
DECREF(arglist);
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
\code{call_object()} returns a Python object pointer: this is
|
|
|
|
the return value of the Python function. \code{call_object()} is
|
1994-08-01 09:22:53 -03:00
|
|
|
`reference-count-neutral' with respect to its arguments. In the
|
|
|
|
example a new tuple was created to serve as the argument list, which
|
|
|
|
is \code{DECREF()}-ed immediately after the call.
|
|
|
|
|
|
|
|
The return value of \code{call_object()} is `new': either it is a
|
|
|
|
brand new object, or it is an existing object whose reference count
|
|
|
|
has been incremented. So, unless you want to save it in a global
|
|
|
|
variable, you should somehow \code{DECREF()} the result, even
|
|
|
|
(especially!) if you are not interested in its value.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Before you do this, however, it is important to check that the return
|
1994-08-01 09:22:53 -03:00
|
|
|
value isn't \code{NULL}. If it is, the Python function terminated by raising
|
1993-11-05 13:11:16 -04:00
|
|
|
an exception. If the C code that called \code{call_object()} is
|
|
|
|
called from Python, it should now return an error indication to its
|
|
|
|
Python caller, so the interpreter can print a stack trace, or the
|
|
|
|
calling Python code can handle the exception. If this is not possible
|
|
|
|
or desirable, the exception should be cleared by calling
|
|
|
|
\code{err_clear()}. For example:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL; /* Pass error back */
|
|
|
|
/* Here maybe use the result */
|
|
|
|
DECREF(result);
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
Depending on the desired interface to the Python callback function,
|
1994-08-01 09:22:53 -03:00
|
|
|
you may also have to provide an argument list to \code{call_object()}.
|
|
|
|
In some cases the argument list is also provided by the Python
|
|
|
|
program, through the same interface that specified the callback
|
|
|
|
function. It can then be saved and used in the same manner as the
|
|
|
|
function object. In other cases, you may have to construct a new
|
|
|
|
tuple to pass as the argument list. The simplest way to do this is to
|
|
|
|
call \code{mkvalue()}. For example, if you want to pass an integral
|
|
|
|
event code, you might use the following code:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
object *arglist;
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
...
|
1994-08-01 09:22:53 -03:00
|
|
|
arglist = mkvalue("(l)", eventcode);
|
|
|
|
result = call_object(my_callback, arglist);
|
|
|
|
DECREF(arglist);
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL; /* Pass error back */
|
|
|
|
/* Here maybe use the result */
|
|
|
|
DECREF(result);
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
Note the placement of DECREF(argument) immediately after the call,
|
|
|
|
before the error check! Also note that strictly spoken this code is
|
1994-08-01 09:22:53 -03:00
|
|
|
not complete: \code{mkvalue()} may run out of memory, and this should
|
|
|
|
be checked.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
\section{Format strings for {\tt getargs()}}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
The \code{getargs()} function is declared in \file{modsupport.h} as
|
|
|
|
follows:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
int getargs(object *arg, char *format, ...);
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The remaining arguments must be addresses of variables whose type is
|
|
|
|
determined by the format string. For the conversion to succeed, the
|
1994-08-01 09:22:53 -03:00
|
|
|
\var{arg} object must match the format and the format must be exhausted.
|
1993-11-05 13:11:16 -04:00
|
|
|
Note that while \code{getargs()} checks that the Python object really
|
1994-08-01 09:22:53 -03:00
|
|
|
is of the specified type, it cannot check the validity of the
|
|
|
|
addresses of C variables provided in the call: if you make mistakes
|
|
|
|
there, your code will probably dump core.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
A non-empty format string consists of a single `format unit'. A
|
|
|
|
format unit describes one Python object; it is usually a single
|
|
|
|
character or a parenthesized sequence of format units. The type of a
|
|
|
|
format units is determined from its first character, the `format
|
|
|
|
letter':
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
\begin{description}
|
|
|
|
|
|
|
|
\item[\samp{s} (string)]
|
|
|
|
The Python object must be a string object. The C argument must be a
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{(char**)} (i.e. the address of a character pointer), and a pointer
|
|
|
|
to the C string contained in the Python object is stored into it. You
|
|
|
|
must not provide storage to store the string; a pointer to an existing
|
|
|
|
string is stored into the character pointer variable whose address you
|
|
|
|
pass. If the next character in the format string is \samp{\#},
|
|
|
|
another C argument of type \code{(int*)} must be present, and the
|
|
|
|
length of the Python string (not counting the trailing zero byte) is
|
|
|
|
stored into it.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{z} (string or zero, i.e. \code{NULL})]
|
|
|
|
Like \samp{s}, but the object may also be None. In this case the
|
1994-08-01 09:22:53 -03:00
|
|
|
string pointer is set to \code{NULL} and if a \samp{\#} is present the
|
|
|
|
size is set to 0.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{b} (byte, i.e. char interpreted as tiny int)]
|
1994-08-01 09:22:53 -03:00
|
|
|
The object must be a Python integer. The C argument must be a
|
|
|
|
\code{(char*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{h} (half, i.e. short)]
|
1994-08-01 09:22:53 -03:00
|
|
|
The object must be a Python integer. The C argument must be a
|
|
|
|
\code{(short*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{i} (int)]
|
1994-08-01 09:22:53 -03:00
|
|
|
The object must be a Python integer. The C argument must be an
|
|
|
|
\code{(int*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{l} (long)]
|
|
|
|
The object must be a (plain!) Python integer. The C argument must be
|
1994-08-01 09:22:53 -03:00
|
|
|
a \code{(long*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{c} (char)]
|
|
|
|
The Python object must be a string of length 1. The C argument must
|
1994-08-01 09:22:53 -03:00
|
|
|
be a \code{(char*)}. (Don't pass an \code{(int*)}!)
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{f} (float)]
|
|
|
|
The object must be a Python int or float. The C argument must be a
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{(float*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{d} (double)]
|
|
|
|
The object must be a Python int or float. The C argument must be a
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{(double*)}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{S} (string object)]
|
|
|
|
The object must be a Python string. The C argument must be an
|
1994-08-01 09:22:53 -03:00
|
|
|
\code{(object**)} (i.e. the address of an object pointer). The C
|
|
|
|
program thus gets back the actual string object that was passed, not
|
|
|
|
just a pointer to its array of characters and its size as for format
|
|
|
|
character \samp{s}. The reference count of the object has not been
|
|
|
|
increased.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{O} (object)]
|
1994-08-01 09:22:53 -03:00
|
|
|
The object can be any Python object, including None, but not
|
|
|
|
\code{NULL}. The C argument must be an \code{(object**)}. This can be
|
|
|
|
used if an argument list must contain objects of a type for which no
|
|
|
|
format letter exist: the caller must then check that it has the right
|
|
|
|
type. The reference count of the object has not been increased.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\item[\samp{(} (tuple)]
|
|
|
|
The object must be a Python tuple. Following the \samp{(} character
|
|
|
|
in the format string must come a number of format units describing the
|
|
|
|
elements of the tuple, followed by a \samp{)} character. Tuple
|
|
|
|
format units may be nested. (There are no exceptions for empty and
|
|
|
|
singleton tuples; \samp{()} specifies an empty tuple and \samp{(i)} a
|
|
|
|
singleton of one integer. Normally you don't want to use the latter,
|
1994-08-01 09:22:53 -03:00
|
|
|
since it is hard for the Python user to specify.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
|
|
|
\end{description}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
More format characters will probably be added as the need arises. It
|
1994-08-01 09:22:53 -03:00
|
|
|
should (but currently isn't) be allowed to use Python long integers
|
|
|
|
whereever integers are expected, and perform a range check. (A range
|
|
|
|
check is in fact always necessary for the \samp{b}, \samp{h} and
|
|
|
|
\samp{i} format letters, but this is currently not implemented.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Some example calls:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
int ok;
|
|
|
|
int i, j;
|
|
|
|
long k, l;
|
|
|
|
char *s;
|
|
|
|
int size;
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
ok = getargs(args, ""); /* No arguments */
|
|
|
|
/* Python call: f() */
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
ok = getargs(args, "s", &s); /* A string */
|
|
|
|
/* Possible Python call: f('whoops!') */
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
ok = getargs(args, "(lls)", &k, &l, &s); /* Two longs and a string */
|
|
|
|
/* Possible Python call: f(1, 2, 'three') */
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
ok = getargs(args, "((ii)s#)", &i, &j, &s, &size);
|
|
|
|
/* A pair of ints and a string, whose size is also returned */
|
|
|
|
/* Possible Python call: f(1, 2, 'three') */
|
|
|
|
|
|
|
|
{
|
|
|
|
int left, top, right, bottom, h, v;
|
|
|
|
ok = getargs(args, "(((ii)(ii))(ii))",
|
|
|
|
&left, &top, &right, &bottom, &h, &v);
|
|
|
|
/* A rectangle and a point */
|
|
|
|
/* Possible Python call:
|
|
|
|
f( ((0, 0), (400, 300)), (10, 10)) */
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Note that the `top level' of a non-empty format string must consist of
|
|
|
|
a single unit; strings like \samp{is} and \samp{(ii)s\#} are not valid
|
|
|
|
format strings. (But \samp{s\#} is.) If you have multiple arguments,
|
|
|
|
the format must therefore always be enclosed in parentheses, as in the
|
|
|
|
examples \samp{((ii)s\#)} and \samp{(((ii)(ii))(ii)}. (The current
|
|
|
|
implementation does not complain when more than one unparenthesized
|
|
|
|
format unit is given. Sorry.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
The \code{getargs()} function does not support variable-length
|
|
|
|
argument lists. In simple cases you can fake these by trying several
|
|
|
|
calls to
|
|
|
|
\code{getargs()} until one succeeds, but you must take care to call
|
|
|
|
\code{err_clear()} before each retry. For example:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
static object *my_method(self, args) object *self, *args; {
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
if (getargs(args, "(ii)", &i, &j)) {
|
|
|
|
k = 0; /* Use default third argument */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(iii)", &i, &j, &k))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* ... use i, j and k here ... */
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
(It is possible to think of an extension to the definition of format
|
1994-08-01 09:22:53 -03:00
|
|
|
strings to accommodate this directly, e.g. placing a \samp{|} in a
|
1993-11-05 13:11:16 -04:00
|
|
|
tuple might specify that the remaining arguments are optional.
|
|
|
|
\code{getargs()} should then return one more than the number of
|
|
|
|
variables stored into.)
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Advanced users note: If you set the `varargs' flag in the method list
|
|
|
|
for a function, the argument will always be a tuple (the `raw argument
|
|
|
|
list'). In this case you must enclose single and empty argument lists
|
1994-08-01 09:22:53 -03:00
|
|
|
in parentheses, e.g. \samp{(s)} and \samp{()}.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
\section{The {\tt mkvalue()} function}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
This function is the counterpart to \code{getargs()}. It is declared
|
1994-08-01 09:22:53 -03:00
|
|
|
in \file{Include/modsupport.h} as follows:
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
object *mkvalue(char *format, ...);
|
|
|
|
\end{verbatim}
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
It supports exactly the same format letters as \code{getargs()}, but
|
|
|
|
the arguments (which are input to the function, not output) must not
|
|
|
|
be pointers, just values. If a byte, short or float is passed to a
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
varargs function, it is widened by the compiler to int or double, so
|
1993-11-05 13:11:16 -04:00
|
|
|
\samp{b} and \samp{h} are treated as \samp{i} and \samp{f} is
|
|
|
|
treated as \samp{d}. \samp{S} is treated as \samp{O}, \samp{s} is
|
|
|
|
treated as \samp{z}. \samp{z\#} and \samp{s\#} are supported: a
|
|
|
|
second argument specifies the length of the data (negative means use
|
|
|
|
\code{strlen()}). \samp{S} and \samp{O} add a reference to their
|
|
|
|
argument (so you should \code{DECREF()} it if you've just created it
|
|
|
|
and aren't going to use it again).
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
If the argument for \samp{O} or \samp{S} is a \code{NULL} pointer, it is
|
1993-11-05 13:11:16 -04:00
|
|
|
assumed that this was caused because the call producing the argument
|
|
|
|
found an error and set an exception. Therefore, \code{mkvalue()} will
|
|
|
|
return \code{NULL} but won't set an exception if one is already set.
|
|
|
|
If no exception is set, \code{SystemError} is set.
|
|
|
|
|
|
|
|
If there is an error in the format string, the \code{SystemError}
|
|
|
|
exception is set, since it is the calling C code's fault, not that of
|
|
|
|
the Python user who sees the exception.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
return mkvalue("(ii)", 0, 0);
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
returns a tuple containing two zeros. (Outer parentheses in the
|
|
|
|
format string are actually superfluous, but you can use them for
|
1993-11-05 13:11:16 -04:00
|
|
|
compatibility with \code{getargs()}, which requires them if more than
|
|
|
|
one argument is expected.)
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\section{Reference counts}
|
|
|
|
|
1993-11-05 13:11:16 -04:00
|
|
|
Here's a useful explanation of \code{INCREF()} and \code{DECREF()}
|
|
|
|
(after an original by Sjoerd Mullender).
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Use \code{XINCREF()} or \code{XDECREF()} instead of \code{INCREF()} or
|
|
|
|
\code{DECREF()} when the argument may be \code{NULL} --- the versions
|
|
|
|
without \samp{X} are faster but wull dump core when they encounter a
|
|
|
|
\code{NULL} pointer.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
The basic idea is, if you create an extra reference to an object, you
|
1993-11-05 13:11:16 -04:00
|
|
|
must \code{INCREF()} it, if you throw away a reference to an object,
|
|
|
|
you must \code{DECREF()} it. Functions such as
|
|
|
|
\code{newstringobject()}, \code{newsizedstringobject()},
|
|
|
|
\code{newintobject()}, etc. create a reference to an object. If you
|
|
|
|
want to throw away the object thus created, you must use
|
|
|
|
\code{DECREF()}.
|
|
|
|
|
|
|
|
If you put an object into a tuple or list using \code{settupleitem()}
|
|
|
|
or \code{setlistitem()}, the idea is that you usually don't want to
|
|
|
|
keep a reference of your own around, so Python does not
|
|
|
|
\code{INCREF()} the elements. It does \code{DECREF()} the old value.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
This means that if you put something into such an object using the
|
1993-11-05 13:11:16 -04:00
|
|
|
functions Python provides for this, you must \code{INCREF()} the
|
|
|
|
object if you also want to keep a separate reference to the object around.
|
|
|
|
Also, if you replace an element, you should \code{INCREF()} the old
|
|
|
|
element first if you want to keep it. If you didn't \code{INCREF()}
|
|
|
|
it before you replaced it, you are not allowed to look at it anymore,
|
|
|
|
since it may have been freed.
|
|
|
|
|
|
|
|
Returning an object to Python (i.e. when your C function returns)
|
|
|
|
creates a reference to an object, but it does not change the reference
|
|
|
|
count. When your code does not keep another reference to the object,
|
|
|
|
you should not \code{INCREF()} or \code{DECREF()} it (assuming it is a
|
|
|
|
newly created object). When you do keep a reference around, you
|
|
|
|
should \code{INCREF()} the object. Also, when you return a global
|
|
|
|
object such as \code{None}, you should \code{INCREF()} it.
|
|
|
|
|
|
|
|
If you want to return a tuple, you should consider using
|
|
|
|
\code{mkvalue()}. This function creates a new tuple with a reference
|
|
|
|
count of 1 which you can return. If any of the elements you put into
|
|
|
|
the tuple are objects (format codes \samp{O} or \samp{S}), they
|
|
|
|
are \code{INCREF()}'ed by \code{mkvalue()}. If you don't want to keep
|
|
|
|
references to those elements around, you should \code{DECREF()} them
|
|
|
|
after having called \code{mkvalue()}.
|
|
|
|
|
|
|
|
Usually you don't have to worry about arguments. They are
|
|
|
|
\code{INCREF()}'ed before your function is called and
|
|
|
|
\code{DECREF()}'ed after your function returns. When you keep a
|
|
|
|
reference to an argument, you should \code{INCREF()} it and
|
|
|
|
\code{DECREF()} when you throw it away. Also, when you return an
|
|
|
|
argument, you should \code{INCREF()} it, because returning the
|
|
|
|
argument creates an extra reference to it.
|
|
|
|
|
|
|
|
If you use \code{getargs()} to parse the arguments, you can get a
|
|
|
|
reference to an object (by using \samp{O} in the format string). This
|
|
|
|
object was not \code{INCREF()}'ed, so you should not \code{DECREF()}
|
|
|
|
it. If you want to keep the object, you must \code{INCREF()} it
|
|
|
|
yourself.
|
|
|
|
|
|
|
|
If you create your own type of objects, you should use \code{NEWOBJ()}
|
|
|
|
to create the object. This sets the reference count to 1. If you
|
|
|
|
want to throw away the object, you should use \code{DECREF()}. When
|
|
|
|
the reference count reaches zero, your type's \code{dealloc()}
|
|
|
|
function is called. In it, you should \code{DECREF()} all object to
|
|
|
|
which you keep references in your object, but you should not use
|
|
|
|
\code{DECREF()} on your object. You should use \code{DEL()} instead.
|
|
|
|
|
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
\section{Writing extensions in \Cpp{}}
|
1993-11-05 13:11:16 -04:00
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
It is possible to write extension modules in \Cpp{}. Some restrictions
|
1993-11-05 13:11:16 -04:00
|
|
|
apply: since the main program (the Python interpreter) is compiled and
|
|
|
|
linked by the C compiler, global or static objects with constructors
|
|
|
|
cannot be used. All functions that will be called directly or
|
|
|
|
indirectly (i.e. via function pointers) by the Python interpreter will
|
|
|
|
have to be declared using \code{extern "C"}; this applies to all
|
|
|
|
`methods' as well as to the module's initialization function.
|
|
|
|
It is unnecessary to enclose the Python header files in
|
|
|
|
\code{extern "C" \{...\}} --- they do this already.
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
\chapter{Embedding Python in another application}
|
|
|
|
|
|
|
|
Embedding Python is similar to extending it, but not quite. The
|
|
|
|
difference is that when you extend Python, the main program of the
|
1994-08-08 09:30:22 -03:00
|
|
|
application is still the Python interpreter, while if you embed
|
1993-11-05 13:11:16 -04:00
|
|
|
Python, the main program may have nothing to do with Python ---
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
instead, some parts of the application occasionally call the Python
|
|
|
|
interpreter to run some Python code.
|
|
|
|
|
|
|
|
So if you are embedding Python, you are providing your own main
|
|
|
|
program. One of the things this main program has to do is initialize
|
|
|
|
the Python interpreter. At the very least, you have to call the
|
1993-11-05 13:11:16 -04:00
|
|
|
function \code{initall()}. There are optional calls to pass command
|
|
|
|
line arguments to Python. Then later you can call the interpreter
|
|
|
|
from any part of the application.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
There are several different ways to call the interpreter: you can pass
|
1993-11-05 13:11:16 -04:00
|
|
|
a string containing Python statements to \code{run_command()}, or you
|
|
|
|
can pass a stdio file pointer and a file name (for identification in
|
|
|
|
error messages only) to \code{run_script()}. You can also call the
|
|
|
|
lower-level operations described in the previous chapters to construct
|
|
|
|
and use Python objects.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
|
|
|
A simple demo of embedding Python can be found in the directory
|
1994-08-01 09:22:53 -03:00
|
|
|
\file{Demo/embed}.
|
1993-11-05 13:11:16 -04:00
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
\section{Embedding Python in \Cpp{}}
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
It is also possible to embed Python in a \Cpp{} program; precisely how this
|
|
|
|
is done will depend on the details of the \Cpp{} system used; in general you
|
|
|
|
will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
|
|
|
|
to compile and link your program. There is no need to recompile Python
|
|
|
|
itself using \Cpp{}.
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
\chapter{Dynamic Loading}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
On most modern systems it is possible to configure Python to support
|
|
|
|
dynamic loading of extension modules implemented in C. When shared
|
|
|
|
libraries are used dynamic loading is configured automatically;
|
|
|
|
otherwise you have to select it as a build option (see below). Once
|
|
|
|
configured, dynamic loading is trivial to use: when a Python program
|
1993-11-19 09:13:22 -04:00
|
|
|
executes \code{import foo}, the search for modules tries to find a
|
1994-08-01 09:22:53 -03:00
|
|
|
file \file{foomodule.o} (\file{foomodule.so} when using shared
|
|
|
|
libraries) in the module search path, and if one is found, it is
|
|
|
|
loaded into the executing binary and executed. Once loaded, the
|
|
|
|
module acts just like a built-in extension module.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
The advantages of dynamic loading are twofold: the `core' Python
|
|
|
|
binary gets smaller, and users can extend Python with their own
|
|
|
|
modules implemented in C without having to build and maintain their
|
|
|
|
own copy of the Python interpreter. There are also disadvantages:
|
|
|
|
dynamic loading isn't available on all systems (this just means that
|
|
|
|
on some systems you have to use static loading), and dynamically
|
|
|
|
loading a module that was compiled for a different version of Python
|
1994-08-01 09:22:53 -03:00
|
|
|
(e.g. with a different representation of objects) may dump core.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
\section{Configuring and building the interpreter for dynamic loading}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
There are three styles of dynamic loading: one using shared libraries,
|
|
|
|
one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
|
|
|
|
loading.
|
|
|
|
|
|
|
|
\subsection{Shared libraries}
|
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
The following systems support dynamic loading using shared libraries:
|
1994-08-01 09:22:53 -03:00
|
|
|
SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
|
|
|
|
systems derived from SVR4, or at least those SVR4 derivatives that
|
|
|
|
support shared libraries (are there any that don't?).
|
|
|
|
|
|
|
|
You don't need to do anything to configure dynamic loading on these
|
|
|
|
systems --- the \file{configure} detects the presence of the
|
|
|
|
\file{<dlfcn.h>} header file and automatically configures dynamic
|
|
|
|
loading.
|
|
|
|
|
|
|
|
\subsection{SGI dynamic loading}
|
|
|
|
|
|
|
|
Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
|
|
|
|
loading. (SGI IRIX 5 might also support it but it is inferior to
|
|
|
|
using shared libraries so there is no reason to; a small test didn't
|
|
|
|
work right away so I gave up trying to support it.)
|
|
|
|
|
|
|
|
Before you build Python, you first need to fetch and build the \code{dl}
|
|
|
|
package written by Jack Jansen. This is available by anonymous ftp
|
|
|
|
from host \file{ftp.cwi.nl}, directory \file{pub/dynload}, file
|
|
|
|
\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
|
|
|
|
instructions in the package's \file{README} file to build it.
|
|
|
|
|
|
|
|
Once you have built \code{dl}, you can configure Python to use it. To
|
|
|
|
this end, you run the \file{configure} script with the option
|
|
|
|
\code{--with-dl=\var{directory}} where \var{directory} is the absolute
|
|
|
|
pathname of the \code{dl} directory.
|
|
|
|
|
|
|
|
Now build and install Python as you normally would (see the
|
|
|
|
\file{README} file in the toplevel Python directory.)
|
|
|
|
|
|
|
|
\subsection{GNU dynamic loading}
|
|
|
|
|
|
|
|
GNU dynamic loading supports (according to its \file{README} file) the
|
|
|
|
following hardware and software combinations: VAX (Ultrix), Sun 3
|
|
|
|
(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
|
|
|
|
Atari ST. There is no reason to use it on a Sparc; I haven't seen a
|
|
|
|
Sun 3 for years so I don't know if these have shared libraries or not.
|
|
|
|
|
|
|
|
You need to fetch and build two packages. One is GNU DLD 3.2.3,
|
|
|
|
available by anonymous ftp from host \file{ftp.cwi.nl}, directory
|
|
|
|
\file{pub/dynload}, file \file{dld-3.2.3.tar.Z}. (As far as I know,
|
|
|
|
no further development on GNU DLD is being done.) The other is an
|
|
|
|
emulation of Jack Jansen's \code{dl} package that I wrote on top of
|
|
|
|
GNU DLD 3.2.3. This is available from the same host and directory,
|
|
|
|
file dl-dld-1.1.tar.Z. (The version number may change --- but I doubt
|
|
|
|
it will.) Follow the instructions in each package's \file{README}
|
|
|
|
file to configure build them.
|
|
|
|
|
|
|
|
Now configure Python. Run the \file{configure} script with the option
|
|
|
|
\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
|
|
|
|
\var{dl-directory} is the absolute pathname of the directory where you
|
|
|
|
have built the \file{dl-dld} package, and \var{dld-directory} is that
|
|
|
|
of the GNU DLD package. The Python interpreter you build hereafter
|
|
|
|
will support GNU dynamic loading.
|
|
|
|
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
\section{Building a dynamically loadable module}
|
|
|
|
|
|
|
|
Since there are three styles of dynamic loading, there are also three
|
|
|
|
groups of instructions for building a dynamically loadable module.
|
|
|
|
Instructions common for all three styles are given first. Assuming
|
|
|
|
your module is called \code{foo}, the source filename must be
|
|
|
|
\file{foomodule.c}, so the object name is \file{foomodule.o}. The
|
|
|
|
module must be written as a normal Python extension module (as
|
|
|
|
described earlier).
|
|
|
|
|
|
|
|
Note that in all cases you will have to create your own Makefile that
|
|
|
|
compiles your module file(s). This Makefile will have to pass two
|
|
|
|
\samp{-I} arguments to the C compiler which will make it find the
|
|
|
|
Python header files. If the Make variable \var{PYTHONTOP} points to
|
|
|
|
the toplevel Python directory, your \var{CFLAGS} Make variable should
|
|
|
|
contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
|
|
|
|
(Most header files are in the \file{Include} subdirectory, but the
|
|
|
|
\file{config.h} header lives in the toplevel directory.) You must
|
|
|
|
also add \samp{-DHAVE_CONFIG_H} to the definition of \var{CFLAGS} to
|
|
|
|
direct the Python headers to include \file{config.h}.
|
|
|
|
|
|
|
|
|
|
|
|
\subsection{Shared libraries}
|
|
|
|
|
|
|
|
You must link the \samp{.o} file to produce a shared library. This is
|
|
|
|
done using a special invocation of the \UNIX{} loader/linker, {\em
|
|
|
|
ld}(1). Unfortunately the invocation differs slightly per system.
|
|
|
|
|
|
|
|
On SunOS 4, use
|
|
|
|
\begin{verbatim}
|
|
|
|
ld foomodule.o -o foomodule.so
|
|
|
|
\end{verbatim}
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
On Solaris 2, use
|
1993-11-19 09:13:22 -04:00
|
|
|
\begin{verbatim}
|
1994-08-01 09:22:53 -03:00
|
|
|
ld -G foomodule.o -o foomodule.so
|
1993-11-19 09:13:22 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
On SGI IRIX 5, use
|
|
|
|
\begin{verbatim}
|
|
|
|
ld -shared foomodule.o -o foomodule.so
|
|
|
|
\end{verbatim}
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
On other systems, consult the manual page for {\em ld}(1) to find what
|
|
|
|
flags, if any, must be used.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
If your extension module uses system libraries that haven't already
|
|
|
|
been linked with Python (e.g. a windowing system), these must be
|
|
|
|
passed to the {\em ld} command as \samp{-l} options after the
|
|
|
|
\samp{.o} file.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
The resulting file \file{foomodule.so} must be copied into a directory
|
|
|
|
along the Python module search path.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
\subsection{SGI dynamic loading}
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
{bf IMPORTANT:} You must compile your extension module with the
|
|
|
|
additional C flag \samp{-G0} (or \samp{-G 0}). This instruct the
|
|
|
|
assembler to generate position-independent code.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
You don't need to link the resulting \file{foomodule.o} file; just
|
|
|
|
copy it into a directory along the Python module search path.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
The first time your extension is loaded, it takes some extra time and
|
|
|
|
a few messages may be printed. This creates a file
|
|
|
|
\file{foomodule.ld} which is an image that can be loaded quickly into
|
|
|
|
the Python interpreter process. When a new Python interpreter is
|
|
|
|
installed, the \code{dl} package detects this and rebuilds
|
|
|
|
\file{foomodule.ld}. The file \file{foomodule.ld} is placed in the
|
|
|
|
directory where \file{foomodule.o} was found, unless this directory is
|
|
|
|
unwritable; in that case it is placed in a temporary
|
|
|
|
directory.\footnote{Check the manual page of the \code{dl} package for
|
|
|
|
details.}
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
If your extension modules uses additional system libraries, you must
|
|
|
|
create a file \file{foomodule.libs} in the same directory as the
|
|
|
|
\file{foomodule.o}. This file should contain one or more lines with
|
|
|
|
whitespace-separated options that will be passed to the linker ---
|
|
|
|
normally only \samp{-l} options or absolute pathnames of libraries
|
|
|
|
(\samp{.a} files) should be used.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
\subsection{GNU dynamic loading}
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
Just copy \file{foomodule.o} into a directory along the Python module
|
|
|
|
search path.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
1994-08-01 09:22:53 -03:00
|
|
|
If your extension modules uses additional system libraries, you must
|
|
|
|
create a file \file{foomodule.libs} in the same directory as the
|
|
|
|
\file{foomodule.o}. This file should contain one or more lines with
|
|
|
|
whitespace-separated absolute pathnames of libraries (\samp{.a}
|
|
|
|
files). No \samp{-l} options can be used.
|
1993-11-19 09:13:22 -04:00
|
|
|
|
|
|
|
|
* ext.tex: documentation for extending, reference counts, and embedding
(formerly ../misc/{EXTENDING,REFCNT,EMBEDDING}). Also affects Makefile.
* text2latex.py: script to do part of the conversion from an plain ASCI
text file (in my particular style) to LaTeX.
(Chapter/section/subsection headers, and verbatim sections.)
* partparse.py, texipre.dat, fix.el, Makefile: Minor cleanup of latex ->
info conversion process (at least it works again, and with less
debugging output). Removed fix.sh.
* lib1.tex (section{Built-in Functions}): adapt description of str() and
repr() to new situation.
* lib3.tex (Module os): added exec*() variants.
* lib3.tex (Module posix): added execve().
* lib2.tex (Module array): documented reality; remove typecode and
itemsize, add byteswap, rename read/write to fromfile/tofile, and
re-alphabetized.
* lib1.tex (Built-in Functions): renamed bagof() to filter().
1993-11-05 10:45:11 -04:00
|
|
|
\input{ext.ind}
|
|
|
|
|
|
|
|
\end{document}
|