blob: 1a9eba4a397e4b90ae4624a843eefb43e66929fd [file] [log] [blame]
Fred Drakedca87921998-01-13 16:53:23 +00001\documentclass[twoside,openright]{report}
Fred Drake1f8449a1998-01-09 05:36:43 +00002\usepackage{myformat}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00003
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004\title{Python/C API Reference Manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00005
6\input{boilerplate}
7
8\makeindex % tell \index to actually write the .idx file
9
10
11\begin{document}
12
Guido van Rossum9231c8f1997-05-15 21:43:21 +000013\maketitle
14
15\input{copyright}
16
17\begin{abstract}
18
19\noindent
Fred Drakee058b4f1998-02-16 06:15:35 +000020This manual documents the API used by \C{} (or \Cpp{}) programmers who
21want to write extension modules or embed Python. It is a companion to
22\emph{Extending and Embedding the Python Interpreter}, which describes
Guido van Rossum9231c8f1997-05-15 21:43:21 +000023the general principles of extension writing but does not document the
24API functions in detail.
25
Guido van Rossum5b8a5231997-12-30 04:38:44 +000026\strong{Warning:} The current version of this document is incomplete.
27I hope that it is nevertheless useful. I will continue to work on it,
28and release new versions from time to time, independent from Python
29source code releases.
30
Guido van Rossum9231c8f1997-05-15 21:43:21 +000031\end{abstract}
32
Fred Drake4d4f9e71998-01-13 22:25:02 +000033\tableofcontents
Guido van Rossum9231c8f1997-05-15 21:43:21 +000034
Guido van Rossum5060b3b1997-08-17 18:02:23 +000035% XXX Consider moving all this back to ext.tex and giving api.tex
36% XXX a *really* short intro only.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000037
38\chapter{Introduction}
Fred Drakef39ed671998-02-26 22:01:23 +000039\label{intro}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000040
Fred Drakeb0a78731998-01-13 18:51:10 +000041The Application Programmer's Interface to Python gives \C{} and \Cpp{}
Guido van Rossum59a61351997-08-14 20:34:33 +000042programmers access to the Python interpreter at a variety of levels.
Fred Drakeb0a78731998-01-13 18:51:10 +000043The API is equally usable from \Cpp{}, but for brevity it is generally
44referred to as the Python/\C{} API. There are two fundamentally
Fred Drakee058b4f1998-02-16 06:15:35 +000045different reasons for using the Python/\C{} API. The first reason is
46to write \emph{extension modules} for specific purposes; these are
47\C{} modules that extend the Python interpreter. This is probably the
48most common use. The second reason is to use Python as a component in
49a larger application; this technique is generally referred to as
50\dfn{embedding} Python in an application.
Guido van Rossum59a61351997-08-14 20:34:33 +000051
Guido van Rossum4a944d71997-08-14 20:35:38 +000052Writing an extension module is a relatively well-understood process,
53where a ``cookbook'' approach works well. There are several tools
54that automate the process to some extent. While people have embedded
55Python in other applications since its early existence, the process of
56embedding Python is less straightforward that writing an extension.
57Python 1.5 introduces a number of new API functions as well as some
58changes to the build process that make embedding much simpler.
Fred Drakee058b4f1998-02-16 06:15:35 +000059This manual describes the \version\ state of affair.
Guido van Rossum59a61351997-08-14 20:34:33 +000060% XXX Eventually, take the historical notes out
61
Guido van Rossum4a944d71997-08-14 20:35:38 +000062Many API functions are useful independent of whether you're embedding
63or extending Python; moreover, most applications that embed Python
64will need to provide a custom extension as well, so it's probably a
65good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000066attempting to embed Python in a real application.
67
Guido van Rossum580aa8d1997-11-25 15:34:51 +000068\section{Include Files}
Fred Drakef39ed671998-02-26 22:01:23 +000069\label{includes}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000070
71All function, type and macro definitions needed to use the Python/C
72API are included in your code by the following line:
73
Fred Drakee058b4f1998-02-16 06:15:35 +000074\begin{verbatim}
75#include "Python.h"
76\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000077
Fred Drakee058b4f1998-02-16 06:15:35 +000078This implies inclusion of the following standard headers:
79\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
80\code{<stdlib.h>} (if available).
Guido van Rossum580aa8d1997-11-25 15:34:51 +000081
82All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000083the included standard headers) have one of the prefixes \samp{Py} or
84\samp{_Py}. Names beginning with \samp{_Py} are for internal use
Guido van Rossum580aa8d1997-11-25 15:34:51 +000085only. Structure member names do not have a reserved prefix.
86
Fred Drakee058b4f1998-02-16 06:15:35 +000087\strong{Important:} user code should never define names that begin
88with \samp{Py} or \samp{_Py}. This confuses the reader, and
89jeopardizes the portability of the user code to future Python
90versions, which may define additional names beginning with one of
91these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000092
Guido van Rossum59a61351997-08-14 20:34:33 +000093\section{Objects, Types and Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +000094\label{objects}
Guido van Rossum59a61351997-08-14 20:34:33 +000095
Guido van Rossum580aa8d1997-11-25 15:34:51 +000096Most Python/C API functions have one or more arguments as well as a
97return value of type \code{PyObject *}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +000098to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +000099object. Since all Python object types are treated the same way by the
100Python language in most situations (e.g., assignments, scope rules,
101and argument passing), it is only fitting that they should be
Fred Drakeb0a78731998-01-13 18:51:10 +0000102represented by a single \C{} type. All Python objects live on the heap:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000103you never declare an automatic or static variable of type
Guido van Rossum4a944d71997-08-14 20:35:38 +0000104\code{PyObject}, only pointer variables of type \code{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +0000105be declared.
106
Fred Drakee058b4f1998-02-16 06:15:35 +0000107All Python objects (even Python integers) have a \dfn{type} and a
108\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000109it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakee058b4f1998-02-16 06:15:35 +0000110many more as explained in the \emph{Python Reference Manual}). For
Guido van Rossum4a944d71997-08-14 20:35:38 +0000111each of the well-known types there is a macro to check whether an
Fred Drakee058b4f1998-02-16 06:15:35 +0000112object is of that type; for instance, \samp{PyList_Check(\var{a})} is
113true iff the object pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000114
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000115\subsection{Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +0000116\label{refcounts}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000117
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000118The reference count is important because today's computers have a
Guido van Rossum4a944d71997-08-14 20:35:38 +0000119finite (and often severly limited) memory size; it counts how many
120different places there are that have a reference to an object. Such a
Fred Drakeb0a78731998-01-13 18:51:10 +0000121place could be another object, or a global (or static) \C{} variable, or
122a local variable in some \C{} function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000123becomes zero, the object is deallocated. If it contains references to
124other objects, their reference count is decremented. Those other
125objects may be deallocated in turn, if this decrement makes their
126reference count become zero, and so on. (There's an obvious problem
127with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000128``don't do that''.)
129
Guido van Rossum4a944d71997-08-14 20:35:38 +0000130Reference counts are always manipulated explicitly. The normal way is
Fred Drakee058b4f1998-02-16 06:15:35 +0000131to use the macro \cfunction{Py_INCREF()} to increment an object's
132reference count by one, and \cfunction{Py_DECREF()} to decrement it by
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000133one. The decref macro is considerably more complex than the incref one,
Guido van Rossum4a944d71997-08-14 20:35:38 +0000134since it must check whether the reference count becomes zero and then
135cause the object's deallocator, which is a function pointer contained
136in the object's type structure. The type-specific deallocator takes
137care of decrementing the reference counts for other objects contained
138in the object, and so on, if this is a compound object type such as a
139list. There's no chance that the reference count can overflow; at
140least as many bits are used to hold the reference count as there are
141distinct memory locations in virtual memory (assuming
142\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000143increment is a simple operation.
144
Guido van Rossum4a944d71997-08-14 20:35:38 +0000145It is not necessary to increment an object's reference count for every
146local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000147object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000148point to it and it goes down by one when the variable goes out of
149scope. However, these two cancel each other out, so at the end the
150reference count hasn't changed. The only real reason to use the
151reference count is to prevent the object from being deallocated as
152long as our variable is pointing to it. If we know that there is at
153least one other reference to the object that lives at least as long as
154our variable, there is no need to increment the reference count
155temporarily. An important situation where this arises is in objects
Fred Drakeb0a78731998-01-13 18:51:10 +0000156that are passed as arguments to \C{} functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000157that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000158reference to every argument for the duration of the call.
159
Fred Drakee058b4f1998-02-16 06:15:35 +0000160However, a common pitfall is to extract an object from a list and
161hold on to it for a while without incrementing its reference count.
162Some other operation might conceivably remove the object from the
163list, decrementing its reference count and possible deallocating it.
164The real danger is that innocent-looking operations may invoke
165arbitrary Python code which could do this; there is a code path which
166allows control to flow back to the user from a \cfunction{Py_DECREF()},
167so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000168
Guido van Rossum4a944d71997-08-14 20:35:38 +0000169A safe approach is to always use the generic operations (functions
Fred Drakee058b4f1998-02-16 06:15:35 +0000170whose name begins with \samp{PyObject_}, \samp{PyNumber_},
171\samp{PySequence_} or \samp{PyMapping_}). These operations always
Guido van Rossum4a944d71997-08-14 20:35:38 +0000172increment the reference count of the object they return. This leaves
Fred Drakee058b4f1998-02-16 06:15:35 +0000173the caller with the responsibility to call \cfunction{Py_DECREF()}
174when they are done with the result; this soon becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000175
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000176\subsubsection{Reference Count Details}
Fred Drakef39ed671998-02-26 22:01:23 +0000177\label{refcountDetails}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000178
179The reference count behavior of functions in the Python/C API is best
180expelained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000181talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000182shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000183properly --- either by passing ownership on (usually to its caller) or
184by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
185a function passes ownership of a reference on to its caller, the
186caller is said to receive a \emph{new} reference. When no ownership
187is transferred, the caller is said to \emph{borrow} the reference.
188Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000189
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000190Conversely, when calling a function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000191object, there are two possibilities: the function \emph{steals} a
192reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000193references; the two notable exceptions are
194\cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which
195steal a reference to the item (but not to the tuple or list into which
196the item it put!). These functions were designed to steal a reference
197because of a common idiom for populating a tuple or list with newly
198created objects; for example, the code to create the tuple \code{(1,
1992, "three")} could look like this (forgetting about error handling for
200the moment; a better way to code this is shown below anyway):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000201
202\begin{verbatim}
203PyObject *t;
204t = PyTuple_New(3);
205PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
206PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
207PyTuple_SetItem(t, 2, PyString_FromString("three"));
208\end{verbatim}
209
Fred Drakee058b4f1998-02-16 06:15:35 +0000210Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
211set tuple items; \cfunction{PySequence_SetItem()} and
212\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
213immutable data type. You should only use
214\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000215yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000216
217Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000218\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
219can also use \cfunction{PySequence_SetItem()}; this illustrates the
220difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000221
222\begin{verbatim}
223PyObject *l, *x;
224l = PyList_New(3);
225x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000226PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000227x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000228PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000229x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000230PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000231\end{verbatim}
232
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000233You might find it strange that the ``recommended'' approach takes more
234code. However, in practice, you will rarely use these ways of
235creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000236\cfunction{Py_BuildValue()}, that can create most common objects from
237\C{} values, directed by a \dfn{format string}. For example, the
238above two blocks of code could be replaced by the following (which
239also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000240
241\begin{verbatim}
242PyObject *t, *l;
243t = Py_BuildValue("(iis)", 1, 2, "three");
244l = Py_BuildValue("[iis]", 1, 2, "three");
245\end{verbatim}
246
Fred Drakee058b4f1998-02-16 06:15:35 +0000247It is much more common to use \cfunction{PyObject_SetItem()} and
248friends with items whose references you are only borrowing, like
249arguments that were passed in to the function you are writing. In
250that case, their behaviour regarding reference counts is much saner,
251since you don't have to increment a reference count so you can give a
252reference away (``have it be stolen''). For example, this function
253sets all items of a list (actually, any mutable sequence) to a given
254item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000255
256\begin{verbatim}
257int set_all(PyObject *target, PyObject *item)
258{
259 int i, n;
260 n = PyObject_Length(target);
261 if (n < 0)
262 return -1;
263 for (i = 0; i < n; i++) {
264 if (PyObject_SetItem(target, i, item) < 0)
265 return -1;
266 }
267 return 0;
268}
269\end{verbatim}
270
271The situation is slightly different for function return values.
272While passing a reference to most functions does not change your
273ownership responsibilities for that reference, many functions that
274return a referece to an object give you ownership of the reference.
275The reason is simple: in many cases, the returned object is created
276on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000277object. Therefore, the generic functions that return object
278references, like \cfunction{PyObject_GetItem()} and
279\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
280the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000281
282It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000283by a function depends on which function you call only --- \emph{the
284plumage} (i.e., the type of the type of the object passed as an
285argument to the function) \emph{doesn't enter into it!} Thus, if you
286extract an item from a list using \cfunction{PyList_GetItem()}, you
287don't own the reference --- but if you obtain the same item from the
288same list using \cfunction{PySequence_GetItem()} (which happens to
289take exactly the same arguments), you do own a reference to the
290returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000291
Fred Drakee058b4f1998-02-16 06:15:35 +0000292Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000293sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000294\cfunction{PyList_GetItem()}, once using
295\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000296
297\begin{verbatim}
298long sum_list(PyObject *list)
299{
300 int i, n;
301 long total = 0;
302 PyObject *item;
303 n = PyList_Size(list);
304 if (n < 0)
305 return -1; /* Not a list */
306 for (i = 0; i < n; i++) {
307 item = PyList_GetItem(list, i); /* Can't fail */
308 if (!PyInt_Check(item)) continue; /* Skip non-integers */
309 total += PyInt_AsLong(item);
310 }
311 return total;
312}
313\end{verbatim}
314
315\begin{verbatim}
316long sum_sequence(PyObject *sequence)
317{
318 int i, n;
319 long total = 0;
320 PyObject *item;
321 n = PyObject_Size(list);
322 if (n < 0)
323 return -1; /* Has no length */
324 for (i = 0; i < n; i++) {
325 item = PySequence_GetItem(list, i);
326 if (item == NULL)
327 return -1; /* Not a sequence, or other failure */
328 if (PyInt_Check(item))
329 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000330 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000331 }
332 return total;
333}
334\end{verbatim}
335
336\subsection{Types}
Fred Drakef39ed671998-02-26 22:01:23 +0000337\label{types}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000338
339There are few other data types that play a significant role in
Fred Drakeb0a78731998-01-13 18:51:10 +0000340the Python/C API; most are simple \C{} types such as \code{int},
Guido van Rossum4a944d71997-08-14 20:35:38 +0000341\code{long}, \code{double} and \code{char *}. A few structure types
342are used to describe static tables used to list the functions exported
343by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000344be discussed together with the functions that use them.
345
346\section{Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000347\label{exceptions}
Guido van Rossum59a61351997-08-14 20:34:33 +0000348
Guido van Rossum4a944d71997-08-14 20:35:38 +0000349The Python programmer only needs to deal with exceptions if specific
350error handling is required; unhandled exceptions are automatically
351propagated to the caller, then to the caller's caller, and so on, till
352they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000353user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000354
Fred Drakeb0a78731998-01-13 18:51:10 +0000355For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000356All functions in the Python/C API can raise exceptions, unless an
357explicit claim is made otherwise in a function's documentation. In
358general, when a function encounters an error, it sets an exception,
359discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000360error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000361return a Boolean true/false result, with false indicating an error.
362Very few functions return no explicit error indicator or have an
363ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000364\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000365
366Exception state is maintained in per-thread storage (this is
367equivalent to using global storage in an unthreaded application). A
Fred Drakee058b4f1998-02-16 06:15:35 +0000368thread can be on one of two states: an exception has occurred, or not.
369The function \cfunction{PyErr_Occurred()} can be used to check for
370this: it returns a borrowed reference to the exception type object
371when an exception has occurred, and \NULL{} otherwise. There are a
372number of functions to set the exception state:
373\cfunction{PyErr_SetString()} is the most common (though not the most
374general) function to set the exception state, and
375\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000376
377The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000378be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000379value, and the traceback. These have the same meanings as the Python
380object \code{sys.exc_type}, \code{sys.exc_value},
381\code{sys.exc_traceback}; however, they are not the same: the Python
382objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000383\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
384exception state only exists while an exception is being passed on
385between \C{} functions until it reaches the Python interpreter, which
386takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000387
388(Note that starting with Python 1.5, the preferred, thread-safe way to
389access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000390\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000391for Python code. Also, the semantics of both ways to access the
392exception state have changed so that a function which catches an
393exception will save and restore its thread's exception state so as to
394preserve the exception state of its caller. This prevents common bugs
395in exception handling code caused by an innocent-looking function
396overwriting the exception being handled; it also reduces the often
397unwanted lifetime extension for objects that are referenced by the
398stack frames in the traceback.)
399
400As a general principle, a function that calls another function to
401perform some task should check whether the called function raised an
402exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000403should discard any object references that it owns, and returns an
404error indicator, but it should \emph{not} set another exception ---
405that would overwrite the exception that was just raised, and lose
406important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000407
408A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000409in the \cfunction{sum_sequence()} example above. It so happens that
410that example doesn't need to clean up any owned references when it
411detects an error. The following example function shows some error
412cleanup. First, to remind you why you like Python, we show the
413equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000414
415\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000416def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000417 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000418 item = dict[key]
419 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000420 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000421 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000422\end{verbatim}
423
Fred Drakeb0a78731998-01-13 18:51:10 +0000424Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000425
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000426\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000427int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000428{
429 /* Objects all initialized to NULL for Py_XDECREF */
430 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000431 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000432
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000433 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000434 if (item == NULL) {
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000435 /* Handle keyError only: */
436 if (!PyErr_ExceptionMatches(PyExc_keyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000437
438 /* Clear the error and use zero: */
439 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000440 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000441 if (item == NULL) goto error;
442 }
443
444 const_one = PyInt_FromLong(1L);
445 if (const_one == NULL) goto error;
446
447 incremented_item = PyNumber_Add(item, const_one);
448 if (incremented_item == NULL) goto error;
449
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000450 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000451 rv = 0; /* Success */
452 /* Continue with cleanup code */
453
454 error:
455 /* Cleanup code, shared by success and failure path */
456
457 /* Use Py_XDECREF() to ignore NULL references */
458 Py_XDECREF(item);
459 Py_XDECREF(const_one);
460 Py_XDECREF(incremented_item);
461
462 return rv; /* -1 for error, 0 for success */
463}
464\end{verbatim}
465
466This example represents an endorsed use of the \code{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000467in \C{}! It illustrates the use of
468\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
469handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
470dispose of owned references that may be \NULL{} (note the \samp{X} in
471the name; \cfunction{Py_DECREF()} would crash when confronted with a
472\NULL{} reference). It is important that the variables used to hold
473owned references are initialized to \NULL{} for this to work;
474likewise, the proposed return value is initialized to \code{-1}
475(failure) and only set to success after the final call made is
476successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000477
Guido van Rossum59a61351997-08-14 20:34:33 +0000478
479\section{Embedding Python}
Fred Drakef39ed671998-02-26 22:01:23 +0000480\label{embedding}
Guido van Rossum59a61351997-08-14 20:34:33 +0000481
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000482The one important task that only embedders (as opposed to extension
483writers) of the Python interpreter have to worry about is the
484initialization, and possibly the finalization, of the Python
485interpreter. Most functionality of the interpreter can only be used
486after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000487
Fred Drakee058b4f1998-02-16 06:15:35 +0000488The basic initialization function is \cfunction{Py_Initialize()}.
489This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000490fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
491\module{__main__}\refbimodindex{__main__} and
492\module{sys}\refbimodindex{sys}. It also initializes the module
493search path (\code{sys.path}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000494
Fred Drakee058b4f1998-02-16 06:15:35 +0000495\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000496(\code{sys.argv}). If this variable is needed by Python code that
497will be executed later, it must be set explicitly with a call to
498\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000499to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000500
Fred Drakeb0a78731998-01-13 18:51:10 +0000501On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000502details are slightly different), \cfunction{Py_Initialize()}
503calculates the module search path based upon its best guess for the
504location of the standard Python interpreter executable, assuming that
505the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000506interpreter executable. In particular, it looks for a directory named
Fred Drakee058b4f1998-02-16 06:15:35 +0000507\file{lib/python\version} (replacing \file{\version} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000508interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000509executable named \file{python} is found on the shell command search
Guido van Rossum42cefd01997-10-05 15:27:29 +0000510path (the environment variable \code{\$PATH}).
511
512For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000513\file{/usr/local/bin/python}, it will assume that the libraries are in
514\file{/usr/local/lib/python\version}. (In fact, this particular path
515is also the ``fallback'' location, used when no executable file named
516\file{python} is found along \code{\$PATH}.) The user can override
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000517this behavior by setting the environment variable \code{\$PYTHONHOME},
518or insert additional directories in front of the standard path by
519setting \code{\$PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000520
Guido van Rossum4a944d71997-08-14 20:35:38 +0000521The embedding application can steer the search by calling
522\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakee058b4f1998-02-16 06:15:35 +0000523\cfunction{Py_Initialize()}. Note that \code{\$PYTHONHOME} still
524overrides this and \code{\$PYTHONPATH} is still inserted in front of
525the standard path. An application that requires total control has to
526provide its own implementation of \cfunction{Py_GetPath()},
527\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
528\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000529\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000530
Guido van Rossum4a944d71997-08-14 20:35:38 +0000531Sometimes, it is desirable to ``uninitialize'' Python. For instance,
532the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000533\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000534use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000535can be accomplished by calling \cfunction{Py_Finalize()}. The function
536\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000537initialized state. More information about these functions is given in
538a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000539
Guido van Rossum4a944d71997-08-14 20:35:38 +0000540
Fred Drakee5bf8b21998-02-12 21:22:28 +0000541\chapter{The Very High Level Layer}
Fred Drakef39ed671998-02-26 22:01:23 +0000542\label{veryhigh}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000543
Fred Drakee5bf8b21998-02-12 21:22:28 +0000544The functions in this chapter will let you execute Python source code
545given in a file or a buffer, but they will not let you interact in a
546more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000547
Fred Drakee5bf8b21998-02-12 21:22:28 +0000548\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000549\end{cfuncdesc}
550
Fred Drakee5bf8b21998-02-12 21:22:28 +0000551\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000552\end{cfuncdesc}
553
Fred Drakee5bf8b21998-02-12 21:22:28 +0000554\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *}
555\end{cfuncdesc}
556
557\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *}
558\end{cfuncdesc}
559
560\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *}
561\end{cfuncdesc}
562
563\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int}
564\end{cfuncdesc}
565
566\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int}
567\end{cfuncdesc}
568
569\begin{cfuncdesc}{PyObject *}{PyRun_String}{char *, int, PyObject *, PyObject *}
570\end{cfuncdesc}
571
572\begin{cfuncdesc}{PyObject *}{PyRun_File}{FILE *, char *, int, PyObject *, PyObject *}
573\end{cfuncdesc}
574
575\begin{cfuncdesc}{PyObject *}{Py_CompileString}{char *, char *, int}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000576\end{cfuncdesc}
577
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000578
579\chapter{Reference Counting}
Fred Drakef39ed671998-02-26 22:01:23 +0000580\label{countingRefs}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000581
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000582The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000583of Python objects.
584
585\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
586Increment the reference count for object \code{o}. The object must
587not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000588\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000589\end{cfuncdesc}
590
591\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000592Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000593\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000594\end{cfuncdesc}
595
596\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000597Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000598not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000599\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
600object's type's deallocation function (which must not be \NULL{}) is
601invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000602
603\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000604code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000605method is deallocated). While exceptions in such code are not
606propagated, the executed code has free access to all Python global
607variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000608variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000609invoked. For example, code to delete an object from a list should
610copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000611the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000612temporary variable.
613\end{cfuncdesc}
614
615\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000616Decrement the reference count for object \var{o}. The object may be
617\NULL{}, in which case the macro has no effect; otherwise the effect
618is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000619applies.
620\end{cfuncdesc}
621
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000622The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000623\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
624\cfunction{_Py_NewReference()}, as well as the global variable
625\code{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000626
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000627XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
628PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
629PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
630
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000631
632\chapter{Exception Handling}
Fred Drakef39ed671998-02-26 22:01:23 +0000633\label{exceptionHandling}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000634
635The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000636exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000637Python exception handling. It works somewhat like the \UNIX{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000638\code{errno} variable: there is a global indicator (per thread) of the
639last error that occurred. Most functions don't clear this on success,
640but will set it to indicate the cause of the error on failure. Most
641functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000642supposed to return a pointer, or \code{-1} if they return an integer
643(exception: the \code{PyArg_Parse*()} functions return \code{1} for
644success and \code{0} for failure). When a function must fail because
645some function it called failed, it generally doesn't set the error
646indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000647
648The error indicator consists of three Python objects corresponding to
649the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
650\code{sys.exc_traceback}. API functions exist to interact with the
651error indicator in various ways. There is a separate error indicator
652for each thread.
653
654% XXX Order of these should be more thoughtful.
655% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000656
657\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000658Print a standard traceback to \code{sys.stderr} and clear the error
659indicator. Call this function only when the error indicator is set.
660(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000661\end{cfuncdesc}
662
Guido van Rossumae110af1997-05-22 20:11:52 +0000663\begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{}
664Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000665\emph{type} (the first argument to the last call to one of the
666\code{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
667not set, return \NULL{}. You do not own a reference to the return
668value, so you do not need to \cfunction{Py_DECREF()} it.
669\strong{Note:} do not compare the return value to a specific
670exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
671below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000672\end{cfuncdesc}
673
674\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000675Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000676\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000677This should only be called when an exception is actually set.
678\end{cfuncdesc}
679
680\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000681Return true if the \var{given} exception matches the exception in
682\var{exc}. If \var{exc} is a class object, this also returns true
683when \var{given} is a subclass. If \var{exc} is a tuple, all
684exceptions in the tuple (and recursively in subtuples) are searched
685for a match. This should only be called when an exception is actually
686set.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000690Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000691\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
692\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
693instance of the same class. This function can be used to instantiate
694the class in that case. If the values are already normalized, nothing
695happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000696\end{cfuncdesc}
697
698\begin{cfuncdesc}{void}{PyErr_Clear}{}
699Clear the error indicator. If the error indicator is not set, there
700is no effect.
701\end{cfuncdesc}
702
703\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
704Retrieve the error indicator into three variables whose addresses are
705passed. If the error indicator is not set, set all three variables to
706\NULL{}. If it is set, it will be cleared and you own a reference to
707each object retrieved. The value and traceback object may be \NULL{}
708even when the type object is not. \strong{Note:} this function is
709normally only used by code that needs to handle exceptions or by code
710that needs to save and restore the error indicator temporarily.
711\end{cfuncdesc}
712
713\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
714Set the error indicator from the three objects. If the error
715indicator is already set, it is cleared first. If the objects are
716\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
717and non-\NULL{} value or traceback. The exception type should be a
718string or class; if it is a class, the value should be an instance of
719that class. Do not pass an invalid exception type or value.
720(Violating these rules will cause subtle problems later.) This call
721takes away a reference to each object, i.e. you must own a reference
722to each object before the call and after the call you no longer own
723these references. (If you don't understand this, don't use this
724function. I warned you.) \strong{Note:} this function is normally
725only used by code that needs to save and restore the error indicator
726temporarily.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
730This is the most common way to set the error indicator. The first
731argument specifies the exception type; it is normally one of the
732standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
733increment its reference count. The second argument is an error
734message; it is converted to a string object.
735\end{cfuncdesc}
736
737\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000738This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000739specify an arbitrary Python object for the ``value'' of the exception.
740You need not increment its reference count.
741\end{cfuncdesc}
742
743\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000744This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000745\end{cfuncdesc}
746
747\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000748This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000749\var{message})}, where \var{message} indicates that a built-in operation
750was invoked with an illegal argument. It is mostly for internal use.
751\end{cfuncdesc}
752
753\begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000754This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000755returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000756\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000757\end{cfuncdesc}
758
759\begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000760This is a convenience function to raise an exception when a \C{} library
761function has returned an error and set the \C{} variable \code{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000762It constructs a tuple object whose first item is the integer
763\code{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000764message (gotten from \cfunction{strerror()}), and then calls
765\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
766the \code{errno} value is \constant{EINTR}, indicating an interrupted
767system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000768the error indicator, leaves it set to that. The function always
769returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000770\samp{return PyErr_SetFromErrno();} when the system call returns an
771error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000772\end{cfuncdesc}
773
774\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000775This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000776\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000777operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000778argument. It is mostly for internal use.
779\end{cfuncdesc}
780
781\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
782This function interacts with Python's signal handling. It checks
783whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000784corresponding signal handler. If the
785\module{signal}\refbimodindex{signal} module is supported, this can
786invoke a signal handler written in Python. In all cases, the default
787effect for \constant{SIGINT} is to raise the
788\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000789error indicator is set and the function returns \code{1}; otherwise
790the function returns \code{0}. The error indicator may or may not be
791cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000792\end{cfuncdesc}
793
794\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
795This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000796the effect of a \constant{SIGINT} signal arriving --- the next time
797\cfunction{PyErr_CheckSignals()} is called,
798\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000799\end{cfuncdesc}
800
Guido van Rossum42cefd01997-10-05 15:27:29 +0000801\begin{cfuncdesc}{PyObject *}{PyErr_NewException}{char *name,
802PyObject *base, PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000803This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000804\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000805of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000806arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000807object derived from the root for all exceptions, the built-in name
Fred Drakee058b4f1998-02-16 06:15:35 +0000808\exception{Exception} (accessible in \C{} as \code{PyExc_Exception}).
809In this case the \code{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000810first part (up to the last dot) of the \var{name} argument, and the
811class name is set to the last part (after the last dot). When the
812user has specified the \code{-X} command line option to use string
813exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000814argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000815created from the entire \var{name} argument is returned. The
816\var{base} argument can be used to specify an alternate base class.
817The \var{dict} argument can be used to specify a dictionary of class
818variables and methods.
819\end{cfuncdesc}
820
821
Guido van Rossumae110af1997-05-22 20:11:52 +0000822\section{Standard Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000823\label{standardExceptions}
Guido van Rossumae110af1997-05-22 20:11:52 +0000824
825All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000826names are \samp{PyExc_} followed by the Python exception name.
827These have the type \code{PyObject *}; they are all either class
828objects or string objects, depending on the use of the \code{-X}
829option to the interpreter. For completeness, here are all the
Fred Drake9d20ac31998-02-16 15:27:08 +0000830variables:
Guido van Rossum42cefd01997-10-05 15:27:29 +0000831\code{PyExc_Exception},
832\code{PyExc_StandardError},
833\code{PyExc_ArithmeticError},
834\code{PyExc_LookupError},
Guido van Rossumae110af1997-05-22 20:11:52 +0000835\code{PyExc_AssertionError},
836\code{PyExc_AttributeError},
837\code{PyExc_EOFError},
838\code{PyExc_FloatingPointError},
839\code{PyExc_IOError},
840\code{PyExc_ImportError},
841\code{PyExc_IndexError},
842\code{PyExc_KeyError},
843\code{PyExc_KeyboardInterrupt},
844\code{PyExc_MemoryError},
845\code{PyExc_NameError},
846\code{PyExc_OverflowError},
847\code{PyExc_RuntimeError},
848\code{PyExc_SyntaxError},
849\code{PyExc_SystemError},
850\code{PyExc_SystemExit},
851\code{PyExc_TypeError},
852\code{PyExc_ValueError},
853\code{PyExc_ZeroDivisionError}.
854
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000855
856\chapter{Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000857\label{utilities}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000858
859The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000860parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000861values.
862
Guido van Rossum42cefd01997-10-05 15:27:29 +0000863\section{OS Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000864\label{os}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000865
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000866\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000867Return true (nonzero) if the standard I/O file \var{fp} with name
868\var{filename} is deemed interactive. This is the case for files for
869which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000870\code{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000871the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000872the strings \code{"<stdin>"} or \code{"???"}.
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000876Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000877The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000878the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000879\end{cfuncdesc}
880
881
Fred Drakee5bf8b21998-02-12 21:22:28 +0000882\section{Process Control}
Fred Drakef39ed671998-02-26 22:01:23 +0000883\label{processControl}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000884
885\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
886Print a fatal error message and kill the process. No cleanup is
887performed. This function should only be invoked when a condition is
888detected that would make it dangerous to continue using the Python
889interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000890corrupted. On \UNIX{}, the standard \C{} library function
891\cfunction{abort()} is called which will attempt to produce a
892\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000893\end{cfuncdesc}
894
895\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000896Exit the current process. This calls \cfunction{Py_Finalize()} and
897then calls the standard \C{} library function
898\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000899\end{cfuncdesc}
900
901\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
902Register a cleanup function to be called by \cfunction{Py_Finalize()}.
903The cleanup function will be called with no arguments and should
904return no value. At most 32 cleanup functions can be registered.
905When the registration is successful, \cfunction{Py_AtExit()} returns
906\code{0}; on failure, it returns \code{-1}. The cleanup function
907registered last is called first. Each cleanup function will be called
908at most once. Since Python's internal finallization will have
909completed before the cleanup function, no Python APIs should be called
910by \var{func}.
911\end{cfuncdesc}
912
913
914\section{Importing Modules}
Fred Drakef39ed671998-02-26 22:01:23 +0000915\label{importing}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000916
917\begin{cfuncdesc}{PyObject *}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000918This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000919below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000920\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000921it specifies a submodule of a package), the \var{fromlist} argument is
922set to the list \code{['*']} so that the return value is the named
923module rather than the top-level package containing it as would
924otherwise be the case. (Unfortunately, this has an additional side
925effect when \var{name} in fact specifies a subpackage instead of a
926submodule: the submodules specified in the package's \code{__all__}
927variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000928or \NULL{} with an exception set on failure (the module may still
Guido van Rossum42cefd01997-10-05 15:27:29 +0000929be created in this case).
930\end{cfuncdesc}
931
932\begin{cfuncdesc}{PyObject *}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000933Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000934Python function \function{__import__()}\bifuncindex{__import__}, as
935the standard \function{__import__()} function calls this function
936directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000937
Guido van Rossum42cefd01997-10-05 15:27:29 +0000938The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000939top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000940(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000941\function{__import__()}, the return value when a submodule of a
942package was requested is normally the top-level package, unless a
943non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000944\end{cfuncdesc}
945
946\begin{cfuncdesc}{PyObject *}{PyImport_Import}{PyObject *name}
947This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000948function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000949\code{__builtins__} of the current globals. This means that the
950import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000951current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
952\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000953\end{cfuncdesc}
954
955\begin{cfuncdesc}{PyObject *}{PyImport_ReloadModule}{PyObject *m}
956Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000957Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000958\function{reload()} function calls this function directly. Return a
959new reference to the reloaded module, or \NULL{} with an exception set
960on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000961\end{cfuncdesc}
962
963\begin{cfuncdesc}{PyObject *}{PyImport_AddModule}{char *name}
964Return the module object corresponding to a module name. The
965\var{name} argument may be of the form \code{package.module}). First
966check the modules dictionary if there's one there, and if not, create
967a new one and insert in in the modules dictionary. Because the former
968action is most common, this does not return a new reference, and you
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000969do not own the returned reference. Return \NULL{} with an
Guido van Rossum42cefd01997-10-05 15:27:29 +0000970exception set on failure.
971\end{cfuncdesc}
972
973\begin{cfuncdesc}{PyObject *}{PyImport_ExecCodeModule}{char *name, PyObject *co}
974Given a module name (possibly of the form \code{package.module}) and a
975code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000976built-in function \function{compile()}\bifuncindex{compile}, load the
977module. Return a new reference to the module object, or \NULL{} with
978an exception set if an error occurred (the module may still be created
979in this case). (This function would reload the module if it was
980already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000981\end{cfuncdesc}
982
983\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000984Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
985and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000986first four bytes of the bytecode file, in little-endian byte order.
987\end{cfuncdesc}
988
989\begin{cfuncdesc}{PyObject *}{PyImport_GetModuleDict}{}
990Return the dictionary used for the module administration
991(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
992variable.
993\end{cfuncdesc}
994
995\begin{cfuncdesc}{void}{_PyImport_Init}{}
996Initialize the import mechanism. For internal use only.
997\end{cfuncdesc}
998
999\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
1000Empty the module table. For internal use only.
1001\end{cfuncdesc}
1002
1003\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1004Finalize the import mechanism. For internal use only.
1005\end{cfuncdesc}
1006
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001007\begin{cfuncdesc}{extern PyObject *}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001008For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001009\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001010
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001011\begin{cfuncdesc}{extern PyObject *}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001012For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001013\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001014
1015\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1016Load a frozen module. Return \code{1} for success, \code{0} if the
1017module is not found, and \code{-1} with an exception set if the
1018initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001019load, use \cfunction{PyImport_ImportModule()}.
1020(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001021already imported.)
1022\end{cfuncdesc}
1023
1024\begin{ctypedesc}{struct _frozen}
1025This is the structure type definition for frozen module descriptors,
1026as generated by the \code{freeze} utility (see \file{Tools/freeze/} in
1027the Python source distribution). Its definition is:
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001028\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001029struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001030 char *name;
1031 unsigned char *code;
1032 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001033};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001034\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001035\end{ctypedesc}
1036
1037\begin{cvardesc}{struct _frozen *}{PyImport_FrozenModules}
1038This pointer is initialized to point to an array of \code{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001039_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001040or zero. When a frozen module is imported, it is searched in this
1041table. Third party code could play tricks with this to provide a
1042dynamically created collection of frozen modules.
1043\end{cvardesc}
1044
1045
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001046\chapter{Abstract Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001047\label{abstract}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001048
1049The functions in this chapter interact with Python objects regardless
1050of their type, or with wide classes of object types (e.g. all
1051numerical types, or all sequence types). When used on object types
1052for which they do not apply, they will flag a Python exception.
1053
1054\section{Object Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001055\label{object}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001056
1057\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001058Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001059The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001060options. The only option currently supported is
1061\constant{Py_Print_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001062\end{cfuncdesc}
1063
1064\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001065Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1066\code{0} otherwise. This is equivalent to the Python expression
1067\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001068This function always succeeds.
1069\end{cfuncdesc}
1070
1071\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001072Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001073Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001074This is the equivalent of the Python expression
1075\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001076\end{cfuncdesc}
1077
1078
1079\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001080Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1081\code{0} otherwise. This is equivalent to the Python expression
1082\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001083This function always succeeds.
1084\end{cfuncdesc}
1085
1086
1087\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001088Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001089Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001090This is the equivalent of the Python expression
1091\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001092\end{cfuncdesc}
1093
1094
1095\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001096Set the value of the attribute named \var{attr_name}, for object
1097\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1098the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1099\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001100\end{cfuncdesc}
1101
1102
1103\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001104Set the value of the attribute named \var{attr_name}, for
1105object \var{o},
1106to the value \var{v}. Returns \code{-1} on failure. This is
1107the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1108\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001109\end{cfuncdesc}
1110
1111
1112\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001113Delete attribute named \var{attr_name}, for object \var{o}. Returns
1114\code{-1} on failure. This is the equivalent of the Python
1115statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001116\end{cfuncdesc}
1117
1118
1119\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001120Delete attribute named \var{attr_name}, for object \var{o}. Returns
1121\code{-1} on failure. This is the equivalent of the Python
1122statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001123\end{cfuncdesc}
1124
1125
1126\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001127Compare the values of \var{o1} and \var{o2} using a routine provided
1128by \var{o1}, if one exists, otherwise with a routine provided by
1129\var{o2}. The result of the comparison is returned in \var{result}.
1130Returns \code{-1} on failure. This is the equivalent of the Python
1131statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001132\end{cfuncdesc}
1133
1134
1135\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001136Compare the values of \var{o1} and \var{o2} using a routine provided
1137by \var{o1}, if one exists, otherwise with a routine provided by
1138\var{o2}. Returns the result of the comparison on success. On error,
1139the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1140detect an error. This is equivalent to the
1141Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001142\end{cfuncdesc}
1143
1144
1145\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001146Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001147string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001148the equivalent of the Python expression \samp{repr(\var{o})}.
1149Called by the \function{repr()}\bifuncindex{repr} built-in function
1150and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001151\end{cfuncdesc}
1152
1153
1154\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001155Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001156string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001157the equivalent of the Python expression \samp{str(\var{o})}.
1158Called by the \function{str()}\bifuncindex{str} built-in function and
1159by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001160\end{cfuncdesc}
1161
1162
1163\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001164Determine if the object \var{o}, is callable. Return \code{1} if the
1165object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001166This function always succeeds.
1167\end{cfuncdesc}
1168
1169
1170\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001171Call a callable Python object \var{callable_object}, with
1172arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001173needed, then args may be \NULL{}. Returns the result of the
1174call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001175of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001176\end{cfuncdesc}
1177
1178\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001179Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001180variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001181using a \cfunction{Py_BuildValue()} style format string. The format may
1182be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001183result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001184the equivalent of the Python expression \samp{apply(\var{o},
1185\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001186\end{cfuncdesc}
1187
1188
1189\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001190Call the method named \var{m} of object \var{o} with a variable number
1191of C arguments. The \C{} arguments are described by a
1192\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1193indicating that no arguments are provided. Returns the result of the
1194call on success, or \NULL{} on failure. This is the equivalent of the
1195Python expression \samp{\var{o}.\var{method}(\var{args})}.
1196Note that Special method names, such as \method{__add__()},
1197\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001198abstract-object routines for these must be used.
1199\end{cfuncdesc}
1200
1201
1202\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001203Compute and return the hash value of an object \var{o}. On
1204failure, return \code{-1}. This is the equivalent of the Python
1205expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001206\end{cfuncdesc}
1207
1208
1209\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001210Returns \code{1} if the object \var{o} is considered to be true, and
1211\code{0} otherwise. This is equivalent to the Python expression
1212\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001213This function always succeeds.
1214\end{cfuncdesc}
1215
1216
1217\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1218On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001219type of object \var{o}. On failure, returns \NULL{}. This is
1220equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001221\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001222\end{cfuncdesc}
1223
1224\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001225Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001226both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001227returned. On error, \code{-1} is returned. This is the equivalent
1228to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001229\end{cfuncdesc}
1230
1231
1232\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001233Return element of \var{o} corresponding to the object \var{key} or
1234\NULL{} on failure. This is the equivalent of the Python expression
1235\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001236\end{cfuncdesc}
1237
1238
1239\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001240Map the object \var{key} to the value \var{v}.
1241Returns \code{-1} on failure. This is the equivalent
1242of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001243\end{cfuncdesc}
1244
1245
1246\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001247Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1248failure. This is the equivalent of the Python statement \samp{del
1249\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001250\end{cfuncdesc}
1251
1252
1253\section{Number Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001254\label{number}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001255
1256\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001257Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001258false otherwise.
1259This function always succeeds.
1260\end{cfuncdesc}
1261
1262
1263\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001264Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1265failure. This is the equivalent of the Python expression
1266\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001267\end{cfuncdesc}
1268
1269
1270\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001271Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1272on failure. This is the equivalent of the Python expression
1273\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001274\end{cfuncdesc}
1275
1276
1277\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001278Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1279failure. This is the equivalent of the Python expression
1280\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001281\end{cfuncdesc}
1282
1283
1284\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001285Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1286failure.
1287This is the equivalent of the Python expression \samp{\var{o1} /
1288\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001289\end{cfuncdesc}
1290
1291
1292\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001293Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1294failure. This is the equivalent of the Python expression
1295\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001296\end{cfuncdesc}
1297
1298
1299\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001300See the built-in function \function{divmod()}\bifuncindex{divmod}.
1301Returns \NULL{} on failure. This is the equivalent of the Python
1302expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001303\end{cfuncdesc}
1304
1305
1306\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001307See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1308\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001309\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
1310If \var{o3} is to be ignored, pass \code{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001311\end{cfuncdesc}
1312
1313
1314\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001315Returns the negation of \var{o} on success, or \NULL{} on failure.
1316This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001317\end{cfuncdesc}
1318
1319
1320\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001321Returns \var{o} on success, or \NULL{} on failure.
1322This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001323\end{cfuncdesc}
1324
1325
1326\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001327Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1328the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001329\end{cfuncdesc}
1330
1331
1332\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001333Returns the bitwise negation of \var{o} on success, or \NULL{} on
1334failure. This is the equivalent of the Python expression
1335\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001336\end{cfuncdesc}
1337
1338
1339\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001340Returns the result of left shifting \var{o1} by \var{o2} on success,
1341or \NULL{} on failure. This is the equivalent of the Python
1342expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001343\end{cfuncdesc}
1344
1345
1346\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001347Returns the result of right shifting \var{o1} by \var{o2} on success,
1348or \NULL{} on failure. This is the equivalent of the Python
1349expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001350\end{cfuncdesc}
1351
1352
1353\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001354Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1355\NULL{} on failure. This is the equivalent of the Python
1356expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001357\end{cfuncdesc}
1358
1359
1360\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001361Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1362or \NULL{} on failure. This is the equivalent of the Python
1363expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001364\end{cfuncdesc}
1365
1366\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001367Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1368failure. This is the equivalent of the Python expression
1369\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001370\end{cfuncdesc}
1371
1372
Fred Drakee058b4f1998-02-16 06:15:35 +00001373\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001374This function takes the addresses of two variables of type
1375\code{PyObject*}.
1376
Fred Drakee058b4f1998-02-16 06:15:35 +00001377If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1378have the same type, increment their reference count and return
1379\code{0} (success). If the objects can be converted to a common
1380numeric type, replace \code{*p1} and \code{*p2} by their converted
1381value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001382If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001383return \code{-1} (failure) and don't increment the reference counts.
1384The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1385Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1386\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001387\end{cfuncdesc}
1388
1389
1390\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001391Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001392\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001393expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001394\end{cfuncdesc}
1395
1396
1397\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001398Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001399or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001400expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001401\end{cfuncdesc}
1402
1403
1404\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001405Returns the \var{o} converted to a float object on success, or \NULL{}
1406on failure. This is the equivalent of the Python expression
1407\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001408\end{cfuncdesc}
1409
1410
Fred Drakef44617d1998-02-12 20:57:15 +00001411\section{Sequence Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001412\label{sequence}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001413
1414\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001415Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001416otherwise.
1417This function always succeeds.
1418\end{cfuncdesc}
1419
1420
1421\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001422Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001423failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001424expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001425\end{cfuncdesc}
1426
1427
1428\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001429Return the result of repeating sequence object \var{o} \var{count}
1430times, or \NULL{} on failure. This is the equivalent of the Python
1431expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001432\end{cfuncdesc}
1433
1434
1435\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001436Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1437is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001438\end{cfuncdesc}
1439
1440
1441\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001442Return the slice of sequence object \var{o} between \var{i1} and
1443\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1444expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001445\end{cfuncdesc}
1446
1447
1448\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001449Assign object \var{v} to the \var{i}th element of \var{o}.
1450Returns \code{-1} on failure. This is the equivalent of the Python
1451statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001452\end{cfuncdesc}
1453
1454\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001455Delete the \var{i}th element of object \var{v}. Returns
1456\code{-1} on failure. This is the equivalent of the Python
1457statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001458\end{cfuncdesc}
1459
1460\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001461Assign the sequence object \var{v} to the slice in sequence
1462object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1463the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001464\end{cfuncdesc}
1465
1466\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001467Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1468Returns \code{-1} on failure. This is the equivalent of the Python
1469statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001470\end{cfuncdesc}
1471
1472\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001473Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1474This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001475\end{cfuncdesc}
1476
1477\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001478Return the number of occurrences of \var{value} in \var{o}, that is,
1479return the number of keys for which \code{\var{o}[\var{key}] ==
1480\var{value}}. On failure, return \code{-1}. This is equivalent to
1481the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001482\end{cfuncdesc}
1483
1484\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001485Determine if \var{o} contains \var{value}. If an item in \var{o} is
1486equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1487error, return \code{-1}. This is equivalent to the Python expression
1488\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001489\end{cfuncdesc}
1490
1491\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001492Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1493\var{value}}. On error, return \code{-1}. This is equivalent to
1494the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001495\end{cfuncdesc}
1496
Fred Drakef39ed671998-02-26 22:01:23 +00001497
Fred Drakef44617d1998-02-12 20:57:15 +00001498\section{Mapping Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001499\label{mapping}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001500
1501\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001502Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001503otherwise.
1504This function always succeeds.
1505\end{cfuncdesc}
1506
1507
1508\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001509Returns the number of keys in object \var{o} on success, and \code{-1}
1510on failure. For objects that do not provide sequence protocol,
1511this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001512\end{cfuncdesc}
1513
1514
1515\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001516Remove the mapping for object \var{key} from the object \var{o}.
1517Return \code{-1} on failure. This is equivalent to
1518the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001519\end{cfuncdesc}
1520
1521
1522\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001523Remove the mapping for object \var{key} from the object \var{o}.
1524Return \code{-1} on failure. This is equivalent to
1525the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001526\end{cfuncdesc}
1527
1528
1529\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001530On success, return \code{1} if the mapping object has the key \var{key}
1531and \code{0} otherwise. This is equivalent to the Python expression
1532\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001533This function always succeeds.
1534\end{cfuncdesc}
1535
1536
1537\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001538Return \code{1} if the mapping object has the key \var{key} and
1539\code{0} otherwise. This is equivalent to the Python expression
1540\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001541This function always succeeds.
1542\end{cfuncdesc}
1543
1544
1545\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001546On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001547failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001548expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001549\end{cfuncdesc}
1550
1551
1552\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001553On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001554failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001555expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001556\end{cfuncdesc}
1557
1558
1559\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001560On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001561each item is a tuple containing a key-value pair. On
1562failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001563expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001564\end{cfuncdesc}
1565
1566\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001567Make object \var{o} empty. Returns \code{1} on success and \code{0}
1568on failure. This is equivalent to the Python statement
1569\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001570\end{cfuncdesc}
1571
1572
1573\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001574Return element of \var{o} corresponding to the object \var{key} or
1575\NULL{} on failure. This is the equivalent of the Python expression
1576\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001577\end{cfuncdesc}
1578
1579\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001580Map the object \var{key} to the value \var{v} in object \var{o}.
1581Returns \code{-1} on failure. This is the equivalent of the Python
1582statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001583\end{cfuncdesc}
1584
1585
1586\section{Constructors}
1587
1588\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1589On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001590file given by \var{file_name}, with a file mode given by \var{mode},
1591where \var{mode} has the same semantics as the standard \C{} routine
1592\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001593\end{cfuncdesc}
1594
1595\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001596Return a new file object for an already opened standard \C{} file
1597pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1598\var{mode}, must be provided as well as a flag, \var{close_on_del},
1599that indicates whether the file is to be closed when the file object
1600is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001601\end{cfuncdesc}
1602
1603\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001604Returns a new float object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001605\NULL{} on failure.
1606\end{cfuncdesc}
1607
1608\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001609Returns a new int object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001610\NULL{} on failure.
1611\end{cfuncdesc}
1612
Fred Drakee058b4f1998-02-16 06:15:35 +00001613\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1614Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001615failure.
1616\end{cfuncdesc}
1617
1618\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001619Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001620\NULL{} on failure.
1621\end{cfuncdesc}
1622
1623\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001624Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001625\NULL{} on failure.
1626\end{cfuncdesc}
1627
1628\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1629Returns a new empty dictionary on success, and \NULL{} on
1630failure.
1631\end{cfuncdesc}
1632
1633\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001634Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001635\NULL{} on failure.
1636\end{cfuncdesc}
1637
Fred Drakee058b4f1998-02-16 06:15:35 +00001638\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1639Returns a new string object with the value \var{v} and length
1640\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1641the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001642\end{cfuncdesc}
1643
Fred Drakee058b4f1998-02-16 06:15:35 +00001644\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1645Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001646failure.
1647\end{cfuncdesc}
1648
1649
1650\chapter{Concrete Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001651\label{concrete}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001652
1653The functions in this chapter are specific to certain Python object
1654types. Passing them an object of the wrong type is not a good idea;
1655if you receive an object from a Python program and you are not sure
1656that it has the right type, you must perform a type check first;
1657e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001658\cfunction{PyDict_Check()}. The chapter is structured like the
1659``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001660
1661
Fred Drakee5bf8b21998-02-12 21:22:28 +00001662\section{Fundamental Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001663\label{fundamental}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001664
Fred Drakee5bf8b21998-02-12 21:22:28 +00001665This section describes Python type objects and the singleton object
1666\code{None}.
1667
1668
1669\subsection{Type Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001670\label{typeObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001671
1672\begin{ctypedesc}{PyTypeObject}
1673
1674\end{ctypedesc}
1675
1676\begin{cvardesc}{PyObject *}{PyType_Type}
1677
1678\end{cvardesc}
1679
1680
1681\subsection{The None Object}
Fred Drakef39ed671998-02-26 22:01:23 +00001682\label{noneObject}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001683
1684\begin{cvardesc}{PyObject *}{Py_None}
1685XXX macro
1686\end{cvardesc}
1687
1688
1689\section{Sequence Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001690\label{sequenceObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001691
1692Generic operations on sequence objects were discussed in the previous
1693chapter; this section deals with the specific kinds of sequence
1694objects that are intrinsic to the Python language.
1695
1696
1697\subsection{String Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001698\label{stringObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001699
1700\begin{ctypedesc}{PyStringObject}
1701This subtype of \code{PyObject} represents a Python string object.
1702\end{ctypedesc}
1703
1704\begin{cvardesc}{PyTypeObject}{PyString_Type}
1705This instance of \code{PyTypeObject} represents the Python string type.
1706\end{cvardesc}
1707
1708\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
1709
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001710\end{cfuncdesc}
1711
Fred Drakee5bf8b21998-02-12 21:22:28 +00001712\begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int}
1713
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001714\end{cfuncdesc}
1715
Fred Drakee5bf8b21998-02-12 21:22:28 +00001716\begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *}
1717
Guido van Rossumae110af1997-05-22 20:11:52 +00001718\end{cfuncdesc}
1719
Fred Drakee5bf8b21998-02-12 21:22:28 +00001720\begin{cfuncdesc}{int}{PyString_Size}{PyObject *}
1721
Guido van Rossumae110af1997-05-22 20:11:52 +00001722\end{cfuncdesc}
1723
Fred Drakee5bf8b21998-02-12 21:22:28 +00001724\begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *}
1725
1726\end{cfuncdesc}
1727
1728\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *}
1729
1730\end{cfuncdesc}
1731
1732\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *}
1733
1734\end{cfuncdesc}
1735
1736\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int}
1737
1738\end{cfuncdesc}
1739
1740\begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *}
1741
1742\end{cfuncdesc}
1743
1744\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **}
1745
1746\end{cfuncdesc}
1747
1748\begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *}
1749
1750\end{cfuncdesc}
1751
1752\begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *}
1753
1754\end{cfuncdesc}
1755
1756\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *}
1757
1758\end{cfuncdesc}
1759
1760
1761\subsection{Tuple Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001762\label{tupleObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001763
1764\begin{ctypedesc}{PyTupleObject}
1765This subtype of \code{PyObject} represents a Python tuple object.
1766\end{ctypedesc}
1767
1768\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1769This instance of \code{PyTypeObject} represents the Python tuple type.
1770\end{cvardesc}
1771
1772\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1773Return true if the argument is a tuple object.
1774\end{cfuncdesc}
1775
1776\begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001777Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001778\end{cfuncdesc}
1779
1780\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001781Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001782of that tuple.
1783\end{cfuncdesc}
1784
1785\begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001786Returns the object at position \var{pos} in the tuple pointed
1787to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
1788raises an \exception{IndexError} exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001789\end{cfuncdesc}
1790
1791\begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001792Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001793\end{cfuncdesc}
1794
1795\begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p,
1796 int low,
1797 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001798Takes a slice of the tuple pointed to by \var{p} from
1799\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001800\end{cfuncdesc}
1801
1802\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1803 int pos,
1804 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001805Inserts a reference to object \var{o} at position \var{pos} of
1806the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001807\end{cfuncdesc}
1808
1809\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1810 int pos,
1811 PyObject *o}
1812
Fred Drakee058b4f1998-02-16 06:15:35 +00001813Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001814should \emph{only} be used to fill in brand new tuples.
1815\end{cfuncdesc}
1816
1817\begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p,
1818 int new,
1819 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001820Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001821\emph{supposed} to be immutable, this should only be used if there is only
1822one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001823already be known to some other part of the code. \var{last_is_sticky} is
1824a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001825it will grow or shrink at the end. Think of this as destroying the old
1826tuple and creating a new one, only more efficiently.
1827\end{cfuncdesc}
1828
1829
1830\subsection{List Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001831\label{listObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001832
1833\begin{ctypedesc}{PyListObject}
1834This subtype of \code{PyObject} represents a Python list object.
1835\end{ctypedesc}
1836
1837\begin{cvardesc}{PyTypeObject}{PyList_Type}
1838This instance of \code{PyTypeObject} represents the Python list type.
1839\end{cvardesc}
1840
1841\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001842Returns true if its argument is a \code{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001843\end{cfuncdesc}
1844
1845\begin{cfuncdesc}{PyObject *}{PyList_New}{int size}
1846
1847\end{cfuncdesc}
1848
1849\begin{cfuncdesc}{int}{PyList_Size}{PyObject *}
1850
1851\end{cfuncdesc}
1852
1853\begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int}
1854
1855\end{cfuncdesc}
1856
1857\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *}
1858
1859\end{cfuncdesc}
1860
1861\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *}
1862
1863\end{cfuncdesc}
1864
1865\begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *}
1866
1867\end{cfuncdesc}
1868
1869\begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int}
1870
1871\end{cfuncdesc}
1872
1873\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *}
1874
1875\end{cfuncdesc}
1876
1877\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *}
1878
1879\end{cfuncdesc}
1880
1881\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *}
1882
1883\end{cfuncdesc}
1884
1885\begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *}
1886
1887\end{cfuncdesc}
1888
1889\begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i}
1890
1891\end{cfuncdesc}
1892
1893\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1894
1895\end{cfuncdesc}
1896
1897
1898\section{Mapping Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001899\label{mapObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001900
1901\subsection{Dictionary Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001902\label{dictObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001903
1904\begin{ctypedesc}{PyDictObject}
1905This subtype of \code{PyObject} represents a Python dictionary object.
1906\end{ctypedesc}
1907
1908\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1909This instance of \code{PyTypeObject} represents the Python dictionary type.
1910\end{cvardesc}
1911
1912\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001913Returns true if its argument is a \code{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001914\end{cfuncdesc}
1915
1916\begin{cfuncdesc}{PyDictObject *}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001917Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001918\end{cfuncdesc}
1919
1920\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001921Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001922\end{cfuncdesc}
1923
1924\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1925 PyObject *key,
1926 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001927Inserts \var{value} into the dictionary with a key of \var{key}. Both
1928\var{key} and \var{value} should be PyObjects, and \var{key} should be
1929hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001930\end{cfuncdesc}
1931
1932\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1933 char *key,
1934 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001935Inserts \var{value} into the dictionary using \var{key}
1936as a key. \var{key} should be a \code{char *}. The key object is
1937created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001938\end{cfuncdesc}
1939
1940\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001941Removes the entry in dictionary \var{p} with key \var{key}.
1942\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001943\end{cfuncdesc}
1944
1945\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001946Removes the entry in dictionary \var{p} which has a key
1947specified by the \code{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001948\end{cfuncdesc}
1949
1950\begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001951Returns the object from dictionary \var{p} which has a key
1952\var{key}. Returns \NULL{} if the key \var{key} is not present.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001953\end{cfuncdesc}
1954
1955\begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001956Does the same, but \var{key} is specified as a
Fred Drakee5bf8b21998-02-12 21:22:28 +00001957\code{char *}, rather than a \code{PyObject *}.
1958\end{cfuncdesc}
1959
1960\begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001961Returns a \code{PyListObject} containing all the items
1962from the dictionary, as in the mapping method \method{items()} (see
1963the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001964\end{cfuncdesc}
1965
1966\begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001967Returns a \code{PyListObject} containing all the keys
1968from the dictionary, as in the mapping method \method{keys()} (see the
1969\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001970\end{cfuncdesc}
1971
1972\begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001973Returns a \code{PyListObject} containing all the values
1974from the dictionary \var{p}, as in the mapping method
1975\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001976\end{cfuncdesc}
1977
1978\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001979Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001980\end{cfuncdesc}
1981
1982\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
1983 int ppos,
1984 PyObject **pkey,
1985 PyObject **pvalue}
1986
1987\end{cfuncdesc}
1988
1989
1990\section{Numeric Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001991\label{numericObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001992
1993\subsection{Plain Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001994\label{intObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001995
1996\begin{ctypedesc}{PyIntObject}
1997This subtype of \code{PyObject} represents a Python integer object.
1998\end{ctypedesc}
1999
2000\begin{cvardesc}{PyTypeObject}{PyInt_Type}
2001This instance of \code{PyTypeObject} represents the Python plain
2002integer type.
2003\end{cvardesc}
2004
2005\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
2006
2007\end{cfuncdesc}
2008
2009\begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00002010Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002011
2012The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00002013integers between \code{-1} and \code{100}, when you create an int in
2014that range you actually just get back a reference to the existing
2015object. So it should be possible to change the value of \code{1}. I
2016suspect the behaviour of python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00002017\end{cfuncdesc}
2018
2019\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002020Returns the value of the object \var{io}. No error checking is
2021performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002022\end{cfuncdesc}
2023
2024\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002025Will first attempt to cast the object to a \code{PyIntObject}, if
2026it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002027\end{cfuncdesc}
2028
2029\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002030Returns the systems idea of the largest integer it can handle
2031(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002032\end{cfuncdesc}
2033
2034
2035\subsection{Long Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002036\label{longObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002037
2038\begin{ctypedesc}{PyLongObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002039This subtype of \code{PyObject} represents a Python long integer
2040object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002041\end{ctypedesc}
2042
2043\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002044This instance of \code{PyTypeObject} represents the Python long
2045integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002046\end{cvardesc}
2047
2048\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002049Returns true if its argument is a \code{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002050\end{cfuncdesc}
2051
2052\begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long}
2053
2054\end{cfuncdesc}
2055
2056\begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long}
2057
2058\end{cfuncdesc}
2059
2060\begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double}
2061
2062\end{cfuncdesc}
2063
2064\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *}
2065
2066\end{cfuncdesc}
2067
2068\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject }
2069
2070\end{cfuncdesc}
2071
2072\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *}
2073
2074\end{cfuncdesc}
2075
2076\begin{cfuncdesc}{PyObject *}{PyLong_FromString}{char *, char **, int}
2077
2078\end{cfuncdesc}
2079
2080
2081\subsection{Floating Point Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002082\label{floatObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002083
2084\begin{ctypedesc}{PyFloatObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002085This subtype of \code{PyObject} represents a Python floating point
2086object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002087\end{ctypedesc}
2088
2089\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002090This instance of \code{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002091point type.
2092\end{cvardesc}
2093
2094\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002095Returns true if its argument is a \code{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002096\end{cfuncdesc}
2097
2098\begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double}
2099
2100\end{cfuncdesc}
2101
2102\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *}
2103
2104\end{cfuncdesc}
2105
2106\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *}
2107
2108\end{cfuncdesc}
2109
2110
2111\subsection{Complex Number Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002112\label{complexObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002113
2114\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002115The \C{} structure which corresponds to the value portion of a Python
2116complex number object. Most of the functions for dealing with complex
2117number objects use structures of this type as input or output values,
2118as appropriate. It is defined as:
2119
Fred Drakee058b4f1998-02-16 06:15:35 +00002120\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002121typedef struct {
2122 double real;
2123 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002124} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002125\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002126\end{ctypedesc}
2127
2128\begin{ctypedesc}{PyComplexObject}
2129This subtype of \code{PyObject} represents a Python complex number object.
2130\end{ctypedesc}
2131
2132\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2133This instance of \code{PyTypeObject} represents the Python complex
2134number type.
2135\end{cvardesc}
2136
2137\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002138Returns true if its argument is a \code{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002139\end{cfuncdesc}
2140
2141\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex}
2142
2143\end{cfuncdesc}
2144
2145\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex}
2146
2147\end{cfuncdesc}
2148
2149\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex}
2150
2151\end{cfuncdesc}
2152
2153\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex}
2154
2155\end{cfuncdesc}
2156
2157\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex}
2158
2159\end{cfuncdesc}
2160
2161\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex}
2162
2163\end{cfuncdesc}
2164
2165\begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex}
2166
2167\end{cfuncdesc}
2168
2169\begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag}
2170
2171\end{cfuncdesc}
2172
2173\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
2174
2175\end{cfuncdesc}
2176
2177\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
2178
2179\end{cfuncdesc}
2180
2181\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
2182
2183\end{cfuncdesc}
2184
2185
2186
2187\section{Other Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002188\label{otherObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002189
2190\subsection{File Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002191\label{fileObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002192
2193\begin{ctypedesc}{PyFileObject}
2194This subtype of \code{PyObject} represents a Python file object.
2195\end{ctypedesc}
2196
2197\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2198This instance of \code{PyTypeObject} represents the Python file type.
2199\end{cvardesc}
2200
2201\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002202Returns true if its argument is a \code{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002203\end{cfuncdesc}
2204
2205\begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode}
Fred Drakee058b4f1998-02-16 06:15:35 +00002206Creates a new \code{PyFileObject} pointing to the file
2207specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002208\end{cfuncdesc}
2209
2210\begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp,
Fred Drakeb92dce31998-02-25 15:40:22 +00002211 char *name, char *mode, int (*close)}
Fred Drakee058b4f1998-02-16 06:15:35 +00002212Creates a new \code{PyFileObject} from the already-open \var{fp}.
2213The function \var{close} will be called when the file should be
2214closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002215\end{cfuncdesc}
2216
2217\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002218Returns the file object associated with \var{p} as a \code{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002219\end{cfuncdesc}
2220
2221\begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n}
2222undocumented as yet
2223\end{cfuncdesc}
2224
2225\begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002226Returns the name of the file specified by \var{p} as a
2227\code{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002228\end{cfuncdesc}
2229
2230\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002231Available on systems with \cfunction{setvbuf()} only. This should
2232only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002233\end{cfuncdesc}
2234
2235\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakee058b4f1998-02-16 06:15:35 +00002236Sets the \code{softspace} attribute of \var{p} to \var{newflag}.
2237Returns the previosu value. This function clears any errors, and will
2238return \code{0} as the previous value if the attribute either does not
2239exist or if there were errors in retrieving it. There is no way to
2240detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002241\end{cfuncdesc}
2242
2243\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002244Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002245\end{cfuncdesc}
2246
2247\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002248Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002249\end{cfuncdesc}
2250
2251
2252\subsection{CObjects}
Fred Drakef39ed671998-02-26 22:01:23 +00002253\label{cObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002254
2255XXX
2256
2257
Guido van Rossum4a944d71997-08-14 20:35:38 +00002258\chapter{Initialization, Finalization, and Threads}
Fred Drakef39ed671998-02-26 22:01:23 +00002259\label{initialization}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002260
Guido van Rossum4a944d71997-08-14 20:35:38 +00002261\begin{cfuncdesc}{void}{Py_Initialize}{}
2262Initialize the Python interpreter. In an application embedding
2263Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002264functions; with the exception of \cfunction{Py_SetProgramName()},
2265\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2266and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2267loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002268modules \module{__builtin__}\refbimodindex{__builtin__},
2269\module{__main__}\refbimodindex{__main__} and
2270\module{sys}\refbimodindex{sys}. It also initializes the module
2271search path (\code{sys.path}).%
2272\indexiii{module}{search}{path}
2273It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2274that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002275\cfunction{Py_Finalize()} first). There is no return value; it is a
2276fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002277\end{cfuncdesc}
2278
2279\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002280Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002281initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2282called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002283again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002284\end{cfuncdesc}
2285
2286\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002287Undo all initializations made by \cfunction{Py_Initialize()} and
2288subsequent use of Python/C API functions, and destroy all
2289sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2290created and not yet destroyed since the last call to
2291\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2292by the Python interpreter. This is a no-op when called for a second
2293time (without calling \cfunction{Py_Initialize()} again first). There
2294is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002295
2296This function is provided for a number of reasons. An embedding
2297application might want to restart Python without having to restart the
2298application itself. An application that has loaded the Python
2299interpreter from a dynamically loadable library (or DLL) might want to
2300free all memory allocated by Python before unloading the DLL. During a
2301hunt for memory leaks in an application a developer might want to free
2302all memory allocated by Python before exiting from the application.
2303
Fred Drakee058b4f1998-02-16 06:15:35 +00002304\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002305modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002306(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002307(even functions) or modules. Dynamically loaded extension modules
2308loaded by Python are not unloaded. Small amounts of memory allocated
2309by the Python interpreter may not be freed (if you find a leak, please
2310report it). Memory tied up in circular references between objects is
2311not freed. Some memory allocated by extension modules may not be
2312freed. Some extension may not work properly if their initialization
2313routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002314calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2315than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002316\end{cfuncdesc}
2317
2318\begin{cfuncdesc}{PyThreadState *}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002319Create a new sub-interpreter. This is an (almost) totally separate
2320environment for the execution of Python code. In particular, the new
2321interpreter has separate, independent versions of all imported
2322modules, including the fundamental modules
2323\module{__builtin__}\refbimodindex{__builtin__},
2324\module{__main__}\refbimodindex{__main__} and
2325\module{sys}\refbimodindex{sys}. The table of loaded modules
2326(\code{sys.modules}) and the module search path (\code{sys.path}) are
2327also separate. The new environment has no \code{sys.argv} variable.
2328It has new standard I/O stream file objects \code{sys.stdin},
2329\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakeb0a78731998-01-13 18:51:10 +00002330same underlying \code{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002331
2332The return value points to the first thread state created in the new
2333sub-interpreter. This thread state is made the current thread state.
2334Note that no actual thread is created; see the discussion of thread
2335states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002336\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002337is stored in the current thread state and there may not be a current
2338thread state. (Like all other Python/C API functions, the global
2339interpreter lock must be held before calling this function and is
2340still held when it returns; however, unlike most other Python/C API
2341functions, there needn't be a current thread state on entry.)
2342
2343Extension modules are shared between (sub-)interpreters as follows:
2344the first time a particular extension is imported, it is initialized
2345normally, and a (shallow) copy of its module's dictionary is
2346squirreled away. When the same extension is imported by another
2347(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002348contents of this copy; the extension's \code{init} function is not
2349called. Note that this is different from what happens when an
2350extension is imported after the interpreter has been completely
2351re-initialized by calling \cfunction{Py_Finalize()} and
2352\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002353function \emph{is} called again.
2354
Fred Drakee058b4f1998-02-16 06:15:35 +00002355\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002356interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002357isn't perfect --- for example, using low-level file operations like
Guido van Rossum4a944d71997-08-14 20:35:38 +00002358\code{os.close()} they can (accidentally or maliciously) affect each
2359other's open files. Because of the way extensions are shared between
2360(sub-)interpreters, some extensions may not work properly; this is
2361especially likely when the extension makes use of (static) global
2362variables, or when the extension manipulates its module's dictionary
2363after its initialization. It is possible to insert objects created in
2364one sub-interpreter into a namespace of another sub-interpreter; this
2365should be done with great care to avoid sharing user-defined
2366functions, methods, instances or classes between sub-interpreters,
2367since import operations executed by such objects may affect the
2368wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2369a hard-to-fix bug that will be addressed in a future release.)
2370\end{cfuncdesc}
2371
2372\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
2373Destroy the (sub-)interpreter represented by the given thread state.
2374The given thread state must be the current thread state. See the
2375discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002376thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002377interpreted are destroyed. (The global interpreter lock must be held
2378before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002379\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002380been explicitly destroyed at that point.
2381\end{cfuncdesc}
2382
2383\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +00002384This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002385for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002386the value of the \code{argv[0]} argument to the \cfunction{main()} function
2387of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002388functions below to find the Python run-time libraries relative to the
2389interpreter executable. The default value is \code{"python"}. The
2390argument should point to a zero-terminated character string in static
2391storage whose contents will not change for the duration of the
2392program's execution. No code in the Python interpreter will change
2393the contents of this storage.
2394\end{cfuncdesc}
2395
2396\begin{cfuncdesc}{char *}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002397Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002398default. The returned string points into static storage; the caller
2399should not modify its value.
2400\end{cfuncdesc}
2401
2402\begin{cfuncdesc}{char *}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002403Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002404is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002405set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002406for example, if the program name is \code{"/usr/local/bin/python"},
2407the prefix is \code{"/usr/local"}. The returned string points into
2408static storage; the caller should not modify its value. This
2409corresponds to the \code{prefix} variable in the top-level
Fred Drakee058b4f1998-02-16 06:15:35 +00002410\file{Makefile} and the \code{--prefix} argument to the
2411\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002412Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002413also the next function.
2414\end{cfuncdesc}
2415
2416\begin{cfuncdesc}{char *}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002417Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002418files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002419program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002420variables; for example, if the program name is
2421\code{"/usr/local/bin/python"}, the exec-prefix is
2422\code{"/usr/local"}. The returned string points into static storage;
2423the caller should not modify its value. This corresponds to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002424\code{exec_prefix} variable in the top-level \file{Makefile} and the
2425\code{--exec_prefix} argument to the \program{configure} script at build
Guido van Rossum4a944d71997-08-14 20:35:38 +00002426time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002427\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002428
2429Background: The exec-prefix differs from the prefix when platform
2430dependent files (such as executables and shared libraries) are
2431installed in a different directory tree. In a typical installation,
2432platform dependent files may be installed in the
2433\code{"/usr/local/plat"} subtree while platform independent may be
2434installed in \code{"/usr/local"}.
2435
2436Generally speaking, a platform is a combination of hardware and
2437software families, e.g. Sparc machines running the Solaris 2.x
2438operating system are considered the same platform, but Intel machines
2439running Solaris 2.x are another platform, and Intel machines running
2440Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002441operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002442operating systems are a different story; the installation strategies
2443on those systems are so different that the prefix and exec-prefix are
2444meaningless, and set to the empty string. Note that compiled Python
2445bytecode files are platform independent (but not independent from the
2446Python version by which they were compiled!).
2447
Fred Drakee058b4f1998-02-16 06:15:35 +00002448System administrators will know how to configure the \program{mount} or
2449\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002450while having \code{"/usr/local/plat"} be a different filesystem for each
2451platform.
2452\end{cfuncdesc}
2453
2454\begin{cfuncdesc}{char *}{Py_GetProgramFullPath}{}
2455Return the full program name of the Python executable; this is
2456computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002457from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002458returned string points into static storage; the caller should not
2459modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002460\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002461\end{cfuncdesc}
2462
2463\begin{cfuncdesc}{char *}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002464\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002465Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002466program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002467environment variables. The returned string consists of a series of
2468directory names separated by a platform dependent delimiter character.
Fred Drakee5bc4971998-02-12 23:36:49 +00002469The delimiter character is \code{':'} on \UNIX{}, \code{';'} on
2470DOS/Windows, and \code{'\\n'} (the \ASCII{} newline character) on
2471Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002472should not modify its value. The value is available to Python code
2473as the list \code{sys.path}, which may be modified to change the
2474future search path for loaded modules.
2475
2476% XXX should give the exact rules
2477\end{cfuncdesc}
2478
2479\begin{cfuncdesc}{const char *}{Py_GetVersion}{}
2480Return the version of this Python interpreter. This is a string that
2481looks something like
2482
Guido van Rossum09270b51997-08-15 18:57:32 +00002483\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002484"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002485\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002486
2487The first word (up to the first space character) is the current Python
2488version; the first three characters are the major and minor version
2489separated by a period. The returned string points into static storage;
2490the caller should not modify its value. The value is available to
2491Python code as the list \code{sys.version}.
2492\end{cfuncdesc}
2493
2494\begin{cfuncdesc}{const char *}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002495Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002496this is formed from the ``official'' name of the operating system,
2497converted to lower case, followed by the major revision number; e.g.,
2498for Solaris 2.x, which is also known as SunOS 5.x, the value is
2499\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2500is \code{"win"}. The returned string points into static storage;
2501the caller should not modify its value. The value is available to
2502Python code as \code{sys.platform}.
2503\end{cfuncdesc}
2504
2505\begin{cfuncdesc}{const char *}{Py_GetCopyright}{}
2506Return the official copyright string for the current Python version,
2507for example
2508
2509\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2510
2511The returned string points into static storage; the caller should not
2512modify its value. The value is available to Python code as the list
2513\code{sys.copyright}.
2514\end{cfuncdesc}
2515
2516\begin{cfuncdesc}{const char *}{Py_GetCompiler}{}
2517Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002518version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002519
Fred Drakee058b4f1998-02-16 06:15:35 +00002520\begin{verbatim}
2521"[GCC 2.7.2.2]"
2522\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002523
2524The returned string points into static storage; the caller should not
2525modify its value. The value is available to Python code as part of
2526the variable \code{sys.version}.
2527\end{cfuncdesc}
2528
2529\begin{cfuncdesc}{const char *}{Py_GetBuildInfo}{}
2530Return information about the sequence number and build date and time
2531of the current Python interpreter instance, for example
2532
Guido van Rossum09270b51997-08-15 18:57:32 +00002533\begin{verbatim}
2534"#67, Aug 1 1997, 22:34:28"
2535\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002536
2537The returned string points into static storage; the caller should not
2538modify its value. The value is available to Python code as part of
2539the variable \code{sys.version}.
2540\end{cfuncdesc}
2541
2542\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2543% XXX
2544\end{cfuncdesc}
2545
2546% XXX Other PySys thingies (doesn't really belong in this chapter)
2547
2548\section{Thread State and the Global Interpreter Lock}
Fred Drakef39ed671998-02-26 22:01:23 +00002549\label{threads}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002550
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002551The Python interpreter is not fully thread safe. In order to support
2552multi-threaded Python programs, there's a global lock that must be
2553held by the current thread before it can safely access Python objects.
2554Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00002555a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002556increment the reference count of the same object, the reference count
2557could end up being incremented only once instead of twice.
2558
2559Therefore, the rule exists that only the thread that has acquired the
2560global interpreter lock may operate on Python objects or call Python/C
2561API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002562the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002563default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002564\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002565reacquired around potentially blocking I/O operations like reading or
2566writing a file, so that other threads can run while the thread that
2567requests the I/O is waiting for the I/O operation to complete.
2568
2569The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002570separate per thread --- for this it uses a data structure called
2571\code{PyThreadState}. This is new in Python 1.5; in earlier versions,
2572such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002573cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002574when the application uses \function{sys.exc_info()} to access the
2575exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002576
2577There's one global variable left, however: the pointer to the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002578\code{PyThreadState} structure. While most thread packages have a way
Fred Drake9d20ac31998-02-16 15:27:08 +00002579to store ``per-thread global data,'' Python's internal platform
Fred Drakee058b4f1998-02-16 06:15:35 +00002580independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002581the current thread state must be manipulated explicitly.
2582
2583This is easy enough in most cases. Most code manipulating the global
2584interpreter lock has the following simple structure:
2585
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002586\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002587Save the thread state in a local variable.
2588Release the interpreter lock.
2589...Do some blocking I/O operation...
2590Reacquire the interpreter lock.
2591Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002592\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002593
2594This is so common that a pair of macros exists to simplify it:
2595
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002596\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002597Py_BEGIN_ALLOW_THREADS
2598...Do some blocking I/O operation...
2599Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002600\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002601
Fred Drakee058b4f1998-02-16 06:15:35 +00002602The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2603a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2604the block. Another advantage of using these two macros is that when
2605Python is compiled without thread support, they are defined empty,
2606thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002607
2608When thread support is enabled, the block above expands to the
2609following code:
2610
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002611\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002612{
2613 PyThreadState *_save;
2614 _save = PyEval_SaveThread();
2615 ...Do some blocking I/O operation...
2616 PyEval_RestoreThread(_save);
2617}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002618\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002619
2620Using even lower level primitives, we can get roughly the same effect
2621as follows:
2622
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002623\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002624{
2625 PyThreadState *_save;
2626 _save = PyThreadState_Swap(NULL);
2627 PyEval_ReleaseLock();
2628 ...Do some blocking I/O operation...
2629 PyEval_AcquireLock();
2630 PyThreadState_Swap(_save);
2631}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002632\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002633
2634There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002635\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002636global variable \code{errno}, since the lock manipulation does not
2637guarantee that \code{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002638is disabled, \cfunction{PyEval_SaveThread()} and
2639\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2640case, \cfunction{PyEval_ReleaseLock()} and
2641\cfunction{PyEval_AcquireLock()} are not available. This is done so
2642that dynamically loaded extensions compiled with thread support
2643enabled can be loaded by an interpreter that was compiled with
2644disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002645
2646The global interpreter lock is used to protect the pointer to the
2647current thread state. When releasing the lock and saving the thread
2648state, the current thread state pointer must be retrieved before the
2649lock is released (since another thread could immediately acquire the
2650lock and store its own thread state in the global variable).
2651Reversely, when acquiring the lock and restoring the thread state, the
2652lock must be acquired before storing the thread state pointer.
2653
2654Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002655threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002656lock, nor is there a thread state data structure for them. Such
2657threads must bootstrap themselves into existence, by first creating a
2658thread state data structure, then acquiring the lock, and finally
2659storing their thread state pointer, before they can start using the
2660Python/C API. When they are done, they should reset the thread state
2661pointer, release the lock, and finally free their thread state data
2662structure.
2663
2664When creating a thread data structure, you need to provide an
2665interpreter state data structure. The interpreter state data
2666structure hold global data that is shared by all threads in an
2667interpreter, for example the module administration
2668(\code{sys.modules}). Depending on your needs, you can either create
2669a new interpreter state data structure, or share the interpreter state
2670data structure used by the Python main thread (to access the latter,
2671you must obtain the thread state and access its \code{interp} member;
2672this must be done by a thread that is created by Python or by the main
2673thread after Python is initialized).
2674
2675XXX More?
2676
2677\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002678This data structure represents the state shared by a number of
2679cooperating threads. Threads belonging to the same interpreter
2680share their module administration and a few other internal items.
2681There are no public members in this structure.
2682
2683Threads belonging to different interpreters initially share nothing,
2684except process state like available memory, open file descriptors and
2685such. The global interpreter lock is also shared by all threads,
2686regardless of to which interpreter they belong.
2687\end{ctypedesc}
2688
2689\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002690This data structure represents the state of a single thread. The only
2691public data member is \code{PyInterpreterState *interp}, which points
2692to this thread's interpreter state.
2693\end{ctypedesc}
2694
2695\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2696Initialize and acquire the global interpreter lock. It should be
2697called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002698in any other thread operations such as
2699\cfunction{PyEval_ReleaseLock()} or
2700\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2701calling \cfunction{PyEval_SaveThread()} or
2702\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002703
2704This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002705this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002706
2707When only the main thread exists, no lock operations are needed. This
2708is a common situation (most Python programs do not use threads), and
2709the lock operations slow the interpreter down a bit. Therefore, the
2710lock is not created initially. This situation is equivalent to having
2711acquired the lock: when there is only a single thread, all object
2712accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002713lock, it also acquires it. Before the Python
2714\module{thread}\refbimodindex{thread} module creates a new thread,
2715knowing that either it has the lock or the lock hasn't been created
2716yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2717returns, it is guaranteed that the lock has been created and that it
2718has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002719
2720It is \strong{not} safe to call this function when it is unknown which
2721thread (if any) currently has the global interpreter lock.
2722
2723This function is not available when thread support is disabled at
2724compile time.
2725\end{cfuncdesc}
2726
Guido van Rossum4a944d71997-08-14 20:35:38 +00002727\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002728Acquire the global interpreter lock. The lock must have been created
2729earlier. If this thread already has the lock, a deadlock ensues.
2730This function is not available when thread support is disabled at
2731compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002732\end{cfuncdesc}
2733
2734\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002735Release the global interpreter lock. The lock must have been created
2736earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002737disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002738\end{cfuncdesc}
2739
2740\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002741Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002742state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002743have been created earlier. If this thread already has the lock,
2744deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002745is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002746\end{cfuncdesc}
2747
2748\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002749Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002750interpreter lock. The lock must have been created earlier and must be
2751held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002752be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002753thread state --- if it isn't, a fatal error is reported. This
2754function is not available when thread support is disabled at compile
2755time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002756\end{cfuncdesc}
2757
2758\begin{cfuncdesc}{PyThreadState *}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002759Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002760support is enabled) and reset the thread state to \NULL{},
2761returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002762the lock has been created, the current thread must have acquired it.
2763(This function is available even when thread support is disabled at
2764compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002765\end{cfuncdesc}
2766
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002767\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002768Acquire the interpreter lock (if it has been created and thread
2769support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002770must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002771thread must not have acquired it, otherwise deadlock ensues. (This
2772function is available even when thread support is disabled at compile
2773time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002774\end{cfuncdesc}
2775
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002776% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2777\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2778This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002779\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002780Note that it contains an opening brace; it must be matched with a
2781following \code{Py_END_ALLOW_THREADS} macro. See above for further
2782discussion of this macro. It is a no-op when thread support is
2783disabled at compile time.
2784\end{ctypedesc}
2785
2786\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2787This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002788\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002789Note that it contains a closing brace; it must be matched with an
2790earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2791discussion of this macro. It is a no-op when thread support is
2792disabled at compile time.
2793\end{ctypedesc}
2794
2795\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002796This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002797is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2798brace. It is a no-op when thread support is disabled at compile
2799time.
2800\end{ctypedesc}
2801
2802\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002803This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002804equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2805and variable declaration. It is a no-op when thread support is
2806disabled at compile time.
2807\end{ctypedesc}
2808
2809All of the following functions are only available when thread support
2810is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00002811interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002812
2813\begin{cfuncdesc}{PyInterpreterState *}{PyInterpreterState_New}{}
2814Create a new interpreter state object. The interpreter lock must be
2815held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002816\end{cfuncdesc}
2817
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002818\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2819Reset all information in an interpreter state object. The interpreter
2820lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002821\end{cfuncdesc}
2822
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002823\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2824Destroy an interpreter state object. The interpreter lock need not be
2825held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002826call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002827\end{cfuncdesc}
2828
2829\begin{cfuncdesc}{PyThreadState *}{PyThreadState_New}{PyInterpreterState *interp}
2830Create a new thread state object belonging to the given interpreter
2831object. The interpreter lock must be held.
2832\end{cfuncdesc}
2833
2834\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2835Reset all information in a thread state object. The interpreter lock
2836must be held.
2837\end{cfuncdesc}
2838
2839\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2840Destroy a thread state object. The interpreter lock need not be
2841held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002842call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002843\end{cfuncdesc}
2844
2845\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Get}{}
2846Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002847When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002848error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002849\end{cfuncdesc}
2850
2851\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Swap}{PyThreadState *tstate}
2852Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002853argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002854must be held.
2855\end{cfuncdesc}
2856
2857
Fred Drakee058b4f1998-02-16 06:15:35 +00002858\chapter{Defining New Object Types}
Fred Drakef39ed671998-02-26 22:01:23 +00002859\label{newTypes}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002860
Fred Drakee058b4f1998-02-16 06:15:35 +00002861\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
2862\end{cfuncdesc}
2863
2864\begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size}
2865\end{cfuncdesc}
2866
2867\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2868\end{cfuncdesc}
2869
2870\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2871\end{cfuncdesc}
2872
Guido van Rossumae110af1997-05-22 20:11:52 +00002873
2874PyObject, PyVarObject
2875
2876PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2877
2878Typedefs:
2879unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2880intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2881getreadbufferproc, getwritebufferproc, getsegcountproc,
2882destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2883setattrofunc, cmpfunc, reprfunc, hashfunc
2884
2885PyNumberMethods
2886
2887PySequenceMethods
2888
2889PyMappingMethods
2890
2891PyBufferProcs
2892
2893PyTypeObject
2894
2895DL_IMPORT
2896
2897PyType_Type
2898
2899Py*_Check
2900
2901Py_None, _Py_NoneStruct
2902
Guido van Rossumae110af1997-05-22 20:11:52 +00002903
Fred Drakee5bf8b21998-02-12 21:22:28 +00002904\chapter{Debugging}
Fred Drakef39ed671998-02-26 22:01:23 +00002905\label{debugging}
Guido van Rossumae110af1997-05-22 20:11:52 +00002906
Fred Drakee5bf8b21998-02-12 21:22:28 +00002907XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002908
2909
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002910\input{api.ind} % Index -- must be last
2911
2912\end{document}