blob: 02e31c925bb40b47a6fa50bcd7470df0a06b1dcb [file] [log] [blame]
Fred Drake6659c301998-03-03 22:02:19 +00001\documentclass{manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002
Guido van Rossum9faf4c51997-10-07 14:38:54 +00003\title{Python/C API Reference Manual}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00004
5\input{boilerplate}
6
7\makeindex % tell \index to actually write the .idx file
8
9
10\begin{document}
11
Guido van Rossum9231c8f1997-05-15 21:43:21 +000012\maketitle
13
Fred Drake9f86b661998-07-28 21:55:19 +000014\ifhtml
15\chapter*{Front Matter\label{front}}
16\fi
17
Guido van Rossum9231c8f1997-05-15 21:43:21 +000018\input{copyright}
19
20\begin{abstract}
21
22\noindent
Fred Drakee058b4f1998-02-16 06:15:35 +000023This manual documents the API used by \C{} (or \Cpp{}) programmers who
24want to write extension modules or embed Python. It is a companion to
25\emph{Extending and Embedding the Python Interpreter}, which describes
Guido van Rossum9231c8f1997-05-15 21:43:21 +000026the general principles of extension writing but does not document the
27API functions in detail.
28
Guido van Rossum5b8a5231997-12-30 04:38:44 +000029\strong{Warning:} The current version of this document is incomplete.
30I hope that it is nevertheless useful. I will continue to work on it,
31and release new versions from time to time, independent from Python
32source code releases.
33
Guido van Rossum9231c8f1997-05-15 21:43:21 +000034\end{abstract}
35
Fred Drake4d4f9e71998-01-13 22:25:02 +000036\tableofcontents
Guido van Rossum9231c8f1997-05-15 21:43:21 +000037
Guido van Rossum5060b3b1997-08-17 18:02:23 +000038% XXX Consider moving all this back to ext.tex and giving api.tex
39% XXX a *really* short intro only.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000040
Fred Drakeefd146c1999-02-15 15:30:45 +000041\chapter{Introduction \label{intro}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000042
Fred Drakeb0a78731998-01-13 18:51:10 +000043The Application Programmer's Interface to Python gives \C{} and \Cpp{}
Guido van Rossum59a61351997-08-14 20:34:33 +000044programmers access to the Python interpreter at a variety of levels.
Fred Drakeb0a78731998-01-13 18:51:10 +000045The API is equally usable from \Cpp{}, but for brevity it is generally
46referred to as the Python/\C{} API. There are two fundamentally
Fred Drakee058b4f1998-02-16 06:15:35 +000047different reasons for using the Python/\C{} API. The first reason is
48to write \emph{extension modules} for specific purposes; these are
49\C{} modules that extend the Python interpreter. This is probably the
50most common use. The second reason is to use Python as a component in
51a larger application; this technique is generally referred to as
52\dfn{embedding} Python in an application.
Guido van Rossum59a61351997-08-14 20:34:33 +000053
Guido van Rossum4a944d71997-08-14 20:35:38 +000054Writing an extension module is a relatively well-understood process,
55where a ``cookbook'' approach works well. There are several tools
56that automate the process to some extent. While people have embedded
57Python in other applications since its early existence, the process of
58embedding Python is less straightforward that writing an extension.
59Python 1.5 introduces a number of new API functions as well as some
60changes to the build process that make embedding much simpler.
Fred Drakec6fa34e1998-04-02 06:47:24 +000061This manual describes the \version\ state of affairs.
Guido van Rossum59a61351997-08-14 20:34:33 +000062% XXX Eventually, take the historical notes out
63
Guido van Rossum4a944d71997-08-14 20:35:38 +000064Many API functions are useful independent of whether you're embedding
65or extending Python; moreover, most applications that embed Python
66will need to provide a custom extension as well, so it's probably a
67good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000068attempting to embed Python in a real application.
69
Fred Drakeefd146c1999-02-15 15:30:45 +000070
71\section{Include Files \label{includes}}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000072
73All function, type and macro definitions needed to use the Python/C
74API are included in your code by the following line:
75
Fred Drakee058b4f1998-02-16 06:15:35 +000076\begin{verbatim}
77#include "Python.h"
78\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000079
Fred Drakee058b4f1998-02-16 06:15:35 +000080This implies inclusion of the following standard headers:
81\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
82\code{<stdlib.h>} (if available).
Guido van Rossum580aa8d1997-11-25 15:34:51 +000083
84All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000085the included standard headers) have one of the prefixes \samp{Py} or
86\samp{_Py}. Names beginning with \samp{_Py} are for internal use
Guido van Rossum580aa8d1997-11-25 15:34:51 +000087only. Structure member names do not have a reserved prefix.
88
Fred Drakee058b4f1998-02-16 06:15:35 +000089\strong{Important:} user code should never define names that begin
90with \samp{Py} or \samp{_Py}. This confuses the reader, and
91jeopardizes the portability of the user code to future Python
92versions, which may define additional names beginning with one of
93these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000094
Fred Drakeefd146c1999-02-15 15:30:45 +000095
96\section{Objects, Types and Reference Counts \label{objects}}
Guido van Rossum59a61351997-08-14 20:34:33 +000097
Guido van Rossum580aa8d1997-11-25 15:34:51 +000098Most Python/C API functions have one or more arguments as well as a
Fred Drakef8830d11998-04-23 14:06:01 +000099return value of type \ctype{PyObject *}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +0000100to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000101object. Since all Python object types are treated the same way by the
102Python language in most situations (e.g., assignments, scope rules,
103and argument passing), it is only fitting that they should be
Fred Drakeb0a78731998-01-13 18:51:10 +0000104represented by a single \C{} type. All Python objects live on the heap:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000105you never declare an automatic or static variable of type
Fred Drakef8830d11998-04-23 14:06:01 +0000106\ctype{PyObject}, only pointer variables of type \ctype{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +0000107be declared.
108
Fred Drakee058b4f1998-02-16 06:15:35 +0000109All Python objects (even Python integers) have a \dfn{type} and a
110\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000111it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakee058b4f1998-02-16 06:15:35 +0000112many more as explained in the \emph{Python Reference Manual}). For
Guido van Rossum4a944d71997-08-14 20:35:38 +0000113each of the well-known types there is a macro to check whether an
Fred Drakee058b4f1998-02-16 06:15:35 +0000114object is of that type; for instance, \samp{PyList_Check(\var{a})} is
115true iff the object pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000116
Fred Drakeefd146c1999-02-15 15:30:45 +0000117
118\subsection{Reference Counts \label{refcounts}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000119
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000120The reference count is important because today's computers have a
Fred Drake003d8da1998-04-13 00:53:42 +0000121finite (and often severely limited) memory size; it counts how many
Guido van Rossum4a944d71997-08-14 20:35:38 +0000122different places there are that have a reference to an object. Such a
Fred Drakeb0a78731998-01-13 18:51:10 +0000123place could be another object, or a global (or static) \C{} variable, or
124a local variable in some \C{} function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000125becomes zero, the object is deallocated. If it contains references to
126other objects, their reference count is decremented. Those other
127objects may be deallocated in turn, if this decrement makes their
128reference count become zero, and so on. (There's an obvious problem
129with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000130``don't do that''.)
131
Guido van Rossum4a944d71997-08-14 20:35:38 +0000132Reference counts are always manipulated explicitly. The normal way is
Fred Drakee058b4f1998-02-16 06:15:35 +0000133to use the macro \cfunction{Py_INCREF()} to increment an object's
134reference count by one, and \cfunction{Py_DECREF()} to decrement it by
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000135one. The decref macro is considerably more complex than the incref one,
Guido van Rossum4a944d71997-08-14 20:35:38 +0000136since it must check whether the reference count becomes zero and then
137cause the object's deallocator, which is a function pointer contained
138in the object's type structure. The type-specific deallocator takes
139care of decrementing the reference counts for other objects contained
140in the object, and so on, if this is a compound object type such as a
141list. There's no chance that the reference count can overflow; at
142least as many bits are used to hold the reference count as there are
143distinct memory locations in virtual memory (assuming
144\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000145increment is a simple operation.
146
Guido van Rossum4a944d71997-08-14 20:35:38 +0000147It is not necessary to increment an object's reference count for every
148local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000149object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000150point to it and it goes down by one when the variable goes out of
151scope. However, these two cancel each other out, so at the end the
152reference count hasn't changed. The only real reason to use the
153reference count is to prevent the object from being deallocated as
154long as our variable is pointing to it. If we know that there is at
155least one other reference to the object that lives at least as long as
156our variable, there is no need to increment the reference count
157temporarily. An important situation where this arises is in objects
Fred Drakeb0a78731998-01-13 18:51:10 +0000158that are passed as arguments to \C{} functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000159that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000160reference to every argument for the duration of the call.
161
Fred Drakee058b4f1998-02-16 06:15:35 +0000162However, a common pitfall is to extract an object from a list and
163hold on to it for a while without incrementing its reference count.
164Some other operation might conceivably remove the object from the
165list, decrementing its reference count and possible deallocating it.
166The real danger is that innocent-looking operations may invoke
167arbitrary Python code which could do this; there is a code path which
168allows control to flow back to the user from a \cfunction{Py_DECREF()},
169so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000170
Guido van Rossum4a944d71997-08-14 20:35:38 +0000171A safe approach is to always use the generic operations (functions
Fred Drakee058b4f1998-02-16 06:15:35 +0000172whose name begins with \samp{PyObject_}, \samp{PyNumber_},
173\samp{PySequence_} or \samp{PyMapping_}). These operations always
Guido van Rossum4a944d71997-08-14 20:35:38 +0000174increment the reference count of the object they return. This leaves
Fred Drakee058b4f1998-02-16 06:15:35 +0000175the caller with the responsibility to call \cfunction{Py_DECREF()}
176when they are done with the result; this soon becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000177
Fred Drakeefd146c1999-02-15 15:30:45 +0000178
179\subsubsection{Reference Count Details \label{refcountDetails}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000180
181The reference count behavior of functions in the Python/C API is best
182expelained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000183talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000184shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000185properly --- either by passing ownership on (usually to its caller) or
186by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
187a function passes ownership of a reference on to its caller, the
188caller is said to receive a \emph{new} reference. When no ownership
189is transferred, the caller is said to \emph{borrow} the reference.
190Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000191
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000192Conversely, when calling a function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000193object, there are two possibilities: the function \emph{steals} a
194reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000195references; the two notable exceptions are
196\cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which
197steal a reference to the item (but not to the tuple or list into which
Fred Drake003d8da1998-04-13 00:53:42 +0000198the item is put!). These functions were designed to steal a reference
Fred Drakee058b4f1998-02-16 06:15:35 +0000199because of a common idiom for populating a tuple or list with newly
200created objects; for example, the code to create the tuple \code{(1,
2012, "three")} could look like this (forgetting about error handling for
202the moment; a better way to code this is shown below anyway):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000203
204\begin{verbatim}
205PyObject *t;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000206
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000207t = PyTuple_New(3);
208PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
209PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
210PyTuple_SetItem(t, 2, PyString_FromString("three"));
211\end{verbatim}
212
Fred Drakee058b4f1998-02-16 06:15:35 +0000213Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
214set tuple items; \cfunction{PySequence_SetItem()} and
215\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
216immutable data type. You should only use
217\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000218yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000219
220Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000221\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
222can also use \cfunction{PySequence_SetItem()}; this illustrates the
223difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000224
225\begin{verbatim}
226PyObject *l, *x;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000227
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000228l = PyList_New(3);
229x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000230PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000231x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000232PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000233x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000234PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000235\end{verbatim}
236
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000237You might find it strange that the ``recommended'' approach takes more
238code. However, in practice, you will rarely use these ways of
239creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000240\cfunction{Py_BuildValue()}, that can create most common objects from
241\C{} values, directed by a \dfn{format string}. For example, the
242above two blocks of code could be replaced by the following (which
243also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000244
245\begin{verbatim}
246PyObject *t, *l;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000247
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000248t = Py_BuildValue("(iis)", 1, 2, "three");
249l = Py_BuildValue("[iis]", 1, 2, "three");
250\end{verbatim}
251
Fred Drakee058b4f1998-02-16 06:15:35 +0000252It is much more common to use \cfunction{PyObject_SetItem()} and
253friends with items whose references you are only borrowing, like
254arguments that were passed in to the function you are writing. In
255that case, their behaviour regarding reference counts is much saner,
256since you don't have to increment a reference count so you can give a
257reference away (``have it be stolen''). For example, this function
258sets all items of a list (actually, any mutable sequence) to a given
259item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000260
261\begin{verbatim}
262int set_all(PyObject *target, PyObject *item)
263{
264 int i, n;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000265
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000266 n = PyObject_Length(target);
267 if (n < 0)
268 return -1;
269 for (i = 0; i < n; i++) {
270 if (PyObject_SetItem(target, i, item) < 0)
271 return -1;
272 }
273 return 0;
274}
275\end{verbatim}
276
277The situation is slightly different for function return values.
278While passing a reference to most functions does not change your
279ownership responsibilities for that reference, many functions that
280return a referece to an object give you ownership of the reference.
281The reason is simple: in many cases, the returned object is created
282on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000283object. Therefore, the generic functions that return object
284references, like \cfunction{PyObject_GetItem()} and
285\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
286the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000287
288It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000289by a function depends on which function you call only --- \emph{the
290plumage} (i.e., the type of the type of the object passed as an
291argument to the function) \emph{doesn't enter into it!} Thus, if you
292extract an item from a list using \cfunction{PyList_GetItem()}, you
293don't own the reference --- but if you obtain the same item from the
294same list using \cfunction{PySequence_GetItem()} (which happens to
295take exactly the same arguments), you do own a reference to the
296returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000297
Fred Drakee058b4f1998-02-16 06:15:35 +0000298Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000299sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000300\cfunction{PyList_GetItem()}, once using
301\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000302
303\begin{verbatim}
304long sum_list(PyObject *list)
305{
306 int i, n;
307 long total = 0;
308 PyObject *item;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000309
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000310 n = PyList_Size(list);
311 if (n < 0)
312 return -1; /* Not a list */
313 for (i = 0; i < n; i++) {
314 item = PyList_GetItem(list, i); /* Can't fail */
315 if (!PyInt_Check(item)) continue; /* Skip non-integers */
316 total += PyInt_AsLong(item);
317 }
318 return total;
319}
320\end{verbatim}
321
322\begin{verbatim}
323long sum_sequence(PyObject *sequence)
324{
325 int i, n;
326 long total = 0;
327 PyObject *item;
328 n = PyObject_Size(list);
329 if (n < 0)
330 return -1; /* Has no length */
331 for (i = 0; i < n; i++) {
332 item = PySequence_GetItem(list, i);
333 if (item == NULL)
334 return -1; /* Not a sequence, or other failure */
335 if (PyInt_Check(item))
336 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000337 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000338 }
339 return total;
340}
341\end{verbatim}
342
Fred Drakeefd146c1999-02-15 15:30:45 +0000343
344\subsection{Types \label{types}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000345
346There are few other data types that play a significant role in
Fred Drakef8830d11998-04-23 14:06:01 +0000347the Python/C API; most are simple \C{} types such as \ctype{int},
348\ctype{long}, \ctype{double} and \ctype{char *}. A few structure types
Guido van Rossum4a944d71997-08-14 20:35:38 +0000349are used to describe static tables used to list the functions exported
350by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000351be discussed together with the functions that use them.
352
Fred Drakeefd146c1999-02-15 15:30:45 +0000353
354\section{Exceptions \label{exceptions}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000355
Guido van Rossum4a944d71997-08-14 20:35:38 +0000356The Python programmer only needs to deal with exceptions if specific
357error handling is required; unhandled exceptions are automatically
358propagated to the caller, then to the caller's caller, and so on, till
359they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000360user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000361
Fred Drakeb0a78731998-01-13 18:51:10 +0000362For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000363All functions in the Python/C API can raise exceptions, unless an
364explicit claim is made otherwise in a function's documentation. In
365general, when a function encounters an error, it sets an exception,
366discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000367error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000368return a Boolean true/false result, with false indicating an error.
369Very few functions return no explicit error indicator or have an
370ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000371\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000372
373Exception state is maintained in per-thread storage (this is
374equivalent to using global storage in an unthreaded application). A
Fred Drakec6fa34e1998-04-02 06:47:24 +0000375thread can be in one of two states: an exception has occurred, or not.
Fred Drakee058b4f1998-02-16 06:15:35 +0000376The function \cfunction{PyErr_Occurred()} can be used to check for
377this: it returns a borrowed reference to the exception type object
378when an exception has occurred, and \NULL{} otherwise. There are a
379number of functions to set the exception state:
380\cfunction{PyErr_SetString()} is the most common (though not the most
381general) function to set the exception state, and
382\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000383
384The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000385be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000386value, and the traceback. These have the same meanings as the Python
387object \code{sys.exc_type}, \code{sys.exc_value},
388\code{sys.exc_traceback}; however, they are not the same: the Python
389objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000390\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
391exception state only exists while an exception is being passed on
392between \C{} functions until it reaches the Python interpreter, which
393takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000394
Fred Drakec6fa34e1998-04-02 06:47:24 +0000395Note that starting with Python 1.5, the preferred, thread-safe way to
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000396access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000397\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000398for Python code. Also, the semantics of both ways to access the
399exception state have changed so that a function which catches an
400exception will save and restore its thread's exception state so as to
401preserve the exception state of its caller. This prevents common bugs
402in exception handling code caused by an innocent-looking function
403overwriting the exception being handled; it also reduces the often
404unwanted lifetime extension for objects that are referenced by the
Fred Drakec6fa34e1998-04-02 06:47:24 +0000405stack frames in the traceback.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000406
407As a general principle, a function that calls another function to
408perform some task should check whether the called function raised an
409exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000410should discard any object references that it owns, and returns an
411error indicator, but it should \emph{not} set another exception ---
412that would overwrite the exception that was just raised, and lose
413important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000414
415A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000416in the \cfunction{sum_sequence()} example above. It so happens that
417that example doesn't need to clean up any owned references when it
418detects an error. The following example function shows some error
419cleanup. First, to remind you why you like Python, we show the
420equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000421
422\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000423def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000424 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000425 item = dict[key]
426 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000427 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000428 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000429\end{verbatim}
430
Fred Drakeb0a78731998-01-13 18:51:10 +0000431Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000432
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000433\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000434int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000435{
436 /* Objects all initialized to NULL for Py_XDECREF */
437 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000438 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000439
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000440 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000441 if (item == NULL) {
Fred Drakec6fa34e1998-04-02 06:47:24 +0000442 /* Handle KeyError only: */
443 if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000444
445 /* Clear the error and use zero: */
446 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000447 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000448 if (item == NULL) goto error;
449 }
450
451 const_one = PyInt_FromLong(1L);
452 if (const_one == NULL) goto error;
453
454 incremented_item = PyNumber_Add(item, const_one);
455 if (incremented_item == NULL) goto error;
456
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000457 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000458 rv = 0; /* Success */
459 /* Continue with cleanup code */
460
461 error:
462 /* Cleanup code, shared by success and failure path */
463
464 /* Use Py_XDECREF() to ignore NULL references */
465 Py_XDECREF(item);
466 Py_XDECREF(const_one);
467 Py_XDECREF(incremented_item);
468
469 return rv; /* -1 for error, 0 for success */
470}
471\end{verbatim}
472
Fred Drakef8830d11998-04-23 14:06:01 +0000473This example represents an endorsed use of the \keyword{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000474in \C{}! It illustrates the use of
475\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
476handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
477dispose of owned references that may be \NULL{} (note the \samp{X} in
478the name; \cfunction{Py_DECREF()} would crash when confronted with a
479\NULL{} reference). It is important that the variables used to hold
480owned references are initialized to \NULL{} for this to work;
481likewise, the proposed return value is initialized to \code{-1}
482(failure) and only set to success after the final call made is
483successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000484
Guido van Rossum59a61351997-08-14 20:34:33 +0000485
Fred Drakeefd146c1999-02-15 15:30:45 +0000486\section{Embedding Python \label{embedding}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000487
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000488The one important task that only embedders (as opposed to extension
489writers) of the Python interpreter have to worry about is the
490initialization, and possibly the finalization, of the Python
491interpreter. Most functionality of the interpreter can only be used
492after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000493
Fred Drakee058b4f1998-02-16 06:15:35 +0000494The basic initialization function is \cfunction{Py_Initialize()}.
495This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000496fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
497\module{__main__}\refbimodindex{__main__} and
498\module{sys}\refbimodindex{sys}. It also initializes the module
Fred Drakec6fa34e1998-04-02 06:47:24 +0000499search path (\code{sys.path}).%
500\indexiii{module}{search}{path}
Guido van Rossum59a61351997-08-14 20:34:33 +0000501
Fred Drakee058b4f1998-02-16 06:15:35 +0000502\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000503(\code{sys.argv}). If this variable is needed by Python code that
504will be executed later, it must be set explicitly with a call to
505\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000506to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000507
Fred Drakeb0a78731998-01-13 18:51:10 +0000508On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000509details are slightly different), \cfunction{Py_Initialize()}
510calculates the module search path based upon its best guess for the
511location of the standard Python interpreter executable, assuming that
512the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000513interpreter executable. In particular, it looks for a directory named
Fred Drake2de75ec1998-04-09 14:12:11 +0000514\file{lib/python1.5} (replacing \file{1.5} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000515interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000516executable named \file{python} is found on the shell command search
Fred Drakec6fa34e1998-04-02 06:47:24 +0000517path (the environment variable \envvar{PATH}).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000518
519For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000520\file{/usr/local/bin/python}, it will assume that the libraries are in
Fred Drake2de75ec1998-04-09 14:12:11 +0000521\file{/usr/local/lib/python1.5}. (In fact, this particular path
Fred Drakee058b4f1998-02-16 06:15:35 +0000522is also the ``fallback'' location, used when no executable file named
Fred Drakec6fa34e1998-04-02 06:47:24 +0000523\file{python} is found along \envvar{PATH}.) The user can override
524this behavior by setting the environment variable \envvar{PYTHONHOME},
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000525or insert additional directories in front of the standard path by
Fred Drakec6fa34e1998-04-02 06:47:24 +0000526setting \envvar{PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000527
Guido van Rossum4a944d71997-08-14 20:35:38 +0000528The embedding application can steer the search by calling
529\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakec6fa34e1998-04-02 06:47:24 +0000530\cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still
531overrides this and \envvar{PYTHONPATH} is still inserted in front of
Fred Drakee058b4f1998-02-16 06:15:35 +0000532the standard path. An application that requires total control has to
533provide its own implementation of \cfunction{Py_GetPath()},
534\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
535\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000536\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000537
Guido van Rossum4a944d71997-08-14 20:35:38 +0000538Sometimes, it is desirable to ``uninitialize'' Python. For instance,
539the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000540\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000541use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000542can be accomplished by calling \cfunction{Py_Finalize()}. The function
543\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000544initialized state. More information about these functions is given in
545a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000546
Guido van Rossum4a944d71997-08-14 20:35:38 +0000547
Fred Drakeefd146c1999-02-15 15:30:45 +0000548\chapter{The Very High Level Layer \label{veryhigh}}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000549
Fred Drakee5bf8b21998-02-12 21:22:28 +0000550The functions in this chapter will let you execute Python source code
551given in a file or a buffer, but they will not let you interact in a
552more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000553
Fred Drakec6fa34e1998-04-02 06:47:24 +0000554\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000555\end{cfuncdesc}
556
Fred Drakec6fa34e1998-04-02 06:47:24 +0000557\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000558\end{cfuncdesc}
559
Fred Drakec6fa34e1998-04-02 06:47:24 +0000560\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000561\end{cfuncdesc}
562
Fred Drakec6fa34e1998-04-02 06:47:24 +0000563\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000564\end{cfuncdesc}
565
Fred Drakec6fa34e1998-04-02 06:47:24 +0000566\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000567\end{cfuncdesc}
568
Fred Drakec6fa34e1998-04-02 06:47:24 +0000569\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str,
570 int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000571\end{cfuncdesc}
572
Fred Drakec6fa34e1998-04-02 06:47:24 +0000573\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp,
574 char *filename, int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000575\end{cfuncdesc}
576
Fred Drakec6fa34e1998-04-02 06:47:24 +0000577\begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start,
578 PyObject *globals,
579 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000580\end{cfuncdesc}
581
Fred Drakec6fa34e1998-04-02 06:47:24 +0000582\begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename,
583 int start, PyObject *globals,
584 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000585\end{cfuncdesc}
586
Fred Drakec6fa34e1998-04-02 06:47:24 +0000587\begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename,
588 int start}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000589\end{cfuncdesc}
590
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000591
Fred Drakeefd146c1999-02-15 15:30:45 +0000592\chapter{Reference Counting \label{countingRefs}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000593
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000594The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000595of Python objects.
596
597\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
Fred Drakec6fa34e1998-04-02 06:47:24 +0000598Increment the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000599not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000600\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000601\end{cfuncdesc}
602
603\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000604Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000605\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000606\end{cfuncdesc}
607
608\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000609Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000610not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000611\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
612object's type's deallocation function (which must not be \NULL{}) is
613invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000614
615\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000616code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000617method is deallocated). While exceptions in such code are not
618propagated, the executed code has free access to all Python global
619variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000620variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000621invoked. For example, code to delete an object from a list should
622copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000623the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000624temporary variable.
625\end{cfuncdesc}
626
627\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000628Decrement the reference count for object \var{o}. The object may be
629\NULL{}, in which case the macro has no effect; otherwise the effect
630is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000631applies.
632\end{cfuncdesc}
633
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000634The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000635\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
636\cfunction{_Py_NewReference()}, as well as the global variable
Fred Drakef8830d11998-04-23 14:06:01 +0000637\cdata{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000638
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000639XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
640PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
641PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
642
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000643
Fred Drakeefd146c1999-02-15 15:30:45 +0000644\chapter{Exception Handling \label{exceptionHandling}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000645
646The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000647exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000648Python exception handling. It works somewhat like the \UNIX{}
Fred Drakef8830d11998-04-23 14:06:01 +0000649\cdata{errno} variable: there is a global indicator (per thread) of the
Guido van Rossumae110af1997-05-22 20:11:52 +0000650last error that occurred. Most functions don't clear this on success,
651but will set it to indicate the cause of the error on failure. Most
652functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000653supposed to return a pointer, or \code{-1} if they return an integer
Fred Drakef8830d11998-04-23 14:06:01 +0000654(exception: the \cfunction{PyArg_Parse*()} functions return \code{1} for
Fred Drakee058b4f1998-02-16 06:15:35 +0000655success and \code{0} for failure). When a function must fail because
656some function it called failed, it generally doesn't set the error
657indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000658
659The error indicator consists of three Python objects corresponding to
660the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
661\code{sys.exc_traceback}. API functions exist to interact with the
662error indicator in various ways. There is a separate error indicator
663for each thread.
664
665% XXX Order of these should be more thoughtful.
666% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000667
668\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000669Print a standard traceback to \code{sys.stderr} and clear the error
670indicator. Call this function only when the error indicator is set.
671(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000672\end{cfuncdesc}
673
Fred Drakec6fa34e1998-04-02 06:47:24 +0000674\begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000675Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000676\emph{type} (the first argument to the last call to one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000677\cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
Fred Drakee058b4f1998-02-16 06:15:35 +0000678not set, return \NULL{}. You do not own a reference to the return
679value, so you do not need to \cfunction{Py_DECREF()} it.
680\strong{Note:} do not compare the return value to a specific
681exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
682below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000683\end{cfuncdesc}
684
685\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000686Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000687\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000688This should only be called when an exception is actually set.
689\end{cfuncdesc}
690
691\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000692Return true if the \var{given} exception matches the exception in
693\var{exc}. If \var{exc} is a class object, this also returns true
694when \var{given} is a subclass. If \var{exc} is a tuple, all
695exceptions in the tuple (and recursively in subtuples) are searched
696for a match. This should only be called when an exception is actually
697set.
698\end{cfuncdesc}
699
700\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000701Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000702\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
703\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
704instance of the same class. This function can be used to instantiate
705the class in that case. If the values are already normalized, nothing
706happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000707\end{cfuncdesc}
708
709\begin{cfuncdesc}{void}{PyErr_Clear}{}
710Clear the error indicator. If the error indicator is not set, there
711is no effect.
712\end{cfuncdesc}
713
714\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
715Retrieve the error indicator into three variables whose addresses are
716passed. If the error indicator is not set, set all three variables to
717\NULL{}. If it is set, it will be cleared and you own a reference to
718each object retrieved. The value and traceback object may be \NULL{}
719even when the type object is not. \strong{Note:} this function is
720normally only used by code that needs to handle exceptions or by code
721that needs to save and restore the error indicator temporarily.
722\end{cfuncdesc}
723
724\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
725Set the error indicator from the three objects. If the error
726indicator is already set, it is cleared first. If the objects are
727\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
728and non-\NULL{} value or traceback. The exception type should be a
729string or class; if it is a class, the value should be an instance of
730that class. Do not pass an invalid exception type or value.
731(Violating these rules will cause subtle problems later.) This call
732takes away a reference to each object, i.e. you must own a reference
733to each object before the call and after the call you no longer own
734these references. (If you don't understand this, don't use this
735function. I warned you.) \strong{Note:} this function is normally
736only used by code that needs to save and restore the error indicator
737temporarily.
738\end{cfuncdesc}
739
740\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
741This is the most common way to set the error indicator. The first
742argument specifies the exception type; it is normally one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000743standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not
Guido van Rossumae110af1997-05-22 20:11:52 +0000744increment its reference count. The second argument is an error
745message; it is converted to a string object.
746\end{cfuncdesc}
747
748\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000749This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000750specify an arbitrary Python object for the ``value'' of the exception.
751You need not increment its reference count.
752\end{cfuncdesc}
753
754\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000755This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000756\end{cfuncdesc}
757
758\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000759This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000760\var{message})}, where \var{message} indicates that a built-in operation
761was invoked with an illegal argument. It is mostly for internal use.
762\end{cfuncdesc}
763
Fred Drakec6fa34e1998-04-02 06:47:24 +0000764\begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000765This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000766returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000767\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000768\end{cfuncdesc}
769
Fred Drakec6fa34e1998-04-02 06:47:24 +0000770\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000771This is a convenience function to raise an exception when a \C{} library
Fred Drakef8830d11998-04-23 14:06:01 +0000772function has returned an error and set the \C{} variable \cdata{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000773It constructs a tuple object whose first item is the integer
Fred Drakef8830d11998-04-23 14:06:01 +0000774\cdata{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000775message (gotten from \cfunction{strerror()}), and then calls
776\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
Fred Drakef8830d11998-04-23 14:06:01 +0000777the \cdata{errno} value is \constant{EINTR}, indicating an interrupted
Fred Drakee058b4f1998-02-16 06:15:35 +0000778system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000779the error indicator, leaves it set to that. The function always
780returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000781\samp{return PyErr_SetFromErrno();} when the system call returns an
782error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000783\end{cfuncdesc}
784
785\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000786This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000787\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000788operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000789argument. It is mostly for internal use.
790\end{cfuncdesc}
791
792\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
793This function interacts with Python's signal handling. It checks
794whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000795corresponding signal handler. If the
796\module{signal}\refbimodindex{signal} module is supported, this can
797invoke a signal handler written in Python. In all cases, the default
798effect for \constant{SIGINT} is to raise the
799\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000800error indicator is set and the function returns \code{1}; otherwise
801the function returns \code{0}. The error indicator may or may not be
802cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000803\end{cfuncdesc}
804
805\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
806This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000807the effect of a \constant{SIGINT} signal arriving --- the next time
808\cfunction{PyErr_CheckSignals()} is called,
809\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000810\end{cfuncdesc}
811
Fred Drakec6fa34e1998-04-02 06:47:24 +0000812\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
813 PyObject *base,
814 PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000815This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000816\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000817of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000818arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000819object derived from the root for all exceptions, the built-in name
Fred Drakef8830d11998-04-23 14:06:01 +0000820\exception{Exception} (accessible in \C{} as \cdata{PyExc_Exception}).
821In this case the \member{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000822first part (up to the last dot) of the \var{name} argument, and the
823class name is set to the last part (after the last dot). When the
824user has specified the \code{-X} command line option to use string
825exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000826argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000827created from the entire \var{name} argument is returned. The
828\var{base} argument can be used to specify an alternate base class.
829The \var{dict} argument can be used to specify a dictionary of class
830variables and methods.
831\end{cfuncdesc}
832
833
Fred Drakeefd146c1999-02-15 15:30:45 +0000834\section{Standard Exceptions \label{standardExceptions}}
Guido van Rossumae110af1997-05-22 20:11:52 +0000835
836All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000837names are \samp{PyExc_} followed by the Python exception name.
Fred Drakef8830d11998-04-23 14:06:01 +0000838These have the type \ctype{PyObject *}; they are all either class
Fred Drakee058b4f1998-02-16 06:15:35 +0000839objects or string objects, depending on the use of the \code{-X}
840option to the interpreter. For completeness, here are all the
Fred Drake9d20ac31998-02-16 15:27:08 +0000841variables:
Fred Drakef8830d11998-04-23 14:06:01 +0000842\cdata{PyExc_Exception},
843\cdata{PyExc_StandardError},
844\cdata{PyExc_ArithmeticError},
845\cdata{PyExc_LookupError},
846\cdata{PyExc_AssertionError},
847\cdata{PyExc_AttributeError},
848\cdata{PyExc_EOFError},
849\cdata{PyExc_FloatingPointError},
850\cdata{PyExc_IOError},
851\cdata{PyExc_ImportError},
852\cdata{PyExc_IndexError},
853\cdata{PyExc_KeyError},
854\cdata{PyExc_KeyboardInterrupt},
855\cdata{PyExc_MemoryError},
856\cdata{PyExc_NameError},
857\cdata{PyExc_OverflowError},
858\cdata{PyExc_RuntimeError},
859\cdata{PyExc_SyntaxError},
860\cdata{PyExc_SystemError},
861\cdata{PyExc_SystemExit},
862\cdata{PyExc_TypeError},
863\cdata{PyExc_ValueError},
864\cdata{PyExc_ZeroDivisionError}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000865
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000866
Fred Drakeefd146c1999-02-15 15:30:45 +0000867\chapter{Utilities \label{utilities}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000868
869The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000870parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000871values.
872
Fred Drakeefd146c1999-02-15 15:30:45 +0000873\section{OS Utilities \label{os}}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000874
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000875\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000876Return true (nonzero) if the standard I/O file \var{fp} with name
877\var{filename} is deemed interactive. This is the case for files for
878which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Fred Drakef8830d11998-04-23 14:06:01 +0000879\cdata{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000880the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000881the strings \code{"<stdin>"} or \code{"???"}.
882\end{cfuncdesc}
883
884\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000885Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000886The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000887the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000888\end{cfuncdesc}
889
890
Fred Drakeefd146c1999-02-15 15:30:45 +0000891\section{Process Control \label{processControl}}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000892
893\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
894Print a fatal error message and kill the process. No cleanup is
895performed. This function should only be invoked when a condition is
896detected that would make it dangerous to continue using the Python
897interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000898corrupted. On \UNIX{}, the standard \C{} library function
899\cfunction{abort()} is called which will attempt to produce a
900\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000901\end{cfuncdesc}
902
903\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000904Exit the current process. This calls \cfunction{Py_Finalize()} and
905then calls the standard \C{} library function
906\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000907\end{cfuncdesc}
908
909\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
910Register a cleanup function to be called by \cfunction{Py_Finalize()}.
911The cleanup function will be called with no arguments and should
912return no value. At most 32 cleanup functions can be registered.
913When the registration is successful, \cfunction{Py_AtExit()} returns
914\code{0}; on failure, it returns \code{-1}. The cleanup function
915registered last is called first. Each cleanup function will be called
916at most once. Since Python's internal finallization will have
917completed before the cleanup function, no Python APIs should be called
918by \var{func}.
919\end{cfuncdesc}
920
921
Fred Drakeefd146c1999-02-15 15:30:45 +0000922\section{Importing Modules \label{importing}}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000923
Fred Drakec6fa34e1998-04-02 06:47:24 +0000924\begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000925This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000926below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000927\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000928it specifies a submodule of a package), the \var{fromlist} argument is
929set to the list \code{['*']} so that the return value is the named
930module rather than the top-level package containing it as would
931otherwise be the case. (Unfortunately, this has an additional side
932effect when \var{name} in fact specifies a subpackage instead of a
933submodule: the submodules specified in the package's \code{__all__}
934variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000935or \NULL{} with an exception set on failure (the module may still
Fred Drakec6fa34e1998-04-02 06:47:24 +0000936be created in this case --- examine \code{sys.modules} to find out).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000937\end{cfuncdesc}
938
Fred Drakec6fa34e1998-04-02 06:47:24 +0000939\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000940Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000941Python function \function{__import__()}\bifuncindex{__import__}, as
942the standard \function{__import__()} function calls this function
943directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000944
Guido van Rossum42cefd01997-10-05 15:27:29 +0000945The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000946top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000947(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000948\function{__import__()}, the return value when a submodule of a
949package was requested is normally the top-level package, unless a
950non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000951\end{cfuncdesc}
952
Fred Drakec6fa34e1998-04-02 06:47:24 +0000953\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000954This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000955function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000956\code{__builtins__} of the current globals. This means that the
957import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000958current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
959\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000960\end{cfuncdesc}
961
Fred Drakec6fa34e1998-04-02 06:47:24 +0000962\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000963Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000964Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000965\function{reload()} function calls this function directly. Return a
966new reference to the reloaded module, or \NULL{} with an exception set
967on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000968\end{cfuncdesc}
969
Fred Drakec6fa34e1998-04-02 06:47:24 +0000970\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000971Return the module object corresponding to a module name. The
972\var{name} argument may be of the form \code{package.module}). First
973check the modules dictionary if there's one there, and if not, create
974a new one and insert in in the modules dictionary. Because the former
975action is most common, this does not return a new reference, and you
Guido van Rossuma096a2e1998-11-02 17:02:42 +0000976do not own the returned reference.
977Warning: this function does not load or import the module; if the
978module wasn't already loaded, you will get an empty module object.
979Use \cfunction{PyImport_ImportModule()} or one of its variants to
980import a module.
981Return \NULL{} with an
Fred Drakef8830d11998-04-23 14:06:01 +0000982exception set on failure. \strong{Note:} this function returns
Guido van Rossum44475131998-04-21 15:30:01 +0000983a ``borrowed'' reference.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000984\end{cfuncdesc}
985
Fred Drakec6fa34e1998-04-02 06:47:24 +0000986\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000987Given a module name (possibly of the form \code{package.module}) and a
988code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000989built-in function \function{compile()}\bifuncindex{compile}, load the
990module. Return a new reference to the module object, or \NULL{} with
991an exception set if an error occurred (the module may still be created
992in this case). (This function would reload the module if it was
993already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000994\end{cfuncdesc}
995
996\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000997Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
998and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000999first four bytes of the bytecode file, in little-endian byte order.
1000\end{cfuncdesc}
1001
Fred Drakec6fa34e1998-04-02 06:47:24 +00001002\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001003Return the dictionary used for the module administration
1004(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
1005variable.
1006\end{cfuncdesc}
1007
1008\begin{cfuncdesc}{void}{_PyImport_Init}{}
1009Initialize the import mechanism. For internal use only.
1010\end{cfuncdesc}
1011
1012\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
1013Empty the module table. For internal use only.
1014\end{cfuncdesc}
1015
1016\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1017Finalize the import mechanism. For internal use only.
1018\end{cfuncdesc}
1019
Fred Drakec6fa34e1998-04-02 06:47:24 +00001020\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001021For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001022\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001023
Fred Drakec6fa34e1998-04-02 06:47:24 +00001024\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001025For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001026\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001027
1028\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1029Load a frozen module. Return \code{1} for success, \code{0} if the
1030module is not found, and \code{-1} with an exception set if the
1031initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001032load, use \cfunction{PyImport_ImportModule()}.
1033(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001034already imported.)
1035\end{cfuncdesc}
1036
1037\begin{ctypedesc}{struct _frozen}
1038This is the structure type definition for frozen module descriptors,
Fred Drakec6fa34e1998-04-02 06:47:24 +00001039as generated by the \program{freeze}\index{freeze utility} utility
1040(see \file{Tools/freeze/} in the Python source distribution). Its
1041definition is:
1042
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001043\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001044struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001045 char *name;
1046 unsigned char *code;
1047 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001048};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001049\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001050\end{ctypedesc}
1051
Fred Drakec6fa34e1998-04-02 06:47:24 +00001052\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
Fred Drakef8830d11998-04-23 14:06:01 +00001053This pointer is initialized to point to an array of \ctype{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001054_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001055or zero. When a frozen module is imported, it is searched in this
Fred Drakec6fa34e1998-04-02 06:47:24 +00001056table. Third-party code could play tricks with this to provide a
Guido van Rossum42cefd01997-10-05 15:27:29 +00001057dynamically created collection of frozen modules.
1058\end{cvardesc}
1059
1060
Fred Drakeefd146c1999-02-15 15:30:45 +00001061\chapter{Abstract Objects Layer \label{abstract}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001062
1063The functions in this chapter interact with Python objects regardless
1064of their type, or with wide classes of object types (e.g. all
1065numerical types, or all sequence types). When used on object types
1066for which they do not apply, they will flag a Python exception.
1067
Fred Drakeefd146c1999-02-15 15:30:45 +00001068\section{Object Protocol \label{object}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001069
1070\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001071Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001072The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001073options. The only option currently supported is
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001074\constant{Py_PRINT_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001075\end{cfuncdesc}
1076
1077\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001078Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1079\code{0} otherwise. This is equivalent to the Python expression
1080\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001081This function always succeeds.
1082\end{cfuncdesc}
1083
1084\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001085Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001086Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001087This is the equivalent of the Python expression
1088\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001089\end{cfuncdesc}
1090
1091
1092\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001093Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1094\code{0} otherwise. This is equivalent to the Python expression
1095\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001096This function always succeeds.
1097\end{cfuncdesc}
1098
1099
1100\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001101Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001102Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001103This is the equivalent of the Python expression
1104\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001105\end{cfuncdesc}
1106
1107
1108\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001109Set the value of the attribute named \var{attr_name}, for object
1110\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1111the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1112\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001113\end{cfuncdesc}
1114
1115
1116\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001117Set the value of the attribute named \var{attr_name}, for
1118object \var{o},
1119to the value \var{v}. Returns \code{-1} on failure. This is
1120the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1121\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001122\end{cfuncdesc}
1123
1124
1125\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001126Delete attribute named \var{attr_name}, for object \var{o}. Returns
1127\code{-1} on failure. This is the equivalent of the Python
1128statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001129\end{cfuncdesc}
1130
1131
1132\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001133Delete attribute named \var{attr_name}, for object \var{o}. Returns
1134\code{-1} on failure. This is the equivalent of the Python
1135statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001136\end{cfuncdesc}
1137
1138
1139\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001140Compare the values of \var{o1} and \var{o2} using a routine provided
1141by \var{o1}, if one exists, otherwise with a routine provided by
1142\var{o2}. The result of the comparison is returned in \var{result}.
1143Returns \code{-1} on failure. This is the equivalent of the Python
1144statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001145\end{cfuncdesc}
1146
1147
1148\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001149Compare the values of \var{o1} and \var{o2} using a routine provided
1150by \var{o1}, if one exists, otherwise with a routine provided by
1151\var{o2}. Returns the result of the comparison on success. On error,
1152the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1153detect an error. This is equivalent to the
1154Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001155\end{cfuncdesc}
1156
1157
1158\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001159Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001160string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001161the equivalent of the Python expression \samp{repr(\var{o})}.
1162Called by the \function{repr()}\bifuncindex{repr} built-in function
1163and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001164\end{cfuncdesc}
1165
1166
1167\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001168Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001169string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001170the equivalent of the Python expression \samp{str(\var{o})}.
1171Called by the \function{str()}\bifuncindex{str} built-in function and
1172by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001173\end{cfuncdesc}
1174
1175
1176\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001177Determine if the object \var{o}, is callable. Return \code{1} if the
1178object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001179This function always succeeds.
1180\end{cfuncdesc}
1181
1182
1183\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001184Call a callable Python object \var{callable_object}, with
1185arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001186needed, then args may be \NULL{}. Returns the result of the
1187call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001188of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001189\end{cfuncdesc}
1190
1191\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001192Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001193variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001194using a \cfunction{Py_BuildValue()} style format string. The format may
1195be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001196result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001197the equivalent of the Python expression \samp{apply(\var{o},
1198\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001199\end{cfuncdesc}
1200
1201
1202\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001203Call the method named \var{m} of object \var{o} with a variable number
1204of C arguments. The \C{} arguments are described by a
1205\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1206indicating that no arguments are provided. Returns the result of the
1207call on success, or \NULL{} on failure. This is the equivalent of the
1208Python expression \samp{\var{o}.\var{method}(\var{args})}.
1209Note that Special method names, such as \method{__add__()},
1210\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001211abstract-object routines for these must be used.
1212\end{cfuncdesc}
1213
1214
1215\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001216Compute and return the hash value of an object \var{o}. On
1217failure, return \code{-1}. This is the equivalent of the Python
1218expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001219\end{cfuncdesc}
1220
1221
1222\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001223Returns \code{1} if the object \var{o} is considered to be true, and
1224\code{0} otherwise. This is equivalent to the Python expression
1225\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001226This function always succeeds.
1227\end{cfuncdesc}
1228
1229
1230\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1231On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001232type of object \var{o}. On failure, returns \NULL{}. This is
1233equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001234\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001235\end{cfuncdesc}
1236
1237\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001238Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001239both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001240returned. On error, \code{-1} is returned. This is the equivalent
1241to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001242\end{cfuncdesc}
1243
1244
1245\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001246Return element of \var{o} corresponding to the object \var{key} or
1247\NULL{} on failure. This is the equivalent of the Python expression
1248\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001249\end{cfuncdesc}
1250
1251
1252\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001253Map the object \var{key} to the value \var{v}.
1254Returns \code{-1} on failure. This is the equivalent
1255of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001256\end{cfuncdesc}
1257
1258
Guido van Rossumd1dbf631999-01-22 20:10:49 +00001259\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001260Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1261failure. This is the equivalent of the Python statement \samp{del
1262\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001263\end{cfuncdesc}
1264
1265
Fred Drakeefd146c1999-02-15 15:30:45 +00001266\section{Number Protocol \label{number}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001267
1268\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001269Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001270false otherwise.
1271This function always succeeds.
1272\end{cfuncdesc}
1273
1274
1275\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001276Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1277failure. This is the equivalent of the Python expression
1278\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001279\end{cfuncdesc}
1280
1281
1282\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001283Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1284on failure. This is the equivalent of the Python expression
1285\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001286\end{cfuncdesc}
1287
1288
1289\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001290Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1291failure. This is the equivalent of the Python expression
1292\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001293\end{cfuncdesc}
1294
1295
1296\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001297Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1298failure.
1299This is the equivalent of the Python expression \samp{\var{o1} /
1300\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001301\end{cfuncdesc}
1302
1303
1304\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001305Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1306failure. This is the equivalent of the Python expression
1307\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001308\end{cfuncdesc}
1309
1310
1311\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001312See the built-in function \function{divmod()}\bifuncindex{divmod}.
1313Returns \NULL{} on failure. This is the equivalent of the Python
1314expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001315\end{cfuncdesc}
1316
1317
1318\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001319See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1320\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001321\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
Fred Drakef8830d11998-04-23 14:06:01 +00001322If \var{o3} is to be ignored, pass \cdata{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001323\end{cfuncdesc}
1324
1325
1326\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001327Returns the negation of \var{o} on success, or \NULL{} on failure.
1328This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001329\end{cfuncdesc}
1330
1331
1332\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001333Returns \var{o} on success, or \NULL{} on failure.
1334This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001335\end{cfuncdesc}
1336
1337
1338\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001339Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1340the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001341\end{cfuncdesc}
1342
1343
1344\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001345Returns the bitwise negation of \var{o} on success, or \NULL{} on
1346failure. This is the equivalent of the Python expression
1347\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001348\end{cfuncdesc}
1349
1350
1351\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001352Returns the result of left shifting \var{o1} by \var{o2} on success,
1353or \NULL{} on failure. This is the equivalent of the Python
1354expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001355\end{cfuncdesc}
1356
1357
1358\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001359Returns the result of right shifting \var{o1} by \var{o2} on success,
1360or \NULL{} on failure. This is the equivalent of the Python
1361expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001362\end{cfuncdesc}
1363
1364
1365\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001366Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1367\NULL{} on failure. This is the equivalent of the Python
1368expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001369\end{cfuncdesc}
1370
1371
1372\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001373Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1374or \NULL{} on failure. This is the equivalent of the Python
1375expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001376\end{cfuncdesc}
1377
1378\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001379Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1380failure. This is the equivalent of the Python expression
1381\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001382\end{cfuncdesc}
1383
1384
Fred Drakee058b4f1998-02-16 06:15:35 +00001385\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001386This function takes the addresses of two variables of type
Fred Drakef8830d11998-04-23 14:06:01 +00001387\ctype{PyObject*}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001388
Fred Drakee058b4f1998-02-16 06:15:35 +00001389If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1390have the same type, increment their reference count and return
1391\code{0} (success). If the objects can be converted to a common
1392numeric type, replace \code{*p1} and \code{*p2} by their converted
1393value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001394If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001395return \code{-1} (failure) and don't increment the reference counts.
1396The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1397Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1398\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001399\end{cfuncdesc}
1400
1401
1402\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001403Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001404\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001405expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001406\end{cfuncdesc}
1407
1408
1409\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001410Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001411or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001412expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001413\end{cfuncdesc}
1414
1415
1416\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001417Returns the \var{o} converted to a float object on success, or \NULL{}
1418on failure. This is the equivalent of the Python expression
1419\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001420\end{cfuncdesc}
1421
1422
Fred Drakeefd146c1999-02-15 15:30:45 +00001423\section{Sequence Protocol \label{sequence}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001424
1425\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001426Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001427otherwise.
1428This function always succeeds.
1429\end{cfuncdesc}
1430
1431
1432\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001433Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001434failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001435expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001436\end{cfuncdesc}
1437
1438
1439\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001440Return the result of repeating sequence object \var{o} \var{count}
1441times, or \NULL{} on failure. This is the equivalent of the Python
1442expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001443\end{cfuncdesc}
1444
1445
1446\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001447Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1448is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001449\end{cfuncdesc}
1450
1451
1452\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001453Return the slice of sequence object \var{o} between \var{i1} and
1454\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1455expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001456\end{cfuncdesc}
1457
1458
1459\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001460Assign object \var{v} to the \var{i}th element of \var{o}.
1461Returns \code{-1} on failure. This is the equivalent of the Python
1462statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001463\end{cfuncdesc}
1464
1465\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001466Delete the \var{i}th element of object \var{v}. Returns
1467\code{-1} on failure. This is the equivalent of the Python
1468statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001469\end{cfuncdesc}
1470
1471\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001472Assign the sequence object \var{v} to the slice in sequence
1473object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1474the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001475\end{cfuncdesc}
1476
1477\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001478Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1479Returns \code{-1} on failure. This is the equivalent of the Python
1480statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001481\end{cfuncdesc}
1482
1483\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001484Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1485This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001486\end{cfuncdesc}
1487
1488\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001489Return the number of occurrences of \var{value} in \var{o}, that is,
1490return the number of keys for which \code{\var{o}[\var{key}] ==
1491\var{value}}. On failure, return \code{-1}. This is equivalent to
1492the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001493\end{cfuncdesc}
1494
1495\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001496Determine if \var{o} contains \var{value}. If an item in \var{o} is
1497equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1498error, return \code{-1}. This is equivalent to the Python expression
1499\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001500\end{cfuncdesc}
1501
1502\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001503Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1504\var{value}}. On error, return \code{-1}. This is equivalent to
1505the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001506\end{cfuncdesc}
1507
Fred Drakef39ed671998-02-26 22:01:23 +00001508
Fred Drakeefd146c1999-02-15 15:30:45 +00001509\section{Mapping Protocol \label{mapping}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001510
1511\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001512Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001513otherwise.
1514This function always succeeds.
1515\end{cfuncdesc}
1516
1517
1518\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001519Returns the number of keys in object \var{o} on success, and \code{-1}
1520on failure. For objects that do not provide sequence protocol,
1521this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001522\end{cfuncdesc}
1523
1524
1525\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001526Remove the mapping for object \var{key} from the object \var{o}.
1527Return \code{-1} on failure. This is equivalent to
1528the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001529\end{cfuncdesc}
1530
1531
1532\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001533Remove the mapping for object \var{key} from the object \var{o}.
1534Return \code{-1} on failure. This is equivalent to
1535the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001536\end{cfuncdesc}
1537
1538
1539\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001540On success, return \code{1} if the mapping object has the key \var{key}
1541and \code{0} otherwise. This is equivalent to the Python expression
1542\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001543This function always succeeds.
1544\end{cfuncdesc}
1545
1546
1547\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001548Return \code{1} if the mapping object has the key \var{key} and
1549\code{0} otherwise. This is equivalent to the Python expression
1550\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001551This function always succeeds.
1552\end{cfuncdesc}
1553
1554
1555\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001556On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001557failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001558expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001559\end{cfuncdesc}
1560
1561
1562\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001563On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001564failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001565expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001566\end{cfuncdesc}
1567
1568
1569\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001570On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001571each item is a tuple containing a key-value pair. On
1572failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001573expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001574\end{cfuncdesc}
1575
1576\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001577Make object \var{o} empty. Returns \code{1} on success and \code{0}
1578on failure. This is equivalent to the Python statement
1579\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001580\end{cfuncdesc}
1581
1582
1583\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001584Return element of \var{o} corresponding to the object \var{key} or
1585\NULL{} on failure. This is the equivalent of the Python expression
1586\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001587\end{cfuncdesc}
1588
Guido van Rossum0a0f11b1998-10-16 17:43:53 +00001589\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001590Map the object \var{key} to the value \var{v} in object \var{o}.
1591Returns \code{-1} on failure. This is the equivalent of the Python
1592statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001593\end{cfuncdesc}
1594
1595
1596\section{Constructors}
1597
1598\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1599On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001600file given by \var{file_name}, with a file mode given by \var{mode},
1601where \var{mode} has the same semantics as the standard \C{} routine
1602\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001603\end{cfuncdesc}
1604
1605\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001606Return a new file object for an already opened standard \C{} file
1607pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1608\var{mode}, must be provided as well as a flag, \var{close_on_del},
1609that indicates whether the file is to be closed when the file object
1610is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001611\end{cfuncdesc}
1612
1613\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001614Returns a new float object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001615\NULL{} on failure.
1616\end{cfuncdesc}
1617
1618\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001619Returns a new int object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001620\NULL{} on failure.
1621\end{cfuncdesc}
1622
Fred Drakee058b4f1998-02-16 06:15:35 +00001623\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1624Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001625failure.
1626\end{cfuncdesc}
1627
1628\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001629Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001630\NULL{} on failure.
1631\end{cfuncdesc}
1632
1633\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001634Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001635\NULL{} on failure.
1636\end{cfuncdesc}
1637
1638\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1639Returns a new empty dictionary on success, and \NULL{} on
1640failure.
1641\end{cfuncdesc}
1642
1643\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001644Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001645\NULL{} on failure.
1646\end{cfuncdesc}
1647
Fred Drakee058b4f1998-02-16 06:15:35 +00001648\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1649Returns a new string object with the value \var{v} and length
1650\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1651the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001652\end{cfuncdesc}
1653
Fred Drakee058b4f1998-02-16 06:15:35 +00001654\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1655Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001656failure.
1657\end{cfuncdesc}
1658
1659
Fred Drakeefd146c1999-02-15 15:30:45 +00001660\chapter{Concrete Objects Layer \label{concrete}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001661
1662The functions in this chapter are specific to certain Python object
1663types. Passing them an object of the wrong type is not a good idea;
1664if you receive an object from a Python program and you are not sure
1665that it has the right type, you must perform a type check first;
1666e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001667\cfunction{PyDict_Check()}. The chapter is structured like the
1668``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001669
1670
Fred Drakeefd146c1999-02-15 15:30:45 +00001671\section{Fundamental Objects \label{fundamental}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001672
Fred Drakee5bf8b21998-02-12 21:22:28 +00001673This section describes Python type objects and the singleton object
1674\code{None}.
1675
1676
Fred Drakeefd146c1999-02-15 15:30:45 +00001677\subsection{Type Objects \label{typeObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001678
1679\begin{ctypedesc}{PyTypeObject}
1680
1681\end{ctypedesc}
1682
1683\begin{cvardesc}{PyObject *}{PyType_Type}
Fred Drakeefd146c1999-02-15 15:30:45 +00001684This is the type object for type objects; it is the same object as
1685\code{types.TypeType} in the Python layer.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001686\end{cvardesc}
1687
1688
Fred Drakeefd146c1999-02-15 15:30:45 +00001689\subsection{The None Object \label{noneObject}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001690
1691\begin{cvardesc}{PyObject *}{Py_None}
Guido van Rossum44475131998-04-21 15:30:01 +00001692The Python \code{None} object, denoting lack of value. This object has
1693no methods.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001694\end{cvardesc}
1695
1696
Fred Drakeefd146c1999-02-15 15:30:45 +00001697\section{Sequence Objects \label{sequenceObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001698
1699Generic operations on sequence objects were discussed in the previous
1700chapter; this section deals with the specific kinds of sequence
1701objects that are intrinsic to the Python language.
1702
1703
Fred Drakeefd146c1999-02-15 15:30:45 +00001704\subsection{String Objects \label{stringObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001705
1706\begin{ctypedesc}{PyStringObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001707This subtype of \ctype{PyObject} represents a Python string object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001708\end{ctypedesc}
1709
1710\begin{cvardesc}{PyTypeObject}{PyString_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001711This instance of \ctype{PyTypeObject} represents the Python string type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001712\end{cvardesc}
1713
1714\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001715Returns true if the object \var{o} is a string object.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001716\end{cfuncdesc}
1717
Fred Drakec6fa34e1998-04-02 06:47:24 +00001718\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
1719 int len}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001720Returns a new string object with the value \var{v} and length
1721\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1722the contents of the string are uninitialized.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001723\end{cfuncdesc}
1724
1725\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001726Returns a new string object with the value \var{v} on success, and
1727\NULL{} on failure.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001728\end{cfuncdesc}
1729
1730\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001731Returns the length of the string in string object \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001732\end{cfuncdesc}
1733
1734\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001735Resturns a \NULL{} terminated representation of the contents of \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001736\end{cfuncdesc}
1737
1738\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
1739 PyObject *newpart}
Fred Drake66b989c1999-02-15 20:15:39 +00001740Creates a new string object in \var{*string} containing the
1741contents of \var{newpart} appended to \var{string}. The old value of
1742\var{string} have its reference count decremented. If the new string
1743cannot be created, the old reference to \var{string} will still be
1744discarded and the value of \var{*string} will be set to
1745\NULL{}; the appropriate exception will be set.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001746\end{cfuncdesc}
1747
1748\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
1749 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001750Creates a new string object in \var{*string} containing the contents
Guido van Rossum44475131998-04-21 15:30:01 +00001751of \var{newpart} appended to \var{string}. This version decrements
1752the reference count of \var{newpart}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
Guido van Rossum44475131998-04-21 15:30:01 +00001756A way to resize a string object even though it is ``immutable''.
1757Only use this to build up a brand new string object; don't use this if
1758the string may already be known in other parts of the code.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001759\end{cfuncdesc}
1760
1761\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
1762 PyObject *args}
Guido van Rossum44475131998-04-21 15:30:01 +00001763Returns a new string object from \var{format} and \var{args}. Analogous
1764to \code{\var{format} \% \var{args}}. The \var{args} argument must be
1765a tuple.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001766\end{cfuncdesc}
1767
1768\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
Guido van Rossum44475131998-04-21 15:30:01 +00001769Intern the argument \var{*string} in place. The argument must be the
1770address of a pointer variable pointing to a Python string object.
1771If there is an existing interned string that is the same as
1772\var{*string}, it sets \var{*string} to it (decrementing the reference
1773count of the old string object and incrementing the reference count of
1774the interned string object), otherwise it leaves \var{*string} alone
1775and interns it (incrementing its reference count). (Clarification:
1776even though there is a lot of talk about reference counts, think of
Fred Drakef8830d11998-04-23 14:06:01 +00001777this function as reference-count-neutral; you own the object after
1778the call if and only if you owned it before the call.)
Fred Drakec6fa34e1998-04-02 06:47:24 +00001779\end{cfuncdesc}
1780
1781\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
Fred Drakef8830d11998-04-23 14:06:01 +00001782A combination of \cfunction{PyString_FromString()} and
1783\cfunction{PyString_InternInPlace()}, returning either a new string object
Guido van Rossum44475131998-04-21 15:30:01 +00001784that has been interned, or a new (``owned'') reference to an earlier
1785interned string object with the same value.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001786\end{cfuncdesc}
1787
1788\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
Fred Drakef8830d11998-04-23 14:06:01 +00001789Macro form of \cfunction{PyString_AsString()} but without error checking.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001790\end{cfuncdesc}
1791
Fred Drakec6fa34e1998-04-02 06:47:24 +00001792\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
Fred Drakef8830d11998-04-23 14:06:01 +00001793Macro form of \cfunction{PyString_GetSize()} but without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001794\end{cfuncdesc}
1795
1796
Guido van Rossum44475131998-04-21 15:30:01 +00001797
Fred Drakeefd146c1999-02-15 15:30:45 +00001798\subsection{Tuple Objects \label{tupleObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001799
1800\begin{ctypedesc}{PyTupleObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001801This subtype of \ctype{PyObject} represents a Python tuple object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001802\end{ctypedesc}
1803
1804\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001805This instance of \ctype{PyTypeObject} represents the Python tuple type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001806\end{cvardesc}
1807
1808\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1809Return true if the argument is a tuple object.
1810\end{cfuncdesc}
1811
Fred Drakec6fa34e1998-04-02 06:47:24 +00001812\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001813Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001814\end{cfuncdesc}
1815
1816\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001817Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001818of that tuple.
1819\end{cfuncdesc}
1820
Fred Drakec6fa34e1998-04-02 06:47:24 +00001821\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001822Returns the object at position \var{pos} in the tuple pointed
1823to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drakef8830d11998-04-23 14:06:01 +00001824sets an \exception{IndexError} exception. \strong{Note:} this
1825function returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001826\end{cfuncdesc}
1827
Fred Drakec6fa34e1998-04-02 06:47:24 +00001828\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001829Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001830\end{cfuncdesc}
1831
Fred Drakec6fa34e1998-04-02 06:47:24 +00001832\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001833 int low,
1834 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001835Takes a slice of the tuple pointed to by \var{p} from
1836\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001837\end{cfuncdesc}
1838
1839\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1840 int pos,
1841 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001842Inserts a reference to object \var{o} at position \var{pos} of
1843the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001844\end{cfuncdesc}
1845
1846\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1847 int pos,
1848 PyObject *o}
1849
Fred Drakee058b4f1998-02-16 06:15:35 +00001850Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001851should \emph{only} be used to fill in brand new tuples.
1852\end{cfuncdesc}
1853
Fred Drakec6fa34e1998-04-02 06:47:24 +00001854\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001855 int new,
1856 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001857Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001858\emph{supposed} to be immutable, this should only be used if there is only
1859one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001860already be known to some other part of the code. \var{last_is_sticky} is
1861a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001862it will grow or shrink at the end. Think of this as destroying the old
1863tuple and creating a new one, only more efficiently.
1864\end{cfuncdesc}
1865
1866
Fred Drakeefd146c1999-02-15 15:30:45 +00001867\subsection{List Objects \label{listObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001868
1869\begin{ctypedesc}{PyListObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001870This subtype of \ctype{PyObject} represents a Python list object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001871\end{ctypedesc}
1872
1873\begin{cvardesc}{PyTypeObject}{PyList_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001874This instance of \ctype{PyTypeObject} represents the Python list type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001875\end{cvardesc}
1876
1877\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00001878Returns true if its argument is a \ctype{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001879\end{cfuncdesc}
1880
Fred Drakec6fa34e1998-04-02 06:47:24 +00001881\begin{cfuncdesc}{PyObject*}{PyList_New}{int size}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001882Returns a new list of length \var{len} on success, and \NULL{} on
1883failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001884\end{cfuncdesc}
1885
Fred Drakec6fa34e1998-04-02 06:47:24 +00001886\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001887Returns the length of the list object in \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001888\end{cfuncdesc}
1889
Fred Drakec6fa34e1998-04-02 06:47:24 +00001890\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
Guido van Rossum44475131998-04-21 15:30:01 +00001891Returns the object at position \var{pos} in the list pointed
1892to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drakef8830d11998-04-23 14:06:01 +00001893sets an \exception{IndexError} exception. \strong{Note:} this
1894function returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001895\end{cfuncdesc}
1896
Fred Drakec6fa34e1998-04-02 06:47:24 +00001897\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1898 PyObject *item}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001899Sets the item at index \var{index} in list to \var{item}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001900\end{cfuncdesc}
1901
Fred Drakec6fa34e1998-04-02 06:47:24 +00001902\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
Guido van Rossum44475131998-04-21 15:30:01 +00001903 PyObject *item}
1904Inserts the item \var{item} into list \var{list} in front of index
1905\var{index}. Returns 0 if successful; returns -1 and sets an
1906exception if unsuccessful. Analogous to \code{list.insert(index, item)}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001907\end{cfuncdesc}
1908
Fred Drakec6fa34e1998-04-02 06:47:24 +00001909\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Guido van Rossum44475131998-04-21 15:30:01 +00001910Appends the object \var{item} at the end of list \var{list}. Returns
19110 if successful; returns -1 and sets an exception if unsuccessful.
1912Analogous to \code{list.append(item)}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001913\end{cfuncdesc}
1914
Fred Drakec6fa34e1998-04-02 06:47:24 +00001915\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1916 int low, int high}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001917Returns a list of the objects in \var{list} containing the objects
Guido van Rossum44475131998-04-21 15:30:01 +00001918\emph{between} \var{low} and \var{high}. Returns NULL and sets an
1919exception if unsuccessful.
1920Analogous to \code{list[low:high]}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001921\end{cfuncdesc}
1922
Fred Drakec6fa34e1998-04-02 06:47:24 +00001923\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1924 int low, int high,
1925 PyObject *itemlist}
Guido van Rossum44475131998-04-21 15:30:01 +00001926Sets the slice of \var{list} between \var{low} and \var{high} to the contents
1927of \var{itemlist}. Analogous to \code{list[low:high]=itemlist}. Returns 0
1928on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001929\end{cfuncdesc}
1930
Fred Drakec6fa34e1998-04-02 06:47:24 +00001931\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Guido van Rossum44475131998-04-21 15:30:01 +00001932Sorts the items of \var{list} in place. Returns 0 on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001933\end{cfuncdesc}
1934
Fred Drakec6fa34e1998-04-02 06:47:24 +00001935\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Guido van Rossum44475131998-04-21 15:30:01 +00001936Reverses the items of \var{list} in place. Returns 0 on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001937\end{cfuncdesc}
1938
Fred Drakec6fa34e1998-04-02 06:47:24 +00001939\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001940Returns a new tuple object containing the contents of \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001941\end{cfuncdesc}
1942
Fred Drakec6fa34e1998-04-02 06:47:24 +00001943\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
Fred Drakef8830d11998-04-23 14:06:01 +00001944Macro form of \cfunction{PyList_GetItem()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001945\end{cfuncdesc}
1946
Guido van Rossuma937d141998-04-24 18:22:02 +00001947\begin{cfuncdesc}{PyObject*}{PyList_SET_ITEM}{PyObject *list, int i,
1948 PyObject *o}
1949Macro form of \cfunction{PyList_SetItem()} without error checking.
1950\end{cfuncdesc}
1951
Fred Drakee5bf8b21998-02-12 21:22:28 +00001952\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
Fred Drakef8830d11998-04-23 14:06:01 +00001953Macro form of \cfunction{PyList_GetSize()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001954\end{cfuncdesc}
1955
1956
Fred Drakeefd146c1999-02-15 15:30:45 +00001957\section{Mapping Objects \label{mapObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001958
Fred Drakeefd146c1999-02-15 15:30:45 +00001959\subsection{Dictionary Objects \label{dictObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001960
1961\begin{ctypedesc}{PyDictObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001962This subtype of \ctype{PyObject} represents a Python dictionary object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001963\end{ctypedesc}
1964
1965\begin{cvardesc}{PyTypeObject}{PyDict_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001966This instance of \ctype{PyTypeObject} represents the Python dictionary type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001967\end{cvardesc}
1968
1969\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00001970Returns true if its argument is a \ctype{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001971\end{cfuncdesc}
1972
Fred Drakec6fa34e1998-04-02 06:47:24 +00001973\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001974Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001975\end{cfuncdesc}
1976
1977\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001978Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001979\end{cfuncdesc}
1980
1981\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1982 PyObject *key,
1983 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001984Inserts \var{value} into the dictionary with a key of \var{key}. Both
1985\var{key} and \var{value} should be PyObjects, and \var{key} should be
1986hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001987\end{cfuncdesc}
1988
1989\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1990 char *key,
1991 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001992Inserts \var{value} into the dictionary using \var{key}
Fred Drakef8830d11998-04-23 14:06:01 +00001993as a key. \var{key} should be a \ctype{char *}. The key object is
Fred Drakee058b4f1998-02-16 06:15:35 +00001994created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001995\end{cfuncdesc}
1996
1997\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001998Removes the entry in dictionary \var{p} with key \var{key}.
1999\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002000\end{cfuncdesc}
2001
2002\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002003Removes the entry in dictionary \var{p} which has a key
Fred Drakef8830d11998-04-23 14:06:01 +00002004specified by the \ctype{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002005\end{cfuncdesc}
2006
Fred Drakec6fa34e1998-04-02 06:47:24 +00002007\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002008Returns the object from dictionary \var{p} which has a key
Guido van Rossum44475131998-04-21 15:30:01 +00002009\var{key}. Returns \NULL{} if the key \var{key} is not present, but
Fred Drakef8830d11998-04-23 14:06:01 +00002010without (!) setting an exception. \strong{Note:} this function
2011returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002012\end{cfuncdesc}
2013
Fred Drakec6fa34e1998-04-02 06:47:24 +00002014\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakef8830d11998-04-23 14:06:01 +00002015This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2016specified as a \ctype{char *}, rather than a \ctype{PyObject *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002017\end{cfuncdesc}
2018
Fred Drakec6fa34e1998-04-02 06:47:24 +00002019\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002020Returns a \ctype{PyListObject} containing all the items
Guido van Rossum44475131998-04-21 15:30:01 +00002021from the dictionary, as in the dictinoary method \method{items()} (see
Fred Drakee058b4f1998-02-16 06:15:35 +00002022the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002023\end{cfuncdesc}
2024
Fred Drakec6fa34e1998-04-02 06:47:24 +00002025\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002026Returns a \ctype{PyListObject} containing all the keys
Guido van Rossum44475131998-04-21 15:30:01 +00002027from the dictionary, as in the dictionary method \method{keys()} (see the
Fred Drakee058b4f1998-02-16 06:15:35 +00002028\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002029\end{cfuncdesc}
2030
Fred Drakec6fa34e1998-04-02 06:47:24 +00002031\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002032Returns a \ctype{PyListObject} containing all the values
Guido van Rossum44475131998-04-21 15:30:01 +00002033from the dictionary \var{p}, as in the dictionary method
Fred Drakee058b4f1998-02-16 06:15:35 +00002034\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002035\end{cfuncdesc}
2036
2037\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002038Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002039\end{cfuncdesc}
2040
2041\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
2042 int ppos,
2043 PyObject **pkey,
2044 PyObject **pvalue}
2045
2046\end{cfuncdesc}
2047
2048
Fred Drakeefd146c1999-02-15 15:30:45 +00002049\section{Numeric Objects \label{numericObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002050
Fred Drakeefd146c1999-02-15 15:30:45 +00002051\subsection{Plain Integer Objects \label{intObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002052
2053\begin{ctypedesc}{PyIntObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002054This subtype of \ctype{PyObject} represents a Python integer object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002055\end{ctypedesc}
2056
2057\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002058This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drakee5bf8b21998-02-12 21:22:28 +00002059integer type.
2060\end{cvardesc}
2061
2062\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
2063
2064\end{cfuncdesc}
2065
Fred Drakec6fa34e1998-04-02 06:47:24 +00002066\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00002067Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002068
2069The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00002070integers between \code{-1} and \code{100}, when you create an int in
2071that range you actually just get back a reference to the existing
2072object. So it should be possible to change the value of \code{1}. I
Fred Drake7e9d3141998-04-03 05:02:28 +00002073suspect the behaviour of Python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00002074\end{cfuncdesc}
2075
2076\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002077Returns the value of the object \var{io}. No error checking is
2078performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002079\end{cfuncdesc}
2080
2081\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakef8830d11998-04-23 14:06:01 +00002082Will first attempt to cast the object to a \ctype{PyIntObject}, if
Fred Drakee058b4f1998-02-16 06:15:35 +00002083it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002084\end{cfuncdesc}
2085
2086\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002087Returns the systems idea of the largest integer it can handle
2088(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002089\end{cfuncdesc}
2090
2091
Fred Drakeefd146c1999-02-15 15:30:45 +00002092\subsection{Long Integer Objects \label{longObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002093
2094\begin{ctypedesc}{PyLongObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002095This subtype of \ctype{PyObject} represents a Python long integer
Fred Drakee058b4f1998-02-16 06:15:35 +00002096object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002097\end{ctypedesc}
2098
2099\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002100This instance of \ctype{PyTypeObject} represents the Python long
Fred Drakee058b4f1998-02-16 06:15:35 +00002101integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002102\end{cvardesc}
2103
2104\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002105Returns true if its argument is a \ctype{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002106\end{cfuncdesc}
2107
Fred Drakec6fa34e1998-04-02 06:47:24 +00002108\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakef8830d11998-04-23 14:06:01 +00002109Returns a new \ctype{PyLongObject} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002110\end{cfuncdesc}
2111
Fred Drakec6fa34e1998-04-02 06:47:24 +00002112\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Fred Drakef8830d11998-04-23 14:06:01 +00002113Returns a new \ctype{PyLongObject} object from an unsigned \C{} long.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002114\end{cfuncdesc}
2115
Fred Drakec6fa34e1998-04-02 06:47:24 +00002116\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakef8830d11998-04-23 14:06:01 +00002117Returns a new \ctype{PyLongObject} object from the integer part of \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002118\end{cfuncdesc}
2119
Fred Drakec6fa34e1998-04-02 06:47:24 +00002120\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002121Returns a \C{} \ctype{long} representation of the contents of \var{pylong}.
2122WHAT HAPPENS IF \var{pylong} is greater than \constant{LONG_MAX}?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002123\end{cfuncdesc}
2124
Fred Drakec6fa34e1998-04-02 06:47:24 +00002125\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002126Returns a \C{} \ctype{unsigned long} representation of the contents of
2127\var{pylong}. WHAT HAPPENS IF \var{pylong} is greater than
2128\constant{ULONG_MAX}?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002129\end{cfuncdesc}
2130
Fred Drakec6fa34e1998-04-02 06:47:24 +00002131\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002132Returns a \C{} \ctype{double} representation of the contents of \var{pylong}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002133\end{cfuncdesc}
2134
Fred Drakec6fa34e1998-04-02 06:47:24 +00002135\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
2136 int base}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002137\end{cfuncdesc}
2138
2139
Fred Drakeefd146c1999-02-15 15:30:45 +00002140\subsection{Floating Point Objects \label{floatObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002141
2142\begin{ctypedesc}{PyFloatObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002143This subtype of \ctype{PyObject} represents a Python floating point
Fred Drakee058b4f1998-02-16 06:15:35 +00002144object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002145\end{ctypedesc}
2146
2147\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002148This instance of \ctype{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002149point type.
2150\end{cvardesc}
2151
2152\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002153Returns true if its argument is a \ctype{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002154\end{cfuncdesc}
2155
Fred Drakec6fa34e1998-04-02 06:47:24 +00002156\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakef8830d11998-04-23 14:06:01 +00002157Creates a \ctype{PyFloatObject} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002158\end{cfuncdesc}
2159
Fred Drakec6fa34e1998-04-02 06:47:24 +00002160\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Fred Drakef8830d11998-04-23 14:06:01 +00002161Returns a \C{} \ctype{double} representation of the contents of \var{pyfloat}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002162\end{cfuncdesc}
2163
Fred Drakec6fa34e1998-04-02 06:47:24 +00002164\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Fred Drakef8830d11998-04-23 14:06:01 +00002165Returns a \C{} \ctype{double} representation of the contents of
2166\var{pyfloat}, but without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002167\end{cfuncdesc}
2168
2169
Fred Drakeefd146c1999-02-15 15:30:45 +00002170\subsection{Complex Number Objects \label{complexObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002171
2172\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002173The \C{} structure which corresponds to the value portion of a Python
2174complex number object. Most of the functions for dealing with complex
2175number objects use structures of this type as input or output values,
2176as appropriate. It is defined as:
2177
Fred Drakee058b4f1998-02-16 06:15:35 +00002178\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002179typedef struct {
2180 double real;
2181 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002182} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002183\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002184\end{ctypedesc}
2185
2186\begin{ctypedesc}{PyComplexObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002187This subtype of \ctype{PyObject} represents a Python complex number object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002188\end{ctypedesc}
2189
2190\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002191This instance of \ctype{PyTypeObject} represents the Python complex
Fred Drakee5bf8b21998-02-12 21:22:28 +00002192number type.
2193\end{cvardesc}
2194
2195\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002196Returns true if its argument is a \ctype{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002197\end{cfuncdesc}
2198
Fred Drakec6fa34e1998-04-02 06:47:24 +00002199\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002200\end{cfuncdesc}
2201
Fred Drakec6fa34e1998-04-02 06:47:24 +00002202\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002203\end{cfuncdesc}
2204
Fred Drakec6fa34e1998-04-02 06:47:24 +00002205\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002206\end{cfuncdesc}
2207
Fred Drakec6fa34e1998-04-02 06:47:24 +00002208\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002209\end{cfuncdesc}
2210
Fred Drakec6fa34e1998-04-02 06:47:24 +00002211\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
2212 Py_complex divisor}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002213\end{cfuncdesc}
2214
Fred Drakec6fa34e1998-04-02 06:47:24 +00002215\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002216\end{cfuncdesc}
2217
Fred Drakec6fa34e1998-04-02 06:47:24 +00002218\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002219\end{cfuncdesc}
2220
Fred Drakec6fa34e1998-04-02 06:47:24 +00002221\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Fred Drakef8830d11998-04-23 14:06:01 +00002222Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002223\end{cfuncdesc}
2224
2225\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Fred Drakef8830d11998-04-23 14:06:01 +00002226Returns the real part of \var{op} as a \C{} \ctype{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002227\end{cfuncdesc}
2228
2229\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Fred Drakef8830d11998-04-23 14:06:01 +00002230Returns the imaginary part of \var{op} as a \C{} \ctype{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002231\end{cfuncdesc}
2232
2233\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002234\end{cfuncdesc}
2235
2236
2237
Fred Drakeefd146c1999-02-15 15:30:45 +00002238\section{Other Objects \label{otherObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002239
Fred Drakeefd146c1999-02-15 15:30:45 +00002240\subsection{File Objects \label{fileObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002241
2242\begin{ctypedesc}{PyFileObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002243This subtype of \ctype{PyObject} represents a Python file object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002244\end{ctypedesc}
2245
2246\begin{cvardesc}{PyTypeObject}{PyFile_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002247This instance of \ctype{PyTypeObject} represents the Python file type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002248\end{cvardesc}
2249
2250\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002251Returns true if its argument is a \ctype{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002252\end{cfuncdesc}
2253
Fred Drakec6fa34e1998-04-02 06:47:24 +00002254\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *name, char *mode}
Fred Drakef8830d11998-04-23 14:06:01 +00002255Creates a new \ctype{PyFileObject} pointing to the file
Fred Drakee058b4f1998-02-16 06:15:35 +00002256specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002257\end{cfuncdesc}
2258
Fred Drakec6fa34e1998-04-02 06:47:24 +00002259\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
Fred Drakeb92dce31998-02-25 15:40:22 +00002260 char *name, char *mode, int (*close)}
Fred Drakef8830d11998-04-23 14:06:01 +00002261Creates a new \ctype{PyFileObject} from the already-open \var{fp}.
Fred Drakee058b4f1998-02-16 06:15:35 +00002262The function \var{close} will be called when the file should be
2263closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002264\end{cfuncdesc}
2265
2266\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002267Returns the file object associated with \var{p} as a \ctype{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002268\end{cfuncdesc}
2269
Fred Drakec6fa34e1998-04-02 06:47:24 +00002270\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002271undocumented as yet
2272\end{cfuncdesc}
2273
Fred Drakec6fa34e1998-04-02 06:47:24 +00002274\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002275Returns the name of the file specified by \var{p} as a
Fred Drakef8830d11998-04-23 14:06:01 +00002276\ctype{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002277\end{cfuncdesc}
2278
2279\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002280Available on systems with \cfunction{setvbuf()} only. This should
2281only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002282\end{cfuncdesc}
2283
2284\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakef8830d11998-04-23 14:06:01 +00002285Sets the \member{softspace} attribute of \var{p} to \var{newflag}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002286Returns the previous value. This function clears any errors, and will
Fred Drakee058b4f1998-02-16 06:15:35 +00002287return \code{0} as the previous value if the attribute either does not
2288exist or if there were errors in retrieving it. There is no way to
2289detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002290\end{cfuncdesc}
2291
Fred Drakec6fa34e1998-04-02 06:47:24 +00002292\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2293 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002294Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002295\end{cfuncdesc}
2296
Fred Drakec6fa34e1998-04-02 06:47:24 +00002297\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p,
2298 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002299Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002300\end{cfuncdesc}
2301
2302
Fred Drakeefd146c1999-02-15 15:30:45 +00002303\subsection{Module Objects \label{moduleObjects}}
2304
2305\obindex{module}
2306There are only a few functions special to module objects.
2307
2308\begin{cfuncdesc}{PyObject *}{PyModule_New}{char *name}
2309Return a new module object with the \member{__name__} attribute set to
2310\var{name}. Only the module's \member{__doc__} and \member{__name__}
2311attributes are filled in; the caller is responsible for providing a
2312\member{__file__} attribute.
2313\end{cfuncdesc}
2314
2315\begin{cfuncdesc}{PyObject *}{PyModule_GetDict}{PyObject *module}
2316Return the dictionary object that implements \var{module}'s namespace;
2317this object is the same as the \member{__dict__} attribute of the
2318module object. This function never fails.
2319\end{cfuncdesc}
2320
2321\begin{cfuncdesc}{char *}{PyModule_GetName}{PyObject *module}
2322Return \var{module}'s \member{__name__} value. If the module does not
2323provide one, \exception{SystemError} is raised.
2324\end{cfuncdesc}
2325
2326\begin{cfuncdesc}{char *}{PyModule_GetFilename}{PyObject *module}
2327Return the name of the file from which \var{module} was loaded using
2328\var{module}'s \member{__file__} attribute. If this is not defined,
2329raise \exception{SystemError}.
2330\end{cfuncdesc}
2331
2332
2333\subsection{CObjects \label{cObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002334
Guido van Rossum44475131998-04-21 15:30:01 +00002335\begin{ctypedesc}{PyCObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002336This subtype of \ctype{PyObject} represents an opaque value, useful for
Guido van Rossum44475131998-04-21 15:30:01 +00002337\C{} extension modules who need to pass an opaque value (as a
Fred Drakef8830d11998-04-23 14:06:01 +00002338\ctype{void *} pointer) through Python code to other \C{} code. It is
Guido van Rossum44475131998-04-21 15:30:01 +00002339often used to make a C function pointer defined in one module
2340available to other modules, so the regular import mechanism can be
2341used to access C APIs defined in dynamically loaded modules.
2342\end{ctypedesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002343
Guido van Rossum44475131998-04-21 15:30:01 +00002344\begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtr}{void* cobj,
2345 void (*destr)(void *)}
Fred Drakef8830d11998-04-23 14:06:01 +00002346Creates a \ctype{PyCObject} from the \code{void *} \var{cobj}. The
2347\var{destr} function will be called when the object is reclaimed.
Guido van Rossum44475131998-04-21 15:30:01 +00002348\end{cfuncdesc}
2349
2350\begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2351 void* desc, void (*destr)(void *, void *) }
Fred Drakef8830d11998-04-23 14:06:01 +00002352Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2353\var{destr} function will be called when the object is reclaimed. The
2354\var{desc} argument can be used to pass extra callback data for the
2355destructor function.
Guido van Rossum44475131998-04-21 15:30:01 +00002356\end{cfuncdesc}
2357
2358\begin{cfuncdesc}{void *}{PyCObject_AsVoidPtr}{PyObject* self}
Fred Drakef8830d11998-04-23 14:06:01 +00002359Returns the object \ctype{void *} that the \ctype{PyCObject} \var{self}
Guido van Rossum44475131998-04-21 15:30:01 +00002360was created with.
2361\end{cfuncdesc}
2362
2363\begin{cfuncdesc}{void *}{PyCObject_GetDesc}{PyObject* self}
Fred Drakef8830d11998-04-23 14:06:01 +00002364Returns the description \ctype{void *} that the \ctype{PyCObject}
Guido van Rossum44475131998-04-21 15:30:01 +00002365\var{self} was created with.
2366\end{cfuncdesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002367
Fred Drakeefd146c1999-02-15 15:30:45 +00002368\chapter{Initialization, Finalization, and Threads
2369 \label{initialization}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002370
Guido van Rossum4a944d71997-08-14 20:35:38 +00002371\begin{cfuncdesc}{void}{Py_Initialize}{}
2372Initialize the Python interpreter. In an application embedding
2373Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002374functions; with the exception of \cfunction{Py_SetProgramName()},
2375\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2376and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2377loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002378modules \module{__builtin__}\refbimodindex{__builtin__},
2379\module{__main__}\refbimodindex{__main__} and
2380\module{sys}\refbimodindex{sys}. It also initializes the module
2381search path (\code{sys.path}).%
2382\indexiii{module}{search}{path}
2383It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2384that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002385\cfunction{Py_Finalize()} first). There is no return value; it is a
2386fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002387\end{cfuncdesc}
2388
2389\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002390Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002391initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2392called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002393again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002394\end{cfuncdesc}
2395
2396\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002397Undo all initializations made by \cfunction{Py_Initialize()} and
2398subsequent use of Python/C API functions, and destroy all
2399sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2400created and not yet destroyed since the last call to
2401\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2402by the Python interpreter. This is a no-op when called for a second
2403time (without calling \cfunction{Py_Initialize()} again first). There
2404is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002405
2406This function is provided for a number of reasons. An embedding
2407application might want to restart Python without having to restart the
2408application itself. An application that has loaded the Python
2409interpreter from a dynamically loadable library (or DLL) might want to
2410free all memory allocated by Python before unloading the DLL. During a
2411hunt for memory leaks in an application a developer might want to free
2412all memory allocated by Python before exiting from the application.
2413
Fred Drakee058b4f1998-02-16 06:15:35 +00002414\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002415modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002416(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002417(even functions) or modules. Dynamically loaded extension modules
2418loaded by Python are not unloaded. Small amounts of memory allocated
2419by the Python interpreter may not be freed (if you find a leak, please
2420report it). Memory tied up in circular references between objects is
2421not freed. Some memory allocated by extension modules may not be
2422freed. Some extension may not work properly if their initialization
2423routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002424calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2425than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002426\end{cfuncdesc}
2427
Fred Drakec6fa34e1998-04-02 06:47:24 +00002428\begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002429Create a new sub-interpreter. This is an (almost) totally separate
2430environment for the execution of Python code. In particular, the new
2431interpreter has separate, independent versions of all imported
2432modules, including the fundamental modules
2433\module{__builtin__}\refbimodindex{__builtin__},
2434\module{__main__}\refbimodindex{__main__} and
2435\module{sys}\refbimodindex{sys}. The table of loaded modules
2436(\code{sys.modules}) and the module search path (\code{sys.path}) are
2437also separate. The new environment has no \code{sys.argv} variable.
2438It has new standard I/O stream file objects \code{sys.stdin},
2439\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakef8830d11998-04-23 14:06:01 +00002440same underlying \ctype{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002441
2442The return value points to the first thread state created in the new
2443sub-interpreter. This thread state is made the current thread state.
2444Note that no actual thread is created; see the discussion of thread
2445states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002446\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002447is stored in the current thread state and there may not be a current
2448thread state. (Like all other Python/C API functions, the global
2449interpreter lock must be held before calling this function and is
2450still held when it returns; however, unlike most other Python/C API
2451functions, there needn't be a current thread state on entry.)
2452
2453Extension modules are shared between (sub-)interpreters as follows:
2454the first time a particular extension is imported, it is initialized
2455normally, and a (shallow) copy of its module's dictionary is
2456squirreled away. When the same extension is imported by another
2457(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002458contents of this copy; the extension's \code{init} function is not
2459called. Note that this is different from what happens when an
2460extension is imported after the interpreter has been completely
2461re-initialized by calling \cfunction{Py_Finalize()} and
2462\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002463function \emph{is} called again.
2464
Fred Drakee058b4f1998-02-16 06:15:35 +00002465\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002466interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002467isn't perfect --- for example, using low-level file operations like
Fred Drakef8830d11998-04-23 14:06:01 +00002468\function{os.close()} they can (accidentally or maliciously) affect each
Guido van Rossum4a944d71997-08-14 20:35:38 +00002469other's open files. Because of the way extensions are shared between
2470(sub-)interpreters, some extensions may not work properly; this is
2471especially likely when the extension makes use of (static) global
2472variables, or when the extension manipulates its module's dictionary
2473after its initialization. It is possible to insert objects created in
2474one sub-interpreter into a namespace of another sub-interpreter; this
2475should be done with great care to avoid sharing user-defined
2476functions, methods, instances or classes between sub-interpreters,
2477since import operations executed by such objects may affect the
2478wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2479a hard-to-fix bug that will be addressed in a future release.)
2480\end{cfuncdesc}
2481
2482\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
2483Destroy the (sub-)interpreter represented by the given thread state.
2484The given thread state must be the current thread state. See the
2485discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002486thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002487interpreted are destroyed. (The global interpreter lock must be held
2488before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002489\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002490been explicitly destroyed at that point.
2491\end{cfuncdesc}
2492
2493\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +00002494This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002495for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002496the value of the \code{argv[0]} argument to the \cfunction{main()} function
2497of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002498functions below to find the Python run-time libraries relative to the
2499interpreter executable. The default value is \code{"python"}. The
2500argument should point to a zero-terminated character string in static
2501storage whose contents will not change for the duration of the
2502program's execution. No code in the Python interpreter will change
2503the contents of this storage.
2504\end{cfuncdesc}
2505
Fred Drakec6fa34e1998-04-02 06:47:24 +00002506\begin{cfuncdesc}{char*}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002507Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002508default. The returned string points into static storage; the caller
2509should not modify its value.
2510\end{cfuncdesc}
2511
Fred Drakec6fa34e1998-04-02 06:47:24 +00002512\begin{cfuncdesc}{char*}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002513Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002514is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002515set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002516for example, if the program name is \code{"/usr/local/bin/python"},
2517the prefix is \code{"/usr/local"}. The returned string points into
2518static storage; the caller should not modify its value. This
Fred Drakec94d9341998-04-12 02:39:13 +00002519corresponds to the \makevar{prefix} variable in the top-level
2520\file{Makefile} and the \code{-}\code{-prefix} argument to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002521\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002522Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002523also the next function.
2524\end{cfuncdesc}
2525
Fred Drakec6fa34e1998-04-02 06:47:24 +00002526\begin{cfuncdesc}{char*}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002527Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002528files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002529program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002530variables; for example, if the program name is
2531\code{"/usr/local/bin/python"}, the exec-prefix is
2532\code{"/usr/local"}. The returned string points into static storage;
2533the caller should not modify its value. This corresponds to the
Fred Drakec94d9341998-04-12 02:39:13 +00002534\makevar{exec_prefix} variable in the top-level \file{Makefile} and the
2535\code{-}\code{-exec_prefix} argument to the \program{configure} script
2536at build time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002537\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002538
2539Background: The exec-prefix differs from the prefix when platform
2540dependent files (such as executables and shared libraries) are
2541installed in a different directory tree. In a typical installation,
2542platform dependent files may be installed in the
2543\code{"/usr/local/plat"} subtree while platform independent may be
2544installed in \code{"/usr/local"}.
2545
2546Generally speaking, a platform is a combination of hardware and
2547software families, e.g. Sparc machines running the Solaris 2.x
2548operating system are considered the same platform, but Intel machines
2549running Solaris 2.x are another platform, and Intel machines running
2550Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002551operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002552operating systems are a different story; the installation strategies
2553on those systems are so different that the prefix and exec-prefix are
2554meaningless, and set to the empty string. Note that compiled Python
2555bytecode files are platform independent (but not independent from the
2556Python version by which they were compiled!).
2557
Fred Drakee058b4f1998-02-16 06:15:35 +00002558System administrators will know how to configure the \program{mount} or
2559\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002560while having \code{"/usr/local/plat"} be a different filesystem for each
2561platform.
2562\end{cfuncdesc}
2563
Fred Drakec6fa34e1998-04-02 06:47:24 +00002564\begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002565Return the full program name of the Python executable; this is
2566computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002567from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002568returned string points into static storage; the caller should not
2569modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002570\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002571\end{cfuncdesc}
2572
Fred Drakec6fa34e1998-04-02 06:47:24 +00002573\begin{cfuncdesc}{char*}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002574\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002575Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002576program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002577environment variables. The returned string consists of a series of
2578directory names separated by a platform dependent delimiter character.
Fred Drakef8830d11998-04-23 14:06:01 +00002579The delimiter character is \character{:} on \UNIX{}, \character{;} on
2580DOS/Windows, and \character{\\n} (the \ASCII{} newline character) on
Fred Drakee5bc4971998-02-12 23:36:49 +00002581Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002582should not modify its value. The value is available to Python code
2583as the list \code{sys.path}, which may be modified to change the
2584future search path for loaded modules.
2585
2586% XXX should give the exact rules
2587\end{cfuncdesc}
2588
Fred Drakec6fa34e1998-04-02 06:47:24 +00002589\begin{cfuncdesc}{const char*}{Py_GetVersion}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002590Return the version of this Python interpreter. This is a string that
2591looks something like
2592
Guido van Rossum09270b51997-08-15 18:57:32 +00002593\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002594"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002595\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002596
2597The first word (up to the first space character) is the current Python
2598version; the first three characters are the major and minor version
2599separated by a period. The returned string points into static storage;
2600the caller should not modify its value. The value is available to
2601Python code as the list \code{sys.version}.
2602\end{cfuncdesc}
2603
Fred Drakec6fa34e1998-04-02 06:47:24 +00002604\begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002605Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002606this is formed from the ``official'' name of the operating system,
2607converted to lower case, followed by the major revision number; e.g.,
2608for Solaris 2.x, which is also known as SunOS 5.x, the value is
2609\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2610is \code{"win"}. The returned string points into static storage;
2611the caller should not modify its value. The value is available to
2612Python code as \code{sys.platform}.
2613\end{cfuncdesc}
2614
Fred Drakec6fa34e1998-04-02 06:47:24 +00002615\begin{cfuncdesc}{const char*}{Py_GetCopyright}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002616Return the official copyright string for the current Python version,
2617for example
2618
2619\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2620
2621The returned string points into static storage; the caller should not
2622modify its value. The value is available to Python code as the list
2623\code{sys.copyright}.
2624\end{cfuncdesc}
2625
Fred Drakec6fa34e1998-04-02 06:47:24 +00002626\begin{cfuncdesc}{const char*}{Py_GetCompiler}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002627Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002628version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002629
Fred Drakee058b4f1998-02-16 06:15:35 +00002630\begin{verbatim}
2631"[GCC 2.7.2.2]"
2632\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002633
2634The returned string points into static storage; the caller should not
2635modify its value. The value is available to Python code as part of
2636the variable \code{sys.version}.
2637\end{cfuncdesc}
2638
Fred Drakec6fa34e1998-04-02 06:47:24 +00002639\begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002640Return information about the sequence number and build date and time
2641of the current Python interpreter instance, for example
2642
Guido van Rossum09270b51997-08-15 18:57:32 +00002643\begin{verbatim}
2644"#67, Aug 1 1997, 22:34:28"
2645\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002646
2647The returned string points into static storage; the caller should not
2648modify its value. The value is available to Python code as part of
2649the variable \code{sys.version}.
2650\end{cfuncdesc}
2651
2652\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2653% XXX
2654\end{cfuncdesc}
2655
2656% XXX Other PySys thingies (doesn't really belong in this chapter)
2657
Fred Drakeefd146c1999-02-15 15:30:45 +00002658\section{Thread State and the Global Interpreter Lock
2659 \label{threads}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002660
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002661The Python interpreter is not fully thread safe. In order to support
2662multi-threaded Python programs, there's a global lock that must be
2663held by the current thread before it can safely access Python objects.
2664Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00002665a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002666increment the reference count of the same object, the reference count
2667could end up being incremented only once instead of twice.
2668
2669Therefore, the rule exists that only the thread that has acquired the
2670global interpreter lock may operate on Python objects or call Python/C
2671API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002672the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002673default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002674\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002675reacquired around potentially blocking I/O operations like reading or
2676writing a file, so that other threads can run while the thread that
2677requests the I/O is waiting for the I/O operation to complete.
2678
2679The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002680separate per thread --- for this it uses a data structure called
Fred Drakef8830d11998-04-23 14:06:01 +00002681\ctype{PyThreadState}. This is new in Python 1.5; in earlier versions,
Fred Drakee058b4f1998-02-16 06:15:35 +00002682such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002683cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002684when the application uses \function{sys.exc_info()} to access the
2685exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002686
2687There's one global variable left, however: the pointer to the current
Fred Drakef8830d11998-04-23 14:06:01 +00002688\ctype{PyThreadState} structure. While most thread packages have a way
Fred Drake9d20ac31998-02-16 15:27:08 +00002689to store ``per-thread global data,'' Python's internal platform
Fred Drakee058b4f1998-02-16 06:15:35 +00002690independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002691the current thread state must be manipulated explicitly.
2692
2693This is easy enough in most cases. Most code manipulating the global
2694interpreter lock has the following simple structure:
2695
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002696\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002697Save the thread state in a local variable.
2698Release the interpreter lock.
2699...Do some blocking I/O operation...
2700Reacquire the interpreter lock.
2701Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002702\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002703
2704This is so common that a pair of macros exists to simplify it:
2705
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002706\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002707Py_BEGIN_ALLOW_THREADS
2708...Do some blocking I/O operation...
2709Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002710\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002711
Fred Drakee058b4f1998-02-16 06:15:35 +00002712The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2713a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2714the block. Another advantage of using these two macros is that when
2715Python is compiled without thread support, they are defined empty,
2716thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002717
2718When thread support is enabled, the block above expands to the
2719following code:
2720
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002721\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002722{
2723 PyThreadState *_save;
2724 _save = PyEval_SaveThread();
2725 ...Do some blocking I/O operation...
2726 PyEval_RestoreThread(_save);
2727}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002728\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002729
2730Using even lower level primitives, we can get roughly the same effect
2731as follows:
2732
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002733\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002734{
2735 PyThreadState *_save;
2736 _save = PyThreadState_Swap(NULL);
2737 PyEval_ReleaseLock();
2738 ...Do some blocking I/O operation...
2739 PyEval_AcquireLock();
2740 PyThreadState_Swap(_save);
2741}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002742\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002743
2744There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002745\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Fred Drakef8830d11998-04-23 14:06:01 +00002746global variable \cdata{errno}, since the lock manipulation does not
2747guarantee that \cdata{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002748is disabled, \cfunction{PyEval_SaveThread()} and
2749\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2750case, \cfunction{PyEval_ReleaseLock()} and
2751\cfunction{PyEval_AcquireLock()} are not available. This is done so
2752that dynamically loaded extensions compiled with thread support
2753enabled can be loaded by an interpreter that was compiled with
2754disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002755
2756The global interpreter lock is used to protect the pointer to the
2757current thread state. When releasing the lock and saving the thread
2758state, the current thread state pointer must be retrieved before the
2759lock is released (since another thread could immediately acquire the
2760lock and store its own thread state in the global variable).
2761Reversely, when acquiring the lock and restoring the thread state, the
2762lock must be acquired before storing the thread state pointer.
2763
2764Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002765threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002766lock, nor is there a thread state data structure for them. Such
2767threads must bootstrap themselves into existence, by first creating a
2768thread state data structure, then acquiring the lock, and finally
2769storing their thread state pointer, before they can start using the
2770Python/C API. When they are done, they should reset the thread state
2771pointer, release the lock, and finally free their thread state data
2772structure.
2773
2774When creating a thread data structure, you need to provide an
2775interpreter state data structure. The interpreter state data
2776structure hold global data that is shared by all threads in an
2777interpreter, for example the module administration
2778(\code{sys.modules}). Depending on your needs, you can either create
2779a new interpreter state data structure, or share the interpreter state
2780data structure used by the Python main thread (to access the latter,
Fred Drakef8830d11998-04-23 14:06:01 +00002781you must obtain the thread state and access its \member{interp} member;
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002782this must be done by a thread that is created by Python or by the main
2783thread after Python is initialized).
2784
2785XXX More?
2786
2787\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002788This data structure represents the state shared by a number of
2789cooperating threads. Threads belonging to the same interpreter
2790share their module administration and a few other internal items.
2791There are no public members in this structure.
2792
2793Threads belonging to different interpreters initially share nothing,
2794except process state like available memory, open file descriptors and
2795such. The global interpreter lock is also shared by all threads,
2796regardless of to which interpreter they belong.
2797\end{ctypedesc}
2798
2799\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002800This data structure represents the state of a single thread. The only
Fred Drakef8830d11998-04-23 14:06:01 +00002801public data member is \ctype{PyInterpreterState *}\member{interp},
2802which points to this thread's interpreter state.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002803\end{ctypedesc}
2804
2805\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2806Initialize and acquire the global interpreter lock. It should be
2807called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002808in any other thread operations such as
2809\cfunction{PyEval_ReleaseLock()} or
2810\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2811calling \cfunction{PyEval_SaveThread()} or
2812\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002813
2814This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002815this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002816
2817When only the main thread exists, no lock operations are needed. This
2818is a common situation (most Python programs do not use threads), and
2819the lock operations slow the interpreter down a bit. Therefore, the
2820lock is not created initially. This situation is equivalent to having
2821acquired the lock: when there is only a single thread, all object
2822accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002823lock, it also acquires it. Before the Python
2824\module{thread}\refbimodindex{thread} module creates a new thread,
2825knowing that either it has the lock or the lock hasn't been created
2826yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2827returns, it is guaranteed that the lock has been created and that it
2828has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002829
2830It is \strong{not} safe to call this function when it is unknown which
2831thread (if any) currently has the global interpreter lock.
2832
2833This function is not available when thread support is disabled at
2834compile time.
2835\end{cfuncdesc}
2836
Guido van Rossum4a944d71997-08-14 20:35:38 +00002837\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002838Acquire the global interpreter lock. The lock must have been created
2839earlier. If this thread already has the lock, a deadlock ensues.
2840This function is not available when thread support is disabled at
2841compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002842\end{cfuncdesc}
2843
2844\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002845Release the global interpreter lock. The lock must have been created
2846earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002847disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002848\end{cfuncdesc}
2849
2850\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002851Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002852state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002853have been created earlier. If this thread already has the lock,
2854deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002855is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002856\end{cfuncdesc}
2857
2858\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002859Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002860interpreter lock. The lock must have been created earlier and must be
2861held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002862be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002863thread state --- if it isn't, a fatal error is reported. This
2864function is not available when thread support is disabled at compile
2865time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002866\end{cfuncdesc}
2867
Fred Drakec6fa34e1998-04-02 06:47:24 +00002868\begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002869Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002870support is enabled) and reset the thread state to \NULL{},
2871returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002872the lock has been created, the current thread must have acquired it.
2873(This function is available even when thread support is disabled at
2874compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002875\end{cfuncdesc}
2876
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002877\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002878Acquire the interpreter lock (if it has been created and thread
2879support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002880must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002881thread must not have acquired it, otherwise deadlock ensues. (This
2882function is available even when thread support is disabled at compile
2883time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002884\end{cfuncdesc}
2885
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002886% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2887\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2888This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002889\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002890Note that it contains an opening brace; it must be matched with a
2891following \code{Py_END_ALLOW_THREADS} macro. See above for further
2892discussion of this macro. It is a no-op when thread support is
2893disabled at compile time.
2894\end{ctypedesc}
2895
2896\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2897This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002898\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002899Note that it contains a closing brace; it must be matched with an
2900earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2901discussion of this macro. It is a no-op when thread support is
2902disabled at compile time.
2903\end{ctypedesc}
2904
2905\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002906This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002907is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2908brace. It is a no-op when thread support is disabled at compile
2909time.
2910\end{ctypedesc}
2911
2912\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002913This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002914equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2915and variable declaration. It is a no-op when thread support is
2916disabled at compile time.
2917\end{ctypedesc}
2918
2919All of the following functions are only available when thread support
2920is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00002921interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002922
Fred Drakec6fa34e1998-04-02 06:47:24 +00002923\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{}
Guido van Rossumed9dcc11998-08-07 18:28:03 +00002924Create a new interpreter state object. The interpreter lock need not
2925be held, but may be held if it is necessary to serialize calls to this
2926function.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002927\end{cfuncdesc}
2928
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002929\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2930Reset all information in an interpreter state object. The interpreter
2931lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002932\end{cfuncdesc}
2933
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002934\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2935Destroy an interpreter state object. The interpreter lock need not be
2936held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002937call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002938\end{cfuncdesc}
2939
Fred Drakec6fa34e1998-04-02 06:47:24 +00002940\begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002941Create a new thread state object belonging to the given interpreter
Guido van Rossumed9dcc11998-08-07 18:28:03 +00002942object. The interpreter lock need not be held, but may be held if it
2943is necessary to serialize calls to this function.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002944\end{cfuncdesc}
2945
2946\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2947Reset all information in a thread state object. The interpreter lock
2948must be held.
2949\end{cfuncdesc}
2950
2951\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2952Destroy a thread state object. The interpreter lock need not be
2953held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002954call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002955\end{cfuncdesc}
2956
Fred Drakec6fa34e1998-04-02 06:47:24 +00002957\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002958Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002959When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002960error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002961\end{cfuncdesc}
2962
Fred Drakec6fa34e1998-04-02 06:47:24 +00002963\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002964Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002965argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002966must be held.
2967\end{cfuncdesc}
2968
2969
Fred Drakeefd146c1999-02-15 15:30:45 +00002970\chapter{Defining New Object Types \label{newTypes}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002971
Fred Drakec6fa34e1998-04-02 06:47:24 +00002972\begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002973\end{cfuncdesc}
2974
Fred Drakec6fa34e1998-04-02 06:47:24 +00002975\begin{cfuncdesc}{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
Fred Drakee058b4f1998-02-16 06:15:35 +00002976\end{cfuncdesc}
2977
2978\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2979\end{cfuncdesc}
2980
2981\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2982\end{cfuncdesc}
2983
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002984Py_InitModule (!!!)
2985
2986PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse
2987
2988Py_BuildValue
Guido van Rossumae110af1997-05-22 20:11:52 +00002989
2990PyObject, PyVarObject
2991
2992PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2993
2994Typedefs:
2995unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2996intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2997getreadbufferproc, getwritebufferproc, getsegcountproc,
2998destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2999setattrofunc, cmpfunc, reprfunc, hashfunc
3000
3001PyNumberMethods
3002
3003PySequenceMethods
3004
3005PyMappingMethods
3006
3007PyBufferProcs
3008
3009PyTypeObject
3010
3011DL_IMPORT
3012
3013PyType_Type
3014
3015Py*_Check
3016
3017Py_None, _Py_NoneStruct
3018
Guido van Rossumae110af1997-05-22 20:11:52 +00003019
Fred Drakeefd146c1999-02-15 15:30:45 +00003020\chapter{Debugging \label{debugging}}
Guido van Rossumae110af1997-05-22 20:11:52 +00003021
Fred Drakee5bf8b21998-02-12 21:22:28 +00003022XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00003023
3024
Fred Drakef3aa0e01998-03-17 06:23:13 +00003025\input{api.ind} % Index -- must be last
Guido van Rossum9231c8f1997-05-15 21:43:21 +00003026
3027\end{document}