Commit 57641b95 authored by Fred Drake's avatar Fred Drake

Merge in changes from the 1.5.2p2 release.

parent b3e24d0d
...@@ -75,7 +75,7 @@ Python API is incorporated in a C source file by including the header ...@@ -75,7 +75,7 @@ Python API is incorporated in a C source file by including the header
\code{"Python.h"}. \code{"Python.h"}.
The compilation of an extension module depends on its intended use as The compilation of an extension module depends on its intended use as
well as on your system setup; details are given in a later section. well as on your system setup; details are given in later chapters.
\section{A Simple Example \section{A Simple Example
...@@ -95,7 +95,7 @@ as follows: ...@@ -95,7 +95,7 @@ as follows:
>>> status = spam.system("ls -l") >>> status = spam.system("ls -l")
\end{verbatim} \end{verbatim}
Begin by creating a file \file{spammodule.c}. (In general, if a Begin by creating a file \file{spammodule.c}. (Historically, if a
module is called \samp{spam}, the C file containing its implementation module is called \samp{spam}, the C file containing its implementation
is called \file{spammodule.c}; if the module name is very long, like is called \file{spammodule.c}; if the module name is very long, like
\samp{spammify}, the module name can be just \file{spammify.c}.) \samp{spammify}, the module name can be just \file{spammify.c}.)
...@@ -103,7 +103,7 @@ is called \file{spammodule.c}; if the module name is very long, like ...@@ -103,7 +103,7 @@ is called \file{spammodule.c}; if the module name is very long, like
The first line of our file can be: The first line of our file can be:
\begin{verbatim} \begin{verbatim}
#include "Python.h" #include <Python.h>
\end{verbatim} \end{verbatim}
which pulls in the Python API (you can add a comment describing the which pulls in the Python API (you can add a comment describing the
...@@ -163,7 +163,7 @@ store the converted values. More about this later. ...@@ -163,7 +163,7 @@ store the converted values. More about this later.
the right type and its components have been stored in the variables the right type and its components have been stored in the variables
whose addresses are passed. It returns false (zero) if an invalid whose addresses are passed. It returns false (zero) if an invalid
argument list was passed. In the latter case it also raises an argument list was passed. In the latter case it also raises an
appropriate exception by so the calling function can return appropriate exception so the calling function can return
\NULL{} immediately (as we saw in the example). \NULL{} immediately (as we saw in the example).
...@@ -197,7 +197,7 @@ exception. ...@@ -197,7 +197,7 @@ exception.
Another useful function is \cfunction{PyErr_SetFromErrno()}, which only Another useful function is \cfunction{PyErr_SetFromErrno()}, which only
takes an exception argument and constructs the associated value by takes an exception argument and constructs the associated value by
inspection of the (\UNIX{}) global variable \cdata{errno}. The most inspection of the global variable \cdata{errno}. The most
general function is \cfunction{PyErr_SetObject()}, which takes two object general function is \cfunction{PyErr_SetObject()}, which takes two object
arguments, the exception and its associated value. You don't need to arguments, the exception and its associated value. You don't need to
\cfunction{Py_INCREF()} the objects passed to any of these functions. \cfunction{Py_INCREF()} the objects passed to any of these functions.
...@@ -233,12 +233,12 @@ want to pass the error on to the interpreter but wants to handle it ...@@ -233,12 +233,12 @@ 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 completely by itself (e.g.\ by trying something else or pretending
nothing happened). nothing happened).
Note that a failing \cfunction{malloc()} call must be turned into an Every failing \cfunction{malloc()} call must be turned into an
exception --- the direct caller of \cfunction{malloc()} (or exception --- the direct caller of \cfunction{malloc()} (or
\cfunction{realloc()}) must call \cfunction{PyErr_NoMemory()} and \cfunction{realloc()}) must call \cfunction{PyErr_NoMemory()} and
return a failure indicator itself. All the object-creating functions return a failure indicator itself. All the object-creating functions
(\cfunction{PyInt_FromLong()} etc.) already do this, so only if you (for example, \cfunction{PyInt_FromLong()}) already do this, so this
call \cfunction{malloc()} directly this note is of importance. note is only relevant to those who call \cfunction{malloc()} directly.
Also note that, with the important exception of Also note that, with the important exception of
\cfunction{PyArg_ParseTuple()} and friends, functions that return an \cfunction{PyArg_ParseTuple()} and friends, functions that return an
...@@ -398,7 +398,8 @@ initspam() ...@@ -398,7 +398,8 @@ initspam()
\end{verbatim} \end{verbatim}
When the Python program imports module \module{spam} for the first When the Python program imports module \module{spam} for the first
time, \cfunction{initspam()} is called. It calls time, \cfunction{initspam()} is called. (See below for comments about
embedding Python.) It calls
\cfunction{Py_InitModule()}, which creates a ``module object'' (which \cfunction{Py_InitModule()}, which creates a ``module object'' (which
is inserted in the dictionary \code{sys.modules} under the key is inserted in the dictionary \code{sys.modules} under the key
\code{"spam"}), and inserts built-in function objects into the newly \code{"spam"}), and inserts built-in function objects into the newly
...@@ -409,6 +410,29 @@ that it creates (which is unused here). It aborts with a fatal error ...@@ -409,6 +410,29 @@ that it creates (which is unused here). It aborts with a fatal error
if the module could not be initialized satisfactorily, so the caller if the module could not be initialized satisfactorily, so the caller
doesn't need to check for errors. doesn't need to check for errors.
When embedding Python, the \cfunction{initspam()} function is not
called automatically unless there's an entry in the
\cdata{_PyImport_Inittab} table. The easiest way to handle this is to
statically initialize your statically-linked modules by directly
calling \cfunction{initspam()} after the call to
\cfunction{Py_Initialize()} or \cfunction{PyMac_Initialize()}:
\begin{verbatim}
int main(int argc, char **argv)
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initspam();
\end{verbatim}
And example may be found in the file \file{Demo/embed/demo.c} in the
Python source distribution.
\strong{Note:} Removing entries from \code{sys.modules} or importing \strong{Note:} Removing entries from \code{sys.modules} or importing
compiled modules into multiple interpreters within a process (or compiled modules into multiple interpreters within a process (or
following a \cfunction{fork()} without an intervening following a \cfunction{fork()} without an intervening
...@@ -416,6 +440,16 @@ following a \cfunction{fork()} without an intervening ...@@ -416,6 +440,16 @@ following a \cfunction{fork()} without an intervening
Extension module authors should exercise caution when initializing Extension module authors should exercise caution when initializing
internal data structures. internal data structures.
A more substantial example module is included in the Python source
distribution as \file{Modules/xxmodule.c}. This file may be used as a
template or simply read as an example. The \program{modulator.py}
script included in the source distribution or Windows install provides
a simple graphical user interface for declaring the functions and
objects which a module should implement, and can generate a template
which can be filled in. The script lives in the
\file{Tools/modulator/} directory; see the \file{README} file there
for more information.
\section{Compilation and Linkage \section{Compilation and Linkage
\label{compilation}} \label{compilation}}
...@@ -423,8 +457,10 @@ internal data structures. ...@@ -423,8 +457,10 @@ internal data structures.
There are two more things to do before you can use your new extension: There are two more things to do before you can use your new extension:
compiling and linking it with the Python system. If you use dynamic compiling and linking it with the Python system. If you use dynamic
loading, the details depend on the style of dynamic loading your loading, the details depend on the style of dynamic loading your
system uses; see the chapter ``Dynamic Loading'' for more information system uses; see the chapters about building extension modules on
about this. \UNIX{} (chapter \ref{building-on-unix}) and Windows (chapter
\ref{building-on-windows}) for more information about this.
% XXX Add information about MacOS
If you can't use dynamic loading, or if you want to make your module a 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 permanent part of the Python interpreter, you will have to change the
...@@ -480,8 +516,8 @@ definition: ...@@ -480,8 +516,8 @@ definition:
static PyObject *my_callback = NULL; static PyObject *my_callback = NULL;
static PyObject * static PyObject *
my_set_callback(dummy, arg) my_set_callback(dummy, args)
PyObject *dummy, *arg; PyObject *dummy, *args;
{ {
PyObject *result = NULL; PyObject *result = NULL;
PyObject *temp; PyObject *temp;
...@@ -629,6 +665,10 @@ format unit; and the entry in [square] brackets is the type of the C ...@@ -629,6 +665,10 @@ format unit; and the entry in [square] brackets is the type of the C
variable(s) whose address should be passed. (Use the \samp{\&} variable(s) whose address should be passed. (Use the \samp{\&}
operator to pass a variable's address.) operator to pass a variable's address.)
Note that any Python object references which are provided to the
caller are \emph{borrowed} references; do not decrement their
reference count!
\begin{description} \begin{description}
\item[\samp{s} (string) {[char *]}] \item[\samp{s} (string) {[char *]}]
...@@ -687,8 +727,8 @@ Store a Python object in a C object pointer. This is similar to ...@@ -687,8 +727,8 @@ Store a Python object in a C object pointer. This is similar to
\samp{O}, but takes two C arguments: the first is the address of a \samp{O}, but takes two C arguments: the first is the address of a
Python type object, the second is the address of the C variable (of Python type object, the second is the address of the C variable (of
type \ctype{PyObject *}) into which the object pointer is stored. type \ctype{PyObject *}) into which the object pointer is stored.
If the Python object does not have the required type, a If the Python object does not have the required type,
\exception{TypeError} exception is raised. \exception{TypeError} is raised.
\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}] \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
Convert a Python object to a C variable through a \var{converter} Convert a Python object to a C variable through a \var{converter}
...@@ -708,8 +748,8 @@ should raise an exception. ...@@ -708,8 +748,8 @@ should raise an exception.
\item[\samp{S} (string) {[PyStringObject *]}] \item[\samp{S} (string) {[PyStringObject *]}]
Like \samp{O} but requires that the Python object is a string object. Like \samp{O} but requires that the Python object is a string object.
Raises a \exception{TypeError} exception if the object is not a string Raises \exception{TypeError} if the object is not a string object.
object. The C variable may also be declared as \ctype{PyObject *}. The C variable may also be declared as \ctype{PyObject *}.
\item[\samp{t\#} (read-only character buffer) {[char *, int]}] \item[\samp{t\#} (read-only character buffer) {[char *, int]}]
Like \samp{s\#}, but accepts any object which implements the read-only Like \samp{s\#}, but accepts any object which implements the read-only
...@@ -740,7 +780,7 @@ may be nested. ...@@ -740,7 +780,7 @@ may be nested.
\strong{Note:} Prior to Python version 1.5.2, this format specifier \strong{Note:} Prior to Python version 1.5.2, this format specifier
only accepted a tuple containing the individual parameters, not an only accepted a tuple containing the individual parameters, not an
arbitrary sequence. Code which previously caused a arbitrary sequence. Code which previously caused
\exception{TypeError} to be raised here may now proceed without an \exception{TypeError} to be raised here may now proceed without an
exception. This is not expected to be a problem for existing code. exception. This is not expected to be a problem for existing code.
...@@ -1612,7 +1652,7 @@ comment lines that start with \character{\#}. ...@@ -1612,7 +1652,7 @@ comment lines that start with \character{\#}.
A module description line includes a module name, source files, A module description line includes a module name, source files,
options, variable references, and other input files, such options, variable references, and other input files, such
as libraries or object files. Consider a simple example:: as libraries or object files. Consider a simple example:
\begin{verbatim} \begin{verbatim}
ExtensionClass ExtensionClass.c ExtensionClass ExtensionClass.c
...@@ -1767,7 +1807,7 @@ This chapter briefly explains how to create a Windows extension module ...@@ -1767,7 +1807,7 @@ This chapter briefly explains how to create a Windows extension module
for Python using Microsoft Visual \Cpp{}, and follows with more for Python using Microsoft Visual \Cpp{}, and follows with more
detailed background information on how it works. The explanatory detailed background information on how it works. The explanatory
material is useful for both the Windows programmer learning to build material is useful for both the Windows programmer learning to build
Python extensions and the \UNIX{} programming interested in producing Python extensions and the \UNIX{} programmer interested in producing
software which can be successfully built on both \UNIX{} and Windows. software which can be successfully built on both \UNIX{} and Windows.
...@@ -1787,13 +1827,13 @@ Copy the \file{config.h} from the \file{PC/} directory into the ...@@ -1787,13 +1827,13 @@ Copy the \file{config.h} from the \file{PC/} directory into the
\file{include/} directory created by the installer. \file{include/} directory created by the installer.
Create a \file{Setup} file for your extension module, as described in Create a \file{Setup} file for your extension module, as described in
Chapter \ref{building-on-unix}. chapter \ref{building-on-unix}.
Get David Ascher's \file{compile.py} script from Get David Ascher's \file{compile.py} script from
\url{http://starship.python.net/crew/da/compile/}. Run the script to \url{http://starship.python.net/crew/da/compile/}. Run the script to
create Microsoft Visual \Cpp{} project files. create Microsoft Visual \Cpp{} project files.
Open the DSW file in V\Cpp{} and select \strong{Build}. Open the DSW file in Visual \Cpp{} and select \strong{Build}.
If your module creates a new type, you may have trouble with this line: If your module creates a new type, you may have trouble with this line:
...@@ -1827,7 +1867,7 @@ do this. ...@@ -1827,7 +1867,7 @@ do this.
loading of code. Before you try to build a module that can be loading of code. Before you try to build a module that can be
dynamically loaded, be aware of how your system works. dynamically loaded, be aware of how your system works.
In \UNIX{}, a shared object (.so) file contains code to be used by the In \UNIX{}, a shared object (\file{.so}) file contains code to be used by the
program, and also the names of functions and data that it expects to program, and also the names of functions and data that it expects to
find in the program. When the file is joined to the program, all find in the program. When the file is joined to the program, all
references to those functions and data in the file's code are changed references to those functions and data in the file's code are changed
...@@ -1925,7 +1965,8 @@ interpreter to run some Python code. ...@@ -1925,7 +1965,8 @@ interpreter to run some Python code.
So if you are embedding Python, you are providing your own main 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 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 the Python interpreter. At the very least, you have to call the
function \cfunction{Py_Initialize()}. There are optional calls to function \cfunction{Py_Initialize()} (on MacOS, call
\cfunction{PyMac_Initialize()} instead). There are optional calls to
pass command line arguments to Python. Then later you can call the pass command line arguments to Python. Then later you can call the
interpreter from any part of the application. interpreter from any part of the application.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment