blob: 1c697a795e36685f3ec9a9f80d47de5a326894f5 [file] [log] [blame]
Fred Drakedca87921998-01-13 16:53:23 +00001\documentclass[twoside,openright]{report}
Fred Drake1f8449a1998-01-09 05:36:43 +00002\usepackage{myformat}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00003
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004\title{Python/C API Reference Manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00005
6\input{boilerplate}
7
8\makeindex % tell \index to actually write the .idx file
9
10
11\begin{document}
12
Guido van Rossum9231c8f1997-05-15 21:43:21 +000013\maketitle
14
15\input{copyright}
16
17\begin{abstract}
18
19\noindent
Fred Drakee058b4f1998-02-16 06:15:35 +000020This manual documents the API used by \C{} (or \Cpp{}) programmers who
21want to write extension modules or embed Python. It is a companion to
22\emph{Extending and Embedding the Python Interpreter}, which describes
Guido van Rossum9231c8f1997-05-15 21:43:21 +000023the general principles of extension writing but does not document the
24API functions in detail.
25
Guido van Rossum5b8a5231997-12-30 04:38:44 +000026\strong{Warning:} The current version of this document is incomplete.
27I hope that it is nevertheless useful. I will continue to work on it,
28and release new versions from time to time, independent from Python
29source code releases.
30
Guido van Rossum9231c8f1997-05-15 21:43:21 +000031\end{abstract}
32
Fred Drake4d4f9e71998-01-13 22:25:02 +000033\tableofcontents
Guido van Rossum9231c8f1997-05-15 21:43:21 +000034
Guido van Rossum5060b3b1997-08-17 18:02:23 +000035% XXX Consider moving all this back to ext.tex and giving api.tex
36% XXX a *really* short intro only.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000037
38\chapter{Introduction}
39
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}
68
69All function, type and macro definitions needed to use the Python/C
70API are included in your code by the following line:
71
Fred Drakee058b4f1998-02-16 06:15:35 +000072\begin{verbatim}
73#include "Python.h"
74\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000075
Fred Drakee058b4f1998-02-16 06:15:35 +000076This implies inclusion of the following standard headers:
77\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
78\code{<stdlib.h>} (if available).
Guido van Rossum580aa8d1997-11-25 15:34:51 +000079
80All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000081the included standard headers) have one of the prefixes \samp{Py} or
82\samp{_Py}. Names beginning with \samp{_Py} are for internal use
Guido van Rossum580aa8d1997-11-25 15:34:51 +000083only. Structure member names do not have a reserved prefix.
84
Fred Drakee058b4f1998-02-16 06:15:35 +000085\strong{Important:} user code should never define names that begin
86with \samp{Py} or \samp{_Py}. This confuses the reader, and
87jeopardizes the portability of the user code to future Python
88versions, which may define additional names beginning with one of
89these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000090
Guido van Rossum59a61351997-08-14 20:34:33 +000091\section{Objects, Types and Reference Counts}
92
Guido van Rossum580aa8d1997-11-25 15:34:51 +000093Most Python/C API functions have one or more arguments as well as a
94return value of type \code{PyObject *}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +000095to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +000096object. Since all Python object types are treated the same way by the
97Python language in most situations (e.g., assignments, scope rules,
98and argument passing), it is only fitting that they should be
Fred Drakeb0a78731998-01-13 18:51:10 +000099represented by a single \C{} type. All Python objects live on the heap:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000100you never declare an automatic or static variable of type
Guido van Rossum4a944d71997-08-14 20:35:38 +0000101\code{PyObject}, only pointer variables of type \code{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +0000102be declared.
103
Fred Drakee058b4f1998-02-16 06:15:35 +0000104All Python objects (even Python integers) have a \dfn{type} and a
105\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000106it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakee058b4f1998-02-16 06:15:35 +0000107many more as explained in the \emph{Python Reference Manual}). For
Guido van Rossum4a944d71997-08-14 20:35:38 +0000108each of the well-known types there is a macro to check whether an
Fred Drakee058b4f1998-02-16 06:15:35 +0000109object is of that type; for instance, \samp{PyList_Check(\var{a})} is
110true iff the object pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000111
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000112\subsection{Reference Counts}
113
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000114The reference count is important because today's computers have a
Guido van Rossum4a944d71997-08-14 20:35:38 +0000115finite (and often severly limited) memory size; it counts how many
116different places there are that have a reference to an object. Such a
Fred Drakeb0a78731998-01-13 18:51:10 +0000117place could be another object, or a global (or static) \C{} variable, or
118a local variable in some \C{} function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000119becomes zero, the object is deallocated. If it contains references to
120other objects, their reference count is decremented. Those other
121objects may be deallocated in turn, if this decrement makes their
122reference count become zero, and so on. (There's an obvious problem
123with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000124``don't do that''.)
125
Guido van Rossum4a944d71997-08-14 20:35:38 +0000126Reference counts are always manipulated explicitly. The normal way is
Fred Drakee058b4f1998-02-16 06:15:35 +0000127to use the macro \cfunction{Py_INCREF()} to increment an object's
128reference count by one, and \cfunction{Py_DECREF()} to decrement it by
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000129one. The decref macro is considerably more complex than the incref one,
Guido van Rossum4a944d71997-08-14 20:35:38 +0000130since it must check whether the reference count becomes zero and then
131cause the object's deallocator, which is a function pointer contained
132in the object's type structure. The type-specific deallocator takes
133care of decrementing the reference counts for other objects contained
134in the object, and so on, if this is a compound object type such as a
135list. There's no chance that the reference count can overflow; at
136least as many bits are used to hold the reference count as there are
137distinct memory locations in virtual memory (assuming
138\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000139increment is a simple operation.
140
Guido van Rossum4a944d71997-08-14 20:35:38 +0000141It is not necessary to increment an object's reference count for every
142local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000143object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000144point to it and it goes down by one when the variable goes out of
145scope. However, these two cancel each other out, so at the end the
146reference count hasn't changed. The only real reason to use the
147reference count is to prevent the object from being deallocated as
148long as our variable is pointing to it. If we know that there is at
149least one other reference to the object that lives at least as long as
150our variable, there is no need to increment the reference count
151temporarily. An important situation where this arises is in objects
Fred Drakeb0a78731998-01-13 18:51:10 +0000152that are passed as arguments to \C{} functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000153that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000154reference to every argument for the duration of the call.
155
Fred Drakee058b4f1998-02-16 06:15:35 +0000156However, a common pitfall is to extract an object from a list and
157hold on to it for a while without incrementing its reference count.
158Some other operation might conceivably remove the object from the
159list, decrementing its reference count and possible deallocating it.
160The real danger is that innocent-looking operations may invoke
161arbitrary Python code which could do this; there is a code path which
162allows control to flow back to the user from a \cfunction{Py_DECREF()},
163so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000164
Guido van Rossum4a944d71997-08-14 20:35:38 +0000165A safe approach is to always use the generic operations (functions
Fred Drakee058b4f1998-02-16 06:15:35 +0000166whose name begins with \samp{PyObject_}, \samp{PyNumber_},
167\samp{PySequence_} or \samp{PyMapping_}). These operations always
Guido van Rossum4a944d71997-08-14 20:35:38 +0000168increment the reference count of the object they return. This leaves
Fred Drakee058b4f1998-02-16 06:15:35 +0000169the caller with the responsibility to call \cfunction{Py_DECREF()}
170when they are done with the result; this soon becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000171
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000172\subsubsection{Reference Count Details}
173
174The reference count behavior of functions in the Python/C API is best
175expelained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000176talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000177shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000178properly --- either by passing ownership on (usually to its caller) or
179by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
180a function passes ownership of a reference on to its caller, the
181caller is said to receive a \emph{new} reference. When no ownership
182is transferred, the caller is said to \emph{borrow} the reference.
183Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000184
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000185Conversely, when calling a function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000186object, there are two possibilities: the function \emph{steals} a
187reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000188references; the two notable exceptions are
189\cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which
190steal a reference to the item (but not to the tuple or list into which
191the item it put!). These functions were designed to steal a reference
192because of a common idiom for populating a tuple or list with newly
193created objects; for example, the code to create the tuple \code{(1,
1942, "three")} could look like this (forgetting about error handling for
195the moment; a better way to code this is shown below anyway):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000196
197\begin{verbatim}
198PyObject *t;
199t = PyTuple_New(3);
200PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
201PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
202PyTuple_SetItem(t, 2, PyString_FromString("three"));
203\end{verbatim}
204
Fred Drakee058b4f1998-02-16 06:15:35 +0000205Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
206set tuple items; \cfunction{PySequence_SetItem()} and
207\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
208immutable data type. You should only use
209\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000210yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000211
212Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000213\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
214can also use \cfunction{PySequence_SetItem()}; this illustrates the
215difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000216
217\begin{verbatim}
218PyObject *l, *x;
219l = PyList_New(3);
220x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000221PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000222x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000223PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000224x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000225PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000226\end{verbatim}
227
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000228You might find it strange that the ``recommended'' approach takes more
229code. However, in practice, you will rarely use these ways of
230creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000231\cfunction{Py_BuildValue()}, that can create most common objects from
232\C{} values, directed by a \dfn{format string}. For example, the
233above two blocks of code could be replaced by the following (which
234also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000235
236\begin{verbatim}
237PyObject *t, *l;
238t = Py_BuildValue("(iis)", 1, 2, "three");
239l = Py_BuildValue("[iis]", 1, 2, "three");
240\end{verbatim}
241
Fred Drakee058b4f1998-02-16 06:15:35 +0000242It is much more common to use \cfunction{PyObject_SetItem()} and
243friends with items whose references you are only borrowing, like
244arguments that were passed in to the function you are writing. In
245that case, their behaviour regarding reference counts is much saner,
246since you don't have to increment a reference count so you can give a
247reference away (``have it be stolen''). For example, this function
248sets all items of a list (actually, any mutable sequence) to a given
249item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000250
251\begin{verbatim}
252int set_all(PyObject *target, PyObject *item)
253{
254 int i, n;
255 n = PyObject_Length(target);
256 if (n < 0)
257 return -1;
258 for (i = 0; i < n; i++) {
259 if (PyObject_SetItem(target, i, item) < 0)
260 return -1;
261 }
262 return 0;
263}
264\end{verbatim}
265
266The situation is slightly different for function return values.
267While passing a reference to most functions does not change your
268ownership responsibilities for that reference, many functions that
269return a referece to an object give you ownership of the reference.
270The reason is simple: in many cases, the returned object is created
271on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000272object. Therefore, the generic functions that return object
273references, like \cfunction{PyObject_GetItem()} and
274\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
275the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000276
277It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000278by a function depends on which function you call only --- \emph{the
279plumage} (i.e., the type of the type of the object passed as an
280argument to the function) \emph{doesn't enter into it!} Thus, if you
281extract an item from a list using \cfunction{PyList_GetItem()}, you
282don't own the reference --- but if you obtain the same item from the
283same list using \cfunction{PySequence_GetItem()} (which happens to
284take exactly the same arguments), you do own a reference to the
285returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000286
Fred Drakee058b4f1998-02-16 06:15:35 +0000287Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000288sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000289\cfunction{PyList_GetItem()}, once using
290\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000291
292\begin{verbatim}
293long sum_list(PyObject *list)
294{
295 int i, n;
296 long total = 0;
297 PyObject *item;
298 n = PyList_Size(list);
299 if (n < 0)
300 return -1; /* Not a list */
301 for (i = 0; i < n; i++) {
302 item = PyList_GetItem(list, i); /* Can't fail */
303 if (!PyInt_Check(item)) continue; /* Skip non-integers */
304 total += PyInt_AsLong(item);
305 }
306 return total;
307}
308\end{verbatim}
309
310\begin{verbatim}
311long sum_sequence(PyObject *sequence)
312{
313 int i, n;
314 long total = 0;
315 PyObject *item;
316 n = PyObject_Size(list);
317 if (n < 0)
318 return -1; /* Has no length */
319 for (i = 0; i < n; i++) {
320 item = PySequence_GetItem(list, i);
321 if (item == NULL)
322 return -1; /* Not a sequence, or other failure */
323 if (PyInt_Check(item))
324 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000325 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000326 }
327 return total;
328}
329\end{verbatim}
330
331\subsection{Types}
332
333There are few other data types that play a significant role in
Fred Drakeb0a78731998-01-13 18:51:10 +0000334the Python/C API; most are simple \C{} types such as \code{int},
Guido van Rossum4a944d71997-08-14 20:35:38 +0000335\code{long}, \code{double} and \code{char *}. A few structure types
336are used to describe static tables used to list the functions exported
337by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000338be discussed together with the functions that use them.
339
340\section{Exceptions}
341
Guido van Rossum4a944d71997-08-14 20:35:38 +0000342The Python programmer only needs to deal with exceptions if specific
343error handling is required; unhandled exceptions are automatically
344propagated to the caller, then to the caller's caller, and so on, till
345they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000346user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000347
Fred Drakeb0a78731998-01-13 18:51:10 +0000348For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000349All functions in the Python/C API can raise exceptions, unless an
350explicit claim is made otherwise in a function's documentation. In
351general, when a function encounters an error, it sets an exception,
352discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000353error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000354return a Boolean true/false result, with false indicating an error.
355Very few functions return no explicit error indicator or have an
356ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000357\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000358
359Exception state is maintained in per-thread storage (this is
360equivalent to using global storage in an unthreaded application). A
Fred Drakee058b4f1998-02-16 06:15:35 +0000361thread can be on one of two states: an exception has occurred, or not.
362The function \cfunction{PyErr_Occurred()} can be used to check for
363this: it returns a borrowed reference to the exception type object
364when an exception has occurred, and \NULL{} otherwise. There are a
365number of functions to set the exception state:
366\cfunction{PyErr_SetString()} is the most common (though not the most
367general) function to set the exception state, and
368\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000369
370The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000371be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000372value, and the traceback. These have the same meanings as the Python
373object \code{sys.exc_type}, \code{sys.exc_value},
374\code{sys.exc_traceback}; however, they are not the same: the Python
375objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000376\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
377exception state only exists while an exception is being passed on
378between \C{} functions until it reaches the Python interpreter, which
379takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000380
381(Note that starting with Python 1.5, the preferred, thread-safe way to
382access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000383\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000384for Python code. Also, the semantics of both ways to access the
385exception state have changed so that a function which catches an
386exception will save and restore its thread's exception state so as to
387preserve the exception state of its caller. This prevents common bugs
388in exception handling code caused by an innocent-looking function
389overwriting the exception being handled; it also reduces the often
390unwanted lifetime extension for objects that are referenced by the
391stack frames in the traceback.)
392
393As a general principle, a function that calls another function to
394perform some task should check whether the called function raised an
395exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000396should discard any object references that it owns, and returns an
397error indicator, but it should \emph{not} set another exception ---
398that would overwrite the exception that was just raised, and lose
399important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000400
401A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000402in the \cfunction{sum_sequence()} example above. It so happens that
403that example doesn't need to clean up any owned references when it
404detects an error. The following example function shows some error
405cleanup. First, to remind you why you like Python, we show the
406equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000407
408\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000409def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000410 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000411 item = dict[key]
412 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000413 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000414 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000415\end{verbatim}
416
Fred Drakeb0a78731998-01-13 18:51:10 +0000417Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000418
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000419\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000420int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000421{
422 /* Objects all initialized to NULL for Py_XDECREF */
423 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000424 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000425
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000426 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000427 if (item == NULL) {
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000428 /* Handle keyError only: */
429 if (!PyErr_ExceptionMatches(PyExc_keyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000430
431 /* Clear the error and use zero: */
432 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000433 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000434 if (item == NULL) goto error;
435 }
436
437 const_one = PyInt_FromLong(1L);
438 if (const_one == NULL) goto error;
439
440 incremented_item = PyNumber_Add(item, const_one);
441 if (incremented_item == NULL) goto error;
442
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000443 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000444 rv = 0; /* Success */
445 /* Continue with cleanup code */
446
447 error:
448 /* Cleanup code, shared by success and failure path */
449
450 /* Use Py_XDECREF() to ignore NULL references */
451 Py_XDECREF(item);
452 Py_XDECREF(const_one);
453 Py_XDECREF(incremented_item);
454
455 return rv; /* -1 for error, 0 for success */
456}
457\end{verbatim}
458
459This example represents an endorsed use of the \code{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000460in \C{}! It illustrates the use of
461\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
462handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
463dispose of owned references that may be \NULL{} (note the \samp{X} in
464the name; \cfunction{Py_DECREF()} would crash when confronted with a
465\NULL{} reference). It is important that the variables used to hold
466owned references are initialized to \NULL{} for this to work;
467likewise, the proposed return value is initialized to \code{-1}
468(failure) and only set to success after the final call made is
469successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000470
Guido van Rossum59a61351997-08-14 20:34:33 +0000471
472\section{Embedding Python}
473
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000474The one important task that only embedders (as opposed to extension
475writers) of the Python interpreter have to worry about is the
476initialization, and possibly the finalization, of the Python
477interpreter. Most functionality of the interpreter can only be used
478after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000479
Fred Drakee058b4f1998-02-16 06:15:35 +0000480The basic initialization function is \cfunction{Py_Initialize()}.
481This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000482fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
483\module{__main__}\refbimodindex{__main__} and
484\module{sys}\refbimodindex{sys}. It also initializes the module
485search path (\code{sys.path}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000486
Fred Drakee058b4f1998-02-16 06:15:35 +0000487\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000488(\code{sys.argv}). If this variable is needed by Python code that
489will be executed later, it must be set explicitly with a call to
490\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000491to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000492
Fred Drakeb0a78731998-01-13 18:51:10 +0000493On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000494details are slightly different), \cfunction{Py_Initialize()}
495calculates the module search path based upon its best guess for the
496location of the standard Python interpreter executable, assuming that
497the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000498interpreter executable. In particular, it looks for a directory named
Fred Drakee058b4f1998-02-16 06:15:35 +0000499\file{lib/python\version} (replacing \file{\version} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000500interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000501executable named \file{python} is found on the shell command search
Guido van Rossum42cefd01997-10-05 15:27:29 +0000502path (the environment variable \code{\$PATH}).
503
504For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000505\file{/usr/local/bin/python}, it will assume that the libraries are in
506\file{/usr/local/lib/python\version}. (In fact, this particular path
507is also the ``fallback'' location, used when no executable file named
508\file{python} is found along \code{\$PATH}.) The user can override
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000509this behavior by setting the environment variable \code{\$PYTHONHOME},
510or insert additional directories in front of the standard path by
511setting \code{\$PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000512
Guido van Rossum4a944d71997-08-14 20:35:38 +0000513The embedding application can steer the search by calling
514\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakee058b4f1998-02-16 06:15:35 +0000515\cfunction{Py_Initialize()}. Note that \code{\$PYTHONHOME} still
516overrides this and \code{\$PYTHONPATH} is still inserted in front of
517the standard path. An application that requires total control has to
518provide its own implementation of \cfunction{Py_GetPath()},
519\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
520\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000521\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000522
Guido van Rossum4a944d71997-08-14 20:35:38 +0000523Sometimes, it is desirable to ``uninitialize'' Python. For instance,
524the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000525\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000526use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000527can be accomplished by calling \cfunction{Py_Finalize()}. The function
528\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000529initialized state. More information about these functions is given in
530a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000531
Guido van Rossum4a944d71997-08-14 20:35:38 +0000532
Fred Drakee5bf8b21998-02-12 21:22:28 +0000533\chapter{The Very High Level Layer}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000534
Fred Drakee5bf8b21998-02-12 21:22:28 +0000535The functions in this chapter will let you execute Python source code
536given in a file or a buffer, but they will not let you interact in a
537more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000538
Fred Drakee5bf8b21998-02-12 21:22:28 +0000539\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000540\end{cfuncdesc}
541
Fred Drakee5bf8b21998-02-12 21:22:28 +0000542\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000543\end{cfuncdesc}
544
Fred Drakee5bf8b21998-02-12 21:22:28 +0000545\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *}
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *}
549\end{cfuncdesc}
550
551\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *}
552\end{cfuncdesc}
553
554\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int}
555\end{cfuncdesc}
556
557\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int}
558\end{cfuncdesc}
559
560\begin{cfuncdesc}{PyObject *}{PyRun_String}{char *, int, PyObject *, PyObject *}
561\end{cfuncdesc}
562
563\begin{cfuncdesc}{PyObject *}{PyRun_File}{FILE *, char *, int, PyObject *, PyObject *}
564\end{cfuncdesc}
565
566\begin{cfuncdesc}{PyObject *}{Py_CompileString}{char *, char *, int}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000567\end{cfuncdesc}
568
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000569
570\chapter{Reference Counting}
571
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000572The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000573of Python objects.
574
575\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
576Increment the reference count for object \code{o}. The object must
577not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000578\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000579\end{cfuncdesc}
580
581\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000582Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000583\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000584\end{cfuncdesc}
585
586\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000587Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000588not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000589\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
590object's type's deallocation function (which must not be \NULL{}) is
591invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000592
593\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000594code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000595method is deallocated). While exceptions in such code are not
596propagated, the executed code has free access to all Python global
597variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000598variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000599invoked. For example, code to delete an object from a list should
600copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000601the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000602temporary variable.
603\end{cfuncdesc}
604
605\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000606Decrement the reference count for object \var{o}. The object may be
607\NULL{}, in which case the macro has no effect; otherwise the effect
608is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000609applies.
610\end{cfuncdesc}
611
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000612The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000613\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
614\cfunction{_Py_NewReference()}, as well as the global variable
615\code{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000616
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000617XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
618PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
619PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
620
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000621
622\chapter{Exception Handling}
623
624The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000625exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000626Python exception handling. It works somewhat like the \UNIX{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000627\code{errno} variable: there is a global indicator (per thread) of the
628last error that occurred. Most functions don't clear this on success,
629but will set it to indicate the cause of the error on failure. Most
630functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000631supposed to return a pointer, or \code{-1} if they return an integer
632(exception: the \code{PyArg_Parse*()} functions return \code{1} for
633success and \code{0} for failure). When a function must fail because
634some function it called failed, it generally doesn't set the error
635indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000636
637The error indicator consists of three Python objects corresponding to
638the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
639\code{sys.exc_traceback}. API functions exist to interact with the
640error indicator in various ways. There is a separate error indicator
641for each thread.
642
643% XXX Order of these should be more thoughtful.
644% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000645
646\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000647Print a standard traceback to \code{sys.stderr} and clear the error
648indicator. Call this function only when the error indicator is set.
649(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000650\end{cfuncdesc}
651
Guido van Rossumae110af1997-05-22 20:11:52 +0000652\begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{}
653Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000654\emph{type} (the first argument to the last call to one of the
655\code{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
656not set, return \NULL{}. You do not own a reference to the return
657value, so you do not need to \cfunction{Py_DECREF()} it.
658\strong{Note:} do not compare the return value to a specific
659exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
660below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000661\end{cfuncdesc}
662
663\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000664\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000665Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000666\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000667This should only be called when an exception is actually set.
668\end{cfuncdesc}
669
670\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000671\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000672Return true if the \var{given} exception matches the exception in
673\var{exc}. If \var{exc} is a class object, this also returns true
674when \var{given} is a subclass. If \var{exc} is a tuple, all
675exceptions in the tuple (and recursively in subtuples) are searched
676for a match. This should only be called when an exception is actually
677set.
678\end{cfuncdesc}
679
680\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000681\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000682Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000683\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
684\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
685instance of the same class. This function can be used to instantiate
686the class in that case. If the values are already normalized, nothing
687happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000688\end{cfuncdesc}
689
690\begin{cfuncdesc}{void}{PyErr_Clear}{}
691Clear the error indicator. If the error indicator is not set, there
692is no effect.
693\end{cfuncdesc}
694
695\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
696Retrieve the error indicator into three variables whose addresses are
697passed. If the error indicator is not set, set all three variables to
698\NULL{}. If it is set, it will be cleared and you own a reference to
699each object retrieved. The value and traceback object may be \NULL{}
700even when the type object is not. \strong{Note:} this function is
701normally only used by code that needs to handle exceptions or by code
702that needs to save and restore the error indicator temporarily.
703\end{cfuncdesc}
704
705\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
706Set the error indicator from the three objects. If the error
707indicator is already set, it is cleared first. If the objects are
708\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
709and non-\NULL{} value or traceback. The exception type should be a
710string or class; if it is a class, the value should be an instance of
711that class. Do not pass an invalid exception type or value.
712(Violating these rules will cause subtle problems later.) This call
713takes away a reference to each object, i.e. you must own a reference
714to each object before the call and after the call you no longer own
715these references. (If you don't understand this, don't use this
716function. I warned you.) \strong{Note:} this function is normally
717only used by code that needs to save and restore the error indicator
718temporarily.
719\end{cfuncdesc}
720
721\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
722This is the most common way to set the error indicator. The first
723argument specifies the exception type; it is normally one of the
724standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
725increment its reference count. The second argument is an error
726message; it is converted to a string object.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000730This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000731specify an arbitrary Python object for the ``value'' of the exception.
732You need not increment its reference count.
733\end{cfuncdesc}
734
735\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000736This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000737\end{cfuncdesc}
738
739\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000740This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000741\var{message})}, where \var{message} indicates that a built-in operation
742was invoked with an illegal argument. It is mostly for internal use.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000746This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000747returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000748\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000749\end{cfuncdesc}
750
751\begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000752This is a convenience function to raise an exception when a \C{} library
753function has returned an error and set the \C{} variable \code{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000754It constructs a tuple object whose first item is the integer
755\code{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000756message (gotten from \cfunction{strerror()}), and then calls
757\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
758the \code{errno} value is \constant{EINTR}, indicating an interrupted
759system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000760the error indicator, leaves it set to that. The function always
761returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000762\samp{return PyErr_SetFromErrno();} when the system call returns an
763error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000764\end{cfuncdesc}
765
766\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000767This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000768\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000769operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000770argument. It is mostly for internal use.
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
774This function interacts with Python's signal handling. It checks
775whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000776corresponding signal handler. If the
777\module{signal}\refbimodindex{signal} module is supported, this can
778invoke a signal handler written in Python. In all cases, the default
779effect for \constant{SIGINT} is to raise the
780\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000781error indicator is set and the function returns \code{1}; otherwise
782the function returns \code{0}. The error indicator may or may not be
783cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000784\end{cfuncdesc}
785
786\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
787This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000788the effect of a \constant{SIGINT} signal arriving --- the next time
789\cfunction{PyErr_CheckSignals()} is called,
790\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000791\end{cfuncdesc}
792
Guido van Rossum42cefd01997-10-05 15:27:29 +0000793\begin{cfuncdesc}{PyObject *}{PyErr_NewException}{char *name,
794PyObject *base, PyObject *dict}
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000795\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000796This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000797\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000798of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000799arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000800object derived from the root for all exceptions, the built-in name
Fred Drakee058b4f1998-02-16 06:15:35 +0000801\exception{Exception} (accessible in \C{} as \code{PyExc_Exception}).
802In this case the \code{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000803first part (up to the last dot) of the \var{name} argument, and the
804class name is set to the last part (after the last dot). When the
805user has specified the \code{-X} command line option to use string
806exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000807argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000808created from the entire \var{name} argument is returned. The
809\var{base} argument can be used to specify an alternate base class.
810The \var{dict} argument can be used to specify a dictionary of class
811variables and methods.
812\end{cfuncdesc}
813
814
Guido van Rossumae110af1997-05-22 20:11:52 +0000815\section{Standard Exceptions}
816
817All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000818names are \samp{PyExc_} followed by the Python exception name.
819These have the type \code{PyObject *}; they are all either class
820objects or string objects, depending on the use of the \code{-X}
821option to the interpreter. For completeness, here are all the
822variables (the first four are new in Python 1.5a4):
Guido van Rossum42cefd01997-10-05 15:27:29 +0000823\code{PyExc_Exception},
824\code{PyExc_StandardError},
825\code{PyExc_ArithmeticError},
826\code{PyExc_LookupError},
Guido van Rossumae110af1997-05-22 20:11:52 +0000827\code{PyExc_AssertionError},
828\code{PyExc_AttributeError},
829\code{PyExc_EOFError},
830\code{PyExc_FloatingPointError},
831\code{PyExc_IOError},
832\code{PyExc_ImportError},
833\code{PyExc_IndexError},
834\code{PyExc_KeyError},
835\code{PyExc_KeyboardInterrupt},
836\code{PyExc_MemoryError},
837\code{PyExc_NameError},
838\code{PyExc_OverflowError},
839\code{PyExc_RuntimeError},
840\code{PyExc_SyntaxError},
841\code{PyExc_SystemError},
842\code{PyExc_SystemExit},
843\code{PyExc_TypeError},
844\code{PyExc_ValueError},
845\code{PyExc_ZeroDivisionError}.
846
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000847
848\chapter{Utilities}
849
850The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000851parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000852values.
853
Guido van Rossum42cefd01997-10-05 15:27:29 +0000854\section{OS Utilities}
855
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000856\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000857Return true (nonzero) if the standard I/O file \var{fp} with name
858\var{filename} is deemed interactive. This is the case for files for
859which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000860\code{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000861the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000862the strings \code{"<stdin>"} or \code{"???"}.
863\end{cfuncdesc}
864
865\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000866Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000867The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000868the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000869\end{cfuncdesc}
870
871
Fred Drakee5bf8b21998-02-12 21:22:28 +0000872\section{Process Control}
873
874\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
875Print a fatal error message and kill the process. No cleanup is
876performed. This function should only be invoked when a condition is
877detected that would make it dangerous to continue using the Python
878interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000879corrupted. On \UNIX{}, the standard \C{} library function
880\cfunction{abort()} is called which will attempt to produce a
881\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000882\end{cfuncdesc}
883
884\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000885Exit the current process. This calls \cfunction{Py_Finalize()} and
886then calls the standard \C{} library function
887\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000888\end{cfuncdesc}
889
890\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
891Register a cleanup function to be called by \cfunction{Py_Finalize()}.
892The cleanup function will be called with no arguments and should
893return no value. At most 32 cleanup functions can be registered.
894When the registration is successful, \cfunction{Py_AtExit()} returns
895\code{0}; on failure, it returns \code{-1}. The cleanup function
896registered last is called first. Each cleanup function will be called
897at most once. Since Python's internal finallization will have
898completed before the cleanup function, no Python APIs should be called
899by \var{func}.
900\end{cfuncdesc}
901
902
903\section{Importing Modules}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000904
905\begin{cfuncdesc}{PyObject *}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000906This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000907below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000908\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000909it specifies a submodule of a package), the \var{fromlist} argument is
910set to the list \code{['*']} so that the return value is the named
911module rather than the top-level package containing it as would
912otherwise be the case. (Unfortunately, this has an additional side
913effect when \var{name} in fact specifies a subpackage instead of a
914submodule: the submodules specified in the package's \code{__all__}
915variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000916or \NULL{} with an exception set on failure (the module may still
Guido van Rossum42cefd01997-10-05 15:27:29 +0000917be created in this case).
918\end{cfuncdesc}
919
920\begin{cfuncdesc}{PyObject *}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000921\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000922Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000923Python function \function{__import__()}\bifuncindex{__import__}, as
924the standard \function{__import__()} function calls this function
925directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000926
Guido van Rossum42cefd01997-10-05 15:27:29 +0000927The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000928top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000929(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000930\function{__import__()}, the return value when a submodule of a
931package was requested is normally the top-level package, unless a
932non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000933\end{cfuncdesc}
934
935\begin{cfuncdesc}{PyObject *}{PyImport_Import}{PyObject *name}
936This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000937function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000938\code{__builtins__} of the current globals. This means that the
939import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000940current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
941\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000942\end{cfuncdesc}
943
944\begin{cfuncdesc}{PyObject *}{PyImport_ReloadModule}{PyObject *m}
945Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000946Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000947\function{reload()} function calls this function directly. Return a
948new reference to the reloaded module, or \NULL{} with an exception set
949on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000950\end{cfuncdesc}
951
952\begin{cfuncdesc}{PyObject *}{PyImport_AddModule}{char *name}
953Return the module object corresponding to a module name. The
954\var{name} argument may be of the form \code{package.module}). First
955check the modules dictionary if there's one there, and if not, create
956a new one and insert in in the modules dictionary. Because the former
957action is most common, this does not return a new reference, and you
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000958do not own the returned reference. Return \NULL{} with an
Guido van Rossum42cefd01997-10-05 15:27:29 +0000959exception set on failure.
960\end{cfuncdesc}
961
962\begin{cfuncdesc}{PyObject *}{PyImport_ExecCodeModule}{char *name, PyObject *co}
963Given a module name (possibly of the form \code{package.module}) and a
964code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000965built-in function \function{compile()}\bifuncindex{compile}, load the
966module. Return a new reference to the module object, or \NULL{} with
967an exception set if an error occurred (the module may still be created
968in this case). (This function would reload the module if it was
969already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000970\end{cfuncdesc}
971
972\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000973Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
974and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000975first four bytes of the bytecode file, in little-endian byte order.
976\end{cfuncdesc}
977
978\begin{cfuncdesc}{PyObject *}{PyImport_GetModuleDict}{}
979Return the dictionary used for the module administration
980(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
981variable.
982\end{cfuncdesc}
983
984\begin{cfuncdesc}{void}{_PyImport_Init}{}
985Initialize the import mechanism. For internal use only.
986\end{cfuncdesc}
987
988\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
989Empty the module table. For internal use only.
990\end{cfuncdesc}
991
992\begin{cfuncdesc}{void}{_PyImport_Fini}{}
993Finalize the import mechanism. For internal use only.
994\end{cfuncdesc}
995
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000996\begin{cfuncdesc}{extern PyObject *}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000997For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000998\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000999
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001000\begin{cfuncdesc}{extern PyObject *}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001001For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001002\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001003
1004\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1005Load a frozen module. Return \code{1} for success, \code{0} if the
1006module is not found, and \code{-1} with an exception set if the
1007initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001008load, use \cfunction{PyImport_ImportModule()}.
1009(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001010already imported.)
1011\end{cfuncdesc}
1012
1013\begin{ctypedesc}{struct _frozen}
1014This is the structure type definition for frozen module descriptors,
1015as generated by the \code{freeze} utility (see \file{Tools/freeze/} in
1016the Python source distribution). Its definition is:
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001017\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001018struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001019 char *name;
1020 unsigned char *code;
1021 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001022};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001023\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001024\end{ctypedesc}
1025
1026\begin{cvardesc}{struct _frozen *}{PyImport_FrozenModules}
1027This pointer is initialized to point to an array of \code{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001028_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001029or zero. When a frozen module is imported, it is searched in this
1030table. Third party code could play tricks with this to provide a
1031dynamically created collection of frozen modules.
1032\end{cvardesc}
1033
1034
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001035\chapter{Abstract Objects Layer}
1036
1037The functions in this chapter interact with Python objects regardless
1038of their type, or with wide classes of object types (e.g. all
1039numerical types, or all sequence types). When used on object types
1040for which they do not apply, they will flag a Python exception.
1041
1042\section{Object Protocol}
1043
1044\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001045Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001046The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001047options. The only option currently supported is
1048\constant{Py_Print_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001049\end{cfuncdesc}
1050
1051\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001052Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1053\code{0} otherwise. This is equivalent to the Python expression
1054\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001055This function always succeeds.
1056\end{cfuncdesc}
1057
1058\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001059Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001060Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001061This is the equivalent of the Python expression
1062\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001063\end{cfuncdesc}
1064
1065
1066\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001067Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1068\code{0} otherwise. This is equivalent to the Python expression
1069\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001070This function always succeeds.
1071\end{cfuncdesc}
1072
1073
1074\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001075Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001076Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001077This is the equivalent of the Python expression
1078\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001079\end{cfuncdesc}
1080
1081
1082\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001083Set the value of the attribute named \var{attr_name}, for object
1084\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1085the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1086\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001087\end{cfuncdesc}
1088
1089
1090\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001091Set the value of the attribute named \var{attr_name}, for
1092object \var{o},
1093to the value \var{v}. Returns \code{-1} on failure. This is
1094the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1095\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001096\end{cfuncdesc}
1097
1098
1099\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001100Delete attribute named \var{attr_name}, for object \var{o}. Returns
1101\code{-1} on failure. This is the equivalent of the Python
1102statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001103\end{cfuncdesc}
1104
1105
1106\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001107Delete attribute named \var{attr_name}, for object \var{o}. Returns
1108\code{-1} on failure. This is the equivalent of the Python
1109statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001110\end{cfuncdesc}
1111
1112
1113\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001114Compare the values of \var{o1} and \var{o2} using a routine provided
1115by \var{o1}, if one exists, otherwise with a routine provided by
1116\var{o2}. The result of the comparison is returned in \var{result}.
1117Returns \code{-1} on failure. This is the equivalent of the Python
1118statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001119\end{cfuncdesc}
1120
1121
1122\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001123Compare the values of \var{o1} and \var{o2} using a routine provided
1124by \var{o1}, if one exists, otherwise with a routine provided by
1125\var{o2}. Returns the result of the comparison on success. On error,
1126the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1127detect an error. This is equivalent to the
1128Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001129\end{cfuncdesc}
1130
1131
1132\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001133Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001134string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001135the equivalent of the Python expression \samp{repr(\var{o})}.
1136Called by the \function{repr()}\bifuncindex{repr} built-in function
1137and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001138\end{cfuncdesc}
1139
1140
1141\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001142Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001143string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001144the equivalent of the Python expression \samp{str(\var{o})}.
1145Called by the \function{str()}\bifuncindex{str} built-in function and
1146by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001147\end{cfuncdesc}
1148
1149
1150\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001151Determine if the object \var{o}, is callable. Return \code{1} if the
1152object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001153This function always succeeds.
1154\end{cfuncdesc}
1155
1156
1157\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001158Call a callable Python object \var{callable_object}, with
1159arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001160needed, then args may be \NULL{}. Returns the result of the
1161call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001162of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001163\end{cfuncdesc}
1164
1165\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001166Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001167variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001168using a \cfunction{Py_BuildValue()} style format string. The format may
1169be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001170result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001171the equivalent of the Python expression \samp{apply(\var{o},
1172\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001173\end{cfuncdesc}
1174
1175
1176\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001177Call the method named \var{m} of object \var{o} with a variable number
1178of C arguments. The \C{} arguments are described by a
1179\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1180indicating that no arguments are provided. Returns the result of the
1181call on success, or \NULL{} on failure. This is the equivalent of the
1182Python expression \samp{\var{o}.\var{method}(\var{args})}.
1183Note that Special method names, such as \method{__add__()},
1184\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001185abstract-object routines for these must be used.
1186\end{cfuncdesc}
1187
1188
1189\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001190Compute and return the hash value of an object \var{o}. On
1191failure, return \code{-1}. This is the equivalent of the Python
1192expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001193\end{cfuncdesc}
1194
1195
1196\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001197Returns \code{1} if the object \var{o} is considered to be true, and
1198\code{0} otherwise. This is equivalent to the Python expression
1199\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001200This function always succeeds.
1201\end{cfuncdesc}
1202
1203
1204\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1205On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001206type of object \var{o}. On failure, returns \NULL{}. This is
1207equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001208\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001209\end{cfuncdesc}
1210
1211\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001212Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001213both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001214returned. On error, \code{-1} is returned. This is the equivalent
1215to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001216\end{cfuncdesc}
1217
1218
1219\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001220Return element of \var{o} corresponding to the object \var{key} or
1221\NULL{} on failure. This is the equivalent of the Python expression
1222\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001223\end{cfuncdesc}
1224
1225
1226\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001227Map the object \var{key} to the value \var{v}.
1228Returns \code{-1} on failure. This is the equivalent
1229of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001230\end{cfuncdesc}
1231
1232
1233\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001234Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1235failure. This is the equivalent of the Python statement \samp{del
1236\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001237\end{cfuncdesc}
1238
1239
1240\section{Number Protocol}
1241
1242\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001243Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001244false otherwise.
1245This function always succeeds.
1246\end{cfuncdesc}
1247
1248
1249\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001250Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1251failure. This is the equivalent of the Python expression
1252\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001253\end{cfuncdesc}
1254
1255
1256\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001257Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1258on failure. This is the equivalent of the Python expression
1259\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001260\end{cfuncdesc}
1261
1262
1263\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001264Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1265failure. This is the equivalent of the Python expression
1266\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001267\end{cfuncdesc}
1268
1269
1270\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001271Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1272failure.
1273This is the equivalent of the Python expression \samp{\var{o1} /
1274\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001275\end{cfuncdesc}
1276
1277
1278\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001279Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1280failure. This is the equivalent of the Python expression
1281\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001282\end{cfuncdesc}
1283
1284
1285\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001286See the built-in function \function{divmod()}\bifuncindex{divmod}.
1287Returns \NULL{} on failure. This is the equivalent of the Python
1288expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001289\end{cfuncdesc}
1290
1291
1292\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001293See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1294\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001295\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
1296If \var{o3} is to be ignored, pass \code{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001297\end{cfuncdesc}
1298
1299
1300\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001301Returns the negation of \var{o} on success, or \NULL{} on failure.
1302This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001303\end{cfuncdesc}
1304
1305
1306\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001307Returns \var{o} on success, or \NULL{} on failure.
1308This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001309\end{cfuncdesc}
1310
1311
1312\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001313Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1314the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001315\end{cfuncdesc}
1316
1317
1318\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001319Returns the bitwise negation of \var{o} on success, or \NULL{} on
1320failure. This is the equivalent of the Python expression
1321\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001322\end{cfuncdesc}
1323
1324
1325\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001326Returns the result of left shifting \var{o1} by \var{o2} on success,
1327or \NULL{} on failure. This is the equivalent of the Python
1328expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001329\end{cfuncdesc}
1330
1331
1332\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001333Returns the result of right shifting \var{o1} by \var{o2} on success,
1334or \NULL{} on failure. This is the equivalent of the Python
1335expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001336\end{cfuncdesc}
1337
1338
1339\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001340Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1341\NULL{} on failure. This is the equivalent of the Python
1342expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001343\end{cfuncdesc}
1344
1345
1346\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001347Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1348or \NULL{} on failure. This is the equivalent of the Python
1349expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001350\end{cfuncdesc}
1351
1352\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001353Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1354failure. This is the equivalent of the Python expression
1355\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001356\end{cfuncdesc}
1357
1358
Fred Drakee058b4f1998-02-16 06:15:35 +00001359\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001360This function takes the addresses of two variables of type
1361\code{PyObject*}.
1362
Fred Drakee058b4f1998-02-16 06:15:35 +00001363If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1364have the same type, increment their reference count and return
1365\code{0} (success). If the objects can be converted to a common
1366numeric type, replace \code{*p1} and \code{*p2} by their converted
1367value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001368If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001369return \code{-1} (failure) and don't increment the reference counts.
1370The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1371Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1372\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001373\end{cfuncdesc}
1374
1375
1376\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001377Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001378\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001379expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001380\end{cfuncdesc}
1381
1382
1383\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001384Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001385or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001386expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001387\end{cfuncdesc}
1388
1389
1390\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001391Returns the \var{o} converted to a float object on success, or \NULL{}
1392on failure. This is the equivalent of the Python expression
1393\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001394\end{cfuncdesc}
1395
1396
Fred Drakef44617d1998-02-12 20:57:15 +00001397\section{Sequence Protocol}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001398
1399\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001400Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001401otherwise.
1402This function always succeeds.
1403\end{cfuncdesc}
1404
1405
1406\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001407Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001408failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001409expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001410\end{cfuncdesc}
1411
1412
1413\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001414Return the result of repeating sequence object \var{o} \var{count}
1415times, or \NULL{} on failure. This is the equivalent of the Python
1416expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001417\end{cfuncdesc}
1418
1419
1420\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001421Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1422is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001423\end{cfuncdesc}
1424
1425
1426\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001427Return the slice of sequence object \var{o} between \var{i1} and
1428\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1429expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001430\end{cfuncdesc}
1431
1432
1433\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001434Assign object \var{v} to the \var{i}th element of \var{o}.
1435Returns \code{-1} on failure. This is the equivalent of the Python
1436statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001437\end{cfuncdesc}
1438
1439\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001440Delete the \var{i}th element of object \var{v}. Returns
1441\code{-1} on failure. This is the equivalent of the Python
1442statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001443\end{cfuncdesc}
1444
1445\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001446Assign the sequence object \var{v} to the slice in sequence
1447object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1448the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001449\end{cfuncdesc}
1450
1451\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001452Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1453Returns \code{-1} on failure. This is the equivalent of the Python
1454statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001455\end{cfuncdesc}
1456
1457\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001458Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1459This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001460\end{cfuncdesc}
1461
1462\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001463Return the number of occurrences of \var{value} in \var{o}, that is,
1464return the number of keys for which \code{\var{o}[\var{key}] ==
1465\var{value}}. On failure, return \code{-1}. This is equivalent to
1466the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001467\end{cfuncdesc}
1468
1469\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001470Determine if \var{o} contains \var{value}. If an item in \var{o} is
1471equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1472error, return \code{-1}. This is equivalent to the Python expression
1473\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001474\end{cfuncdesc}
1475
1476\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001477Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1478\var{value}}. On error, return \code{-1}. This is equivalent to
1479the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001480\end{cfuncdesc}
1481
Fred Drakef44617d1998-02-12 20:57:15 +00001482\section{Mapping Protocol}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001483
1484\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001485Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001486otherwise.
1487This function always succeeds.
1488\end{cfuncdesc}
1489
1490
1491\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001492Returns the number of keys in object \var{o} on success, and \code{-1}
1493on failure. For objects that do not provide sequence protocol,
1494this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001495\end{cfuncdesc}
1496
1497
1498\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001499Remove the mapping for object \var{key} from the object \var{o}.
1500Return \code{-1} on failure. This is equivalent to
1501the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001502\end{cfuncdesc}
1503
1504
1505\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001506Remove the mapping for object \var{key} from the object \var{o}.
1507Return \code{-1} on failure. This is equivalent to
1508the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001509\end{cfuncdesc}
1510
1511
1512\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001513On success, return \code{1} if the mapping object has the key \var{key}
1514and \code{0} otherwise. This is equivalent to the Python expression
1515\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001516This function always succeeds.
1517\end{cfuncdesc}
1518
1519
1520\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001521Return \code{1} if the mapping object has the key \var{key} and
1522\code{0} otherwise. This is equivalent to the Python expression
1523\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001524This function always succeeds.
1525\end{cfuncdesc}
1526
1527
1528\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001529On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001530failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001531expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001532\end{cfuncdesc}
1533
1534
1535\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001536On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001537failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001538expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001539\end{cfuncdesc}
1540
1541
1542\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001543On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001544each item is a tuple containing a key-value pair. On
1545failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001546expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001547\end{cfuncdesc}
1548
1549\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001550Make object \var{o} empty. Returns \code{1} on success and \code{0}
1551on failure. This is equivalent to the Python statement
1552\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001553\end{cfuncdesc}
1554
1555
1556\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001557Return element of \var{o} corresponding to the object \var{key} or
1558\NULL{} on failure. This is the equivalent of the Python expression
1559\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001560\end{cfuncdesc}
1561
1562\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001563Map the object \var{key} to the value \var{v} in object \var{o}.
1564Returns \code{-1} on failure. This is the equivalent of the Python
1565statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001566\end{cfuncdesc}
1567
1568
1569\section{Constructors}
1570
1571\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1572On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001573file given by \var{file_name}, with a file mode given by \var{mode},
1574where \var{mode} has the same semantics as the standard \C{} routine
1575\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001576\end{cfuncdesc}
1577
1578\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001579Return a new file object for an already opened standard \C{} file
1580pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1581\var{mode}, must be provided as well as a flag, \var{close_on_del},
1582that indicates whether the file is to be closed when the file object
1583is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001584\end{cfuncdesc}
1585
1586\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001587Returns a new float object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001588\NULL{} on failure.
1589\end{cfuncdesc}
1590
1591\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001592Returns a new int object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001593\NULL{} on failure.
1594\end{cfuncdesc}
1595
Fred Drakee058b4f1998-02-16 06:15:35 +00001596\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1597Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001598failure.
1599\end{cfuncdesc}
1600
1601\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001602Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001603\NULL{} on failure.
1604\end{cfuncdesc}
1605
1606\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001607Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001608\NULL{} on failure.
1609\end{cfuncdesc}
1610
1611\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1612Returns a new empty dictionary on success, and \NULL{} on
1613failure.
1614\end{cfuncdesc}
1615
1616\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001617Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001618\NULL{} on failure.
1619\end{cfuncdesc}
1620
Fred Drakee058b4f1998-02-16 06:15:35 +00001621\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1622Returns a new string object with the value \var{v} and length
1623\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1624the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001625\end{cfuncdesc}
1626
Fred Drakee058b4f1998-02-16 06:15:35 +00001627\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1628Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001629failure.
1630\end{cfuncdesc}
1631
1632
1633\chapter{Concrete Objects Layer}
1634
1635The functions in this chapter are specific to certain Python object
1636types. Passing them an object of the wrong type is not a good idea;
1637if you receive an object from a Python program and you are not sure
1638that it has the right type, you must perform a type check first;
1639e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001640\cfunction{PyDict_Check()}. The chapter is structured like the
1641``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001642
1643
Fred Drakee5bf8b21998-02-12 21:22:28 +00001644\section{Fundamental Objects}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001645
Fred Drakee5bf8b21998-02-12 21:22:28 +00001646This section describes Python type objects and the singleton object
1647\code{None}.
1648
1649
1650\subsection{Type Objects}
1651
1652\begin{ctypedesc}{PyTypeObject}
1653
1654\end{ctypedesc}
1655
1656\begin{cvardesc}{PyObject *}{PyType_Type}
1657
1658\end{cvardesc}
1659
1660
1661\subsection{The None Object}
1662
1663\begin{cvardesc}{PyObject *}{Py_None}
1664XXX macro
1665\end{cvardesc}
1666
1667
1668\section{Sequence Objects}
1669
1670Generic operations on sequence objects were discussed in the previous
1671chapter; this section deals with the specific kinds of sequence
1672objects that are intrinsic to the Python language.
1673
1674
1675\subsection{String Objects}
1676
1677\begin{ctypedesc}{PyStringObject}
1678This subtype of \code{PyObject} represents a Python string object.
1679\end{ctypedesc}
1680
1681\begin{cvardesc}{PyTypeObject}{PyString_Type}
1682This instance of \code{PyTypeObject} represents the Python string type.
1683\end{cvardesc}
1684
1685\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
1686
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001687\end{cfuncdesc}
1688
Fred Drakee5bf8b21998-02-12 21:22:28 +00001689\begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int}
1690
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001691\end{cfuncdesc}
1692
Fred Drakee5bf8b21998-02-12 21:22:28 +00001693\begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *}
1694
Guido van Rossumae110af1997-05-22 20:11:52 +00001695\end{cfuncdesc}
1696
Fred Drakee5bf8b21998-02-12 21:22:28 +00001697\begin{cfuncdesc}{int}{PyString_Size}{PyObject *}
1698
Guido van Rossumae110af1997-05-22 20:11:52 +00001699\end{cfuncdesc}
1700
Fred Drakee5bf8b21998-02-12 21:22:28 +00001701\begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *}
1702
1703\end{cfuncdesc}
1704
1705\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *}
1706
1707\end{cfuncdesc}
1708
1709\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *}
1710
1711\end{cfuncdesc}
1712
1713\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int}
1714
1715\end{cfuncdesc}
1716
1717\begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *}
1718
1719\end{cfuncdesc}
1720
1721\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **}
1722
1723\end{cfuncdesc}
1724
1725\begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *}
1726
1727\end{cfuncdesc}
1728
1729\begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *}
1730
1731\end{cfuncdesc}
1732
1733\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *}
1734
1735\end{cfuncdesc}
1736
1737
1738\subsection{Tuple Objects}
1739
1740\begin{ctypedesc}{PyTupleObject}
1741This subtype of \code{PyObject} represents a Python tuple object.
1742\end{ctypedesc}
1743
1744\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1745This instance of \code{PyTypeObject} represents the Python tuple type.
1746\end{cvardesc}
1747
1748\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1749Return true if the argument is a tuple object.
1750\end{cfuncdesc}
1751
1752\begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001753Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001754\end{cfuncdesc}
1755
1756\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001757Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001758of that tuple.
1759\end{cfuncdesc}
1760
1761\begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001762Returns the object at position \var{pos} in the tuple pointed
1763to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
1764raises an \exception{IndexError} exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001765\end{cfuncdesc}
1766
1767\begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001768Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001769\end{cfuncdesc}
1770
1771\begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p,
1772 int low,
1773 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001774Takes a slice of the tuple pointed to by \var{p} from
1775\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001776\end{cfuncdesc}
1777
1778\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1779 int pos,
1780 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001781Inserts a reference to object \var{o} at position \var{pos} of
1782the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001783\end{cfuncdesc}
1784
1785\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1786 int pos,
1787 PyObject *o}
1788
Fred Drakee058b4f1998-02-16 06:15:35 +00001789Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001790should \emph{only} be used to fill in brand new tuples.
1791\end{cfuncdesc}
1792
1793\begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p,
1794 int new,
1795 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001796Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001797\emph{supposed} to be immutable, this should only be used if there is only
1798one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001799already be known to some other part of the code. \var{last_is_sticky} is
1800a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001801it will grow or shrink at the end. Think of this as destroying the old
1802tuple and creating a new one, only more efficiently.
1803\end{cfuncdesc}
1804
1805
1806\subsection{List Objects}
1807
1808\begin{ctypedesc}{PyListObject}
1809This subtype of \code{PyObject} represents a Python list object.
1810\end{ctypedesc}
1811
1812\begin{cvardesc}{PyTypeObject}{PyList_Type}
1813This instance of \code{PyTypeObject} represents the Python list type.
1814\end{cvardesc}
1815
1816\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001817Returns true if its argument is a \code{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001818\end{cfuncdesc}
1819
1820\begin{cfuncdesc}{PyObject *}{PyList_New}{int size}
1821
1822\end{cfuncdesc}
1823
1824\begin{cfuncdesc}{int}{PyList_Size}{PyObject *}
1825
1826\end{cfuncdesc}
1827
1828\begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int}
1829
1830\end{cfuncdesc}
1831
1832\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *}
1833
1834\end{cfuncdesc}
1835
1836\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *}
1837
1838\end{cfuncdesc}
1839
1840\begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *}
1841
1842\end{cfuncdesc}
1843
1844\begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int}
1845
1846\end{cfuncdesc}
1847
1848\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *}
1849
1850\end{cfuncdesc}
1851
1852\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *}
1853
1854\end{cfuncdesc}
1855
1856\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *}
1857
1858\end{cfuncdesc}
1859
1860\begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *}
1861
1862\end{cfuncdesc}
1863
1864\begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i}
1865
1866\end{cfuncdesc}
1867
1868\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1869
1870\end{cfuncdesc}
1871
1872
1873\section{Mapping Objects}
1874
1875\subsection{Dictionary Objects}
1876
1877\begin{ctypedesc}{PyDictObject}
1878This subtype of \code{PyObject} represents a Python dictionary object.
1879\end{ctypedesc}
1880
1881\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1882This instance of \code{PyTypeObject} represents the Python dictionary type.
1883\end{cvardesc}
1884
1885\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001886Returns true if its argument is a \code{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001887\end{cfuncdesc}
1888
1889\begin{cfuncdesc}{PyDictObject *}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001890Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001891\end{cfuncdesc}
1892
1893\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001894Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001895\end{cfuncdesc}
1896
1897\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1898 PyObject *key,
1899 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001900Inserts \var{value} into the dictionary with a key of \var{key}. Both
1901\var{key} and \var{value} should be PyObjects, and \var{key} should be
1902hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001903\end{cfuncdesc}
1904
1905\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1906 char *key,
1907 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001908Inserts \var{value} into the dictionary using \var{key}
1909as a key. \var{key} should be a \code{char *}. The key object is
1910created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001911\end{cfuncdesc}
1912
1913\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001914Removes the entry in dictionary \var{p} with key \var{key}.
1915\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001916\end{cfuncdesc}
1917
1918\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001919Removes the entry in dictionary \var{p} which has a key
1920specified by the \code{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001921\end{cfuncdesc}
1922
1923\begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001924Returns the object from dictionary \var{p} which has a key
1925\var{key}. Returns \NULL{} if the key \var{key} is not present.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001926\end{cfuncdesc}
1927
1928\begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001929Does the same, but \var{key} is specified as a
Fred Drakee5bf8b21998-02-12 21:22:28 +00001930\code{char *}, rather than a \code{PyObject *}.
1931\end{cfuncdesc}
1932
1933\begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001934Returns a \code{PyListObject} containing all the items
1935from the dictionary, as in the mapping method \method{items()} (see
1936the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001937\end{cfuncdesc}
1938
1939\begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001940Returns a \code{PyListObject} containing all the keys
1941from the dictionary, as in the mapping method \method{keys()} (see the
1942\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001943\end{cfuncdesc}
1944
1945\begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001946Returns a \code{PyListObject} containing all the values
1947from the dictionary \var{p}, as in the mapping method
1948\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00001949\end{cfuncdesc}
1950
1951\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001952Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001953\end{cfuncdesc}
1954
1955\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
1956 int ppos,
1957 PyObject **pkey,
1958 PyObject **pvalue}
1959
1960\end{cfuncdesc}
1961
1962
1963\section{Numeric Objects}
1964
1965\subsection{Plain Integer Objects}
1966
1967\begin{ctypedesc}{PyIntObject}
1968This subtype of \code{PyObject} represents a Python integer object.
1969\end{ctypedesc}
1970
1971\begin{cvardesc}{PyTypeObject}{PyInt_Type}
1972This instance of \code{PyTypeObject} represents the Python plain
1973integer type.
1974\end{cvardesc}
1975
1976\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
1977
1978\end{cfuncdesc}
1979
1980\begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00001981Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001982
1983The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00001984integers between \code{-1} and \code{100}, when you create an int in
1985that range you actually just get back a reference to the existing
1986object. So it should be possible to change the value of \code{1}. I
1987suspect the behaviour of python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00001988\end{cfuncdesc}
1989
1990\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00001991Returns the value of the object \var{io}. No error checking is
1992performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001993\end{cfuncdesc}
1994
1995\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00001996Will first attempt to cast the object to a \code{PyIntObject}, if
1997it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001998\end{cfuncdesc}
1999
2000\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002001Returns the systems idea of the largest integer it can handle
2002(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002003\end{cfuncdesc}
2004
2005
2006\subsection{Long Integer Objects}
2007
2008\begin{ctypedesc}{PyLongObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002009This subtype of \code{PyObject} represents a Python long integer
2010object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002011\end{ctypedesc}
2012
2013\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002014This instance of \code{PyTypeObject} represents the Python long
2015integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002016\end{cvardesc}
2017
2018\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002019Returns true if its argument is a \code{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002020\end{cfuncdesc}
2021
2022\begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long}
2023
2024\end{cfuncdesc}
2025
2026\begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long}
2027
2028\end{cfuncdesc}
2029
2030\begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double}
2031
2032\end{cfuncdesc}
2033
2034\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *}
2035
2036\end{cfuncdesc}
2037
2038\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject }
2039
2040\end{cfuncdesc}
2041
2042\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *}
2043
2044\end{cfuncdesc}
2045
2046\begin{cfuncdesc}{PyObject *}{PyLong_FromString}{char *, char **, int}
2047
2048\end{cfuncdesc}
2049
2050
2051\subsection{Floating Point Objects}
2052
2053\begin{ctypedesc}{PyFloatObject}
Fred Drakee058b4f1998-02-16 06:15:35 +00002054This subtype of \code{PyObject} represents a Python floating point
2055object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002056\end{ctypedesc}
2057
2058\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002059This instance of \code{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002060point type.
2061\end{cvardesc}
2062
2063\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002064Returns true if its argument is a \code{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002065\end{cfuncdesc}
2066
2067\begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double}
2068
2069\end{cfuncdesc}
2070
2071\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *}
2072
2073\end{cfuncdesc}
2074
2075\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *}
2076
2077\end{cfuncdesc}
2078
2079
2080\subsection{Complex Number Objects}
2081
2082\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002083The \C{} structure which corresponds to the value portion of a Python
2084complex number object. Most of the functions for dealing with complex
2085number objects use structures of this type as input or output values,
2086as appropriate. It is defined as:
2087
Fred Drakee058b4f1998-02-16 06:15:35 +00002088\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002089typedef struct {
2090 double real;
2091 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002092} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002093\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002094\end{ctypedesc}
2095
2096\begin{ctypedesc}{PyComplexObject}
2097This subtype of \code{PyObject} represents a Python complex number object.
2098\end{ctypedesc}
2099
2100\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2101This instance of \code{PyTypeObject} represents the Python complex
2102number type.
2103\end{cvardesc}
2104
2105\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002106Returns true if its argument is a \code{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002107\end{cfuncdesc}
2108
2109\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex}
2110
2111\end{cfuncdesc}
2112
2113\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex}
2114
2115\end{cfuncdesc}
2116
2117\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex}
2118
2119\end{cfuncdesc}
2120
2121\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex}
2122
2123\end{cfuncdesc}
2124
2125\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex}
2126
2127\end{cfuncdesc}
2128
2129\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex}
2130
2131\end{cfuncdesc}
2132
2133\begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex}
2134
2135\end{cfuncdesc}
2136
2137\begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag}
2138
2139\end{cfuncdesc}
2140
2141\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
2142
2143\end{cfuncdesc}
2144
2145\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
2146
2147\end{cfuncdesc}
2148
2149\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
2150
2151\end{cfuncdesc}
2152
2153
2154
2155\section{Other Objects}
2156
2157\subsection{File Objects}
2158
2159\begin{ctypedesc}{PyFileObject}
2160This subtype of \code{PyObject} represents a Python file object.
2161\end{ctypedesc}
2162
2163\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2164This instance of \code{PyTypeObject} represents the Python file type.
2165\end{cvardesc}
2166
2167\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002168Returns true if its argument is a \code{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002169\end{cfuncdesc}
2170
2171\begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode}
Fred Drakee058b4f1998-02-16 06:15:35 +00002172Creates a new \code{PyFileObject} pointing to the file
2173specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002174\end{cfuncdesc}
2175
2176\begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp,
2177 char *name, char *mode, int (*close})
Fred Drakee058b4f1998-02-16 06:15:35 +00002178Creates a new \code{PyFileObject} from the already-open \var{fp}.
2179The function \var{close} will be called when the file should be
2180closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002181\end{cfuncdesc}
2182
2183\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002184Returns the file object associated with \var{p} as a \code{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002185\end{cfuncdesc}
2186
2187\begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n}
2188undocumented as yet
2189\end{cfuncdesc}
2190
2191\begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002192Returns the name of the file specified by \var{p} as a
2193\code{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002194\end{cfuncdesc}
2195
2196\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002197Available on systems with \cfunction{setvbuf()} only. This should
2198only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002199\end{cfuncdesc}
2200
2201\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakee058b4f1998-02-16 06:15:35 +00002202Sets the \code{softspace} attribute of \var{p} to \var{newflag}.
2203Returns the previosu value. This function clears any errors, and will
2204return \code{0} as the previous value if the attribute either does not
2205exist or if there were errors in retrieving it. There is no way to
2206detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002207\end{cfuncdesc}
2208
2209\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002210Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002211\end{cfuncdesc}
2212
2213\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002214Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002215\end{cfuncdesc}
2216
2217
2218\subsection{CObjects}
2219
2220XXX
2221
2222
Guido van Rossum4a944d71997-08-14 20:35:38 +00002223\chapter{Initialization, Finalization, and Threads}
2224
Guido van Rossum4a944d71997-08-14 20:35:38 +00002225\begin{cfuncdesc}{void}{Py_Initialize}{}
2226Initialize the Python interpreter. In an application embedding
2227Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002228functions; with the exception of \cfunction{Py_SetProgramName()},
2229\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2230and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2231loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002232modules \module{__builtin__}\refbimodindex{__builtin__},
2233\module{__main__}\refbimodindex{__main__} and
2234\module{sys}\refbimodindex{sys}. It also initializes the module
2235search path (\code{sys.path}).%
2236\indexiii{module}{search}{path}
2237It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2238that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002239\cfunction{Py_Finalize()} first). There is no return value; it is a
2240fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002241\end{cfuncdesc}
2242
2243\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002244\strong{(NEW in 1.5a4!)}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002245Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002246initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2247called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002248again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002249\end{cfuncdesc}
2250
2251\begin{cfuncdesc}{void}{Py_Finalize}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002252\strong{(NEW in 1.5a3!)}
Fred Drakee058b4f1998-02-16 06:15:35 +00002253Undo all initializations made by \cfunction{Py_Initialize()} and
2254subsequent use of Python/C API functions, and destroy all
2255sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2256created and not yet destroyed since the last call to
2257\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2258by the Python interpreter. This is a no-op when called for a second
2259time (without calling \cfunction{Py_Initialize()} again first). There
2260is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002261
2262This function is provided for a number of reasons. An embedding
2263application might want to restart Python without having to restart the
2264application itself. An application that has loaded the Python
2265interpreter from a dynamically loadable library (or DLL) might want to
2266free all memory allocated by Python before unloading the DLL. During a
2267hunt for memory leaks in an application a developer might want to free
2268all memory allocated by Python before exiting from the application.
2269
Fred Drakee058b4f1998-02-16 06:15:35 +00002270\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002271modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002272(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002273(even functions) or modules. Dynamically loaded extension modules
2274loaded by Python are not unloaded. Small amounts of memory allocated
2275by the Python interpreter may not be freed (if you find a leak, please
2276report it). Memory tied up in circular references between objects is
2277not freed. Some memory allocated by extension modules may not be
2278freed. Some extension may not work properly if their initialization
2279routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002280calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2281than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002282\end{cfuncdesc}
2283
2284\begin{cfuncdesc}{PyThreadState *}{Py_NewInterpreter}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002285\strong{(NEW in 1.5a3!)}
Fred Drake4de05a91998-02-16 14:25:26 +00002286Create a new sub-interpreter. This is an (almost) totally separate
2287environment for the execution of Python code. In particular, the new
2288interpreter has separate, independent versions of all imported
2289modules, including the fundamental modules
2290\module{__builtin__}\refbimodindex{__builtin__},
2291\module{__main__}\refbimodindex{__main__} and
2292\module{sys}\refbimodindex{sys}. The table of loaded modules
2293(\code{sys.modules}) and the module search path (\code{sys.path}) are
2294also separate. The new environment has no \code{sys.argv} variable.
2295It has new standard I/O stream file objects \code{sys.stdin},
2296\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakeb0a78731998-01-13 18:51:10 +00002297same underlying \code{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002298
2299The return value points to the first thread state created in the new
2300sub-interpreter. This thread state is made the current thread state.
2301Note that no actual thread is created; see the discussion of thread
2302states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002303\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002304is stored in the current thread state and there may not be a current
2305thread state. (Like all other Python/C API functions, the global
2306interpreter lock must be held before calling this function and is
2307still held when it returns; however, unlike most other Python/C API
2308functions, there needn't be a current thread state on entry.)
2309
2310Extension modules are shared between (sub-)interpreters as follows:
2311the first time a particular extension is imported, it is initialized
2312normally, and a (shallow) copy of its module's dictionary is
2313squirreled away. When the same extension is imported by another
2314(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002315contents of this copy; the extension's \code{init} function is not
2316called. Note that this is different from what happens when an
2317extension is imported after the interpreter has been completely
2318re-initialized by calling \cfunction{Py_Finalize()} and
2319\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002320function \emph{is} called again.
2321
Fred Drakee058b4f1998-02-16 06:15:35 +00002322\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002323interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002324isn't perfect --- for example, using low-level file operations like
Guido van Rossum4a944d71997-08-14 20:35:38 +00002325\code{os.close()} they can (accidentally or maliciously) affect each
2326other's open files. Because of the way extensions are shared between
2327(sub-)interpreters, some extensions may not work properly; this is
2328especially likely when the extension makes use of (static) global
2329variables, or when the extension manipulates its module's dictionary
2330after its initialization. It is possible to insert objects created in
2331one sub-interpreter into a namespace of another sub-interpreter; this
2332should be done with great care to avoid sharing user-defined
2333functions, methods, instances or classes between sub-interpreters,
2334since import operations executed by such objects may affect the
2335wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2336a hard-to-fix bug that will be addressed in a future release.)
2337\end{cfuncdesc}
2338
2339\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002340\strong{(NEW in 1.5a3!)}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002341Destroy the (sub-)interpreter represented by the given thread state.
2342The given thread state must be the current thread state. See the
2343discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002344thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002345interpreted are destroyed. (The global interpreter lock must be held
2346before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002347\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002348been explicitly destroyed at that point.
2349\end{cfuncdesc}
2350
2351\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002352\strong{(NEW in 1.5a3!)}
Fred Drakee058b4f1998-02-16 06:15:35 +00002353This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002354for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002355the value of the \code{argv[0]} argument to the \cfunction{main()} function
2356of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002357functions below to find the Python run-time libraries relative to the
2358interpreter executable. The default value is \code{"python"}. The
2359argument should point to a zero-terminated character string in static
2360storage whose contents will not change for the duration of the
2361program's execution. No code in the Python interpreter will change
2362the contents of this storage.
2363\end{cfuncdesc}
2364
2365\begin{cfuncdesc}{char *}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002366Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002367default. The returned string points into static storage; the caller
2368should not modify its value.
2369\end{cfuncdesc}
2370
2371\begin{cfuncdesc}{char *}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002372Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002373is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002374set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002375for example, if the program name is \code{"/usr/local/bin/python"},
2376the prefix is \code{"/usr/local"}. The returned string points into
2377static storage; the caller should not modify its value. This
2378corresponds to the \code{prefix} variable in the top-level
Fred Drakee058b4f1998-02-16 06:15:35 +00002379\file{Makefile} and the \code{--prefix} argument to the
2380\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002381Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002382also the next function.
2383\end{cfuncdesc}
2384
2385\begin{cfuncdesc}{char *}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002386Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002387files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002388program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002389variables; for example, if the program name is
2390\code{"/usr/local/bin/python"}, the exec-prefix is
2391\code{"/usr/local"}. The returned string points into static storage;
2392the caller should not modify its value. This corresponds to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002393\code{exec_prefix} variable in the top-level \file{Makefile} and the
2394\code{--exec_prefix} argument to the \program{configure} script at build
Guido van Rossum4a944d71997-08-14 20:35:38 +00002395time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002396\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002397
2398Background: The exec-prefix differs from the prefix when platform
2399dependent files (such as executables and shared libraries) are
2400installed in a different directory tree. In a typical installation,
2401platform dependent files may be installed in the
2402\code{"/usr/local/plat"} subtree while platform independent may be
2403installed in \code{"/usr/local"}.
2404
2405Generally speaking, a platform is a combination of hardware and
2406software families, e.g. Sparc machines running the Solaris 2.x
2407operating system are considered the same platform, but Intel machines
2408running Solaris 2.x are another platform, and Intel machines running
2409Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002410operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002411operating systems are a different story; the installation strategies
2412on those systems are so different that the prefix and exec-prefix are
2413meaningless, and set to the empty string. Note that compiled Python
2414bytecode files are platform independent (but not independent from the
2415Python version by which they were compiled!).
2416
Fred Drakee058b4f1998-02-16 06:15:35 +00002417System administrators will know how to configure the \program{mount} or
2418\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002419while having \code{"/usr/local/plat"} be a different filesystem for each
2420platform.
2421\end{cfuncdesc}
2422
2423\begin{cfuncdesc}{char *}{Py_GetProgramFullPath}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002424\strong{(NEW in 1.5a3!)}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002425Return the full program name of the Python executable; this is
2426computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002427from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002428returned string points into static storage; the caller should not
2429modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002430\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002431\end{cfuncdesc}
2432
2433\begin{cfuncdesc}{char *}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002434\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002435Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002436program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002437environment variables. The returned string consists of a series of
2438directory names separated by a platform dependent delimiter character.
Fred Drakee5bc4971998-02-12 23:36:49 +00002439The delimiter character is \code{':'} on \UNIX{}, \code{';'} on
2440DOS/Windows, and \code{'\\n'} (the \ASCII{} newline character) on
2441Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002442should not modify its value. The value is available to Python code
2443as the list \code{sys.path}, which may be modified to change the
2444future search path for loaded modules.
2445
2446% XXX should give the exact rules
2447\end{cfuncdesc}
2448
2449\begin{cfuncdesc}{const char *}{Py_GetVersion}{}
2450Return the version of this Python interpreter. This is a string that
2451looks something like
2452
Guido van Rossum09270b51997-08-15 18:57:32 +00002453\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002454"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002455\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002456
2457The first word (up to the first space character) is the current Python
2458version; the first three characters are the major and minor version
2459separated by a period. The returned string points into static storage;
2460the caller should not modify its value. The value is available to
2461Python code as the list \code{sys.version}.
2462\end{cfuncdesc}
2463
2464\begin{cfuncdesc}{const char *}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002465Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002466this is formed from the ``official'' name of the operating system,
2467converted to lower case, followed by the major revision number; e.g.,
2468for Solaris 2.x, which is also known as SunOS 5.x, the value is
2469\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2470is \code{"win"}. The returned string points into static storage;
2471the caller should not modify its value. The value is available to
2472Python code as \code{sys.platform}.
2473\end{cfuncdesc}
2474
2475\begin{cfuncdesc}{const char *}{Py_GetCopyright}{}
2476Return the official copyright string for the current Python version,
2477for example
2478
2479\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2480
2481The returned string points into static storage; the caller should not
2482modify its value. The value is available to Python code as the list
2483\code{sys.copyright}.
2484\end{cfuncdesc}
2485
2486\begin{cfuncdesc}{const char *}{Py_GetCompiler}{}
2487Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002488version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002489
Fred Drakee058b4f1998-02-16 06:15:35 +00002490\begin{verbatim}
2491"[GCC 2.7.2.2]"
2492\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002493
2494The returned string points into static storage; the caller should not
2495modify its value. The value is available to Python code as part of
2496the variable \code{sys.version}.
2497\end{cfuncdesc}
2498
2499\begin{cfuncdesc}{const char *}{Py_GetBuildInfo}{}
2500Return information about the sequence number and build date and time
2501of the current Python interpreter instance, for example
2502
Guido van Rossum09270b51997-08-15 18:57:32 +00002503\begin{verbatim}
2504"#67, Aug 1 1997, 22:34:28"
2505\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002506
2507The returned string points into static storage; the caller should not
2508modify its value. The value is available to Python code as part of
2509the variable \code{sys.version}.
2510\end{cfuncdesc}
2511
2512\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2513% XXX
2514\end{cfuncdesc}
2515
2516% XXX Other PySys thingies (doesn't really belong in this chapter)
2517
2518\section{Thread State and the Global Interpreter Lock}
2519
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002520The Python interpreter is not fully thread safe. In order to support
2521multi-threaded Python programs, there's a global lock that must be
2522held by the current thread before it can safely access Python objects.
2523Without the lock, even the simplest operations could cause problems in
2524a multi-threaded proram: for example, when two threads simultaneously
2525increment the reference count of the same object, the reference count
2526could end up being incremented only once instead of twice.
2527
2528Therefore, the rule exists that only the thread that has acquired the
2529global interpreter lock may operate on Python objects or call Python/C
2530API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002531the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002532default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002533\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002534reacquired around potentially blocking I/O operations like reading or
2535writing a file, so that other threads can run while the thread that
2536requests the I/O is waiting for the I/O operation to complete.
2537
2538The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002539separate per thread --- for this it uses a data structure called
2540\code{PyThreadState}. This is new in Python 1.5; in earlier versions,
2541such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002542cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002543when the application uses \function{sys.exc_info()} to access the
2544exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002545
2546There's one global variable left, however: the pointer to the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002547\code{PyThreadState} structure. While most thread packages have a way
2548to store ``per-thread global data'', Python's internal platform
2549independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002550the current thread state must be manipulated explicitly.
2551
2552This is easy enough in most cases. Most code manipulating the global
2553interpreter lock has the following simple structure:
2554
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002555\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002556Save the thread state in a local variable.
2557Release the interpreter lock.
2558...Do some blocking I/O operation...
2559Reacquire the interpreter lock.
2560Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002561\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002562
2563This is so common that a pair of macros exists to simplify it:
2564
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002565\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002566Py_BEGIN_ALLOW_THREADS
2567...Do some blocking I/O operation...
2568Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002569\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002570
Fred Drakee058b4f1998-02-16 06:15:35 +00002571The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2572a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2573the block. Another advantage of using these two macros is that when
2574Python is compiled without thread support, they are defined empty,
2575thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002576
2577When thread support is enabled, the block above expands to the
2578following code:
2579
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002580\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002581{
2582 PyThreadState *_save;
2583 _save = PyEval_SaveThread();
2584 ...Do some blocking I/O operation...
2585 PyEval_RestoreThread(_save);
2586}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002587\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002588
2589Using even lower level primitives, we can get roughly the same effect
2590as follows:
2591
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002592\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002593{
2594 PyThreadState *_save;
2595 _save = PyThreadState_Swap(NULL);
2596 PyEval_ReleaseLock();
2597 ...Do some blocking I/O operation...
2598 PyEval_AcquireLock();
2599 PyThreadState_Swap(_save);
2600}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002601\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002602
2603There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002604\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002605global variable \code{errno}, since the lock manipulation does not
2606guarantee that \code{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002607is disabled, \cfunction{PyEval_SaveThread()} and
2608\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2609case, \cfunction{PyEval_ReleaseLock()} and
2610\cfunction{PyEval_AcquireLock()} are not available. This is done so
2611that dynamically loaded extensions compiled with thread support
2612enabled can be loaded by an interpreter that was compiled with
2613disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002614
2615The global interpreter lock is used to protect the pointer to the
2616current thread state. When releasing the lock and saving the thread
2617state, the current thread state pointer must be retrieved before the
2618lock is released (since another thread could immediately acquire the
2619lock and store its own thread state in the global variable).
2620Reversely, when acquiring the lock and restoring the thread state, the
2621lock must be acquired before storing the thread state pointer.
2622
2623Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002624threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002625lock, nor is there a thread state data structure for them. Such
2626threads must bootstrap themselves into existence, by first creating a
2627thread state data structure, then acquiring the lock, and finally
2628storing their thread state pointer, before they can start using the
2629Python/C API. When they are done, they should reset the thread state
2630pointer, release the lock, and finally free their thread state data
2631structure.
2632
2633When creating a thread data structure, you need to provide an
2634interpreter state data structure. The interpreter state data
2635structure hold global data that is shared by all threads in an
2636interpreter, for example the module administration
2637(\code{sys.modules}). Depending on your needs, you can either create
2638a new interpreter state data structure, or share the interpreter state
2639data structure used by the Python main thread (to access the latter,
2640you must obtain the thread state and access its \code{interp} member;
2641this must be done by a thread that is created by Python or by the main
2642thread after Python is initialized).
2643
2644XXX More?
2645
2646\begin{ctypedesc}{PyInterpreterState}
2647\strong{(NEW in 1.5a3!)}
2648This data structure represents the state shared by a number of
2649cooperating threads. Threads belonging to the same interpreter
2650share their module administration and a few other internal items.
2651There are no public members in this structure.
2652
2653Threads belonging to different interpreters initially share nothing,
2654except process state like available memory, open file descriptors and
2655such. The global interpreter lock is also shared by all threads,
2656regardless of to which interpreter they belong.
2657\end{ctypedesc}
2658
2659\begin{ctypedesc}{PyThreadState}
2660\strong{(NEW in 1.5a3!)}
2661This data structure represents the state of a single thread. The only
2662public data member is \code{PyInterpreterState *interp}, which points
2663to this thread's interpreter state.
2664\end{ctypedesc}
2665
2666\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2667Initialize and acquire the global interpreter lock. It should be
2668called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002669in any other thread operations such as
2670\cfunction{PyEval_ReleaseLock()} or
2671\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2672calling \cfunction{PyEval_SaveThread()} or
2673\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002674
2675This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002676this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002677
2678When only the main thread exists, no lock operations are needed. This
2679is a common situation (most Python programs do not use threads), and
2680the lock operations slow the interpreter down a bit. Therefore, the
2681lock is not created initially. This situation is equivalent to having
2682acquired the lock: when there is only a single thread, all object
2683accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002684lock, it also acquires it. Before the Python
2685\module{thread}\refbimodindex{thread} module creates a new thread,
2686knowing that either it has the lock or the lock hasn't been created
2687yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2688returns, it is guaranteed that the lock has been created and that it
2689has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002690
2691It is \strong{not} safe to call this function when it is unknown which
2692thread (if any) currently has the global interpreter lock.
2693
2694This function is not available when thread support is disabled at
2695compile time.
2696\end{cfuncdesc}
2697
Guido van Rossum4a944d71997-08-14 20:35:38 +00002698\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002699\strong{(NEW in 1.5a3!)}
2700Acquire the global interpreter lock. The lock must have been created
2701earlier. If this thread already has the lock, a deadlock ensues.
2702This function is not available when thread support is disabled at
2703compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002704\end{cfuncdesc}
2705
2706\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002707\strong{(NEW in 1.5a3!)}
2708Release the global interpreter lock. The lock must have been created
2709earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002710disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002711\end{cfuncdesc}
2712
2713\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002714\strong{(NEW in 1.5a3!)}
2715Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002716state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002717have been created earlier. If this thread already has the lock,
2718deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002719is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002720\end{cfuncdesc}
2721
2722\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002723\strong{(NEW in 1.5a3!)}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002724Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002725interpreter lock. The lock must have been created earlier and must be
2726held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002727be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002728thread state --- if it isn't, a fatal error is reported. This
2729function is not available when thread support is disabled at compile
2730time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002731\end{cfuncdesc}
2732
2733\begin{cfuncdesc}{PyThreadState *}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002734\strong{(Different return type in 1.5a3!)}
2735Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002736support is enabled) and reset the thread state to \NULL{},
2737returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002738the lock has been created, the current thread must have acquired it.
2739(This function is available even when thread support is disabled at
2740compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002741\end{cfuncdesc}
2742
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002743\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
2744\strong{(Different argument type in 1.5a3!)}
2745Acquire the interpreter lock (if it has been created and thread
2746support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002747must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002748thread must not have acquired it, otherwise deadlock ensues. (This
2749function is available even when thread support is disabled at compile
2750time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002751\end{cfuncdesc}
2752
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002753% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2754\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2755This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002756\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002757Note that it contains an opening brace; it must be matched with a
2758following \code{Py_END_ALLOW_THREADS} macro. See above for further
2759discussion of this macro. It is a no-op when thread support is
2760disabled at compile time.
2761\end{ctypedesc}
2762
2763\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2764This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002765\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002766Note that it contains a closing brace; it must be matched with an
2767earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2768discussion of this macro. It is a no-op when thread support is
2769disabled at compile time.
2770\end{ctypedesc}
2771
2772\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002773This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002774is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2775brace. It is a no-op when thread support is disabled at compile
2776time.
2777\end{ctypedesc}
2778
2779\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002780This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002781equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2782and variable declaration. It is a no-op when thread support is
2783disabled at compile time.
2784\end{ctypedesc}
2785
2786All of the following functions are only available when thread support
2787is enabled at compile time, and must be called only when the
2788interpreter lock has been created. They are all new in 1.5a3.
2789
2790\begin{cfuncdesc}{PyInterpreterState *}{PyInterpreterState_New}{}
2791Create a new interpreter state object. The interpreter lock must be
2792held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002793\end{cfuncdesc}
2794
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002795\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2796Reset all information in an interpreter state object. The interpreter
2797lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002798\end{cfuncdesc}
2799
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002800\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2801Destroy an interpreter state object. The interpreter lock need not be
2802held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002803call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002804\end{cfuncdesc}
2805
2806\begin{cfuncdesc}{PyThreadState *}{PyThreadState_New}{PyInterpreterState *interp}
2807Create a new thread state object belonging to the given interpreter
2808object. The interpreter lock must be held.
2809\end{cfuncdesc}
2810
2811\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2812Reset all information in a thread state object. The interpreter lock
2813must be held.
2814\end{cfuncdesc}
2815
2816\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2817Destroy a thread state object. The interpreter lock need not be
2818held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002819call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002820\end{cfuncdesc}
2821
2822\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Get}{}
2823Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002824When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002825error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002826\end{cfuncdesc}
2827
2828\begin{cfuncdesc}{PyThreadState *}{PyThreadState_Swap}{PyThreadState *tstate}
2829Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002830argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002831must be held.
2832\end{cfuncdesc}
2833
2834
Fred Drakee058b4f1998-02-16 06:15:35 +00002835\chapter{Defining New Object Types}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002836
Fred Drakee058b4f1998-02-16 06:15:35 +00002837\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
2838\end{cfuncdesc}
2839
2840\begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size}
2841\end{cfuncdesc}
2842
2843\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2844\end{cfuncdesc}
2845
2846\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2847\end{cfuncdesc}
2848
Guido van Rossumae110af1997-05-22 20:11:52 +00002849
2850PyObject, PyVarObject
2851
2852PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2853
2854Typedefs:
2855unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2856intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2857getreadbufferproc, getwritebufferproc, getsegcountproc,
2858destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2859setattrofunc, cmpfunc, reprfunc, hashfunc
2860
2861PyNumberMethods
2862
2863PySequenceMethods
2864
2865PyMappingMethods
2866
2867PyBufferProcs
2868
2869PyTypeObject
2870
2871DL_IMPORT
2872
2873PyType_Type
2874
2875Py*_Check
2876
2877Py_None, _Py_NoneStruct
2878
Guido van Rossumae110af1997-05-22 20:11:52 +00002879
Fred Drakee5bf8b21998-02-12 21:22:28 +00002880\chapter{Debugging}
Guido van Rossumae110af1997-05-22 20:11:52 +00002881
Fred Drakee5bf8b21998-02-12 21:22:28 +00002882XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002883
2884
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002885\input{api.ind} % Index -- must be last
2886
2887\end{document}