blob: 7f445007a287329c96728d0d1ae58b8ccff0218c [file] [log] [blame]
Fred Drake6659c301998-03-03 22:02:19 +00001\documentclass{manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002
Guido van Rossum9faf4c51997-10-07 14:38:54 +00003\title{Python/C API Reference Manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00004
5\input{boilerplate}
6
7\makeindex % tell \index to actually write the .idx file
8
9
10\begin{document}
11
Guido van Rossum9231c8f1997-05-15 21:43:21 +000012\maketitle
13
14\input{copyright}
15
16\begin{abstract}
17
18\noindent
Fred Drakee058b4f1998-02-16 06:15:35 +000019This manual documents the API used by \C{} (or \Cpp{}) programmers who
20want to write extension modules or embed Python. It is a companion to
21\emph{Extending and Embedding the Python Interpreter}, which describes
Guido van Rossum9231c8f1997-05-15 21:43:21 +000022the general principles of extension writing but does not document the
23API functions in detail.
24
Guido van Rossum5b8a5231997-12-30 04:38:44 +000025\strong{Warning:} The current version of this document is incomplete.
26I hope that it is nevertheless useful. I will continue to work on it,
27and release new versions from time to time, independent from Python
28source code releases.
29
Guido van Rossum9231c8f1997-05-15 21:43:21 +000030\end{abstract}
31
Fred Drake4d4f9e71998-01-13 22:25:02 +000032\tableofcontents
Guido van Rossum9231c8f1997-05-15 21:43:21 +000033
Guido van Rossum5060b3b1997-08-17 18:02:23 +000034% XXX Consider moving all this back to ext.tex and giving api.tex
35% XXX a *really* short intro only.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000036
37\chapter{Introduction}
Fred Drakef39ed671998-02-26 22:01:23 +000038\label{intro}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000039
Fred Drakeb0a78731998-01-13 18:51:10 +000040The Application Programmer's Interface to Python gives \C{} and \Cpp{}
Guido van Rossum59a61351997-08-14 20:34:33 +000041programmers access to the Python interpreter at a variety of levels.
Fred Drakeb0a78731998-01-13 18:51:10 +000042The API is equally usable from \Cpp{}, but for brevity it is generally
43referred to as the Python/\C{} API. There are two fundamentally
Fred Drakee058b4f1998-02-16 06:15:35 +000044different reasons for using the Python/\C{} API. The first reason is
45to write \emph{extension modules} for specific purposes; these are
46\C{} modules that extend the Python interpreter. This is probably the
47most common use. The second reason is to use Python as a component in
48a larger application; this technique is generally referred to as
49\dfn{embedding} Python in an application.
Guido van Rossum59a61351997-08-14 20:34:33 +000050
Guido van Rossum4a944d71997-08-14 20:35:38 +000051Writing an extension module is a relatively well-understood process,
52where a ``cookbook'' approach works well. There are several tools
53that automate the process to some extent. While people have embedded
54Python in other applications since its early existence, the process of
55embedding Python is less straightforward that writing an extension.
56Python 1.5 introduces a number of new API functions as well as some
57changes to the build process that make embedding much simpler.
Fred Drakec6fa34e1998-04-02 06:47:24 +000058This manual describes the \version\ state of affairs.
Guido van Rossum59a61351997-08-14 20:34:33 +000059% XXX Eventually, take the historical notes out
60
Guido van Rossum4a944d71997-08-14 20:35:38 +000061Many API functions are useful independent of whether you're embedding
62or extending Python; moreover, most applications that embed Python
63will need to provide a custom extension as well, so it's probably a
64good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000065attempting to embed Python in a real application.
66
Guido van Rossum580aa8d1997-11-25 15:34:51 +000067\section{Include Files}
Fred Drakef39ed671998-02-26 22:01:23 +000068\label{includes}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000069
70All function, type and macro definitions needed to use the Python/C
71API are included in your code by the following line:
72
Fred Drakee058b4f1998-02-16 06:15:35 +000073\begin{verbatim}
74#include "Python.h"
75\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000076
Fred Drakee058b4f1998-02-16 06:15:35 +000077This implies inclusion of the following standard headers:
78\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
79\code{<stdlib.h>} (if available).
Guido van Rossum580aa8d1997-11-25 15:34:51 +000080
81All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000082the included standard headers) have one of the prefixes \samp{Py} or
83\samp{_Py}. Names beginning with \samp{_Py} are for internal use
Guido van Rossum580aa8d1997-11-25 15:34:51 +000084only. Structure member names do not have a reserved prefix.
85
Fred Drakee058b4f1998-02-16 06:15:35 +000086\strong{Important:} user code should never define names that begin
87with \samp{Py} or \samp{_Py}. This confuses the reader, and
88jeopardizes the portability of the user code to future Python
89versions, which may define additional names beginning with one of
90these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000091
Guido van Rossum59a61351997-08-14 20:34:33 +000092\section{Objects, Types and Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +000093\label{objects}
Guido van Rossum59a61351997-08-14 20:34:33 +000094
Guido van Rossum580aa8d1997-11-25 15:34:51 +000095Most Python/C API functions have one or more arguments as well as a
96return value of type \code{PyObject *}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +000097to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +000098object. Since all Python object types are treated the same way by the
99Python language in most situations (e.g., assignments, scope rules,
100and argument passing), it is only fitting that they should be
Fred Drakeb0a78731998-01-13 18:51:10 +0000101represented by a single \C{} type. All Python objects live on the heap:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000102you never declare an automatic or static variable of type
Guido van Rossum4a944d71997-08-14 20:35:38 +0000103\code{PyObject}, only pointer variables of type \code{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +0000104be declared.
105
Fred Drakee058b4f1998-02-16 06:15:35 +0000106All Python objects (even Python integers) have a \dfn{type} and a
107\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000108it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakee058b4f1998-02-16 06:15:35 +0000109many more as explained in the \emph{Python Reference Manual}). For
Guido van Rossum4a944d71997-08-14 20:35:38 +0000110each of the well-known types there is a macro to check whether an
Fred Drakee058b4f1998-02-16 06:15:35 +0000111object is of that type; for instance, \samp{PyList_Check(\var{a})} is
112true iff the object pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000113
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000114\subsection{Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +0000115\label{refcounts}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000116
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000117The reference count is important because today's computers have a
Fred Drake003d8da1998-04-13 00:53:42 +0000118finite (and often severely limited) memory size; it counts how many
Guido van Rossum4a944d71997-08-14 20:35:38 +0000119different places there are that have a reference to an object. Such a
Fred Drakeb0a78731998-01-13 18:51:10 +0000120place could be another object, or a global (or static) \C{} variable, or
121a local variable in some \C{} function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000122becomes zero, the object is deallocated. If it contains references to
123other objects, their reference count is decremented. Those other
124objects may be deallocated in turn, if this decrement makes their
125reference count become zero, and so on. (There's an obvious problem
126with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000127``don't do that''.)
128
Guido van Rossum4a944d71997-08-14 20:35:38 +0000129Reference counts are always manipulated explicitly. The normal way is
Fred Drakee058b4f1998-02-16 06:15:35 +0000130to use the macro \cfunction{Py_INCREF()} to increment an object's
131reference count by one, and \cfunction{Py_DECREF()} to decrement it by
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000132one. The decref macro is considerably more complex than the incref one,
Guido van Rossum4a944d71997-08-14 20:35:38 +0000133since it must check whether the reference count becomes zero and then
134cause the object's deallocator, which is a function pointer contained
135in the object's type structure. The type-specific deallocator takes
136care of decrementing the reference counts for other objects contained
137in the object, and so on, if this is a compound object type such as a
138list. There's no chance that the reference count can overflow; at
139least as many bits are used to hold the reference count as there are
140distinct memory locations in virtual memory (assuming
141\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000142increment is a simple operation.
143
Guido van Rossum4a944d71997-08-14 20:35:38 +0000144It is not necessary to increment an object's reference count for every
145local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000146object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000147point to it and it goes down by one when the variable goes out of
148scope. However, these two cancel each other out, so at the end the
149reference count hasn't changed. The only real reason to use the
150reference count is to prevent the object from being deallocated as
151long as our variable is pointing to it. If we know that there is at
152least one other reference to the object that lives at least as long as
153our variable, there is no need to increment the reference count
154temporarily. An important situation where this arises is in objects
Fred Drakeb0a78731998-01-13 18:51:10 +0000155that are passed as arguments to \C{} functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000156that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000157reference to every argument for the duration of the call.
158
Fred Drakee058b4f1998-02-16 06:15:35 +0000159However, a common pitfall is to extract an object from a list and
160hold on to it for a while without incrementing its reference count.
161Some other operation might conceivably remove the object from the
162list, decrementing its reference count and possible deallocating it.
163The real danger is that innocent-looking operations may invoke
164arbitrary Python code which could do this; there is a code path which
165allows control to flow back to the user from a \cfunction{Py_DECREF()},
166so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000167
Guido van Rossum4a944d71997-08-14 20:35:38 +0000168A safe approach is to always use the generic operations (functions
Fred Drakee058b4f1998-02-16 06:15:35 +0000169whose name begins with \samp{PyObject_}, \samp{PyNumber_},
170\samp{PySequence_} or \samp{PyMapping_}). These operations always
Guido van Rossum4a944d71997-08-14 20:35:38 +0000171increment the reference count of the object they return. This leaves
Fred Drakee058b4f1998-02-16 06:15:35 +0000172the caller with the responsibility to call \cfunction{Py_DECREF()}
173when they are done with the result; this soon becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000174
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000175\subsubsection{Reference Count Details}
Fred Drakef39ed671998-02-26 22:01:23 +0000176\label{refcountDetails}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000177
178The reference count behavior of functions in the Python/C API is best
179expelained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000180talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000181shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000182properly --- either by passing ownership on (usually to its caller) or
183by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
184a function passes ownership of a reference on to its caller, the
185caller is said to receive a \emph{new} reference. When no ownership
186is transferred, the caller is said to \emph{borrow} the reference.
187Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000188
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000189Conversely, when calling a function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000190object, there are two possibilities: the function \emph{steals} a
191reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000192references; the two notable exceptions are
193\cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which
194steal a reference to the item (but not to the tuple or list into which
Fred Drake003d8da1998-04-13 00:53:42 +0000195the item is put!). These functions were designed to steal a reference
Fred Drakee058b4f1998-02-16 06:15:35 +0000196because of a common idiom for populating a tuple or list with newly
197created objects; for example, the code to create the tuple \code{(1,
1982, "three")} could look like this (forgetting about error handling for
199the moment; a better way to code this is shown below anyway):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000200
201\begin{verbatim}
202PyObject *t;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000203
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000204t = 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;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000224
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000225l = PyList_New(3);
226x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000227PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000228x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000229PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000230x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000231PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000232\end{verbatim}
233
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000234You might find it strange that the ``recommended'' approach takes more
235code. However, in practice, you will rarely use these ways of
236creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000237\cfunction{Py_BuildValue()}, that can create most common objects from
238\C{} values, directed by a \dfn{format string}. For example, the
239above two blocks of code could be replaced by the following (which
240also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000241
242\begin{verbatim}
243PyObject *t, *l;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000244
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000245t = Py_BuildValue("(iis)", 1, 2, "three");
246l = Py_BuildValue("[iis]", 1, 2, "three");
247\end{verbatim}
248
Fred Drakee058b4f1998-02-16 06:15:35 +0000249It is much more common to use \cfunction{PyObject_SetItem()} and
250friends with items whose references you are only borrowing, like
251arguments that were passed in to the function you are writing. In
252that case, their behaviour regarding reference counts is much saner,
253since you don't have to increment a reference count so you can give a
254reference away (``have it be stolen''). For example, this function
255sets all items of a list (actually, any mutable sequence) to a given
256item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000257
258\begin{verbatim}
259int set_all(PyObject *target, PyObject *item)
260{
261 int i, n;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000262
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000263 n = PyObject_Length(target);
264 if (n < 0)
265 return -1;
266 for (i = 0; i < n; i++) {
267 if (PyObject_SetItem(target, i, item) < 0)
268 return -1;
269 }
270 return 0;
271}
272\end{verbatim}
273
274The situation is slightly different for function return values.
275While passing a reference to most functions does not change your
276ownership responsibilities for that reference, many functions that
277return a referece to an object give you ownership of the reference.
278The reason is simple: in many cases, the returned object is created
279on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000280object. Therefore, the generic functions that return object
281references, like \cfunction{PyObject_GetItem()} and
282\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
283the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000284
285It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000286by a function depends on which function you call only --- \emph{the
287plumage} (i.e., the type of the type of the object passed as an
288argument to the function) \emph{doesn't enter into it!} Thus, if you
289extract an item from a list using \cfunction{PyList_GetItem()}, you
290don't own the reference --- but if you obtain the same item from the
291same list using \cfunction{PySequence_GetItem()} (which happens to
292take exactly the same arguments), you do own a reference to the
293returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000294
Fred Drakee058b4f1998-02-16 06:15:35 +0000295Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000296sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000297\cfunction{PyList_GetItem()}, once using
298\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000299
300\begin{verbatim}
301long sum_list(PyObject *list)
302{
303 int i, n;
304 long total = 0;
305 PyObject *item;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000306
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000307 n = PyList_Size(list);
308 if (n < 0)
309 return -1; /* Not a list */
310 for (i = 0; i < n; i++) {
311 item = PyList_GetItem(list, i); /* Can't fail */
312 if (!PyInt_Check(item)) continue; /* Skip non-integers */
313 total += PyInt_AsLong(item);
314 }
315 return total;
316}
317\end{verbatim}
318
319\begin{verbatim}
320long sum_sequence(PyObject *sequence)
321{
322 int i, n;
323 long total = 0;
324 PyObject *item;
325 n = PyObject_Size(list);
326 if (n < 0)
327 return -1; /* Has no length */
328 for (i = 0; i < n; i++) {
329 item = PySequence_GetItem(list, i);
330 if (item == NULL)
331 return -1; /* Not a sequence, or other failure */
332 if (PyInt_Check(item))
333 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000334 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000335 }
336 return total;
337}
338\end{verbatim}
339
340\subsection{Types}
Fred Drakef39ed671998-02-26 22:01:23 +0000341\label{types}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000342
343There are few other data types that play a significant role in
Fred Drakeb0a78731998-01-13 18:51:10 +0000344the Python/C API; most are simple \C{} types such as \code{int},
Guido van Rossum4a944d71997-08-14 20:35:38 +0000345\code{long}, \code{double} and \code{char *}. A few structure types
346are used to describe static tables used to list the functions exported
347by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000348be discussed together with the functions that use them.
349
350\section{Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000351\label{exceptions}
Guido van Rossum59a61351997-08-14 20:34:33 +0000352
Guido van Rossum4a944d71997-08-14 20:35:38 +0000353The Python programmer only needs to deal with exceptions if specific
354error handling is required; unhandled exceptions are automatically
355propagated to the caller, then to the caller's caller, and so on, till
356they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000357user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000358
Fred Drakeb0a78731998-01-13 18:51:10 +0000359For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000360All functions in the Python/C API can raise exceptions, unless an
361explicit claim is made otherwise in a function's documentation. In
362general, when a function encounters an error, it sets an exception,
363discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000364error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000365return a Boolean true/false result, with false indicating an error.
366Very few functions return no explicit error indicator or have an
367ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000368\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000369
370Exception state is maintained in per-thread storage (this is
371equivalent to using global storage in an unthreaded application). A
Fred Drakec6fa34e1998-04-02 06:47:24 +0000372thread can be in one of two states: an exception has occurred, or not.
Fred Drakee058b4f1998-02-16 06:15:35 +0000373The function \cfunction{PyErr_Occurred()} can be used to check for
374this: it returns a borrowed reference to the exception type object
375when an exception has occurred, and \NULL{} otherwise. There are a
376number of functions to set the exception state:
377\cfunction{PyErr_SetString()} is the most common (though not the most
378general) function to set the exception state, and
379\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000380
381The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000382be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000383value, and the traceback. These have the same meanings as the Python
384object \code{sys.exc_type}, \code{sys.exc_value},
385\code{sys.exc_traceback}; however, they are not the same: the Python
386objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000387\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
388exception state only exists while an exception is being passed on
389between \C{} functions until it reaches the Python interpreter, which
390takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000391
Fred Drakec6fa34e1998-04-02 06:47:24 +0000392Note that starting with Python 1.5, the preferred, thread-safe way to
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000393access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000394\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000395for Python code. Also, the semantics of both ways to access the
396exception state have changed so that a function which catches an
397exception will save and restore its thread's exception state so as to
398preserve the exception state of its caller. This prevents common bugs
399in exception handling code caused by an innocent-looking function
400overwriting the exception being handled; it also reduces the often
401unwanted lifetime extension for objects that are referenced by the
Fred Drakec6fa34e1998-04-02 06:47:24 +0000402stack frames in the traceback.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000403
404As a general principle, a function that calls another function to
405perform some task should check whether the called function raised an
406exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000407should discard any object references that it owns, and returns an
408error indicator, but it should \emph{not} set another exception ---
409that would overwrite the exception that was just raised, and lose
410important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000411
412A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000413in the \cfunction{sum_sequence()} example above. It so happens that
414that example doesn't need to clean up any owned references when it
415detects an error. The following example function shows some error
416cleanup. First, to remind you why you like Python, we show the
417equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000418
419\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000420def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000421 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000422 item = dict[key]
423 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000424 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000425 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000426\end{verbatim}
427
Fred Drakeb0a78731998-01-13 18:51:10 +0000428Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000429
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000430\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000431int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000432{
433 /* Objects all initialized to NULL for Py_XDECREF */
434 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000435 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000436
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000437 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000438 if (item == NULL) {
Fred Drakec6fa34e1998-04-02 06:47:24 +0000439 /* Handle KeyError only: */
440 if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000441
442 /* Clear the error and use zero: */
443 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000444 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000445 if (item == NULL) goto error;
446 }
447
448 const_one = PyInt_FromLong(1L);
449 if (const_one == NULL) goto error;
450
451 incremented_item = PyNumber_Add(item, const_one);
452 if (incremented_item == NULL) goto error;
453
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000454 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000455 rv = 0; /* Success */
456 /* Continue with cleanup code */
457
458 error:
459 /* Cleanup code, shared by success and failure path */
460
461 /* Use Py_XDECREF() to ignore NULL references */
462 Py_XDECREF(item);
463 Py_XDECREF(const_one);
464 Py_XDECREF(incremented_item);
465
466 return rv; /* -1 for error, 0 for success */
467}
468\end{verbatim}
469
470This example represents an endorsed use of the \code{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000471in \C{}! It illustrates the use of
472\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
473handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
474dispose of owned references that may be \NULL{} (note the \samp{X} in
475the name; \cfunction{Py_DECREF()} would crash when confronted with a
476\NULL{} reference). It is important that the variables used to hold
477owned references are initialized to \NULL{} for this to work;
478likewise, the proposed return value is initialized to \code{-1}
479(failure) and only set to success after the final call made is
480successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000481
Guido van Rossum59a61351997-08-14 20:34:33 +0000482
483\section{Embedding Python}
Fred Drakef39ed671998-02-26 22:01:23 +0000484\label{embedding}
Guido van Rossum59a61351997-08-14 20:34:33 +0000485
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000486The one important task that only embedders (as opposed to extension
487writers) of the Python interpreter have to worry about is the
488initialization, and possibly the finalization, of the Python
489interpreter. Most functionality of the interpreter can only be used
490after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000491
Fred Drakee058b4f1998-02-16 06:15:35 +0000492The basic initialization function is \cfunction{Py_Initialize()}.
493This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000494fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
495\module{__main__}\refbimodindex{__main__} and
496\module{sys}\refbimodindex{sys}. It also initializes the module
Fred Drakec6fa34e1998-04-02 06:47:24 +0000497search path (\code{sys.path}).%
498\indexiii{module}{search}{path}
Guido van Rossum59a61351997-08-14 20:34:33 +0000499
Fred Drakee058b4f1998-02-16 06:15:35 +0000500\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000501(\code{sys.argv}). If this variable is needed by Python code that
502will be executed later, it must be set explicitly with a call to
503\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000504to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000505
Fred Drakeb0a78731998-01-13 18:51:10 +0000506On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000507details are slightly different), \cfunction{Py_Initialize()}
508calculates the module search path based upon its best guess for the
509location of the standard Python interpreter executable, assuming that
510the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000511interpreter executable. In particular, it looks for a directory named
Fred Drake2de75ec1998-04-09 14:12:11 +0000512\file{lib/python1.5} (replacing \file{1.5} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000513interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000514executable named \file{python} is found on the shell command search
Fred Drakec6fa34e1998-04-02 06:47:24 +0000515path (the environment variable \envvar{PATH}).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000516
517For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000518\file{/usr/local/bin/python}, it will assume that the libraries are in
Fred Drake2de75ec1998-04-09 14:12:11 +0000519\file{/usr/local/lib/python1.5}. (In fact, this particular path
Fred Drakee058b4f1998-02-16 06:15:35 +0000520is also the ``fallback'' location, used when no executable file named
Fred Drakec6fa34e1998-04-02 06:47:24 +0000521\file{python} is found along \envvar{PATH}.) The user can override
522this behavior by setting the environment variable \envvar{PYTHONHOME},
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000523or insert additional directories in front of the standard path by
Fred Drakec6fa34e1998-04-02 06:47:24 +0000524setting \envvar{PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000525
Guido van Rossum4a944d71997-08-14 20:35:38 +0000526The embedding application can steer the search by calling
527\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakec6fa34e1998-04-02 06:47:24 +0000528\cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still
529overrides this and \envvar{PYTHONPATH} is still inserted in front of
Fred Drakee058b4f1998-02-16 06:15:35 +0000530the standard path. An application that requires total control has to
531provide its own implementation of \cfunction{Py_GetPath()},
532\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
533\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000534\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000535
Guido van Rossum4a944d71997-08-14 20:35:38 +0000536Sometimes, it is desirable to ``uninitialize'' Python. For instance,
537the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000538\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000539use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000540can be accomplished by calling \cfunction{Py_Finalize()}. The function
541\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000542initialized state. More information about these functions is given in
543a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000544
Guido van Rossum4a944d71997-08-14 20:35:38 +0000545
Fred Drakee5bf8b21998-02-12 21:22:28 +0000546\chapter{The Very High Level Layer}
Fred Drakef39ed671998-02-26 22:01:23 +0000547\label{veryhigh}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000548
Fred Drakee5bf8b21998-02-12 21:22:28 +0000549The functions in this chapter will let you execute Python source code
550given in a file or a buffer, but they will not let you interact in a
551more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000552
Fred Drakec6fa34e1998-04-02 06:47:24 +0000553\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000554\end{cfuncdesc}
555
Fred Drakec6fa34e1998-04-02 06:47:24 +0000556\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000557\end{cfuncdesc}
558
Fred Drakec6fa34e1998-04-02 06:47:24 +0000559\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000560\end{cfuncdesc}
561
Fred Drakec6fa34e1998-04-02 06:47:24 +0000562\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000563\end{cfuncdesc}
564
Fred Drakec6fa34e1998-04-02 06:47:24 +0000565\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000566\end{cfuncdesc}
567
Fred Drakec6fa34e1998-04-02 06:47:24 +0000568\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str,
569 int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000570\end{cfuncdesc}
571
Fred Drakec6fa34e1998-04-02 06:47:24 +0000572\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp,
573 char *filename, int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000574\end{cfuncdesc}
575
Fred Drakec6fa34e1998-04-02 06:47:24 +0000576\begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start,
577 PyObject *globals,
578 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000579\end{cfuncdesc}
580
Fred Drakec6fa34e1998-04-02 06:47:24 +0000581\begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename,
582 int start, PyObject *globals,
583 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000584\end{cfuncdesc}
585
Fred Drakec6fa34e1998-04-02 06:47:24 +0000586\begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename,
587 int start}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000588\end{cfuncdesc}
589
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000590
591\chapter{Reference Counting}
Fred Drakef39ed671998-02-26 22:01:23 +0000592\label{countingRefs}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000593
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000594The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000595of Python objects.
596
597\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
Fred Drakec6fa34e1998-04-02 06:47:24 +0000598Increment the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000599not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000600\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000601\end{cfuncdesc}
602
603\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000604Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000605\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000606\end{cfuncdesc}
607
608\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000609Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000610not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000611\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
612object's type's deallocation function (which must not be \NULL{}) is
613invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000614
615\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000616code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000617method is deallocated). While exceptions in such code are not
618propagated, the executed code has free access to all Python global
619variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000620variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000621invoked. For example, code to delete an object from a list should
622copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000623the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000624temporary variable.
625\end{cfuncdesc}
626
627\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000628Decrement the reference count for object \var{o}. The object may be
629\NULL{}, in which case the macro has no effect; otherwise the effect
630is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000631applies.
632\end{cfuncdesc}
633
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000634The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000635\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
636\cfunction{_Py_NewReference()}, as well as the global variable
637\code{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000638
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000639XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
640PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
641PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
642
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000643
644\chapter{Exception Handling}
Fred Drakef39ed671998-02-26 22:01:23 +0000645\label{exceptionHandling}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000646
647The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000648exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000649Python exception handling. It works somewhat like the \UNIX{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000650\code{errno} variable: there is a global indicator (per thread) of the
651last error that occurred. Most functions don't clear this on success,
652but will set it to indicate the cause of the error on failure. Most
653functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000654supposed to return a pointer, or \code{-1} if they return an integer
655(exception: the \code{PyArg_Parse*()} functions return \code{1} for
656success and \code{0} for failure). When a function must fail because
657some function it called failed, it generally doesn't set the error
658indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000659
660The error indicator consists of three Python objects corresponding to
661the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
662\code{sys.exc_traceback}. API functions exist to interact with the
663error indicator in various ways. There is a separate error indicator
664for each thread.
665
666% XXX Order of these should be more thoughtful.
667% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000668
669\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000670Print a standard traceback to \code{sys.stderr} and clear the error
671indicator. Call this function only when the error indicator is set.
672(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000673\end{cfuncdesc}
674
Fred Drakec6fa34e1998-04-02 06:47:24 +0000675\begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000676Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000677\emph{type} (the first argument to the last call to one of the
678\code{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
679not set, return \NULL{}. You do not own a reference to the return
680value, so you do not need to \cfunction{Py_DECREF()} it.
681\strong{Note:} do not compare the return value to a specific
682exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
683below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000684\end{cfuncdesc}
685
686\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000687Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000688\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000689This should only be called when an exception is actually set.
690\end{cfuncdesc}
691
692\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000693Return true if the \var{given} exception matches the exception in
694\var{exc}. If \var{exc} is a class object, this also returns true
695when \var{given} is a subclass. If \var{exc} is a tuple, all
696exceptions in the tuple (and recursively in subtuples) are searched
697for a match. This should only be called when an exception is actually
698set.
699\end{cfuncdesc}
700
701\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000702Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000703\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
704\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
705instance of the same class. This function can be used to instantiate
706the class in that case. If the values are already normalized, nothing
707happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000708\end{cfuncdesc}
709
710\begin{cfuncdesc}{void}{PyErr_Clear}{}
711Clear the error indicator. If the error indicator is not set, there
712is no effect.
713\end{cfuncdesc}
714
715\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
716Retrieve the error indicator into three variables whose addresses are
717passed. If the error indicator is not set, set all three variables to
718\NULL{}. If it is set, it will be cleared and you own a reference to
719each object retrieved. The value and traceback object may be \NULL{}
720even when the type object is not. \strong{Note:} this function is
721normally only used by code that needs to handle exceptions or by code
722that needs to save and restore the error indicator temporarily.
723\end{cfuncdesc}
724
725\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
726Set the error indicator from the three objects. If the error
727indicator is already set, it is cleared first. If the objects are
728\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
729and non-\NULL{} value or traceback. The exception type should be a
730string or class; if it is a class, the value should be an instance of
731that class. Do not pass an invalid exception type or value.
732(Violating these rules will cause subtle problems later.) This call
733takes away a reference to each object, i.e. you must own a reference
734to each object before the call and after the call you no longer own
735these references. (If you don't understand this, don't use this
736function. I warned you.) \strong{Note:} this function is normally
737only used by code that needs to save and restore the error indicator
738temporarily.
739\end{cfuncdesc}
740
741\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
742This is the most common way to set the error indicator. The first
743argument specifies the exception type; it is normally one of the
744standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
745increment its reference count. The second argument is an error
746message; it is converted to a string object.
747\end{cfuncdesc}
748
749\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000750This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000751specify an arbitrary Python object for the ``value'' of the exception.
752You need not increment its reference count.
753\end{cfuncdesc}
754
755\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000756This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000757\end{cfuncdesc}
758
759\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000760This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000761\var{message})}, where \var{message} indicates that a built-in operation
762was invoked with an illegal argument. It is mostly for internal use.
763\end{cfuncdesc}
764
Fred Drakec6fa34e1998-04-02 06:47:24 +0000765\begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000766This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000767returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000768\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000769\end{cfuncdesc}
770
Fred Drakec6fa34e1998-04-02 06:47:24 +0000771\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000772This is a convenience function to raise an exception when a \C{} library
773function has returned an error and set the \C{} variable \code{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000774It constructs a tuple object whose first item is the integer
775\code{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000776message (gotten from \cfunction{strerror()}), and then calls
777\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
778the \code{errno} value is \constant{EINTR}, indicating an interrupted
779system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000780the error indicator, leaves it set to that. The function always
781returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000782\samp{return PyErr_SetFromErrno();} when the system call returns an
783error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000784\end{cfuncdesc}
785
786\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000787This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000788\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000789operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000790argument. It is mostly for internal use.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
794This function interacts with Python's signal handling. It checks
795whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000796corresponding signal handler. If the
797\module{signal}\refbimodindex{signal} module is supported, this can
798invoke a signal handler written in Python. In all cases, the default
799effect for \constant{SIGINT} is to raise the
800\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000801error indicator is set and the function returns \code{1}; otherwise
802the function returns \code{0}. The error indicator may or may not be
803cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000804\end{cfuncdesc}
805
806\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
807This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000808the effect of a \constant{SIGINT} signal arriving --- the next time
809\cfunction{PyErr_CheckSignals()} is called,
810\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000811\end{cfuncdesc}
812
Fred Drakec6fa34e1998-04-02 06:47:24 +0000813\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
814 PyObject *base,
815 PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000816This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000817\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000818of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000819arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000820object derived from the root for all exceptions, the built-in name
Fred Drakee058b4f1998-02-16 06:15:35 +0000821\exception{Exception} (accessible in \C{} as \code{PyExc_Exception}).
822In this case the \code{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000823first part (up to the last dot) of the \var{name} argument, and the
824class name is set to the last part (after the last dot). When the
825user has specified the \code{-X} command line option to use string
826exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000827argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000828created from the entire \var{name} argument is returned. The
829\var{base} argument can be used to specify an alternate base class.
830The \var{dict} argument can be used to specify a dictionary of class
831variables and methods.
832\end{cfuncdesc}
833
834
Guido van Rossumae110af1997-05-22 20:11:52 +0000835\section{Standard Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000836\label{standardExceptions}
Guido van Rossumae110af1997-05-22 20:11:52 +0000837
838All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000839names are \samp{PyExc_} followed by the Python exception name.
840These have the type \code{PyObject *}; they are all either class
841objects or string objects, depending on the use of the \code{-X}
842option to the interpreter. For completeness, here are all the
Fred Drake9d20ac31998-02-16 15:27:08 +0000843variables:
Guido van Rossum42cefd01997-10-05 15:27:29 +0000844\code{PyExc_Exception},
845\code{PyExc_StandardError},
846\code{PyExc_ArithmeticError},
847\code{PyExc_LookupError},
Guido van Rossumae110af1997-05-22 20:11:52 +0000848\code{PyExc_AssertionError},
849\code{PyExc_AttributeError},
850\code{PyExc_EOFError},
851\code{PyExc_FloatingPointError},
852\code{PyExc_IOError},
853\code{PyExc_ImportError},
854\code{PyExc_IndexError},
855\code{PyExc_KeyError},
856\code{PyExc_KeyboardInterrupt},
857\code{PyExc_MemoryError},
858\code{PyExc_NameError},
859\code{PyExc_OverflowError},
860\code{PyExc_RuntimeError},
861\code{PyExc_SyntaxError},
862\code{PyExc_SystemError},
863\code{PyExc_SystemExit},
864\code{PyExc_TypeError},
865\code{PyExc_ValueError},
866\code{PyExc_ZeroDivisionError}.
867
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000868
869\chapter{Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000870\label{utilities}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000871
872The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000873parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000874values.
875
Guido van Rossum42cefd01997-10-05 15:27:29 +0000876\section{OS Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000877\label{os}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000878
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000879\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000880Return true (nonzero) if the standard I/O file \var{fp} with name
881\var{filename} is deemed interactive. This is the case for files for
882which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000883\code{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000884the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000885the strings \code{"<stdin>"} or \code{"???"}.
886\end{cfuncdesc}
887
888\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000889Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000890The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000891the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000892\end{cfuncdesc}
893
894
Fred Drakee5bf8b21998-02-12 21:22:28 +0000895\section{Process Control}
Fred Drakef39ed671998-02-26 22:01:23 +0000896\label{processControl}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000897
898\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
899Print a fatal error message and kill the process. No cleanup is
900performed. This function should only be invoked when a condition is
901detected that would make it dangerous to continue using the Python
902interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000903corrupted. On \UNIX{}, the standard \C{} library function
904\cfunction{abort()} is called which will attempt to produce a
905\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000906\end{cfuncdesc}
907
908\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000909Exit the current process. This calls \cfunction{Py_Finalize()} and
910then calls the standard \C{} library function
911\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000912\end{cfuncdesc}
913
914\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
915Register a cleanup function to be called by \cfunction{Py_Finalize()}.
916The cleanup function will be called with no arguments and should
917return no value. At most 32 cleanup functions can be registered.
918When the registration is successful, \cfunction{Py_AtExit()} returns
919\code{0}; on failure, it returns \code{-1}. The cleanup function
920registered last is called first. Each cleanup function will be called
921at most once. Since Python's internal finallization will have
922completed before the cleanup function, no Python APIs should be called
923by \var{func}.
924\end{cfuncdesc}
925
926
927\section{Importing Modules}
Fred Drakef39ed671998-02-26 22:01:23 +0000928\label{importing}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000929
Fred Drakec6fa34e1998-04-02 06:47:24 +0000930\begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000931This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000932below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000933\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000934it specifies a submodule of a package), the \var{fromlist} argument is
935set to the list \code{['*']} so that the return value is the named
936module rather than the top-level package containing it as would
937otherwise be the case. (Unfortunately, this has an additional side
938effect when \var{name} in fact specifies a subpackage instead of a
939submodule: the submodules specified in the package's \code{__all__}
940variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000941or \NULL{} with an exception set on failure (the module may still
Fred Drakec6fa34e1998-04-02 06:47:24 +0000942be created in this case --- examine \code{sys.modules} to find out).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000943\end{cfuncdesc}
944
Fred Drakec6fa34e1998-04-02 06:47:24 +0000945\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000946Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000947Python function \function{__import__()}\bifuncindex{__import__}, as
948the standard \function{__import__()} function calls this function
949directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000950
Guido van Rossum42cefd01997-10-05 15:27:29 +0000951The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000952top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000953(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000954\function{__import__()}, the return value when a submodule of a
955package was requested is normally the top-level package, unless a
956non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000957\end{cfuncdesc}
958
Fred Drakec6fa34e1998-04-02 06:47:24 +0000959\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000960This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000961function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000962\code{__builtins__} of the current globals. This means that the
963import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000964current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
965\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000966\end{cfuncdesc}
967
Fred Drakec6fa34e1998-04-02 06:47:24 +0000968\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000969Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000970Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000971\function{reload()} function calls this function directly. Return a
972new reference to the reloaded module, or \NULL{} with an exception set
973on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000974\end{cfuncdesc}
975
Fred Drakec6fa34e1998-04-02 06:47:24 +0000976\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000977Return the module object corresponding to a module name. The
978\var{name} argument may be of the form \code{package.module}). First
979check the modules dictionary if there's one there, and if not, create
980a new one and insert in in the modules dictionary. Because the former
981action is most common, this does not return a new reference, and you
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000982do not own the returned reference. Return \NULL{} with an
Guido van Rossum42cefd01997-10-05 15:27:29 +0000983exception set on failure.
984\end{cfuncdesc}
985
Fred Drakec6fa34e1998-04-02 06:47:24 +0000986\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000987Given a module name (possibly of the form \code{package.module}) and a
988code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000989built-in function \function{compile()}\bifuncindex{compile}, load the
990module. Return a new reference to the module object, or \NULL{} with
991an exception set if an error occurred (the module may still be created
992in this case). (This function would reload the module if it was
993already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000994\end{cfuncdesc}
995
996\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000997Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
998and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000999first four bytes of the bytecode file, in little-endian byte order.
1000\end{cfuncdesc}
1001
Fred Drakec6fa34e1998-04-02 06:47:24 +00001002\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001003Return the dictionary used for the module administration
1004(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
1005variable.
1006\end{cfuncdesc}
1007
1008\begin{cfuncdesc}{void}{_PyImport_Init}{}
1009Initialize the import mechanism. For internal use only.
1010\end{cfuncdesc}
1011
1012\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
1013Empty the module table. For internal use only.
1014\end{cfuncdesc}
1015
1016\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1017Finalize the import mechanism. For internal use only.
1018\end{cfuncdesc}
1019
Fred Drakec6fa34e1998-04-02 06:47:24 +00001020\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001021For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001022\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001023
Fred Drakec6fa34e1998-04-02 06:47:24 +00001024\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001025For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001026\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001027
1028\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1029Load a frozen module. Return \code{1} for success, \code{0} if the
1030module is not found, and \code{-1} with an exception set if the
1031initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001032load, use \cfunction{PyImport_ImportModule()}.
1033(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001034already imported.)
1035\end{cfuncdesc}
1036
1037\begin{ctypedesc}{struct _frozen}
1038This is the structure type definition for frozen module descriptors,
Fred Drakec6fa34e1998-04-02 06:47:24 +00001039as generated by the \program{freeze}\index{freeze utility} utility
1040(see \file{Tools/freeze/} in the Python source distribution). Its
1041definition is:
1042
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001043\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001044struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001045 char *name;
1046 unsigned char *code;
1047 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001048};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001049\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001050\end{ctypedesc}
1051
Fred Drakec6fa34e1998-04-02 06:47:24 +00001052\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001053This pointer is initialized to point to an array of \code{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001054_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001055or zero. When a frozen module is imported, it is searched in this
Fred Drakec6fa34e1998-04-02 06:47:24 +00001056table. Third-party code could play tricks with this to provide a
Guido van Rossum42cefd01997-10-05 15:27:29 +00001057dynamically created collection of frozen modules.
1058\end{cvardesc}
1059
1060
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001061\chapter{Abstract Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001062\label{abstract}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001063
1064The functions in this chapter interact with Python objects regardless
1065of their type, or with wide classes of object types (e.g. all
1066numerical types, or all sequence types). When used on object types
1067for which they do not apply, they will flag a Python exception.
1068
1069\section{Object Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001070\label{object}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001071
1072\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001073Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001074The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001075options. The only option currently supported is
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001076\constant{Py_PRINT_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001077\end{cfuncdesc}
1078
1079\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *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\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001087Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001088Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001089This is the equivalent of the Python expression
1090\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001091\end{cfuncdesc}
1092
1093
1094\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001095Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1096\code{0} otherwise. This is equivalent to the Python expression
1097\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001098This function always succeeds.
1099\end{cfuncdesc}
1100
1101
1102\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001103Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001104Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001105This is the equivalent of the Python expression
1106\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001107\end{cfuncdesc}
1108
1109
1110\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001111Set the value of the attribute named \var{attr_name}, for object
1112\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1113the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1114\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001115\end{cfuncdesc}
1116
1117
1118\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001119Set the value of the attribute named \var{attr_name}, for
1120object \var{o},
1121to the value \var{v}. Returns \code{-1} on failure. This is
1122the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1123\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001124\end{cfuncdesc}
1125
1126
1127\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001128Delete attribute named \var{attr_name}, for object \var{o}. Returns
1129\code{-1} on failure. This is the equivalent of the Python
1130statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001131\end{cfuncdesc}
1132
1133
1134\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001135Delete attribute named \var{attr_name}, for object \var{o}. Returns
1136\code{-1} on failure. This is the equivalent of the Python
1137statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001138\end{cfuncdesc}
1139
1140
1141\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001142Compare the values of \var{o1} and \var{o2} using a routine provided
1143by \var{o1}, if one exists, otherwise with a routine provided by
1144\var{o2}. The result of the comparison is returned in \var{result}.
1145Returns \code{-1} on failure. This is the equivalent of the Python
1146statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001147\end{cfuncdesc}
1148
1149
1150\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001151Compare the values of \var{o1} and \var{o2} using a routine provided
1152by \var{o1}, if one exists, otherwise with a routine provided by
1153\var{o2}. Returns the result of the comparison on success. On error,
1154the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1155detect an error. This is equivalent to the
1156Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001157\end{cfuncdesc}
1158
1159
1160\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001161Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001162string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001163the equivalent of the Python expression \samp{repr(\var{o})}.
1164Called by the \function{repr()}\bifuncindex{repr} built-in function
1165and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001166\end{cfuncdesc}
1167
1168
1169\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001170Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001171string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001172the equivalent of the Python expression \samp{str(\var{o})}.
1173Called by the \function{str()}\bifuncindex{str} built-in function and
1174by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001175\end{cfuncdesc}
1176
1177
1178\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001179Determine if the object \var{o}, is callable. Return \code{1} if the
1180object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001181This function always succeeds.
1182\end{cfuncdesc}
1183
1184
1185\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001186Call a callable Python object \var{callable_object}, with
1187arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001188needed, then args may be \NULL{}. Returns the result of the
1189call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001190of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001191\end{cfuncdesc}
1192
1193\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001194Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001195variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001196using a \cfunction{Py_BuildValue()} style format string. The format may
1197be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001198result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001199the equivalent of the Python expression \samp{apply(\var{o},
1200\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001201\end{cfuncdesc}
1202
1203
1204\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001205Call the method named \var{m} of object \var{o} with a variable number
1206of C arguments. The \C{} arguments are described by a
1207\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1208indicating that no arguments are provided. Returns the result of the
1209call on success, or \NULL{} on failure. This is the equivalent of the
1210Python expression \samp{\var{o}.\var{method}(\var{args})}.
1211Note that Special method names, such as \method{__add__()},
1212\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001213abstract-object routines for these must be used.
1214\end{cfuncdesc}
1215
1216
1217\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001218Compute and return the hash value of an object \var{o}. On
1219failure, return \code{-1}. This is the equivalent of the Python
1220expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001221\end{cfuncdesc}
1222
1223
1224\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001225Returns \code{1} if the object \var{o} is considered to be true, and
1226\code{0} otherwise. This is equivalent to the Python expression
1227\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001228This function always succeeds.
1229\end{cfuncdesc}
1230
1231
1232\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1233On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001234type of object \var{o}. On failure, returns \NULL{}. This is
1235equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001236\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001237\end{cfuncdesc}
1238
1239\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001240Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001241both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001242returned. On error, \code{-1} is returned. This is the equivalent
1243to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001244\end{cfuncdesc}
1245
1246
1247\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001248Return element of \var{o} corresponding to the object \var{key} or
1249\NULL{} on failure. This is the equivalent of the Python expression
1250\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001251\end{cfuncdesc}
1252
1253
1254\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001255Map the object \var{key} to the value \var{v}.
1256Returns \code{-1} on failure. This is the equivalent
1257of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001258\end{cfuncdesc}
1259
1260
1261\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001262Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1263failure. This is the equivalent of the Python statement \samp{del
1264\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001265\end{cfuncdesc}
1266
1267
1268\section{Number Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001269\label{number}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001270
1271\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001272Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001273false otherwise.
1274This function always succeeds.
1275\end{cfuncdesc}
1276
1277
1278\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001279Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1280failure. This is the equivalent of the Python expression
1281\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001282\end{cfuncdesc}
1283
1284
1285\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001286Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1287on failure. This is the equivalent of the Python expression
1288\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001289\end{cfuncdesc}
1290
1291
1292\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001293Returns the result of multiplying \var{o1} and \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_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001300Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1301failure.
1302This is the equivalent of the Python expression \samp{\var{o1} /
1303\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001304\end{cfuncdesc}
1305
1306
1307\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001308Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1309failure. This is the equivalent of the Python expression
1310\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001311\end{cfuncdesc}
1312
1313
1314\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001315See the built-in function \function{divmod()}\bifuncindex{divmod}.
1316Returns \NULL{} on failure. This is the equivalent of the Python
1317expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001318\end{cfuncdesc}
1319
1320
1321\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001322See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1323\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001324\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
1325If \var{o3} is to be ignored, pass \code{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001326\end{cfuncdesc}
1327
1328
1329\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001330Returns the negation of \var{o} on success, or \NULL{} on failure.
1331This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001332\end{cfuncdesc}
1333
1334
1335\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001336Returns \var{o} on success, or \NULL{} on failure.
1337This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001338\end{cfuncdesc}
1339
1340
1341\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001342Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1343the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001344\end{cfuncdesc}
1345
1346
1347\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001348Returns the bitwise negation of \var{o} on success, or \NULL{} on
1349failure. This is the equivalent of the Python expression
1350\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001351\end{cfuncdesc}
1352
1353
1354\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001355Returns the result of left shifting \var{o1} by \var{o2} on success,
1356or \NULL{} on failure. This is the equivalent of the Python
1357expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001358\end{cfuncdesc}
1359
1360
1361\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001362Returns the result of right shifting \var{o1} by \var{o2} on success,
1363or \NULL{} on failure. This is the equivalent of the Python
1364expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001365\end{cfuncdesc}
1366
1367
1368\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001369Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1370\NULL{} on failure. This is the equivalent of the Python
1371expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001372\end{cfuncdesc}
1373
1374
1375\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001376Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1377or \NULL{} on failure. This is the equivalent of the Python
1378expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001379\end{cfuncdesc}
1380
1381\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001382Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1383failure. This is the equivalent of the Python expression
1384\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001385\end{cfuncdesc}
1386
1387
Fred Drakee058b4f1998-02-16 06:15:35 +00001388\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001389This function takes the addresses of two variables of type
1390\code{PyObject*}.
1391
Fred Drakee058b4f1998-02-16 06:15:35 +00001392If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1393have the same type, increment their reference count and return
1394\code{0} (success). If the objects can be converted to a common
1395numeric type, replace \code{*p1} and \code{*p2} by their converted
1396value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001397If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001398return \code{-1} (failure) and don't increment the reference counts.
1399The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1400Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1401\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001402\end{cfuncdesc}
1403
1404
1405\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001406Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001407\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001408expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001409\end{cfuncdesc}
1410
1411
1412\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001413Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001414or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001415expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001416\end{cfuncdesc}
1417
1418
1419\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001420Returns the \var{o} converted to a float object on success, or \NULL{}
1421on failure. This is the equivalent of the Python expression
1422\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001423\end{cfuncdesc}
1424
1425
Fred Drakef44617d1998-02-12 20:57:15 +00001426\section{Sequence Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001427\label{sequence}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001428
1429\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001430Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001431otherwise.
1432This function always succeeds.
1433\end{cfuncdesc}
1434
1435
1436\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001437Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001438failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001439expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001440\end{cfuncdesc}
1441
1442
1443\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001444Return the result of repeating sequence object \var{o} \var{count}
1445times, or \NULL{} on failure. This is the equivalent of the Python
1446expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001447\end{cfuncdesc}
1448
1449
1450\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001451Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1452is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001453\end{cfuncdesc}
1454
1455
1456\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001457Return the slice of sequence object \var{o} between \var{i1} and
1458\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1459expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001460\end{cfuncdesc}
1461
1462
1463\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001464Assign object \var{v} to the \var{i}th element of \var{o}.
1465Returns \code{-1} on failure. This is the equivalent of the Python
1466statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001467\end{cfuncdesc}
1468
1469\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001470Delete the \var{i}th element of object \var{v}. Returns
1471\code{-1} on failure. This is the equivalent of the Python
1472statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001473\end{cfuncdesc}
1474
1475\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001476Assign the sequence object \var{v} to the slice in sequence
1477object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1478the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001479\end{cfuncdesc}
1480
1481\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001482Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1483Returns \code{-1} on failure. This is the equivalent of the Python
1484statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001485\end{cfuncdesc}
1486
1487\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001488Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1489This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001490\end{cfuncdesc}
1491
1492\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001493Return the number of occurrences of \var{value} in \var{o}, that is,
1494return the number of keys for which \code{\var{o}[\var{key}] ==
1495\var{value}}. On failure, return \code{-1}. This is equivalent to
1496the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001497\end{cfuncdesc}
1498
1499\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001500Determine if \var{o} contains \var{value}. If an item in \var{o} is
1501equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1502error, return \code{-1}. This is equivalent to the Python expression
1503\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001504\end{cfuncdesc}
1505
1506\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001507Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1508\var{value}}. On error, return \code{-1}. This is equivalent to
1509the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001510\end{cfuncdesc}
1511
Fred Drakef39ed671998-02-26 22:01:23 +00001512
Fred Drakef44617d1998-02-12 20:57:15 +00001513\section{Mapping Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001514\label{mapping}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001515
1516\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001517Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001518otherwise.
1519This function always succeeds.
1520\end{cfuncdesc}
1521
1522
1523\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001524Returns the number of keys in object \var{o} on success, and \code{-1}
1525on failure. For objects that do not provide sequence protocol,
1526this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001527\end{cfuncdesc}
1528
1529
1530\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001531Remove the mapping for object \var{key} from the object \var{o}.
1532Return \code{-1} on failure. This is equivalent to
1533the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001534\end{cfuncdesc}
1535
1536
1537\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001538Remove the mapping for object \var{key} from the object \var{o}.
1539Return \code{-1} on failure. This is equivalent to
1540the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001541\end{cfuncdesc}
1542
1543
1544\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001545On success, return \code{1} if the mapping object has the key \var{key}
1546and \code{0} otherwise. This is equivalent to the Python expression
1547\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001548This function always succeeds.
1549\end{cfuncdesc}
1550
1551
1552\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001553Return \code{1} if the mapping object has the key \var{key} and
1554\code{0} otherwise. This is equivalent to the Python expression
1555\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001556This function always succeeds.
1557\end{cfuncdesc}
1558
1559
1560\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001561On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001562failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001563expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001564\end{cfuncdesc}
1565
1566
1567\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001568On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001569failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001570expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001571\end{cfuncdesc}
1572
1573
1574\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001575On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001576each item is a tuple containing a key-value pair. On
1577failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001578expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001579\end{cfuncdesc}
1580
1581\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001582Make object \var{o} empty. Returns \code{1} on success and \code{0}
1583on failure. This is equivalent to the Python statement
1584\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001585\end{cfuncdesc}
1586
1587
1588\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001589Return element of \var{o} corresponding to the object \var{key} or
1590\NULL{} on failure. This is the equivalent of the Python expression
1591\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001592\end{cfuncdesc}
1593
1594\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001595Map the object \var{key} to the value \var{v} in object \var{o}.
1596Returns \code{-1} on failure. This is the equivalent of the Python
1597statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001598\end{cfuncdesc}
1599
1600
1601\section{Constructors}
1602
1603\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1604On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001605file given by \var{file_name}, with a file mode given by \var{mode},
1606where \var{mode} has the same semantics as the standard \C{} routine
1607\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001608\end{cfuncdesc}
1609
1610\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001611Return a new file object for an already opened standard \C{} file
1612pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1613\var{mode}, must be provided as well as a flag, \var{close_on_del},
1614that indicates whether the file is to be closed when the file object
1615is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001616\end{cfuncdesc}
1617
1618\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001619Returns a new float 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*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001624Returns a new int 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
Fred Drakee058b4f1998-02-16 06:15:35 +00001628\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1629Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001630failure.
1631\end{cfuncdesc}
1632
1633\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001634Returns a new long 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
1638\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001639Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001640\NULL{} on failure.
1641\end{cfuncdesc}
1642
1643\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1644Returns a new empty dictionary on success, and \NULL{} on
1645failure.
1646\end{cfuncdesc}
1647
1648\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001649Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001650\NULL{} on failure.
1651\end{cfuncdesc}
1652
Fred Drakee058b4f1998-02-16 06:15:35 +00001653\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1654Returns a new string object with the value \var{v} and length
1655\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1656the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001657\end{cfuncdesc}
1658
Fred Drakee058b4f1998-02-16 06:15:35 +00001659\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1660Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001661failure.
1662\end{cfuncdesc}
1663
1664
1665\chapter{Concrete Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001666\label{concrete}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001667
1668The functions in this chapter are specific to certain Python object
1669types. Passing them an object of the wrong type is not a good idea;
1670if you receive an object from a Python program and you are not sure
1671that it has the right type, you must perform a type check first;
1672e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001673\cfunction{PyDict_Check()}. The chapter is structured like the
1674``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001675
1676
Fred Drakee5bf8b21998-02-12 21:22:28 +00001677\section{Fundamental Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001678\label{fundamental}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001679
Fred Drakee5bf8b21998-02-12 21:22:28 +00001680This section describes Python type objects and the singleton object
1681\code{None}.
1682
1683
1684\subsection{Type Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001685\label{typeObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001686
1687\begin{ctypedesc}{PyTypeObject}
1688
1689\end{ctypedesc}
1690
1691\begin{cvardesc}{PyObject *}{PyType_Type}
1692
1693\end{cvardesc}
1694
1695
1696\subsection{The None Object}
Fred Drakef39ed671998-02-26 22:01:23 +00001697\label{noneObject}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001698
1699\begin{cvardesc}{PyObject *}{Py_None}
1700XXX macro
1701\end{cvardesc}
1702
1703
1704\section{Sequence Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001705\label{sequenceObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001706
1707Generic operations on sequence objects were discussed in the previous
1708chapter; this section deals with the specific kinds of sequence
1709objects that are intrinsic to the Python language.
1710
1711
1712\subsection{String Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001713\label{stringObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001714
1715\begin{ctypedesc}{PyStringObject}
1716This subtype of \code{PyObject} represents a Python string object.
1717\end{ctypedesc}
1718
1719\begin{cvardesc}{PyTypeObject}{PyString_Type}
1720This instance of \code{PyTypeObject} represents the Python string type.
1721\end{cvardesc}
1722
1723\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001724Returns true if the object \var{o} is a string object.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001725\end{cfuncdesc}
1726
Fred Drakec6fa34e1998-04-02 06:47:24 +00001727\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
1728 int len}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001729Returns a new string object with the value \var{v} and length
1730\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1731the contents of the string are uninitialized.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001732\end{cfuncdesc}
1733
1734\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001735Returns a new string object with the value \var{v} on success, and
1736\NULL{} on failure.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001737\end{cfuncdesc}
1738
1739\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001740Returns the length of the string in string object \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001741\end{cfuncdesc}
1742
1743\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001744Resturns a \NULL{} terminated representation of the contents of \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001745\end{cfuncdesc}
1746
1747\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
1748 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001749Creates a new string object in \var{*string} containing the contents
1750of \var{newpart} appended to \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001751\end{cfuncdesc}
1752
1753\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
1754 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001755Creates a new string object in \var{*string} containing the contents
1756of \var{newpart} appended to \var{string}. --WHAT IS THE
1757DIFFERENCE BETWEEN THIS AND PLAIN CONCAT?--
Fred Drakec6fa34e1998-04-02 06:47:24 +00001758\end{cfuncdesc}
1759
1760\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
1761\end{cfuncdesc}
1762
1763\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
1764 PyObject *args}
1765\end{cfuncdesc}
1766
1767\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
1768\end{cfuncdesc}
1769
1770\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
1771\end{cfuncdesc}
1772
1773\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001774
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001775\end{cfuncdesc}
1776
Fred Drakec6fa34e1998-04-02 06:47:24 +00001777\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001778
1779\end{cfuncdesc}
1780
1781
1782\subsection{Tuple Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001783\label{tupleObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001784
1785\begin{ctypedesc}{PyTupleObject}
1786This subtype of \code{PyObject} represents a Python tuple object.
1787\end{ctypedesc}
1788
1789\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1790This instance of \code{PyTypeObject} represents the Python tuple type.
1791\end{cvardesc}
1792
1793\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1794Return true if the argument is a tuple object.
1795\end{cfuncdesc}
1796
Fred Drakec6fa34e1998-04-02 06:47:24 +00001797\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001798Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001799\end{cfuncdesc}
1800
1801\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001802Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001803of that tuple.
1804\end{cfuncdesc}
1805
Fred Drakec6fa34e1998-04-02 06:47:24 +00001806\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001807Returns the object at position \var{pos} in the tuple pointed
1808to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
1809raises an \exception{IndexError} exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001810\end{cfuncdesc}
1811
Fred Drakec6fa34e1998-04-02 06:47:24 +00001812\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001813Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001814\end{cfuncdesc}
1815
Fred Drakec6fa34e1998-04-02 06:47:24 +00001816\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001817 int low,
1818 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001819Takes a slice of the tuple pointed to by \var{p} from
1820\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001821\end{cfuncdesc}
1822
1823\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1824 int pos,
1825 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001826Inserts a reference to object \var{o} at position \var{pos} of
1827the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001828\end{cfuncdesc}
1829
1830\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1831 int pos,
1832 PyObject *o}
1833
Fred Drakee058b4f1998-02-16 06:15:35 +00001834Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001835should \emph{only} be used to fill in brand new tuples.
1836\end{cfuncdesc}
1837
Fred Drakec6fa34e1998-04-02 06:47:24 +00001838\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001839 int new,
1840 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001841Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001842\emph{supposed} to be immutable, this should only be used if there is only
1843one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001844already be known to some other part of the code. \var{last_is_sticky} is
1845a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001846it will grow or shrink at the end. Think of this as destroying the old
1847tuple and creating a new one, only more efficiently.
1848\end{cfuncdesc}
1849
1850
1851\subsection{List Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001852\label{listObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001853
1854\begin{ctypedesc}{PyListObject}
1855This subtype of \code{PyObject} represents a Python list object.
1856\end{ctypedesc}
1857
1858\begin{cvardesc}{PyTypeObject}{PyList_Type}
1859This instance of \code{PyTypeObject} represents the Python list type.
1860\end{cvardesc}
1861
1862\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001863Returns true if its argument is a \code{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001864\end{cfuncdesc}
1865
Fred Drakec6fa34e1998-04-02 06:47:24 +00001866\begin{cfuncdesc}{PyObject*}{PyList_New}{int size}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001867Returns a new list of length \var{len} on success, and \NULL{} on
1868failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001869\end{cfuncdesc}
1870
Fred Drakec6fa34e1998-04-02 06:47:24 +00001871\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001872Returns the length of the list object in \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001873\end{cfuncdesc}
1874
Fred Drakec6fa34e1998-04-02 06:47:24 +00001875\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001876Returns the item in \var{list} at index \var{index}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001877\end{cfuncdesc}
1878
Fred Drakec6fa34e1998-04-02 06:47:24 +00001879\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1880 PyObject *item}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001881Sets the item at index \var{index} in list to \var{item}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001882\end{cfuncdesc}
1883
Fred Drakec6fa34e1998-04-02 06:47:24 +00001884\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1885 PyObject *index}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001886Inserts the item \var{item} into list \var{list} in front of index \var{index}
1887and returns true if successful.
1888For example:
1889\begin{verbatim}
1890PyList_Insert(list, 0, object);
1891\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001892\end{cfuncdesc}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001893would insert \var{object} at the front of the list.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001894
Fred Drakec6fa34e1998-04-02 06:47:24 +00001895\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001896Appends the object \var{item} at the end of list \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001897\end{cfuncdesc}
1898
Fred Drakec6fa34e1998-04-02 06:47:24 +00001899\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1900 int low, int high}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001901Returns a list of the objects in \var{list} containing the objects
1902\emph{between} \var{low} and \var{high}. Analogous to \var{list[low:high]}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001903\end{cfuncdesc}
1904
Fred Drakec6fa34e1998-04-02 06:47:24 +00001905\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1906 int low, int high,
1907 PyObject *itemlist}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001908\end{cfuncdesc}
1909
Fred Drakec6fa34e1998-04-02 06:47:24 +00001910\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001911
Fred Drakee5bf8b21998-02-12 21:22:28 +00001912\end{cfuncdesc}
1913
Fred Drakec6fa34e1998-04-02 06:47:24 +00001914\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001915\end{cfuncdesc}
1916
Fred Drakec6fa34e1998-04-02 06:47:24 +00001917\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001918Returns a new tuple object containing the contents of \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001919\end{cfuncdesc}
1920
Fred Drakec6fa34e1998-04-02 06:47:24 +00001921\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001922\end{cfuncdesc}
1923
1924\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001925\end{cfuncdesc}
1926
1927
1928\section{Mapping Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001929\label{mapObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001930
1931\subsection{Dictionary Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001932\label{dictObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001933
1934\begin{ctypedesc}{PyDictObject}
1935This subtype of \code{PyObject} represents a Python dictionary object.
1936\end{ctypedesc}
1937
1938\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1939This instance of \code{PyTypeObject} represents the Python dictionary type.
1940\end{cvardesc}
1941
1942\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001943Returns true if its argument is a \code{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001944\end{cfuncdesc}
1945
Fred Drakec6fa34e1998-04-02 06:47:24 +00001946\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001947Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001948\end{cfuncdesc}
1949
1950\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001951Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001952\end{cfuncdesc}
1953
1954\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1955 PyObject *key,
1956 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001957Inserts \var{value} into the dictionary with a key of \var{key}. Both
1958\var{key} and \var{value} should be PyObjects, and \var{key} should be
1959hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001960\end{cfuncdesc}
1961
1962\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1963 char *key,
1964 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001965Inserts \var{value} into the dictionary using \var{key}
1966as a key. \var{key} should be a \code{char *}. The key object is
1967created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001968\end{cfuncdesc}
1969
1970\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001971Removes the entry in dictionary \var{p} with key \var{key}.
1972\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001973\end{cfuncdesc}
1974
1975\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001976Removes the entry in dictionary \var{p} which has a key
1977specified by the \code{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001978\end{cfuncdesc}
1979
Fred Drakec6fa34e1998-04-02 06:47:24 +00001980\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001981Returns the object from dictionary \var{p} which has a key
1982\var{key}. Returns \NULL{} if the key \var{key} is not present.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001983\end{cfuncdesc}
1984
Fred Drakec6fa34e1998-04-02 06:47:24 +00001985\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001986Does the same, but \var{key} is specified as a
Fred Drakee5bf8b21998-02-12 21:22:28 +00001987\code{char *}, rather than a \code{PyObject *}.
1988\end{cfuncdesc}
1989
Fred Drakec6fa34e1998-04-02 06:47:24 +00001990\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001991Returns a \code{PyListObject} containing all the items
1992from the dictionary, as in the mapping method \method{items()} (see
1993the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001994\end{cfuncdesc}
1995
Fred Drakec6fa34e1998-04-02 06:47:24 +00001996\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001997Returns a \code{PyListObject} containing all the keys
1998from the dictionary, as in the mapping method \method{keys()} (see the
1999\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002000\end{cfuncdesc}
2001
Fred Drakec6fa34e1998-04-02 06:47:24 +00002002\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002003Returns a \code{PyListObject} containing all the values
2004from the dictionary \var{p}, as in the mapping method
2005\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002006\end{cfuncdesc}
2007
2008\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002009Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002010\end{cfuncdesc}
2011
2012\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
2013 int ppos,
2014 PyObject **pkey,
2015 PyObject **pvalue}
2016
2017\end{cfuncdesc}
2018
2019
2020\section{Numeric Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002021\label{numericObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002022
2023\subsection{Plain Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002024\label{intObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002025
2026\begin{ctypedesc}{PyIntObject}
2027This subtype of \code{PyObject} represents a Python integer object.
2028\end{ctypedesc}
2029
2030\begin{cvardesc}{PyTypeObject}{PyInt_Type}
2031This instance of \code{PyTypeObject} represents the Python plain
2032integer type.
2033\end{cvardesc}
2034
2035\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
2036
2037\end{cfuncdesc}
2038
Fred Drakec6fa34e1998-04-02 06:47:24 +00002039\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00002040Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002041
2042The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00002043integers between \code{-1} and \code{100}, when you create an int in
2044that range you actually just get back a reference to the existing
2045object. So it should be possible to change the value of \code{1}. I
Fred Drake7e9d3141998-04-03 05:02:28 +00002046suspect the behaviour of Python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00002047\end{cfuncdesc}
2048
2049\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002050Returns the value of the object \var{io}. No error checking is
2051performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002052\end{cfuncdesc}
2053
2054\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002055Will first attempt to cast the object to a \code{PyIntObject}, if
2056it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002057\end{cfuncdesc}
2058
2059\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002060Returns the systems idea of the largest integer it can handle
2061(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002062\end{cfuncdesc}
2063
2064
2065\subsection{Long Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002066\label{longObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002067
2068\begin{ctypedesc}{PyLongObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002069This subtype of \code{PyObject} represents a Python long integer
2070object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002071\end{ctypedesc}
2072
2073\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002074This instance of \code{PyTypeObject} represents the Python long
2075integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002076\end{cvardesc}
2077
2078\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002079Returns true if its argument is a \code{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002080\end{cfuncdesc}
2081
Fred Drakec6fa34e1998-04-02 06:47:24 +00002082\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002083Returns a new \code{PyLong} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002084\end{cfuncdesc}
2085
Fred Drakec6fa34e1998-04-02 06:47:24 +00002086\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002087Returns a new \code{PyLong} object from an unsigned \C{} long.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002088\end{cfuncdesc}
2089
Fred Drakec6fa34e1998-04-02 06:47:24 +00002090\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002091Returns a new \code{PyLong} object from the integer part of \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002092\end{cfuncdesc}
2093
Fred Drakec6fa34e1998-04-02 06:47:24 +00002094\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002095Returns a \C{} \code{long} representation of the contents of \var{pylong}.
2096WHAT HAPPENS IF \var{pylong} > MAXLONG?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002097\end{cfuncdesc}
2098
Fred Drakec6fa34e1998-04-02 06:47:24 +00002099\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002100Returns a \C{} \code{unsigned long} representation of the contents of
2101\var{pylong}. WHAT HAPPENS IF \var{pylong} > MAXLONG?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002102\end{cfuncdesc}
2103
Fred Drakec6fa34e1998-04-02 06:47:24 +00002104\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002105Returns a \C{} \code{double} representation of teh contents of \var{pylong}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002106\end{cfuncdesc}
2107
Fred Drakec6fa34e1998-04-02 06:47:24 +00002108\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
2109 int base}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002110\end{cfuncdesc}
2111
2112
2113\subsection{Floating Point Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002114\label{floatObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002115
2116\begin{ctypedesc}{PyFloatObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002117This subtype of \code{PyObject} represents a Python floating point
2118object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002119\end{ctypedesc}
2120
2121\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002122This instance of \code{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002123point type.
2124\end{cvardesc}
2125
2126\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002127Returns true if its argument is a \code{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002128\end{cfuncdesc}
2129
Fred Drakec6fa34e1998-04-02 06:47:24 +00002130\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002131Creates a \code{PyFloat} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002132\end{cfuncdesc}
2133
Fred Drakec6fa34e1998-04-02 06:47:24 +00002134\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002135Returns a \C{} \code{double} representation of the contents of \var{pyfloat}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002136\end{cfuncdesc}
2137
Fred Drakec6fa34e1998-04-02 06:47:24 +00002138\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002139\end{cfuncdesc}
2140
2141
2142\subsection{Complex Number Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002143\label{complexObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002144
2145\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002146The \C{} structure which corresponds to the value portion of a Python
2147complex number object. Most of the functions for dealing with complex
2148number objects use structures of this type as input or output values,
2149as appropriate. It is defined as:
2150
Fred Drakee058b4f1998-02-16 06:15:35 +00002151\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002152typedef struct {
2153 double real;
2154 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002155} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002156\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002157\end{ctypedesc}
2158
2159\begin{ctypedesc}{PyComplexObject}
2160This subtype of \code{PyObject} represents a Python complex number object.
2161\end{ctypedesc}
2162
2163\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2164This instance of \code{PyTypeObject} represents the Python complex
2165number type.
2166\end{cvardesc}
2167
2168\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002169Returns true if its argument is a \code{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002170\end{cfuncdesc}
2171
Fred Drakec6fa34e1998-04-02 06:47:24 +00002172\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002173\end{cfuncdesc}
2174
Fred Drakec6fa34e1998-04-02 06:47:24 +00002175\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002176\end{cfuncdesc}
2177
Fred Drakec6fa34e1998-04-02 06:47:24 +00002178\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002179\end{cfuncdesc}
2180
Fred Drakec6fa34e1998-04-02 06:47:24 +00002181\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002182\end{cfuncdesc}
2183
Fred Drakec6fa34e1998-04-02 06:47:24 +00002184\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
2185 Py_complex divisor}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002186\end{cfuncdesc}
2187
Fred Drakec6fa34e1998-04-02 06:47:24 +00002188\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002189\end{cfuncdesc}
2190
Fred Drakec6fa34e1998-04-02 06:47:24 +00002191\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002192\end{cfuncdesc}
2193
Fred Drakec6fa34e1998-04-02 06:47:24 +00002194\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002195Returns a new \code{PyComplex} object from \var{real} and \var{imag}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002196\end{cfuncdesc}
2197
2198\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002199Returns the real part of \var{op} as a \C{} \code{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002200\end{cfuncdesc}
2201
2202\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002203Returns the imaginary part of \var{op} as a \C{} \code{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002204\end{cfuncdesc}
2205
2206\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002207\end{cfuncdesc}
2208
2209
2210
2211\section{Other Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002212\label{otherObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002213
2214\subsection{File Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002215\label{fileObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002216
2217\begin{ctypedesc}{PyFileObject}
2218This subtype of \code{PyObject} represents a Python file object.
2219\end{ctypedesc}
2220
2221\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2222This instance of \code{PyTypeObject} represents the Python file type.
2223\end{cvardesc}
2224
2225\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002226Returns true if its argument is a \code{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002227\end{cfuncdesc}
2228
Fred Drakec6fa34e1998-04-02 06:47:24 +00002229\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *name, char *mode}
Fred Drakee058b4f1998-02-16 06:15:35 +00002230Creates a new \code{PyFileObject} pointing to the file
2231specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002232\end{cfuncdesc}
2233
Fred Drakec6fa34e1998-04-02 06:47:24 +00002234\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
Fred Drakeb92dce31998-02-25 15:40:22 +00002235 char *name, char *mode, int (*close)}
Fred Drakee058b4f1998-02-16 06:15:35 +00002236Creates a new \code{PyFileObject} from the already-open \var{fp}.
2237The function \var{close} will be called when the file should be
2238closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002239\end{cfuncdesc}
2240
2241\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002242Returns the file object associated with \var{p} as a \code{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002243\end{cfuncdesc}
2244
Fred Drakec6fa34e1998-04-02 06:47:24 +00002245\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002246undocumented as yet
2247\end{cfuncdesc}
2248
Fred Drakec6fa34e1998-04-02 06:47:24 +00002249\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002250Returns the name of the file specified by \var{p} as a
2251\code{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002252\end{cfuncdesc}
2253
2254\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002255Available on systems with \cfunction{setvbuf()} only. This should
2256only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002257\end{cfuncdesc}
2258
2259\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakee058b4f1998-02-16 06:15:35 +00002260Sets the \code{softspace} attribute of \var{p} to \var{newflag}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002261Returns the previous value. This function clears any errors, and will
Fred Drakee058b4f1998-02-16 06:15:35 +00002262return \code{0} as the previous value if the attribute either does not
2263exist or if there were errors in retrieving it. There is no way to
2264detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002265\end{cfuncdesc}
2266
Fred Drakec6fa34e1998-04-02 06:47:24 +00002267\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2268 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002269Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002270\end{cfuncdesc}
2271
Fred Drakec6fa34e1998-04-02 06:47:24 +00002272\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p,
2273 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002274Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002275\end{cfuncdesc}
2276
2277
2278\subsection{CObjects}
Fred Drakef39ed671998-02-26 22:01:23 +00002279\label{cObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002280
2281XXX
2282
2283
Guido van Rossum4a944d71997-08-14 20:35:38 +00002284\chapter{Initialization, Finalization, and Threads}
Fred Drakef39ed671998-02-26 22:01:23 +00002285\label{initialization}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002286
Guido van Rossum4a944d71997-08-14 20:35:38 +00002287\begin{cfuncdesc}{void}{Py_Initialize}{}
2288Initialize the Python interpreter. In an application embedding
2289Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002290functions; with the exception of \cfunction{Py_SetProgramName()},
2291\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2292and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2293loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002294modules \module{__builtin__}\refbimodindex{__builtin__},
2295\module{__main__}\refbimodindex{__main__} and
2296\module{sys}\refbimodindex{sys}. It also initializes the module
2297search path (\code{sys.path}).%
2298\indexiii{module}{search}{path}
2299It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2300that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002301\cfunction{Py_Finalize()} first). There is no return value; it is a
2302fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002303\end{cfuncdesc}
2304
2305\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002306Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002307initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2308called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002309again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002310\end{cfuncdesc}
2311
2312\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002313Undo all initializations made by \cfunction{Py_Initialize()} and
2314subsequent use of Python/C API functions, and destroy all
2315sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2316created and not yet destroyed since the last call to
2317\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2318by the Python interpreter. This is a no-op when called for a second
2319time (without calling \cfunction{Py_Initialize()} again first). There
2320is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002321
2322This function is provided for a number of reasons. An embedding
2323application might want to restart Python without having to restart the
2324application itself. An application that has loaded the Python
2325interpreter from a dynamically loadable library (or DLL) might want to
2326free all memory allocated by Python before unloading the DLL. During a
2327hunt for memory leaks in an application a developer might want to free
2328all memory allocated by Python before exiting from the application.
2329
Fred Drakee058b4f1998-02-16 06:15:35 +00002330\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002331modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002332(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002333(even functions) or modules. Dynamically loaded extension modules
2334loaded by Python are not unloaded. Small amounts of memory allocated
2335by the Python interpreter may not be freed (if you find a leak, please
2336report it). Memory tied up in circular references between objects is
2337not freed. Some memory allocated by extension modules may not be
2338freed. Some extension may not work properly if their initialization
2339routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002340calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2341than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002342\end{cfuncdesc}
2343
Fred Drakec6fa34e1998-04-02 06:47:24 +00002344\begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002345Create a new sub-interpreter. This is an (almost) totally separate
2346environment for the execution of Python code. In particular, the new
2347interpreter has separate, independent versions of all imported
2348modules, including the fundamental modules
2349\module{__builtin__}\refbimodindex{__builtin__},
2350\module{__main__}\refbimodindex{__main__} and
2351\module{sys}\refbimodindex{sys}. The table of loaded modules
2352(\code{sys.modules}) and the module search path (\code{sys.path}) are
2353also separate. The new environment has no \code{sys.argv} variable.
2354It has new standard I/O stream file objects \code{sys.stdin},
2355\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakeb0a78731998-01-13 18:51:10 +00002356same underlying \code{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002357
2358The return value points to the first thread state created in the new
2359sub-interpreter. This thread state is made the current thread state.
2360Note that no actual thread is created; see the discussion of thread
2361states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002362\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002363is stored in the current thread state and there may not be a current
2364thread state. (Like all other Python/C API functions, the global
2365interpreter lock must be held before calling this function and is
2366still held when it returns; however, unlike most other Python/C API
2367functions, there needn't be a current thread state on entry.)
2368
2369Extension modules are shared between (sub-)interpreters as follows:
2370the first time a particular extension is imported, it is initialized
2371normally, and a (shallow) copy of its module's dictionary is
2372squirreled away. When the same extension is imported by another
2373(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002374contents of this copy; the extension's \code{init} function is not
2375called. Note that this is different from what happens when an
2376extension is imported after the interpreter has been completely
2377re-initialized by calling \cfunction{Py_Finalize()} and
2378\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002379function \emph{is} called again.
2380
Fred Drakee058b4f1998-02-16 06:15:35 +00002381\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002382interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002383isn't perfect --- for example, using low-level file operations like
Guido van Rossum4a944d71997-08-14 20:35:38 +00002384\code{os.close()} they can (accidentally or maliciously) affect each
2385other's open files. Because of the way extensions are shared between
2386(sub-)interpreters, some extensions may not work properly; this is
2387especially likely when the extension makes use of (static) global
2388variables, or when the extension manipulates its module's dictionary
2389after its initialization. It is possible to insert objects created in
2390one sub-interpreter into a namespace of another sub-interpreter; this
2391should be done with great care to avoid sharing user-defined
2392functions, methods, instances or classes between sub-interpreters,
2393since import operations executed by such objects may affect the
2394wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2395a hard-to-fix bug that will be addressed in a future release.)
2396\end{cfuncdesc}
2397
2398\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
2399Destroy the (sub-)interpreter represented by the given thread state.
2400The given thread state must be the current thread state. See the
2401discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002402thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002403interpreted are destroyed. (The global interpreter lock must be held
2404before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002405\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002406been explicitly destroyed at that point.
2407\end{cfuncdesc}
2408
2409\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +00002410This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002411for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002412the value of the \code{argv[0]} argument to the \cfunction{main()} function
2413of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002414functions below to find the Python run-time libraries relative to the
2415interpreter executable. The default value is \code{"python"}. The
2416argument should point to a zero-terminated character string in static
2417storage whose contents will not change for the duration of the
2418program's execution. No code in the Python interpreter will change
2419the contents of this storage.
2420\end{cfuncdesc}
2421
Fred Drakec6fa34e1998-04-02 06:47:24 +00002422\begin{cfuncdesc}{char*}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002423Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002424default. The returned string points into static storage; the caller
2425should not modify its value.
2426\end{cfuncdesc}
2427
Fred Drakec6fa34e1998-04-02 06:47:24 +00002428\begin{cfuncdesc}{char*}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002429Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002430is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002431set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002432for example, if the program name is \code{"/usr/local/bin/python"},
2433the prefix is \code{"/usr/local"}. The returned string points into
2434static storage; the caller should not modify its value. This
Fred Drakec94d9341998-04-12 02:39:13 +00002435corresponds to the \makevar{prefix} variable in the top-level
2436\file{Makefile} and the \code{-}\code{-prefix} argument to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002437\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002438Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002439also the next function.
2440\end{cfuncdesc}
2441
Fred Drakec6fa34e1998-04-02 06:47:24 +00002442\begin{cfuncdesc}{char*}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002443Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002444files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002445program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002446variables; for example, if the program name is
2447\code{"/usr/local/bin/python"}, the exec-prefix is
2448\code{"/usr/local"}. The returned string points into static storage;
2449the caller should not modify its value. This corresponds to the
Fred Drakec94d9341998-04-12 02:39:13 +00002450\makevar{exec_prefix} variable in the top-level \file{Makefile} and the
2451\code{-}\code{-exec_prefix} argument to the \program{configure} script
2452at build time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002453\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002454
2455Background: The exec-prefix differs from the prefix when platform
2456dependent files (such as executables and shared libraries) are
2457installed in a different directory tree. In a typical installation,
2458platform dependent files may be installed in the
2459\code{"/usr/local/plat"} subtree while platform independent may be
2460installed in \code{"/usr/local"}.
2461
2462Generally speaking, a platform is a combination of hardware and
2463software families, e.g. Sparc machines running the Solaris 2.x
2464operating system are considered the same platform, but Intel machines
2465running Solaris 2.x are another platform, and Intel machines running
2466Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002467operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002468operating systems are a different story; the installation strategies
2469on those systems are so different that the prefix and exec-prefix are
2470meaningless, and set to the empty string. Note that compiled Python
2471bytecode files are platform independent (but not independent from the
2472Python version by which they were compiled!).
2473
Fred Drakee058b4f1998-02-16 06:15:35 +00002474System administrators will know how to configure the \program{mount} or
2475\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002476while having \code{"/usr/local/plat"} be a different filesystem for each
2477platform.
2478\end{cfuncdesc}
2479
Fred Drakec6fa34e1998-04-02 06:47:24 +00002480\begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002481Return the full program name of the Python executable; this is
2482computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002483from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002484returned string points into static storage; the caller should not
2485modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002486\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002487\end{cfuncdesc}
2488
Fred Drakec6fa34e1998-04-02 06:47:24 +00002489\begin{cfuncdesc}{char*}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002490\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002491Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002492program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002493environment variables. The returned string consists of a series of
2494directory names separated by a platform dependent delimiter character.
Fred Drakee5bc4971998-02-12 23:36:49 +00002495The delimiter character is \code{':'} on \UNIX{}, \code{';'} on
2496DOS/Windows, and \code{'\\n'} (the \ASCII{} newline character) on
2497Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002498should not modify its value. The value is available to Python code
2499as the list \code{sys.path}, which may be modified to change the
2500future search path for loaded modules.
2501
2502% XXX should give the exact rules
2503\end{cfuncdesc}
2504
Fred Drakec6fa34e1998-04-02 06:47:24 +00002505\begin{cfuncdesc}{const char*}{Py_GetVersion}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002506Return the version of this Python interpreter. This is a string that
2507looks something like
2508
Guido van Rossum09270b51997-08-15 18:57:32 +00002509\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002510"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002511\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002512
2513The first word (up to the first space character) is the current Python
2514version; the first three characters are the major and minor version
2515separated by a period. The returned string points into static storage;
2516the caller should not modify its value. The value is available to
2517Python code as the list \code{sys.version}.
2518\end{cfuncdesc}
2519
Fred Drakec6fa34e1998-04-02 06:47:24 +00002520\begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002521Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002522this is formed from the ``official'' name of the operating system,
2523converted to lower case, followed by the major revision number; e.g.,
2524for Solaris 2.x, which is also known as SunOS 5.x, the value is
2525\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2526is \code{"win"}. The returned string points into static storage;
2527the caller should not modify its value. The value is available to
2528Python code as \code{sys.platform}.
2529\end{cfuncdesc}
2530
Fred Drakec6fa34e1998-04-02 06:47:24 +00002531\begin{cfuncdesc}{const char*}{Py_GetCopyright}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002532Return the official copyright string for the current Python version,
2533for example
2534
2535\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2536
2537The returned string points into static storage; the caller should not
2538modify its value. The value is available to Python code as the list
2539\code{sys.copyright}.
2540\end{cfuncdesc}
2541
Fred Drakec6fa34e1998-04-02 06:47:24 +00002542\begin{cfuncdesc}{const char*}{Py_GetCompiler}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002543Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002544version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002545
Fred Drakee058b4f1998-02-16 06:15:35 +00002546\begin{verbatim}
2547"[GCC 2.7.2.2]"
2548\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002549
2550The returned string points into static storage; the caller should not
2551modify its value. The value is available to Python code as part of
2552the variable \code{sys.version}.
2553\end{cfuncdesc}
2554
Fred Drakec6fa34e1998-04-02 06:47:24 +00002555\begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002556Return information about the sequence number and build date and time
2557of the current Python interpreter instance, for example
2558
Guido van Rossum09270b51997-08-15 18:57:32 +00002559\begin{verbatim}
2560"#67, Aug 1 1997, 22:34:28"
2561\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002562
2563The returned string points into static storage; the caller should not
2564modify its value. The value is available to Python code as part of
2565the variable \code{sys.version}.
2566\end{cfuncdesc}
2567
2568\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2569% XXX
2570\end{cfuncdesc}
2571
2572% XXX Other PySys thingies (doesn't really belong in this chapter)
2573
2574\section{Thread State and the Global Interpreter Lock}
Fred Drakef39ed671998-02-26 22:01:23 +00002575\label{threads}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002576
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002577The Python interpreter is not fully thread safe. In order to support
2578multi-threaded Python programs, there's a global lock that must be
2579held by the current thread before it can safely access Python objects.
2580Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00002581a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002582increment the reference count of the same object, the reference count
2583could end up being incremented only once instead of twice.
2584
2585Therefore, the rule exists that only the thread that has acquired the
2586global interpreter lock may operate on Python objects or call Python/C
2587API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002588the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002589default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002590\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002591reacquired around potentially blocking I/O operations like reading or
2592writing a file, so that other threads can run while the thread that
2593requests the I/O is waiting for the I/O operation to complete.
2594
2595The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002596separate per thread --- for this it uses a data structure called
2597\code{PyThreadState}. This is new in Python 1.5; in earlier versions,
2598such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002599cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002600when the application uses \function{sys.exc_info()} to access the
2601exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002602
2603There's one global variable left, however: the pointer to the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002604\code{PyThreadState} structure. While most thread packages have a way
Fred Drake9d20ac31998-02-16 15:27:08 +00002605to store ``per-thread global data,'' Python's internal platform
Fred Drakee058b4f1998-02-16 06:15:35 +00002606independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002607the current thread state must be manipulated explicitly.
2608
2609This is easy enough in most cases. Most code manipulating the global
2610interpreter lock has the following simple structure:
2611
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002612\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002613Save the thread state in a local variable.
2614Release the interpreter lock.
2615...Do some blocking I/O operation...
2616Reacquire the interpreter lock.
2617Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002618\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002619
2620This is so common that a pair of macros exists to simplify it:
2621
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002622\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002623Py_BEGIN_ALLOW_THREADS
2624...Do some blocking I/O operation...
2625Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002626\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002627
Fred Drakee058b4f1998-02-16 06:15:35 +00002628The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2629a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2630the block. Another advantage of using these two macros is that when
2631Python is compiled without thread support, they are defined empty,
2632thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002633
2634When thread support is enabled, the block above expands to the
2635following code:
2636
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002637\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002638{
2639 PyThreadState *_save;
2640 _save = PyEval_SaveThread();
2641 ...Do some blocking I/O operation...
2642 PyEval_RestoreThread(_save);
2643}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002644\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002645
2646Using even lower level primitives, we can get roughly the same effect
2647as follows:
2648
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002649\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002650{
2651 PyThreadState *_save;
2652 _save = PyThreadState_Swap(NULL);
2653 PyEval_ReleaseLock();
2654 ...Do some blocking I/O operation...
2655 PyEval_AcquireLock();
2656 PyThreadState_Swap(_save);
2657}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002658\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002659
2660There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002661\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002662global variable \code{errno}, since the lock manipulation does not
2663guarantee that \code{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002664is disabled, \cfunction{PyEval_SaveThread()} and
2665\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2666case, \cfunction{PyEval_ReleaseLock()} and
2667\cfunction{PyEval_AcquireLock()} are not available. This is done so
2668that dynamically loaded extensions compiled with thread support
2669enabled can be loaded by an interpreter that was compiled with
2670disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002671
2672The global interpreter lock is used to protect the pointer to the
2673current thread state. When releasing the lock and saving the thread
2674state, the current thread state pointer must be retrieved before the
2675lock is released (since another thread could immediately acquire the
2676lock and store its own thread state in the global variable).
2677Reversely, when acquiring the lock and restoring the thread state, the
2678lock must be acquired before storing the thread state pointer.
2679
2680Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002681threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002682lock, nor is there a thread state data structure for them. Such
2683threads must bootstrap themselves into existence, by first creating a
2684thread state data structure, then acquiring the lock, and finally
2685storing their thread state pointer, before they can start using the
2686Python/C API. When they are done, they should reset the thread state
2687pointer, release the lock, and finally free their thread state data
2688structure.
2689
2690When creating a thread data structure, you need to provide an
2691interpreter state data structure. The interpreter state data
2692structure hold global data that is shared by all threads in an
2693interpreter, for example the module administration
2694(\code{sys.modules}). Depending on your needs, you can either create
2695a new interpreter state data structure, or share the interpreter state
2696data structure used by the Python main thread (to access the latter,
2697you must obtain the thread state and access its \code{interp} member;
2698this must be done by a thread that is created by Python or by the main
2699thread after Python is initialized).
2700
2701XXX More?
2702
2703\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002704This data structure represents the state shared by a number of
2705cooperating threads. Threads belonging to the same interpreter
2706share their module administration and a few other internal items.
2707There are no public members in this structure.
2708
2709Threads belonging to different interpreters initially share nothing,
2710except process state like available memory, open file descriptors and
2711such. The global interpreter lock is also shared by all threads,
2712regardless of to which interpreter they belong.
2713\end{ctypedesc}
2714
2715\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002716This data structure represents the state of a single thread. The only
2717public data member is \code{PyInterpreterState *interp}, which points
2718to this thread's interpreter state.
2719\end{ctypedesc}
2720
2721\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2722Initialize and acquire the global interpreter lock. It should be
2723called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002724in any other thread operations such as
2725\cfunction{PyEval_ReleaseLock()} or
2726\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2727calling \cfunction{PyEval_SaveThread()} or
2728\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002729
2730This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002731this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002732
2733When only the main thread exists, no lock operations are needed. This
2734is a common situation (most Python programs do not use threads), and
2735the lock operations slow the interpreter down a bit. Therefore, the
2736lock is not created initially. This situation is equivalent to having
2737acquired the lock: when there is only a single thread, all object
2738accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002739lock, it also acquires it. Before the Python
2740\module{thread}\refbimodindex{thread} module creates a new thread,
2741knowing that either it has the lock or the lock hasn't been created
2742yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2743returns, it is guaranteed that the lock has been created and that it
2744has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002745
2746It is \strong{not} safe to call this function when it is unknown which
2747thread (if any) currently has the global interpreter lock.
2748
2749This function is not available when thread support is disabled at
2750compile time.
2751\end{cfuncdesc}
2752
Guido van Rossum4a944d71997-08-14 20:35:38 +00002753\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002754Acquire the global interpreter lock. The lock must have been created
2755earlier. If this thread already has the lock, a deadlock ensues.
2756This function is not available when thread support is disabled at
2757compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002758\end{cfuncdesc}
2759
2760\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002761Release the global interpreter lock. The lock must have been created
2762earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002763disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002764\end{cfuncdesc}
2765
2766\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002767Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002768state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002769have been created earlier. If this thread already has the lock,
2770deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002771is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002772\end{cfuncdesc}
2773
2774\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002775Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002776interpreter lock. The lock must have been created earlier and must be
2777held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002778be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002779thread state --- if it isn't, a fatal error is reported. This
2780function is not available when thread support is disabled at compile
2781time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002782\end{cfuncdesc}
2783
Fred Drakec6fa34e1998-04-02 06:47:24 +00002784\begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002785Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002786support is enabled) and reset the thread state to \NULL{},
2787returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002788the lock has been created, the current thread must have acquired it.
2789(This function is available even when thread support is disabled at
2790compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002791\end{cfuncdesc}
2792
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002793\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002794Acquire the interpreter lock (if it has been created and thread
2795support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002796must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002797thread must not have acquired it, otherwise deadlock ensues. (This
2798function is available even when thread support is disabled at compile
2799time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002800\end{cfuncdesc}
2801
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002802% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2803\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2804This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002805\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002806Note that it contains an opening brace; it must be matched with a
2807following \code{Py_END_ALLOW_THREADS} macro. See above for further
2808discussion of this macro. It is a no-op when thread support is
2809disabled at compile time.
2810\end{ctypedesc}
2811
2812\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2813This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002814\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002815Note that it contains a closing brace; it must be matched with an
2816earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2817discussion of this macro. It is a no-op when thread support is
2818disabled at compile time.
2819\end{ctypedesc}
2820
2821\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002822This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002823is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2824brace. It is a no-op when thread support is disabled at compile
2825time.
2826\end{ctypedesc}
2827
2828\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002829This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002830equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2831and variable declaration. It is a no-op when thread support is
2832disabled at compile time.
2833\end{ctypedesc}
2834
2835All of the following functions are only available when thread support
2836is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00002837interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002838
Fred Drakec6fa34e1998-04-02 06:47:24 +00002839\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002840Create a new interpreter state object. The interpreter lock must be
2841held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002842\end{cfuncdesc}
2843
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002844\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2845Reset all information in an interpreter state object. The interpreter
2846lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002847\end{cfuncdesc}
2848
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002849\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2850Destroy an interpreter state object. The interpreter lock need not be
2851held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002852call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002853\end{cfuncdesc}
2854
Fred Drakec6fa34e1998-04-02 06:47:24 +00002855\begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002856Create a new thread state object belonging to the given interpreter
2857object. The interpreter lock must be held.
2858\end{cfuncdesc}
2859
2860\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2861Reset all information in a thread state object. The interpreter lock
2862must be held.
2863\end{cfuncdesc}
2864
2865\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2866Destroy a thread state object. The interpreter lock need not be
2867held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002868call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002869\end{cfuncdesc}
2870
Fred Drakec6fa34e1998-04-02 06:47:24 +00002871\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002872Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002873When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002874error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002875\end{cfuncdesc}
2876
Fred Drakec6fa34e1998-04-02 06:47:24 +00002877\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002878Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002879argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002880must be held.
2881\end{cfuncdesc}
2882
2883
Fred Drakee058b4f1998-02-16 06:15:35 +00002884\chapter{Defining New Object Types}
Fred Drakef39ed671998-02-26 22:01:23 +00002885\label{newTypes}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002886
Fred Drakec6fa34e1998-04-02 06:47:24 +00002887\begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002888\end{cfuncdesc}
2889
Fred Drakec6fa34e1998-04-02 06:47:24 +00002890\begin{cfuncdesc}{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
Fred Drakee058b4f1998-02-16 06:15:35 +00002891\end{cfuncdesc}
2892
2893\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2894\end{cfuncdesc}
2895
2896\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2897\end{cfuncdesc}
2898
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002899Py_InitModule (!!!)
2900
2901PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse
2902
2903Py_BuildValue
Guido van Rossumae110af1997-05-22 20:11:52 +00002904
2905PyObject, PyVarObject
2906
2907PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2908
2909Typedefs:
2910unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2911intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2912getreadbufferproc, getwritebufferproc, getsegcountproc,
2913destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2914setattrofunc, cmpfunc, reprfunc, hashfunc
2915
2916PyNumberMethods
2917
2918PySequenceMethods
2919
2920PyMappingMethods
2921
2922PyBufferProcs
2923
2924PyTypeObject
2925
2926DL_IMPORT
2927
2928PyType_Type
2929
2930Py*_Check
2931
2932Py_None, _Py_NoneStruct
2933
Guido van Rossumae110af1997-05-22 20:11:52 +00002934
Fred Drakee5bf8b21998-02-12 21:22:28 +00002935\chapter{Debugging}
Fred Drakef39ed671998-02-26 22:01:23 +00002936\label{debugging}
Guido van Rossumae110af1997-05-22 20:11:52 +00002937
Fred Drakee5bf8b21998-02-12 21:22:28 +00002938XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002939
2940
Fred Drakef3aa0e01998-03-17 06:23:13 +00002941\input{api.ind} % Index -- must be last
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002942
2943\end{document}