blob: 7d998485fbde64a9171062cc1ea7a6ebf27c4f67 [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 Drakee058b4f1998-02-16 06:15:35 +000058This manual describes the \version\ state of affair.
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
Guido van Rossum4a944d71997-08-14 20:35:38 +0000118finite (and often severly limited) memory size; it counts how many
119different 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
195the item it put!). These functions were designed to steal a reference
196because 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;
203t = PyTuple_New(3);
204PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
205PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
206PyTuple_SetItem(t, 2, PyString_FromString("three"));
207\end{verbatim}
208
Fred Drakee058b4f1998-02-16 06:15:35 +0000209Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
210set tuple items; \cfunction{PySequence_SetItem()} and
211\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
212immutable data type. You should only use
213\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000214yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000215
216Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000217\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
218can also use \cfunction{PySequence_SetItem()}; this illustrates the
219difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000220
221\begin{verbatim}
222PyObject *l, *x;
223l = PyList_New(3);
224x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000225PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000226x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000227PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000228x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000229PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000230\end{verbatim}
231
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000232You might find it strange that the ``recommended'' approach takes more
233code. However, in practice, you will rarely use these ways of
234creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000235\cfunction{Py_BuildValue()}, that can create most common objects from
236\C{} values, directed by a \dfn{format string}. For example, the
237above two blocks of code could be replaced by the following (which
238also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000239
240\begin{verbatim}
241PyObject *t, *l;
242t = Py_BuildValue("(iis)", 1, 2, "three");
243l = Py_BuildValue("[iis]", 1, 2, "three");
244\end{verbatim}
245
Fred Drakee058b4f1998-02-16 06:15:35 +0000246It is much more common to use \cfunction{PyObject_SetItem()} and
247friends with items whose references you are only borrowing, like
248arguments that were passed in to the function you are writing. In
249that case, their behaviour regarding reference counts is much saner,
250since you don't have to increment a reference count so you can give a
251reference away (``have it be stolen''). For example, this function
252sets all items of a list (actually, any mutable sequence) to a given
253item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000254
255\begin{verbatim}
256int set_all(PyObject *target, PyObject *item)
257{
258 int i, n;
259 n = PyObject_Length(target);
260 if (n < 0)
261 return -1;
262 for (i = 0; i < n; i++) {
263 if (PyObject_SetItem(target, i, item) < 0)
264 return -1;
265 }
266 return 0;
267}
268\end{verbatim}
269
270The situation is slightly different for function return values.
271While passing a reference to most functions does not change your
272ownership responsibilities for that reference, many functions that
273return a referece to an object give you ownership of the reference.
274The reason is simple: in many cases, the returned object is created
275on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000276object. Therefore, the generic functions that return object
277references, like \cfunction{PyObject_GetItem()} and
278\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
279the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000280
281It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000282by a function depends on which function you call only --- \emph{the
283plumage} (i.e., the type of the type of the object passed as an
284argument to the function) \emph{doesn't enter into it!} Thus, if you
285extract an item from a list using \cfunction{PyList_GetItem()}, you
286don't own the reference --- but if you obtain the same item from the
287same list using \cfunction{PySequence_GetItem()} (which happens to
288take exactly the same arguments), you do own a reference to the
289returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000290
Fred Drakee058b4f1998-02-16 06:15:35 +0000291Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000292sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000293\cfunction{PyList_GetItem()}, once using
294\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000295
296\begin{verbatim}
297long sum_list(PyObject *list)
298{
299 int i, n;
300 long total = 0;
301 PyObject *item;
302 n = PyList_Size(list);
303 if (n < 0)
304 return -1; /* Not a list */
305 for (i = 0; i < n; i++) {
306 item = PyList_GetItem(list, i); /* Can't fail */
307 if (!PyInt_Check(item)) continue; /* Skip non-integers */
308 total += PyInt_AsLong(item);
309 }
310 return total;
311}
312\end{verbatim}
313
314\begin{verbatim}
315long sum_sequence(PyObject *sequence)
316{
317 int i, n;
318 long total = 0;
319 PyObject *item;
320 n = PyObject_Size(list);
321 if (n < 0)
322 return -1; /* Has no length */
323 for (i = 0; i < n; i++) {
324 item = PySequence_GetItem(list, i);
325 if (item == NULL)
326 return -1; /* Not a sequence, or other failure */
327 if (PyInt_Check(item))
328 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000329 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000330 }
331 return total;
332}
333\end{verbatim}
334
335\subsection{Types}
Fred Drakef39ed671998-02-26 22:01:23 +0000336\label{types}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000337
338There are few other data types that play a significant role in
Fred Drakeb0a78731998-01-13 18:51:10 +0000339the Python/C API; most are simple \C{} types such as \code{int},
Guido van Rossum4a944d71997-08-14 20:35:38 +0000340\code{long}, \code{double} and \code{char *}. A few structure types
341are used to describe static tables used to list the functions exported
342by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000343be discussed together with the functions that use them.
344
345\section{Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000346\label{exceptions}
Guido van Rossum59a61351997-08-14 20:34:33 +0000347
Guido van Rossum4a944d71997-08-14 20:35:38 +0000348The Python programmer only needs to deal with exceptions if specific
349error handling is required; unhandled exceptions are automatically
350propagated to the caller, then to the caller's caller, and so on, till
351they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000352user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000353
Fred Drakeb0a78731998-01-13 18:51:10 +0000354For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000355All functions in the Python/C API can raise exceptions, unless an
356explicit claim is made otherwise in a function's documentation. In
357general, when a function encounters an error, it sets an exception,
358discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000359error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000360return a Boolean true/false result, with false indicating an error.
361Very few functions return no explicit error indicator or have an
362ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000363\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000364
365Exception state is maintained in per-thread storage (this is
366equivalent to using global storage in an unthreaded application). A
Fred Drakee058b4f1998-02-16 06:15:35 +0000367thread can be on one of two states: an exception has occurred, or not.
368The function \cfunction{PyErr_Occurred()} can be used to check for
369this: it returns a borrowed reference to the exception type object
370when an exception has occurred, and \NULL{} otherwise. There are a
371number of functions to set the exception state:
372\cfunction{PyErr_SetString()} is the most common (though not the most
373general) function to set the exception state, and
374\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000375
376The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000377be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000378value, and the traceback. These have the same meanings as the Python
379object \code{sys.exc_type}, \code{sys.exc_value},
380\code{sys.exc_traceback}; however, they are not the same: the Python
381objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000382\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
383exception state only exists while an exception is being passed on
384between \C{} functions until it reaches the Python interpreter, which
385takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000386
387(Note that starting with Python 1.5, the preferred, thread-safe way to
388access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000389\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000390for Python code. Also, the semantics of both ways to access the
391exception state have changed so that a function which catches an
392exception will save and restore its thread's exception state so as to
393preserve the exception state of its caller. This prevents common bugs
394in exception handling code caused by an innocent-looking function
395overwriting the exception being handled; it also reduces the often
396unwanted lifetime extension for objects that are referenced by the
397stack frames in the traceback.)
398
399As a general principle, a function that calls another function to
400perform some task should check whether the called function raised an
401exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000402should discard any object references that it owns, and returns an
403error indicator, but it should \emph{not} set another exception ---
404that would overwrite the exception that was just raised, and lose
405important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000406
407A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000408in the \cfunction{sum_sequence()} example above. It so happens that
409that example doesn't need to clean up any owned references when it
410detects an error. The following example function shows some error
411cleanup. First, to remind you why you like Python, we show the
412equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000413
414\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000415def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000416 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000417 item = dict[key]
418 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000419 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000420 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000421\end{verbatim}
422
Fred Drakeb0a78731998-01-13 18:51:10 +0000423Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000424
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000425\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000426int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000427{
428 /* Objects all initialized to NULL for Py_XDECREF */
429 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000430 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000431
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000432 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000433 if (item == NULL) {
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000434 /* Handle keyError only: */
435 if (!PyErr_ExceptionMatches(PyExc_keyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000436
437 /* Clear the error and use zero: */
438 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000439 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000440 if (item == NULL) goto error;
441 }
442
443 const_one = PyInt_FromLong(1L);
444 if (const_one == NULL) goto error;
445
446 incremented_item = PyNumber_Add(item, const_one);
447 if (incremented_item == NULL) goto error;
448
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000449 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000450 rv = 0; /* Success */
451 /* Continue with cleanup code */
452
453 error:
454 /* Cleanup code, shared by success and failure path */
455
456 /* Use Py_XDECREF() to ignore NULL references */
457 Py_XDECREF(item);
458 Py_XDECREF(const_one);
459 Py_XDECREF(incremented_item);
460
461 return rv; /* -1 for error, 0 for success */
462}
463\end{verbatim}
464
465This example represents an endorsed use of the \code{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000466in \C{}! It illustrates the use of
467\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
468handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
469dispose of owned references that may be \NULL{} (note the \samp{X} in
470the name; \cfunction{Py_DECREF()} would crash when confronted with a
471\NULL{} reference). It is important that the variables used to hold
472owned references are initialized to \NULL{} for this to work;
473likewise, the proposed return value is initialized to \code{-1}
474(failure) and only set to success after the final call made is
475successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000476
Guido van Rossum59a61351997-08-14 20:34:33 +0000477
478\section{Embedding Python}
Fred Drakef39ed671998-02-26 22:01:23 +0000479\label{embedding}
Guido van Rossum59a61351997-08-14 20:34:33 +0000480
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000481The one important task that only embedders (as opposed to extension
482writers) of the Python interpreter have to worry about is the
483initialization, and possibly the finalization, of the Python
484interpreter. Most functionality of the interpreter can only be used
485after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000486
Fred Drakee058b4f1998-02-16 06:15:35 +0000487The basic initialization function is \cfunction{Py_Initialize()}.
488This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000489fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
490\module{__main__}\refbimodindex{__main__} and
491\module{sys}\refbimodindex{sys}. It also initializes the module
492search path (\code{sys.path}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000493
Fred Drakee058b4f1998-02-16 06:15:35 +0000494\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000495(\code{sys.argv}). If this variable is needed by Python code that
496will be executed later, it must be set explicitly with a call to
497\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000498to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000499
Fred Drakeb0a78731998-01-13 18:51:10 +0000500On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000501details are slightly different), \cfunction{Py_Initialize()}
502calculates the module search path based upon its best guess for the
503location of the standard Python interpreter executable, assuming that
504the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000505interpreter executable. In particular, it looks for a directory named
Fred Drakee058b4f1998-02-16 06:15:35 +0000506\file{lib/python\version} (replacing \file{\version} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000507interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000508executable named \file{python} is found on the shell command search
Guido van Rossum42cefd01997-10-05 15:27:29 +0000509path (the environment variable \code{\$PATH}).
510
511For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000512\file{/usr/local/bin/python}, it will assume that the libraries are in
513\file{/usr/local/lib/python\version}. (In fact, this particular path
514is also the ``fallback'' location, used when no executable file named
515\file{python} is found along \code{\$PATH}.) The user can override
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000516this behavior by setting the environment variable \code{\$PYTHONHOME},
517or insert additional directories in front of the standard path by
518setting \code{\$PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000519
Guido van Rossum4a944d71997-08-14 20:35:38 +0000520The embedding application can steer the search by calling
521\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakee058b4f1998-02-16 06:15:35 +0000522\cfunction{Py_Initialize()}. Note that \code{\$PYTHONHOME} still
523overrides this and \code{\$PYTHONPATH} is still inserted in front of
524the standard path. An application that requires total control has to
525provide its own implementation of \cfunction{Py_GetPath()},
526\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
527\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000528\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000529
Guido van Rossum4a944d71997-08-14 20:35:38 +0000530Sometimes, it is desirable to ``uninitialize'' Python. For instance,
531the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000532\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000533use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000534can be accomplished by calling \cfunction{Py_Finalize()}. The function
535\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000536initialized state. More information about these functions is given in
537a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000538
Guido van Rossum4a944d71997-08-14 20:35:38 +0000539
Fred Drakee5bf8b21998-02-12 21:22:28 +0000540\chapter{The Very High Level Layer}
Fred Drakef39ed671998-02-26 22:01:23 +0000541\label{veryhigh}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000542
Fred Drakee5bf8b21998-02-12 21:22:28 +0000543The functions in this chapter will let you execute Python source code
544given in a file or a buffer, but they will not let you interact in a
545more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000546
Fred Drakee5bf8b21998-02-12 21:22:28 +0000547\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000548\end{cfuncdesc}
549
Fred Drakee5bf8b21998-02-12 21:22:28 +0000550\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000551\end{cfuncdesc}
552
Fred Drakee5bf8b21998-02-12 21:22:28 +0000553\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *}
554\end{cfuncdesc}
555
556\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *}
557\end{cfuncdesc}
558
559\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *}
560\end{cfuncdesc}
561
562\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int}
563\end{cfuncdesc}
564
565\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int}
566\end{cfuncdesc}
567
568\begin{cfuncdesc}{PyObject *}{PyRun_String}{char *, int, PyObject *, PyObject *}
569\end{cfuncdesc}
570
571\begin{cfuncdesc}{PyObject *}{PyRun_File}{FILE *, char *, int, PyObject *, PyObject *}
572\end{cfuncdesc}
573
574\begin{cfuncdesc}{PyObject *}{Py_CompileString}{char *, char *, int}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000575\end{cfuncdesc}
576
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000577
578\chapter{Reference Counting}
Fred Drakef39ed671998-02-26 22:01:23 +0000579\label{countingRefs}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000580
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000581The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000582of Python objects.
583
584\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
585Increment the reference count for object \code{o}. The object must
586not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000587\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000588\end{cfuncdesc}
589
590\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000591Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000592\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000593\end{cfuncdesc}
594
595\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000596Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000597not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000598\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
599object's type's deallocation function (which must not be \NULL{}) is
600invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000601
602\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000603code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000604method is deallocated). While exceptions in such code are not
605propagated, the executed code has free access to all Python global
606variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000607variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000608invoked. For example, code to delete an object from a list should
609copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000610the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000611temporary variable.
612\end{cfuncdesc}
613
614\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000615Decrement the reference count for object \var{o}. The object may be
616\NULL{}, in which case the macro has no effect; otherwise the effect
617is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000618applies.
619\end{cfuncdesc}
620
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000621The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000622\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
623\cfunction{_Py_NewReference()}, as well as the global variable
624\code{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000625
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000626XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
627PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
628PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
629
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000630
631\chapter{Exception Handling}
Fred Drakef39ed671998-02-26 22:01:23 +0000632\label{exceptionHandling}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000633
634The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000635exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000636Python exception handling. It works somewhat like the \UNIX{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000637\code{errno} variable: there is a global indicator (per thread) of the
638last error that occurred. Most functions don't clear this on success,
639but will set it to indicate the cause of the error on failure. Most
640functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000641supposed to return a pointer, or \code{-1} if they return an integer
642(exception: the \code{PyArg_Parse*()} functions return \code{1} for
643success and \code{0} for failure). When a function must fail because
644some function it called failed, it generally doesn't set the error
645indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000646
647The error indicator consists of three Python objects corresponding to
648the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
649\code{sys.exc_traceback}. API functions exist to interact with the
650error indicator in various ways. There is a separate error indicator
651for each thread.
652
653% XXX Order of these should be more thoughtful.
654% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000655
656\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000657Print a standard traceback to \code{sys.stderr} and clear the error
658indicator. Call this function only when the error indicator is set.
659(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000660\end{cfuncdesc}
661
Guido van Rossumae110af1997-05-22 20:11:52 +0000662\begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{}
663Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000664\emph{type} (the first argument to the last call to one of the
665\code{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
666not set, return \NULL{}. You do not own a reference to the return
667value, so you do not need to \cfunction{Py_DECREF()} it.
668\strong{Note:} do not compare the return value to a specific
669exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
670below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000671\end{cfuncdesc}
672
673\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000674Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000675\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000676This should only be called when an exception is actually set.
677\end{cfuncdesc}
678
679\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000680Return true if the \var{given} exception matches the exception in
681\var{exc}. If \var{exc} is a class object, this also returns true
682when \var{given} is a subclass. If \var{exc} is a tuple, all
683exceptions in the tuple (and recursively in subtuples) are searched
684for a match. This should only be called when an exception is actually
685set.
686\end{cfuncdesc}
687
688\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000689Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000690\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
691\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
692instance of the same class. This function can be used to instantiate
693the class in that case. If the values are already normalized, nothing
694happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000695\end{cfuncdesc}
696
697\begin{cfuncdesc}{void}{PyErr_Clear}{}
698Clear the error indicator. If the error indicator is not set, there
699is no effect.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
703Retrieve the error indicator into three variables whose addresses are
704passed. If the error indicator is not set, set all three variables to
705\NULL{}. If it is set, it will be cleared and you own a reference to
706each object retrieved. The value and traceback object may be \NULL{}
707even when the type object is not. \strong{Note:} this function is
708normally only used by code that needs to handle exceptions or by code
709that needs to save and restore the error indicator temporarily.
710\end{cfuncdesc}
711
712\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
713Set the error indicator from the three objects. If the error
714indicator is already set, it is cleared first. If the objects are
715\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
716and non-\NULL{} value or traceback. The exception type should be a
717string or class; if it is a class, the value should be an instance of
718that class. Do not pass an invalid exception type or value.
719(Violating these rules will cause subtle problems later.) This call
720takes away a reference to each object, i.e. you must own a reference
721to each object before the call and after the call you no longer own
722these references. (If you don't understand this, don't use this
723function. I warned you.) \strong{Note:} this function is normally
724only used by code that needs to save and restore the error indicator
725temporarily.
726\end{cfuncdesc}
727
728\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
729This is the most common way to set the error indicator. The first
730argument specifies the exception type; it is normally one of the
731standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
732increment its reference count. The second argument is an error
733message; it is converted to a string object.
734\end{cfuncdesc}
735
736\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000737This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000738specify an arbitrary Python object for the ``value'' of the exception.
739You need not increment its reference count.
740\end{cfuncdesc}
741
742\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000743This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000744\end{cfuncdesc}
745
746\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000747This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000748\var{message})}, where \var{message} indicates that a built-in operation
749was invoked with an illegal argument. It is mostly for internal use.
750\end{cfuncdesc}
751
752\begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000753This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000754returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000755\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000756\end{cfuncdesc}
757
758\begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000759This is a convenience function to raise an exception when a \C{} library
760function has returned an error and set the \C{} variable \code{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000761It constructs a tuple object whose first item is the integer
762\code{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000763message (gotten from \cfunction{strerror()}), and then calls
764\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
765the \code{errno} value is \constant{EINTR}, indicating an interrupted
766system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000767the error indicator, leaves it set to that. The function always
768returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000769\samp{return PyErr_SetFromErrno();} when the system call returns an
770error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000771\end{cfuncdesc}
772
773\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000774This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000775\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000776operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000777argument. It is mostly for internal use.
778\end{cfuncdesc}
779
780\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
781This function interacts with Python's signal handling. It checks
782whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000783corresponding signal handler. If the
784\module{signal}\refbimodindex{signal} module is supported, this can
785invoke a signal handler written in Python. In all cases, the default
786effect for \constant{SIGINT} is to raise the
787\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000788error indicator is set and the function returns \code{1}; otherwise
789the function returns \code{0}. The error indicator may or may not be
790cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000791\end{cfuncdesc}
792
793\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
794This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000795the effect of a \constant{SIGINT} signal arriving --- the next time
796\cfunction{PyErr_CheckSignals()} is called,
797\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000798\end{cfuncdesc}
799
Guido van Rossum42cefd01997-10-05 15:27:29 +0000800\begin{cfuncdesc}{PyObject *}{PyErr_NewException}{char *name,
801PyObject *base, PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000802This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000803\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000804of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000805arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000806object derived from the root for all exceptions, the built-in name
Fred Drakee058b4f1998-02-16 06:15:35 +0000807\exception{Exception} (accessible in \C{} as \code{PyExc_Exception}).
808In this case the \code{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000809first part (up to the last dot) of the \var{name} argument, and the
810class name is set to the last part (after the last dot). When the
811user has specified the \code{-X} command line option to use string
812exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000813argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000814created from the entire \var{name} argument is returned. The
815\var{base} argument can be used to specify an alternate base class.
816The \var{dict} argument can be used to specify a dictionary of class
817variables and methods.
818\end{cfuncdesc}
819
820
Guido van Rossumae110af1997-05-22 20:11:52 +0000821\section{Standard Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000822\label{standardExceptions}
Guido van Rossumae110af1997-05-22 20:11:52 +0000823
824All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000825names are \samp{PyExc_} followed by the Python exception name.
826These have the type \code{PyObject *}; they are all either class
827objects or string objects, depending on the use of the \code{-X}
828option to the interpreter. For completeness, here are all the
Fred Drake9d20ac31998-02-16 15:27:08 +0000829variables:
Guido van Rossum42cefd01997-10-05 15:27:29 +0000830\code{PyExc_Exception},
831\code{PyExc_StandardError},
832\code{PyExc_ArithmeticError},
833\code{PyExc_LookupError},
Guido van Rossumae110af1997-05-22 20:11:52 +0000834\code{PyExc_AssertionError},
835\code{PyExc_AttributeError},
836\code{PyExc_EOFError},
837\code{PyExc_FloatingPointError},
838\code{PyExc_IOError},
839\code{PyExc_ImportError},
840\code{PyExc_IndexError},
841\code{PyExc_KeyError},
842\code{PyExc_KeyboardInterrupt},
843\code{PyExc_MemoryError},
844\code{PyExc_NameError},
845\code{PyExc_OverflowError},
846\code{PyExc_RuntimeError},
847\code{PyExc_SyntaxError},
848\code{PyExc_SystemError},
849\code{PyExc_SystemExit},
850\code{PyExc_TypeError},
851\code{PyExc_ValueError},
852\code{PyExc_ZeroDivisionError}.
853
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000854
855\chapter{Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000856\label{utilities}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000857
858The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000859parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000860values.
861
Guido van Rossum42cefd01997-10-05 15:27:29 +0000862\section{OS Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000863\label{os}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000864
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000865\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000866Return true (nonzero) if the standard I/O file \var{fp} with name
867\var{filename} is deemed interactive. This is the case for files for
868which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000869\code{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000870the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000871the strings \code{"<stdin>"} or \code{"???"}.
872\end{cfuncdesc}
873
874\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000875Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000876The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000877the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000878\end{cfuncdesc}
879
880
Fred Drakee5bf8b21998-02-12 21:22:28 +0000881\section{Process Control}
Fred Drakef39ed671998-02-26 22:01:23 +0000882\label{processControl}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000883
884\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
885Print a fatal error message and kill the process. No cleanup is
886performed. This function should only be invoked when a condition is
887detected that would make it dangerous to continue using the Python
888interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000889corrupted. On \UNIX{}, the standard \C{} library function
890\cfunction{abort()} is called which will attempt to produce a
891\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000892\end{cfuncdesc}
893
894\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000895Exit the current process. This calls \cfunction{Py_Finalize()} and
896then calls the standard \C{} library function
897\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000898\end{cfuncdesc}
899
900\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
901Register a cleanup function to be called by \cfunction{Py_Finalize()}.
902The cleanup function will be called with no arguments and should
903return no value. At most 32 cleanup functions can be registered.
904When the registration is successful, \cfunction{Py_AtExit()} returns
905\code{0}; on failure, it returns \code{-1}. The cleanup function
906registered last is called first. Each cleanup function will be called
907at most once. Since Python's internal finallization will have
908completed before the cleanup function, no Python APIs should be called
909by \var{func}.
910\end{cfuncdesc}
911
912
913\section{Importing Modules}
Fred Drakef39ed671998-02-26 22:01:23 +0000914\label{importing}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000915
916\begin{cfuncdesc}{PyObject *}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000917This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000918below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000919\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000920it specifies a submodule of a package), the \var{fromlist} argument is
921set to the list \code{['*']} so that the return value is the named
922module rather than the top-level package containing it as would
923otherwise be the case. (Unfortunately, this has an additional side
924effect when \var{name} in fact specifies a subpackage instead of a
925submodule: the submodules specified in the package's \code{__all__}
926variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000927or \NULL{} with an exception set on failure (the module may still
Guido van Rossum42cefd01997-10-05 15:27:29 +0000928be created in this case).
929\end{cfuncdesc}
930
931\begin{cfuncdesc}{PyObject *}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000932Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000933Python function \function{__import__()}\bifuncindex{__import__}, as
934the standard \function{__import__()} function calls this function
935directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000936
Guido van Rossum42cefd01997-10-05 15:27:29 +0000937The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000938top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000939(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000940\function{__import__()}, the return value when a submodule of a
941package was requested is normally the top-level package, unless a
942non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000943\end{cfuncdesc}
944
945\begin{cfuncdesc}{PyObject *}{PyImport_Import}{PyObject *name}
946This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000947function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000948\code{__builtins__} of the current globals. This means that the
949import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000950current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
951\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000952\end{cfuncdesc}
953
954\begin{cfuncdesc}{PyObject *}{PyImport_ReloadModule}{PyObject *m}
955Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000956Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000957\function{reload()} function calls this function directly. Return a
958new reference to the reloaded module, or \NULL{} with an exception set
959on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000960\end{cfuncdesc}
961
962\begin{cfuncdesc}{PyObject *}{PyImport_AddModule}{char *name}
963Return the module object corresponding to a module name. The
964\var{name} argument may be of the form \code{package.module}). First
965check the modules dictionary if there's one there, and if not, create
966a new one and insert in in the modules dictionary. Because the former
967action is most common, this does not return a new reference, and you
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000968do not own the returned reference. Return \NULL{} with an
Guido van Rossum42cefd01997-10-05 15:27:29 +0000969exception set on failure.
970\end{cfuncdesc}
971
972\begin{cfuncdesc}{PyObject *}{PyImport_ExecCodeModule}{char *name, PyObject *co}
973Given a module name (possibly of the form \code{package.module}) and a
974code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000975built-in function \function{compile()}\bifuncindex{compile}, load the
976module. Return a new reference to the module object, or \NULL{} with
977an exception set if an error occurred (the module may still be created
978in this case). (This function would reload the module if it was
979already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000980\end{cfuncdesc}
981
982\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000983Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
984and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000985first four bytes of the bytecode file, in little-endian byte order.
986\end{cfuncdesc}
987
988\begin{cfuncdesc}{PyObject *}{PyImport_GetModuleDict}{}
989Return the dictionary used for the module administration
990(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
991variable.
992\end{cfuncdesc}
993
994\begin{cfuncdesc}{void}{_PyImport_Init}{}
995Initialize the import mechanism. For internal use only.
996\end{cfuncdesc}
997
998\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
999Empty the module table. For internal use only.
1000\end{cfuncdesc}
1001
1002\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1003Finalize the import mechanism. For internal use only.
1004\end{cfuncdesc}
1005
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001006\begin{cfuncdesc}{extern PyObject *}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001007For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001008\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001009
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001010\begin{cfuncdesc}{extern PyObject *}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001011For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001012\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001013
1014\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1015Load a frozen module. Return \code{1} for success, \code{0} if the
1016module is not found, and \code{-1} with an exception set if the
1017initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001018load, use \cfunction{PyImport_ImportModule()}.
1019(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001020already imported.)
1021\end{cfuncdesc}
1022
1023\begin{ctypedesc}{struct _frozen}
1024This is the structure type definition for frozen module descriptors,
1025as generated by the \code{freeze} utility (see \file{Tools/freeze/} in
1026the Python source distribution). Its definition is:
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001027\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001028struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001029 char *name;
1030 unsigned char *code;
1031 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001032};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001033\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001034\end{ctypedesc}
1035
1036\begin{cvardesc}{struct _frozen *}{PyImport_FrozenModules}
1037This pointer is initialized to point to an array of \code{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001038_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001039or zero. When a frozen module is imported, it is searched in this
1040table. Third party code could play tricks with this to provide a
1041dynamically created collection of frozen modules.
1042\end{cvardesc}
1043
1044
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001045\chapter{Abstract Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001046\label{abstract}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001047
1048The functions in this chapter interact with Python objects regardless
1049of their type, or with wide classes of object types (e.g. all
1050numerical types, or all sequence types). When used on object types
1051for which they do not apply, they will flag a Python exception.
1052
1053\section{Object Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001054\label{object}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001055
1056\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001057Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001058The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001059options. The only option currently supported is
1060\constant{Py_Print_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001061\end{cfuncdesc}
1062
1063\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001064Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1065\code{0} otherwise. This is equivalent to the Python expression
1066\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001067This function always succeeds.
1068\end{cfuncdesc}
1069
1070\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001071Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001072Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001073This is the equivalent of the Python expression
1074\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001075\end{cfuncdesc}
1076
1077
1078\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001079Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1080\code{0} otherwise. This is equivalent to the Python expression
1081\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001082This function always succeeds.
1083\end{cfuncdesc}
1084
1085
1086\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *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_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001095Set the value of the attribute named \var{attr_name}, for object
1096\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1097the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1098\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001099\end{cfuncdesc}
1100
1101
1102\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001103Set the value of the attribute named \var{attr_name}, for
1104object \var{o},
1105to the value \var{v}. Returns \code{-1} on failure. This is
1106the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1107\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001108\end{cfuncdesc}
1109
1110
1111\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001112Delete attribute named \var{attr_name}, for object \var{o}. Returns
1113\code{-1} on failure. This is the equivalent of the Python
1114statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001115\end{cfuncdesc}
1116
1117
1118\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001119Delete attribute named \var{attr_name}, for object \var{o}. Returns
1120\code{-1} on failure. This is the equivalent of the Python
1121statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001122\end{cfuncdesc}
1123
1124
1125\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001126Compare the values of \var{o1} and \var{o2} using a routine provided
1127by \var{o1}, if one exists, otherwise with a routine provided by
1128\var{o2}. The result of the comparison is returned in \var{result}.
1129Returns \code{-1} on failure. This is the equivalent of the Python
1130statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001131\end{cfuncdesc}
1132
1133
1134\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001135Compare the values of \var{o1} and \var{o2} using a routine provided
1136by \var{o1}, if one exists, otherwise with a routine provided by
1137\var{o2}. Returns the result of the comparison on success. On error,
1138the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1139detect an error. This is equivalent to the
1140Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001141\end{cfuncdesc}
1142
1143
1144\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001145Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001146string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001147the equivalent of the Python expression \samp{repr(\var{o})}.
1148Called by the \function{repr()}\bifuncindex{repr} built-in function
1149and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001150\end{cfuncdesc}
1151
1152
1153\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001154Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001155string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001156the equivalent of the Python expression \samp{str(\var{o})}.
1157Called by the \function{str()}\bifuncindex{str} built-in function and
1158by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001159\end{cfuncdesc}
1160
1161
1162\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001163Determine if the object \var{o}, is callable. Return \code{1} if the
1164object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001165This function always succeeds.
1166\end{cfuncdesc}
1167
1168
1169\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001170Call a callable Python object \var{callable_object}, with
1171arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001172needed, then args may be \NULL{}. Returns the result of the
1173call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001174of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001175\end{cfuncdesc}
1176
1177\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001178Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001179variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001180using a \cfunction{Py_BuildValue()} style format string. The format may
1181be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001182result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001183the equivalent of the Python expression \samp{apply(\var{o},
1184\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001185\end{cfuncdesc}
1186
1187
1188\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001189Call the method named \var{m} of object \var{o} with a variable number
1190of C arguments. The \C{} arguments are described by a
1191\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1192indicating that no arguments are provided. Returns the result of the
1193call on success, or \NULL{} on failure. This is the equivalent of the
1194Python expression \samp{\var{o}.\var{method}(\var{args})}.
1195Note that Special method names, such as \method{__add__()},
1196\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001197abstract-object routines for these must be used.
1198\end{cfuncdesc}
1199
1200
1201\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001202Compute and return the hash value of an object \var{o}. On
1203failure, return \code{-1}. This is the equivalent of the Python
1204expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001205\end{cfuncdesc}
1206
1207
1208\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001209Returns \code{1} if the object \var{o} is considered to be true, and
1210\code{0} otherwise. This is equivalent to the Python expression
1211\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001212This function always succeeds.
1213\end{cfuncdesc}
1214
1215
1216\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1217On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001218type of object \var{o}. On failure, returns \NULL{}. This is
1219equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001220\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001221\end{cfuncdesc}
1222
1223\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001224Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001225both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001226returned. On error, \code{-1} is returned. This is the equivalent
1227to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001228\end{cfuncdesc}
1229
1230
1231\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001232Return element of \var{o} corresponding to the object \var{key} or
1233\NULL{} on failure. This is the equivalent of the Python expression
1234\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001235\end{cfuncdesc}
1236
1237
1238\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001239Map the object \var{key} to the value \var{v}.
1240Returns \code{-1} on failure. This is the equivalent
1241of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001242\end{cfuncdesc}
1243
1244
1245\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001246Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1247failure. This is the equivalent of the Python statement \samp{del
1248\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001249\end{cfuncdesc}
1250
1251
1252\section{Number Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001253\label{number}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001254
1255\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001256Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001257false otherwise.
1258This function always succeeds.
1259\end{cfuncdesc}
1260
1261
1262\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001263Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1264failure. This is the equivalent of the Python expression
1265\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001266\end{cfuncdesc}
1267
1268
1269\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001270Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1271on failure. This is the equivalent of the Python expression
1272\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001273\end{cfuncdesc}
1274
1275
1276\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001277Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1278failure. This is the equivalent of the Python expression
1279\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001280\end{cfuncdesc}
1281
1282
1283\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001284Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1285failure.
1286This is the equivalent of the Python expression \samp{\var{o1} /
1287\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001288\end{cfuncdesc}
1289
1290
1291\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001292Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1293failure. This is the equivalent of the Python expression
1294\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001295\end{cfuncdesc}
1296
1297
1298\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001299See the built-in function \function{divmod()}\bifuncindex{divmod}.
1300Returns \NULL{} on failure. This is the equivalent of the Python
1301expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001302\end{cfuncdesc}
1303
1304
1305\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001306See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1307\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001308\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
1309If \var{o3} is to be ignored, pass \code{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001310\end{cfuncdesc}
1311
1312
1313\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001314Returns the negation of \var{o} on success, or \NULL{} on failure.
1315This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001316\end{cfuncdesc}
1317
1318
1319\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001320Returns \var{o} on success, or \NULL{} on failure.
1321This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001322\end{cfuncdesc}
1323
1324
1325\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001326Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1327the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001328\end{cfuncdesc}
1329
1330
1331\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001332Returns the bitwise negation of \var{o} on success, or \NULL{} on
1333failure. This is the equivalent of the Python expression
1334\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001335\end{cfuncdesc}
1336
1337
1338\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001339Returns the result of left shifting \var{o1} by \var{o2} on success,
1340or \NULL{} on failure. This is the equivalent of the Python
1341expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001342\end{cfuncdesc}
1343
1344
1345\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001346Returns the result of right shifting \var{o1} by \var{o2} on success,
1347or \NULL{} on failure. This is the equivalent of the Python
1348expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001349\end{cfuncdesc}
1350
1351
1352\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001353Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1354\NULL{} on failure. This is the equivalent of the Python
1355expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001356\end{cfuncdesc}
1357
1358
1359\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001360Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1361or \NULL{} on failure. This is the equivalent of the Python
1362expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001363\end{cfuncdesc}
1364
1365\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001366Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1367failure. This is the equivalent of the Python expression
1368\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001369\end{cfuncdesc}
1370
1371
Fred Drakee058b4f1998-02-16 06:15:35 +00001372\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001373This function takes the addresses of two variables of type
1374\code{PyObject*}.
1375
Fred Drakee058b4f1998-02-16 06:15:35 +00001376If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1377have the same type, increment their reference count and return
1378\code{0} (success). If the objects can be converted to a common
1379numeric type, replace \code{*p1} and \code{*p2} by their converted
1380value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001381If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001382return \code{-1} (failure) and don't increment the reference counts.
1383The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1384Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1385\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001386\end{cfuncdesc}
1387
1388
1389\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001390Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001391\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001392expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001393\end{cfuncdesc}
1394
1395
1396\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001397Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001398or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001399expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001400\end{cfuncdesc}
1401
1402
1403\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001404Returns the \var{o} converted to a float object on success, or \NULL{}
1405on failure. This is the equivalent of the Python expression
1406\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001407\end{cfuncdesc}
1408
1409
Fred Drakef44617d1998-02-12 20:57:15 +00001410\section{Sequence Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001411\label{sequence}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001412
1413\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001414Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001415otherwise.
1416This function always succeeds.
1417\end{cfuncdesc}
1418
1419
1420\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001421Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001422failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001423expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001424\end{cfuncdesc}
1425
1426
1427\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001428Return the result of repeating sequence object \var{o} \var{count}
1429times, or \NULL{} on failure. This is the equivalent of the Python
1430expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001431\end{cfuncdesc}
1432
1433
1434\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001435Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1436is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001437\end{cfuncdesc}
1438
1439
1440\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001441Return the slice of sequence object \var{o} between \var{i1} and
1442\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1443expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001444\end{cfuncdesc}
1445
1446
1447\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001448Assign object \var{v} to the \var{i}th element of \var{o}.
1449Returns \code{-1} on failure. This is the equivalent of the Python
1450statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001451\end{cfuncdesc}
1452
1453\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001454Delete the \var{i}th element of object \var{v}. Returns
1455\code{-1} on failure. This is the equivalent of the Python
1456statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001457\end{cfuncdesc}
1458
1459\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001460Assign the sequence object \var{v} to the slice in sequence
1461object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1462the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001463\end{cfuncdesc}
1464
1465\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001466Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1467Returns \code{-1} on failure. This is the equivalent of the Python
1468statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001469\end{cfuncdesc}
1470
1471\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001472Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1473This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001474\end{cfuncdesc}
1475
1476\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001477Return the number of occurrences of \var{value} in \var{o}, that is,
1478return the number of keys for which \code{\var{o}[\var{key}] ==
1479\var{value}}. On failure, return \code{-1}. This is equivalent to
1480the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001481\end{cfuncdesc}
1482
1483\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001484Determine if \var{o} contains \var{value}. If an item in \var{o} is
1485equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1486error, return \code{-1}. This is equivalent to the Python expression
1487\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001488\end{cfuncdesc}
1489
1490\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001491Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1492\var{value}}. On error, return \code{-1}. This is equivalent to
1493the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001494\end{cfuncdesc}
1495
Fred Drakef39ed671998-02-26 22:01:23 +00001496
Fred Drakef44617d1998-02-12 20:57:15 +00001497\section{Mapping Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001498\label{mapping}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001499
1500\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001501Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001502otherwise.
1503This function always succeeds.
1504\end{cfuncdesc}
1505
1506
1507\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001508Returns the number of keys in object \var{o} on success, and \code{-1}
1509on failure. For objects that do not provide sequence protocol,
1510this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001511\end{cfuncdesc}
1512
1513
1514\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001515Remove the mapping for object \var{key} from the object \var{o}.
1516Return \code{-1} on failure. This is equivalent to
1517the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001518\end{cfuncdesc}
1519
1520
1521\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001522Remove the mapping for object \var{key} from the object \var{o}.
1523Return \code{-1} on failure. This is equivalent to
1524the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001525\end{cfuncdesc}
1526
1527
1528\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001529On success, return \code{1} if the mapping object has the key \var{key}
1530and \code{0} otherwise. This is equivalent to the Python expression
1531\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001532This function always succeeds.
1533\end{cfuncdesc}
1534
1535
1536\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001537Return \code{1} if the mapping object has the key \var{key} and
1538\code{0} otherwise. This is equivalent to the Python expression
1539\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001540This function always succeeds.
1541\end{cfuncdesc}
1542
1543
1544\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001545On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001546failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001547expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001548\end{cfuncdesc}
1549
1550
1551\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001552On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001553failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001554expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001555\end{cfuncdesc}
1556
1557
1558\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001559On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001560each item is a tuple containing a key-value pair. On
1561failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001562expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001563\end{cfuncdesc}
1564
1565\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001566Make object \var{o} empty. Returns \code{1} on success and \code{0}
1567on failure. This is equivalent to the Python statement
1568\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001569\end{cfuncdesc}
1570
1571
1572\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001573Return element of \var{o} corresponding to the object \var{key} or
1574\NULL{} on failure. This is the equivalent of the Python expression
1575\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001576\end{cfuncdesc}
1577
1578\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001579Map the object \var{key} to the value \var{v} in object \var{o}.
1580Returns \code{-1} on failure. This is the equivalent of the Python
1581statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001582\end{cfuncdesc}
1583
1584
1585\section{Constructors}
1586
1587\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1588On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001589file given by \var{file_name}, with a file mode given by \var{mode},
1590where \var{mode} has the same semantics as the standard \C{} routine
1591\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001592\end{cfuncdesc}
1593
1594\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001595Return a new file object for an already opened standard \C{} file
1596pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1597\var{mode}, must be provided as well as a flag, \var{close_on_del},
1598that indicates whether the file is to be closed when the file object
1599is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001600\end{cfuncdesc}
1601
1602\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001603Returns a new float object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001604\NULL{} on failure.
1605\end{cfuncdesc}
1606
1607\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001608Returns a new int object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001609\NULL{} on failure.
1610\end{cfuncdesc}
1611
Fred Drakee058b4f1998-02-16 06:15:35 +00001612\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1613Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001614failure.
1615\end{cfuncdesc}
1616
1617\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001618Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001619\NULL{} on failure.
1620\end{cfuncdesc}
1621
1622\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001623Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001624\NULL{} on failure.
1625\end{cfuncdesc}
1626
1627\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1628Returns a new empty dictionary on success, and \NULL{} on
1629failure.
1630\end{cfuncdesc}
1631
1632\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001633Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001634\NULL{} on failure.
1635\end{cfuncdesc}
1636
Fred Drakee058b4f1998-02-16 06:15:35 +00001637\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1638Returns a new string object with the value \var{v} and length
1639\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1640the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001641\end{cfuncdesc}
1642
Fred Drakee058b4f1998-02-16 06:15:35 +00001643\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1644Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001645failure.
1646\end{cfuncdesc}
1647
1648
1649\chapter{Concrete Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001650\label{concrete}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001651
1652The functions in this chapter are specific to certain Python object
1653types. Passing them an object of the wrong type is not a good idea;
1654if you receive an object from a Python program and you are not sure
1655that it has the right type, you must perform a type check first;
1656e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001657\cfunction{PyDict_Check()}. The chapter is structured like the
1658``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001659
1660
Fred Drakee5bf8b21998-02-12 21:22:28 +00001661\section{Fundamental Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001662\label{fundamental}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001663
Fred Drakee5bf8b21998-02-12 21:22:28 +00001664This section describes Python type objects and the singleton object
1665\code{None}.
1666
1667
1668\subsection{Type Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001669\label{typeObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001670
1671\begin{ctypedesc}{PyTypeObject}
1672
1673\end{ctypedesc}
1674
1675\begin{cvardesc}{PyObject *}{PyType_Type}
1676
1677\end{cvardesc}
1678
1679
1680\subsection{The None Object}
Fred Drakef39ed671998-02-26 22:01:23 +00001681\label{noneObject}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001682
1683\begin{cvardesc}{PyObject *}{Py_None}
1684XXX macro
1685\end{cvardesc}
1686
1687
1688\section{Sequence Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001689\label{sequenceObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001690
1691Generic operations on sequence objects were discussed in the previous
1692chapter; this section deals with the specific kinds of sequence
1693objects that are intrinsic to the Python language.
1694
1695
1696\subsection{String Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001697\label{stringObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001698
1699\begin{ctypedesc}{PyStringObject}
1700This subtype of \code{PyObject} represents a Python string object.
1701\end{ctypedesc}
1702
1703\begin{cvardesc}{PyTypeObject}{PyString_Type}
1704This instance of \code{PyTypeObject} represents the Python string type.
1705\end{cvardesc}
1706
1707\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
1708
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001709\end{cfuncdesc}
1710
Fred Drakee5bf8b21998-02-12 21:22:28 +00001711\begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int}
1712
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001713\end{cfuncdesc}
1714
Fred Drakee5bf8b21998-02-12 21:22:28 +00001715\begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *}
1716
Guido van Rossumae110af1997-05-22 20:11:52 +00001717\end{cfuncdesc}
1718
Fred Drakee5bf8b21998-02-12 21:22:28 +00001719\begin{cfuncdesc}{int}{PyString_Size}{PyObject *}
1720
Guido van Rossumae110af1997-05-22 20:11:52 +00001721\end{cfuncdesc}
1722
Fred Drakee5bf8b21998-02-12 21:22:28 +00001723\begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *}
1724
1725\end{cfuncdesc}
1726
1727\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *}
1728
1729\end{cfuncdesc}
1730
1731\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *}
1732
1733\end{cfuncdesc}
1734
1735\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int}
1736
1737\end{cfuncdesc}
1738
1739\begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *}
1740
1741\end{cfuncdesc}
1742
1743\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **}
1744
1745\end{cfuncdesc}
1746
1747\begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *}
1748
1749\end{cfuncdesc}
1750
1751\begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *}
1752
1753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *}
1756
1757\end{cfuncdesc}
1758
1759
1760\subsection{Tuple Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001761\label{tupleObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001762
1763\begin{ctypedesc}{PyTupleObject}
1764This subtype of \code{PyObject} represents a Python tuple object.
1765\end{ctypedesc}
1766
1767\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1768This instance of \code{PyTypeObject} represents the Python tuple type.
1769\end{cvardesc}
1770
1771\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1772Return true if the argument is a tuple object.
1773\end{cfuncdesc}
1774
1775\begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001776Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001777\end{cfuncdesc}
1778
1779\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001780Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001781of that tuple.
1782\end{cfuncdesc}
1783
1784\begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001785Returns the object at position \var{pos} in the tuple pointed
1786to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
1787raises an \exception{IndexError} exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001788\end{cfuncdesc}
1789
1790\begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001791Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001792\end{cfuncdesc}
1793
1794\begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p,
1795 int low,
1796 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001797Takes a slice of the tuple pointed to by \var{p} from
1798\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001799\end{cfuncdesc}
1800
1801\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1802 int pos,
1803 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001804Inserts a reference to object \var{o} at position \var{pos} of
1805the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001806\end{cfuncdesc}
1807
1808\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1809 int pos,
1810 PyObject *o}
1811
Fred Drakee058b4f1998-02-16 06:15:35 +00001812Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001813should \emph{only} be used to fill in brand new tuples.
1814\end{cfuncdesc}
1815
1816\begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p,
1817 int new,
1818 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001819Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001820\emph{supposed} to be immutable, this should only be used if there is only
1821one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001822already be known to some other part of the code. \var{last_is_sticky} is
1823a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001824it will grow or shrink at the end. Think of this as destroying the old
1825tuple and creating a new one, only more efficiently.
1826\end{cfuncdesc}
1827
1828
1829\subsection{List Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001830\label{listObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001831
1832\begin{ctypedesc}{PyListObject}
1833This subtype of \code{PyObject} represents a Python list object.
1834\end{ctypedesc}
1835
1836\begin{cvardesc}{PyTypeObject}{PyList_Type}
1837This instance of \code{PyTypeObject} represents the Python list type.
1838\end{cvardesc}
1839
1840\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001841Returns true if its argument is a \code{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001842\end{cfuncdesc}
1843
1844\begin{cfuncdesc}{PyObject *}{PyList_New}{int size}
1845
1846\end{cfuncdesc}
1847
1848\begin{cfuncdesc}{int}{PyList_Size}{PyObject *}
1849
1850\end{cfuncdesc}
1851
1852\begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int}
1853
1854\end{cfuncdesc}
1855
1856\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *}
1857
1858\end{cfuncdesc}
1859
1860\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *}
1861
1862\end{cfuncdesc}
1863
1864\begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *}
1865
1866\end{cfuncdesc}
1867
1868\begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int}
1869
1870\end{cfuncdesc}
1871
1872\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *}
1873
1874\end{cfuncdesc}
1875
1876\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *}
1877
1878\end{cfuncdesc}
1879
1880\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *}
1881
1882\end{cfuncdesc}
1883
1884\begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *}
1885
1886\end{cfuncdesc}
1887
1888\begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i}
1889
1890\end{cfuncdesc}
1891
1892\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1893
1894\end{cfuncdesc}
1895
1896
1897\section{Mapping Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001898\label{mapObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001899
1900\subsection{Dictionary Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001901\label{dictObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001902
1903\begin{ctypedesc}{PyDictObject}
1904This subtype of \code{PyObject} represents a Python dictionary object.
1905\end{ctypedesc}
1906
1907\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1908This instance of \code{PyTypeObject} represents the Python dictionary type.
1909\end{cvardesc}
1910
1911\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001912Returns true if its argument is a \code{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001913\end{cfuncdesc}
1914
1915\begin{cfuncdesc}{PyDictObject *}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001916Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001917\end{cfuncdesc}
1918
1919\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001920Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001921\end{cfuncdesc}
1922
1923\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1924 PyObject *key,
1925 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001926Inserts \var{value} into the dictionary with a key of \var{key}. Both
1927\var{key} and \var{value} should be PyObjects, and \var{key} should be
1928hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001929\end{cfuncdesc}
1930
1931\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1932 char *key,
1933 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001934Inserts \var{value} into the dictionary using \var{key}
1935as a key. \var{key} should be a \code{char *}. The key object is
1936created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001937\end{cfuncdesc}
1938
1939\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001940Removes the entry in dictionary \var{p} with key \var{key}.
1941\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001942\end{cfuncdesc}
1943
1944\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001945Removes the entry in dictionary \var{p} which has a key
1946specified by the \code{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001947\end{cfuncdesc}
1948
1949\begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001950Returns the object from dictionary \var{p} which has a key
1951\var{key}. Returns \NULL{} if the key \var{key} is not present.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001952\end{cfuncdesc}
1953
1954\begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001955Does the same, but \var{key} is specified as a
Fred Drakee5bf8b21998-02-12 21:22:28 +00001956\code{char *}, rather than a \code{PyObject *}.
1957\end{cfuncdesc}
1958
1959\begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001960Returns a \code{PyListObject} containing all the items
1961from the dictionary, as in the mapping method \method{items()} (see
1962the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001963\end{cfuncdesc}
1964
1965\begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001966Returns a \code{PyListObject} containing all the keys
1967from the dictionary, as in the mapping method \method{keys()} (see the
1968\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001969\end{cfuncdesc}
1970
1971\begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001972Returns a \code{PyListObject} containing all the values
1973from the dictionary \var{p}, as in the mapping method
1974\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001975\end{cfuncdesc}
1976
1977\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001978Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001979\end{cfuncdesc}
1980
1981\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
1982 int ppos,
1983 PyObject **pkey,
1984 PyObject **pvalue}
1985
1986\end{cfuncdesc}
1987
1988
1989\section{Numeric Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001990\label{numericObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001991
1992\subsection{Plain Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001993\label{intObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001994
1995\begin{ctypedesc}{PyIntObject}
1996This subtype of \code{PyObject} represents a Python integer object.
1997\end{ctypedesc}
1998
1999\begin{cvardesc}{PyTypeObject}{PyInt_Type}
2000This instance of \code{PyTypeObject} represents the Python plain
2001integer type.
2002\end{cvardesc}
2003
2004\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
2005
2006\end{cfuncdesc}
2007
2008\begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00002009Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002010
2011The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00002012integers between \code{-1} and \code{100}, when you create an int in
2013that range you actually just get back a reference to the existing
2014object. So it should be possible to change the value of \code{1}. I
2015suspect the behaviour of python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00002016\end{cfuncdesc}
2017
2018\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002019Returns the value of the object \var{io}. No error checking is
2020performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002021\end{cfuncdesc}
2022
2023\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002024Will first attempt to cast the object to a \code{PyIntObject}, if
2025it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002026\end{cfuncdesc}
2027
2028\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002029Returns the systems idea of the largest integer it can handle
2030(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002031\end{cfuncdesc}
2032
2033
2034\subsection{Long Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002035\label{longObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002036
2037\begin{ctypedesc}{PyLongObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002038This subtype of \code{PyObject} represents a Python long integer
2039object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002040\end{ctypedesc}
2041
2042\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002043This instance of \code{PyTypeObject} represents the Python long
2044integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002045\end{cvardesc}
2046
2047\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002048Returns true if its argument is a \code{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002049\end{cfuncdesc}
2050
2051\begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long}
2052
2053\end{cfuncdesc}
2054
2055\begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long}
2056
2057\end{cfuncdesc}
2058
2059\begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double}
2060
2061\end{cfuncdesc}
2062
2063\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *}
2064
2065\end{cfuncdesc}
2066
2067\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject }
2068
2069\end{cfuncdesc}
2070
2071\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *}
2072
2073\end{cfuncdesc}
2074
2075\begin{cfuncdesc}{PyObject *}{PyLong_FromString}{char *, char **, int}
2076
2077\end{cfuncdesc}
2078
2079
2080\subsection{Floating Point Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002081\label{floatObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002082
2083\begin{ctypedesc}{PyFloatObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002084This subtype of \code{PyObject} represents a Python floating point
2085object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002086\end{ctypedesc}
2087
2088\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002089This instance of \code{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002090point type.
2091\end{cvardesc}
2092
2093\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002094Returns true if its argument is a \code{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002095\end{cfuncdesc}
2096
2097\begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double}
2098
2099\end{cfuncdesc}
2100
2101\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *}
2102
2103\end{cfuncdesc}
2104
2105\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *}
2106
2107\end{cfuncdesc}
2108
2109
2110\subsection{Complex Number Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002111\label{complexObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002112
2113\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002114The \C{} structure which corresponds to the value portion of a Python
2115complex number object. Most of the functions for dealing with complex
2116number objects use structures of this type as input or output values,
2117as appropriate. It is defined as:
2118
Fred Drakee058b4f1998-02-16 06:15:35 +00002119\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002120typedef struct {
2121 double real;
2122 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002123} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002124\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002125\end{ctypedesc}
2126
2127\begin{ctypedesc}{PyComplexObject}
2128This subtype of \code{PyObject} represents a Python complex number object.
2129\end{ctypedesc}
2130
2131\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2132This instance of \code{PyTypeObject} represents the Python complex
2133number type.
2134\end{cvardesc}
2135
2136\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002137Returns true if its argument is a \code{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002138\end{cfuncdesc}
2139
2140\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex}
2141
2142\end{cfuncdesc}
2143
2144\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex}
2145
2146\end{cfuncdesc}
2147
2148\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex}
2149
2150\end{cfuncdesc}
2151
2152\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex}
2153
2154\end{cfuncdesc}
2155
2156\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex}
2157
2158\end{cfuncdesc}
2159
2160\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex}
2161
2162\end{cfuncdesc}
2163
2164\begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex}
2165
2166\end{cfuncdesc}
2167
2168\begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag}
2169
2170\end{cfuncdesc}
2171
2172\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
2173
2174\end{cfuncdesc}
2175
2176\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
2177
2178\end{cfuncdesc}
2179
2180\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
2181
2182\end{cfuncdesc}
2183
2184
2185
2186\section{Other Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002187\label{otherObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002188
2189\subsection{File Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002190\label{fileObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002191
2192\begin{ctypedesc}{PyFileObject}
2193This subtype of \code{PyObject} represents a Python file object.
2194\end{ctypedesc}
2195
2196\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2197This instance of \code{PyTypeObject} represents the Python file type.
2198\end{cvardesc}
2199
2200\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002201Returns true if its argument is a \code{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002202\end{cfuncdesc}
2203
2204\begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode}
Fred Drakee058b4f1998-02-16 06:15:35 +00002205Creates a new \code{PyFileObject} pointing to the file
2206specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002207\end{cfuncdesc}
2208
2209\begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp,
Fred Drakeb92dce31998-02-25 15:40:22 +00002210 char *name, char *mode, int (*close)}
Fred Drakee058b4f1998-02-16 06:15:35 +00002211Creates a new \code{PyFileObject} from the already-open \var{fp}.
2212The function \var{close} will be called when the file should be
2213closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002214\end{cfuncdesc}
2215
2216\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002217Returns the file object associated with \var{p} as a \code{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002218\end{cfuncdesc}
2219
2220\begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n}
2221undocumented as yet
2222\end{cfuncdesc}
2223
2224\begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002225Returns the name of the file specified by \var{p} as a
2226\code{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002227\end{cfuncdesc}
2228
2229\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002230Available on systems with \cfunction{setvbuf()} only. This should
2231only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002232\end{cfuncdesc}
2233
2234\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakee058b4f1998-02-16 06:15:35 +00002235Sets the \code{softspace} attribute of \var{p} to \var{newflag}.
2236Returns the previosu value. This function clears any errors, and will
2237return \code{0} as the previous value if the attribute either does not
2238exist or if there were errors in retrieving it. There is no way to
2239detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002240\end{cfuncdesc}
2241
2242\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002243Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002244\end{cfuncdesc}
2245
2246\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002247Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002248\end{cfuncdesc}
2249
2250
2251\subsection{CObjects}
Fred Drakef39ed671998-02-26 22:01:23 +00002252\label{cObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002253
2254XXX
2255
2256
Guido van Rossum4a944d71997-08-14 20:35:38 +00002257\chapter{Initialization, Finalization, and Threads}
Fred Drakef39ed671998-02-26 22:01:23 +00002258\label{initialization}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002259
Guido van Rossum4a944d71997-08-14 20:35:38 +00002260\begin{cfuncdesc}{void}{Py_Initialize}{}
2261Initialize the Python interpreter. In an application embedding
2262Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002263functions; with the exception of \cfunction{Py_SetProgramName()},
2264\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2265and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2266loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002267modules \module{__builtin__}\refbimodindex{__builtin__},
2268\module{__main__}\refbimodindex{__main__} and
2269\module{sys}\refbimodindex{sys}. It also initializes the module
2270search path (\code{sys.path}).%
2271\indexiii{module}{search}{path}
2272It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2273that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002274\cfunction{Py_Finalize()} first). There is no return value; it is a
2275fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002276\end{cfuncdesc}
2277
2278\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002279Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002280initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2281called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002282again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002283\end{cfuncdesc}
2284
2285\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002286Undo all initializations made by \cfunction{Py_Initialize()} and
2287subsequent use of Python/C API functions, and destroy all
2288sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2289created and not yet destroyed since the last call to
2290\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2291by the Python interpreter. This is a no-op when called for a second
2292time (without calling \cfunction{Py_Initialize()} again first). There
2293is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002294
2295This function is provided for a number of reasons. An embedding
2296application might want to restart Python without having to restart the
2297application itself. An application that has loaded the Python
2298interpreter from a dynamically loadable library (or DLL) might want to
2299free all memory allocated by Python before unloading the DLL. During a
2300hunt for memory leaks in an application a developer might want to free
2301all memory allocated by Python before exiting from the application.
2302
Fred Drakee058b4f1998-02-16 06:15:35 +00002303\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002304modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002305(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002306(even functions) or modules. Dynamically loaded extension modules
2307loaded by Python are not unloaded. Small amounts of memory allocated
2308by the Python interpreter may not be freed (if you find a leak, please
2309report it). Memory tied up in circular references between objects is
2310not freed. Some memory allocated by extension modules may not be
2311freed. Some extension may not work properly if their initialization
2312routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002313calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2314than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002315\end{cfuncdesc}
2316
2317\begin{cfuncdesc}{PyThreadState *}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002318Create a new sub-interpreter. This is an (almost) totally separate
2319environment for the execution of Python code. In particular, the new
2320interpreter has separate, independent versions of all imported
2321modules, including the fundamental modules
2322\module{__builtin__}\refbimodindex{__builtin__},
2323\module{__main__}\refbimodindex{__main__} and
2324\module{sys}\refbimodindex{sys}. The table of loaded modules
2325(\code{sys.modules}) and the module search path (\code{sys.path}) are
2326also separate. The new environment has no \code{sys.argv} variable.
2327It has new standard I/O stream file objects \code{sys.stdin},
2328\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakeb0a78731998-01-13 18:51:10 +00002329same underlying \code{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002330
2331The return value points to the first thread state created in the new
2332sub-interpreter. This thread state is made the current thread state.
2333Note that no actual thread is created; see the discussion of thread
2334states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002335\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002336is stored in the current thread state and there may not be a current
2337thread state. (Like all other Python/C API functions, the global
2338interpreter lock must be held before calling this function and is
2339still held when it returns; however, unlike most other Python/C API
2340functions, there needn't be a current thread state on entry.)
2341
2342Extension modules are shared between (sub-)interpreters as follows:
2343the first time a particular extension is imported, it is initialized
2344normally, and a (shallow) copy of its module's dictionary is
2345squirreled away. When the same extension is imported by another
2346(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002347contents of this copy; the extension's \code{init} function is not
2348called. Note that this is different from what happens when an
2349extension is imported after the interpreter has been completely
2350re-initialized by calling \cfunction{Py_Finalize()} and
2351\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002352function \emph{is} called again.
2353
Fred Drakee058b4f1998-02-16 06:15:35 +00002354\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002355interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002356isn't perfect --- for example, using low-level file operations like
Guido van Rossum4a944d71997-08-14 20:35:38 +00002357\code{os.close()} they can (accidentally or maliciously) affect each
2358other's open files. Because of the way extensions are shared between
2359(sub-)interpreters, some extensions may not work properly; this is
2360especially likely when the extension makes use of (static) global
2361variables, or when the extension manipulates its module's dictionary
2362after its initialization. It is possible to insert objects created in
2363one sub-interpreter into a namespace of another sub-interpreter; this
2364should be done with great care to avoid sharing user-defined
2365functions, methods, instances or classes between sub-interpreters,
2366since import operations executed by such objects may affect the
2367wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2368a hard-to-fix bug that will be addressed in a future release.)
2369\end{cfuncdesc}
2370
2371\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
2372Destroy the (sub-)interpreter represented by the given thread state.
2373The given thread state must be the current thread state. See the
2374discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002375thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002376interpreted are destroyed. (The global interpreter lock must be held
2377before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002378\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002379been explicitly destroyed at that point.
2380\end{cfuncdesc}
2381
2382\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +00002383This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002384for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002385the value of the \code{argv[0]} argument to the \cfunction{main()} function
2386of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002387functions below to find the Python run-time libraries relative to the
2388interpreter executable. The default value is \code{"python"}. The
2389argument should point to a zero-terminated character string in static
2390storage whose contents will not change for the duration of the
2391program's execution. No code in the Python interpreter will change
2392the contents of this storage.
2393\end{cfuncdesc}
2394
2395\begin{cfuncdesc}{char *}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002396Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002397default. The returned string points into static storage; the caller
2398should not modify its value.
2399\end{cfuncdesc}
2400
2401\begin{cfuncdesc}{char *}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002402Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002403is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002404set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002405for example, if the program name is \code{"/usr/local/bin/python"},
2406the prefix is \code{"/usr/local"}. The returned string points into
2407static storage; the caller should not modify its value. This
2408corresponds to the \code{prefix} variable in the top-level
Fred Drakee058b4f1998-02-16 06:15:35 +00002409\file{Makefile} and the \code{--prefix} argument to the
2410\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002411Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002412also the next function.
2413\end{cfuncdesc}
2414
2415\begin{cfuncdesc}{char *}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002416Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002417files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002418program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002419variables; for example, if the program name is
2420\code{"/usr/local/bin/python"}, the exec-prefix is
2421\code{"/usr/local"}. The returned string points into static storage;
2422the caller should not modify its value. This corresponds to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002423\code{exec_prefix} variable in the top-level \file{Makefile} and the
2424\code{--exec_prefix} argument to the \program{configure} script at build
Guido van Rossum4a944d71997-08-14 20:35:38 +00002425time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002426\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002427
2428Background: The exec-prefix differs from the prefix when platform
2429dependent files (such as executables and shared libraries) are
2430installed in a different directory tree. In a typical installation,
2431platform dependent files may be installed in the
2432\code{"/usr/local/plat"} subtree while platform independent may be
2433installed in \code{"/usr/local"}.
2434
2435Generally speaking, a platform is a combination of hardware and
2436software families, e.g. Sparc machines running the Solaris 2.x
2437operating system are considered the same platform, but Intel machines
2438running Solaris 2.x are another platform, and Intel machines running
2439Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002440operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002441operating systems are a different story; the installation strategies
2442on those systems are so different that the prefix and exec-prefix are
2443meaningless, and set to the empty string. Note that compiled Python
2444bytecode files are platform independent (but not independent from the
2445Python version by which they were compiled!).
2446
Fred Drakee058b4f1998-02-16 06:15:35 +00002447System administrators will know how to configure the \program{mount} or
2448\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002449while having \code{"/usr/local/plat"} be a different filesystem for each
2450platform.
2451\end{cfuncdesc}
2452
2453\begin{cfuncdesc}{char *}{Py_GetProgramFullPath}{}
2454Return the full program name of the Python executable; this is
2455computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002456from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002457returned string points into static storage; the caller should not
2458modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002459\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002460\end{cfuncdesc}
2461
2462\begin{cfuncdesc}{char *}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002463\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002464Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002465program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002466environment variables. The returned string consists of a series of
2467directory names separated by a platform dependent delimiter character.
Fred Drakee5bc4971998-02-12 23:36:49 +00002468The delimiter character is \code{':'} on \UNIX{}, \code{';'} on
2469DOS/Windows, and \code{'\\n'} (the \ASCII{} newline character) on
2470Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002471should not modify its value. The value is available to Python code
2472as the list \code{sys.path}, which may be modified to change the
2473future search path for loaded modules.
2474
2475% XXX should give the exact rules
2476\end{cfuncdesc}
2477
2478\begin{cfuncdesc}{const char *}{Py_GetVersion}{}
2479Return the version of this Python interpreter. This is a string that
2480looks something like
2481
Guido van Rossum09270b51997-08-15 18:57:32 +00002482\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002483"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002484\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002485
2486The first word (up to the first space character) is the current Python
2487version; the first three characters are the major and minor version
2488separated by a period. The returned string points into static storage;
2489the caller should not modify its value. The value is available to
2490Python code as the list \code{sys.version}.
2491\end{cfuncdesc}
2492
2493\begin{cfuncdesc}{const char *}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002494Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002495this is formed from the ``official'' name of the operating system,
2496converted to lower case, followed by the major revision number; e.g.,
2497for Solaris 2.x, which is also known as SunOS 5.x, the value is
2498\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2499is \code{"win"}. The returned string points into static storage;
2500the caller should not modify its value. The value is available to
2501Python code as \code{sys.platform}.
2502\end{cfuncdesc}
2503
2504\begin{cfuncdesc}{const char *}{Py_GetCopyright}{}
2505Return the official copyright string for the current Python version,
2506for example
2507
2508\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2509
2510The returned string points into static storage; the caller should not
2511modify its value. The value is available to Python code as the list
2512\code{sys.copyright}.
2513\end{cfuncdesc}
2514
2515\begin{cfuncdesc}{const char *}{Py_GetCompiler}{}
2516Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002517version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002518
Fred Drakee058b4f1998-02-16 06:15:35 +00002519\begin{verbatim}
2520"[GCC 2.7.2.2]"
2521\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002522
2523The returned string points into static storage; the caller should not
2524modify its value. The value is available to Python code as part of
2525the variable \code{sys.version}.
2526\end{cfuncdesc}
2527
2528\begin{cfuncdesc}{const char *}{Py_GetBuildInfo}{}
2529Return information about the sequence number and build date and time
2530of the current Python interpreter instance, for example
2531
Guido van Rossum09270b51997-08-15 18:57:32 +00002532\begin{verbatim}
2533"#67, Aug 1 1997, 22:34:28"
2534\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002535
2536The returned string points into static storage; the caller should not
2537modify its value. The value is available to Python code as part of
2538the variable \code{sys.version}.
2539\end{cfuncdesc}
2540
2541\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2542% XXX
2543\end{cfuncdesc}
2544
2545% XXX Other PySys thingies (doesn't really belong in this chapter)
2546
2547\section{Thread State and the Global Interpreter Lock}
Fred Drakef39ed671998-02-26 22:01:23 +00002548\label{threads}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002549
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002550The Python interpreter is not fully thread safe. In order to support
2551multi-threaded Python programs, there's a global lock that must be
2552held by the current thread before it can safely access Python objects.
2553Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00002554a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002555increment the reference count of the same object, the reference count
2556could end up being incremented only once instead of twice.
2557
2558Therefore, the rule exists that only the thread that has acquired the
2559global interpreter lock may operate on Python objects or call Python/C
2560API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002561the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002562default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002563\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002564reacquired around potentially blocking I/O operations like reading or
2565writing a file, so that other threads can run while the thread that
2566requests the I/O is waiting for the I/O operation to complete.
2567
2568The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002569separate per thread --- for this it uses a data structure called
2570\code{PyThreadState}. This is new in Python 1.5; in earlier versions,
2571such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002572cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002573when the application uses \function{sys.exc_info()} to access the
2574exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002575
2576There's one global variable left, however: the pointer to the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002577\code{PyThreadState} structure. While most thread packages have a way
Fred Drake9d20ac31998-02-16 15:27:08 +00002578to store ``per-thread global data,'' Python's internal platform
Fred Drakee058b4f1998-02-16 06:15:35 +00002579independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002580the current thread state must be manipulated explicitly.
2581
2582This is easy enough in most cases. Most code manipulating the global
2583interpreter lock has the following simple structure:
2584
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002585\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002586Save the thread state in a local variable.
2587Release the interpreter lock.
2588...Do some blocking I/O operation...
2589Reacquire the interpreter lock.
2590Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002591\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002592
2593This is so common that a pair of macros exists to simplify it:
2594
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002595\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002596Py_BEGIN_ALLOW_THREADS
2597...Do some blocking I/O operation...
2598Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002599\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002600
Fred Drakee058b4f1998-02-16 06:15:35 +00002601The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2602a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2603the block. Another advantage of using these two macros is that when
2604Python is compiled without thread support, they are defined empty,
2605thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002606
2607When thread support is enabled, the block above expands to the
2608following code:
2609
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002610\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002611{
2612 PyThreadState *_save;
2613 _save = PyEval_SaveThread();
2614 ...Do some blocking I/O operation...
2615 PyEval_RestoreThread(_save);
2616}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002617\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002618
2619Using even lower level primitives, we can get roughly the same effect
2620as follows:
2621
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002622\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002623{
2624 PyThreadState *_save;
2625 _save = PyThreadState_Swap(NULL);
2626 PyEval_ReleaseLock();
2627 ...Do some blocking I/O operation...
2628 PyEval_AcquireLock();
2629 PyThreadState_Swap(_save);
2630}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002631\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002632
2633There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002634\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002635global variable \code{errno}, since the lock manipulation does not
2636guarantee that \code{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002637is disabled, \cfunction{PyEval_SaveThread()} and
2638\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2639case, \cfunction{PyEval_ReleaseLock()} and
2640\cfunction{PyEval_AcquireLock()} are not available. This is done so
2641that dynamically loaded extensions compiled with thread support
2642enabled can be loaded by an interpreter that was compiled with
2643disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002644
2645The global interpreter lock is used to protect the pointer to the
2646current thread state. When releasing the lock and saving the thread
2647state, the current thread state pointer must be retrieved before the
2648lock is released (since another thread could immediately acquire the
2649lock and store its own thread state in the global variable).
2650Reversely, when acquiring the lock and restoring the thread state, the
2651lock must be acquired before storing the thread state pointer.
2652
2653Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002654threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002655lock, nor is there a thread state data structure for them. Such
2656threads must bootstrap themselves into existence, by first creating a
2657thread state data structure, then acquiring the lock, and finally
2658storing their thread state pointer, before they can start using the
2659Python/C API. When they are done, they should reset the thread state
2660pointer, release the lock, and finally free their thread state data
2661structure.
2662
2663When creating a thread data structure, you need to provide an
2664interpreter state data structure. The interpreter state data
2665structure hold global data that is shared by all threads in an
2666interpreter, for example the module administration
2667(\code{sys.modules}). Depending on your needs, you can either create
2668a new interpreter state data structure, or share the interpreter state
2669data structure used by the Python main thread (to access the latter,
2670you must obtain the thread state and access its \code{interp} member;
2671this must be done by a thread that is created by Python or by the main
2672thread after Python is initialized).
2673
2674XXX More?
2675
2676\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002677This data structure represents the state shared by a number of
2678cooperating threads. Threads belonging to the same interpreter
2679share their module administration and a few other internal items.
2680There are no public members in this structure.
2681
2682Threads belonging to different interpreters initially share nothing,
2683except process state like available memory, open file descriptors and
2684such. The global interpreter lock is also shared by all threads,
2685regardless of to which interpreter they belong.
2686\end{ctypedesc}
2687
2688\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002689This data structure represents the state of a single thread. The only
2690public data member is \code{PyInterpreterState *interp}, which points
2691to this thread's interpreter state.
2692\end{ctypedesc}
2693
2694\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2695Initialize and acquire the global interpreter lock. It should be
2696called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002697in any other thread operations such as
2698\cfunction{PyEval_ReleaseLock()} or
2699\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2700calling \cfunction{PyEval_SaveThread()} or
2701\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002702
2703This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002704this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002705
2706When only the main thread exists, no lock operations are needed. This
2707is a common situation (most Python programs do not use threads), and
2708the lock operations slow the interpreter down a bit. Therefore, the
2709lock is not created initially. This situation is equivalent to having
2710acquired the lock: when there is only a single thread, all object
2711accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002712lock, it also acquires it. Before the Python
2713\module{thread}\refbimodindex{thread} module creates a new thread,
2714knowing that either it has the lock or the lock hasn't been created
2715yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2716returns, it is guaranteed that the lock has been created and that it
2717has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002718
2719It is \strong{not} safe to call this function when it is unknown which
2720thread (if any) currently has the global interpreter lock.
2721
2722This function is not available when thread support is disabled at
2723compile time.
2724\end{cfuncdesc}
2725
Guido van Rossum4a944d71997-08-14 20:35:38 +00002726\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002727Acquire the global interpreter lock. The lock must have been created
2728earlier. If this thread already has the lock, a deadlock ensues.
2729This function is not available when thread support is disabled at
2730compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002731\end{cfuncdesc}
2732
2733\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002734Release the global interpreter lock. The lock must have been created
2735earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002736disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002737\end{cfuncdesc}
2738
2739\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002740Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002741state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002742have been created earlier. If this thread already has the lock,
2743deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002744is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002745\end{cfuncdesc}
2746
2747\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002748Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002749interpreter lock. The lock must have been created earlier and must be
2750held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002751be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002752thread state --- if it isn't, a fatal error is reported. This
2753function is not available when thread support is disabled at compile
2754time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002755\end{cfuncdesc}
2756
2757\begin{cfuncdesc}{PyThreadState *}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002758Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002759support is enabled) and reset the thread state to \NULL{},
2760returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002761the lock has been created, the current thread must have acquired it.
2762(This function is available even when thread support is disabled at
2763compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002764\end{cfuncdesc}
2765
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002766\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002767Acquire the interpreter lock (if it has been created and thread
2768support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002769must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002770thread must not have acquired it, otherwise deadlock ensues. (This
2771function is available even when thread support is disabled at compile
2772time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002773\end{cfuncdesc}
2774
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002775% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2776\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2777This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002778\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002779Note that it contains an opening brace; it must be matched with a
2780following \code{Py_END_ALLOW_THREADS} macro. See above for further
2781discussion of this macro. It is a no-op when thread support is
2782disabled at compile time.
2783\end{ctypedesc}
2784
2785\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2786This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002787\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002788Note that it contains a closing brace; it must be matched with an
2789earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2790discussion of this macro. It is a no-op when thread support is
2791disabled at compile time.
2792\end{ctypedesc}
2793
2794\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002795This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002796is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2797brace. It is a no-op when thread support is disabled at compile
2798time.
2799\end{ctypedesc}
2800
2801\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002802This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002803equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2804and variable declaration. It is a no-op when thread support is
2805disabled at compile time.
2806\end{ctypedesc}
2807
2808All of the following functions are only available when thread support
2809is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00002810interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002811
2812\begin{cfuncdesc}{PyInterpreterState *}{PyInterpreterState_New}{}
2813Create a new interpreter state object. The interpreter lock must be
2814held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002815\end{cfuncdesc}
2816
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002817\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2818Reset all information in an interpreter state object. The interpreter
2819lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002820\end{cfuncdesc}
2821
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002822\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2823Destroy an interpreter state object. The interpreter lock need not be
2824held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002825call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002826\end{cfuncdesc}
2827
2828\begin{cfuncdesc}{PyThreadState *}{PyThreadState_New}{PyInterpreterState *interp}
2829Create a new thread state object belonging to the given interpreter
2830object. The interpreter lock must be held.
2831\end{cfuncdesc}
2832
2833\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2834Reset all information in a thread state object. The interpreter lock
2835must be held.
2836\end{cfuncdesc}
2837
2838\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2839Destroy a thread state object. The interpreter lock need not be
2840held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002841call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002842\end{cfuncdesc}
2843
2844\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Get}{}
2845Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002846When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002847error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002848\end{cfuncdesc}
2849
2850\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Swap}{PyThreadState *tstate}
2851Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002852argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002853must be held.
2854\end{cfuncdesc}
2855
2856
Fred Drakee058b4f1998-02-16 06:15:35 +00002857\chapter{Defining New Object Types}
Fred Drakef39ed671998-02-26 22:01:23 +00002858\label{newTypes}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002859
Fred Drakee058b4f1998-02-16 06:15:35 +00002860\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
2861\end{cfuncdesc}
2862
2863\begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size}
2864\end{cfuncdesc}
2865
2866\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2867\end{cfuncdesc}
2868
2869\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2870\end{cfuncdesc}
2871
Guido van Rossumae110af1997-05-22 20:11:52 +00002872
2873PyObject, PyVarObject
2874
2875PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2876
2877Typedefs:
2878unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2879intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2880getreadbufferproc, getwritebufferproc, getsegcountproc,
2881destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2882setattrofunc, cmpfunc, reprfunc, hashfunc
2883
2884PyNumberMethods
2885
2886PySequenceMethods
2887
2888PyMappingMethods
2889
2890PyBufferProcs
2891
2892PyTypeObject
2893
2894DL_IMPORT
2895
2896PyType_Type
2897
2898Py*_Check
2899
2900Py_None, _Py_NoneStruct
2901
Guido van Rossumae110af1997-05-22 20:11:52 +00002902
Fred Drakee5bf8b21998-02-12 21:22:28 +00002903\chapter{Debugging}
Fred Drakef39ed671998-02-26 22:01:23 +00002904\label{debugging}
Guido van Rossumae110af1997-05-22 20:11:52 +00002905
Fred Drakee5bf8b21998-02-12 21:22:28 +00002906XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002907
2908
Fred Drake7d20ffe1998-03-09 16:39:22 +00002909\inputindex{api.ind} % Index -- must be last
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002910
2911\end{document}