blob: 93e061c458fc53c136862c65d4c5ab685d94bdf7 [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
41\chapter{Introduction}
Fred Drakef39ed671998-02-26 22:01:23 +000042\label{intro}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000043
Fred Drakeb0a78731998-01-13 18:51:10 +000044The Application Programmer's Interface to Python gives \C{} and \Cpp{}
Guido van Rossum59a61351997-08-14 20:34:33 +000045programmers access to the Python interpreter at a variety of levels.
Fred Drakeb0a78731998-01-13 18:51:10 +000046The API is equally usable from \Cpp{}, but for brevity it is generally
47referred to as the Python/\C{} API. There are two fundamentally
Fred Drakee058b4f1998-02-16 06:15:35 +000048different reasons for using the Python/\C{} API. The first reason is
49to write \emph{extension modules} for specific purposes; these are
50\C{} modules that extend the Python interpreter. This is probably the
51most common use. The second reason is to use Python as a component in
52a larger application; this technique is generally referred to as
53\dfn{embedding} Python in an application.
Guido van Rossum59a61351997-08-14 20:34:33 +000054
Guido van Rossum4a944d71997-08-14 20:35:38 +000055Writing an extension module is a relatively well-understood process,
56where a ``cookbook'' approach works well. There are several tools
57that automate the process to some extent. While people have embedded
58Python in other applications since its early existence, the process of
59embedding Python is less straightforward that writing an extension.
60Python 1.5 introduces a number of new API functions as well as some
61changes to the build process that make embedding much simpler.
Fred Drakec6fa34e1998-04-02 06:47:24 +000062This manual describes the \version\ state of affairs.
Guido van Rossum59a61351997-08-14 20:34:33 +000063% XXX Eventually, take the historical notes out
64
Guido van Rossum4a944d71997-08-14 20:35:38 +000065Many API functions are useful independent of whether you're embedding
66or extending Python; moreover, most applications that embed Python
67will need to provide a custom extension as well, so it's probably a
68good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000069attempting to embed Python in a real application.
70
Guido van Rossum580aa8d1997-11-25 15:34:51 +000071\section{Include Files}
Fred Drakef39ed671998-02-26 22:01:23 +000072\label{includes}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000073
74All function, type and macro definitions needed to use the Python/C
75API are included in your code by the following line:
76
Fred Drakee058b4f1998-02-16 06:15:35 +000077\begin{verbatim}
78#include "Python.h"
79\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000080
Fred Drakee058b4f1998-02-16 06:15:35 +000081This implies inclusion of the following standard headers:
82\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
83\code{<stdlib.h>} (if available).
Guido van Rossum580aa8d1997-11-25 15:34:51 +000084
85All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000086the included standard headers) have one of the prefixes \samp{Py} or
87\samp{_Py}. Names beginning with \samp{_Py} are for internal use
Guido van Rossum580aa8d1997-11-25 15:34:51 +000088only. Structure member names do not have a reserved prefix.
89
Fred Drakee058b4f1998-02-16 06:15:35 +000090\strong{Important:} user code should never define names that begin
91with \samp{Py} or \samp{_Py}. This confuses the reader, and
92jeopardizes the portability of the user code to future Python
93versions, which may define additional names beginning with one of
94these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000095
Guido van Rossum59a61351997-08-14 20:34:33 +000096\section{Objects, Types and Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +000097\label{objects}
Guido van Rossum59a61351997-08-14 20:34:33 +000098
Guido van Rossum580aa8d1997-11-25 15:34:51 +000099Most Python/C API functions have one or more arguments as well as a
Fred Drakef8830d11998-04-23 14:06:01 +0000100return value of type \ctype{PyObject *}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +0000101to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000102object. Since all Python object types are treated the same way by the
103Python language in most situations (e.g., assignments, scope rules,
104and argument passing), it is only fitting that they should be
Fred Drakeb0a78731998-01-13 18:51:10 +0000105represented by a single \C{} type. All Python objects live on the heap:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000106you never declare an automatic or static variable of type
Fred Drakef8830d11998-04-23 14:06:01 +0000107\ctype{PyObject}, only pointer variables of type \ctype{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +0000108be declared.
109
Fred Drakee058b4f1998-02-16 06:15:35 +0000110All Python objects (even Python integers) have a \dfn{type} and a
111\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000112it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakee058b4f1998-02-16 06:15:35 +0000113many more as explained in the \emph{Python Reference Manual}). For
Guido van Rossum4a944d71997-08-14 20:35:38 +0000114each of the well-known types there is a macro to check whether an
Fred Drakee058b4f1998-02-16 06:15:35 +0000115object is of that type; for instance, \samp{PyList_Check(\var{a})} is
116true iff the object pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000117
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000118\subsection{Reference Counts}
Fred Drakef39ed671998-02-26 22:01:23 +0000119\label{refcounts}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000120
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000121The reference count is important because today's computers have a
Fred Drake003d8da1998-04-13 00:53:42 +0000122finite (and often severely limited) memory size; it counts how many
Guido van Rossum4a944d71997-08-14 20:35:38 +0000123different places there are that have a reference to an object. Such a
Fred Drakeb0a78731998-01-13 18:51:10 +0000124place could be another object, or a global (or static) \C{} variable, or
125a local variable in some \C{} function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000126becomes zero, the object is deallocated. If it contains references to
127other objects, their reference count is decremented. Those other
128objects may be deallocated in turn, if this decrement makes their
129reference count become zero, and so on. (There's an obvious problem
130with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000131``don't do that''.)
132
Guido van Rossum4a944d71997-08-14 20:35:38 +0000133Reference counts are always manipulated explicitly. The normal way is
Fred Drakee058b4f1998-02-16 06:15:35 +0000134to use the macro \cfunction{Py_INCREF()} to increment an object's
135reference count by one, and \cfunction{Py_DECREF()} to decrement it by
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000136one. The decref macro is considerably more complex than the incref one,
Guido van Rossum4a944d71997-08-14 20:35:38 +0000137since it must check whether the reference count becomes zero and then
138cause the object's deallocator, which is a function pointer contained
139in the object's type structure. The type-specific deallocator takes
140care of decrementing the reference counts for other objects contained
141in the object, and so on, if this is a compound object type such as a
142list. There's no chance that the reference count can overflow; at
143least as many bits are used to hold the reference count as there are
144distinct memory locations in virtual memory (assuming
145\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000146increment is a simple operation.
147
Guido van Rossum4a944d71997-08-14 20:35:38 +0000148It is not necessary to increment an object's reference count for every
149local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000150object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000151point to it and it goes down by one when the variable goes out of
152scope. However, these two cancel each other out, so at the end the
153reference count hasn't changed. The only real reason to use the
154reference count is to prevent the object from being deallocated as
155long as our variable is pointing to it. If we know that there is at
156least one other reference to the object that lives at least as long as
157our variable, there is no need to increment the reference count
158temporarily. An important situation where this arises is in objects
Fred Drakeb0a78731998-01-13 18:51:10 +0000159that are passed as arguments to \C{} functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000160that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000161reference to every argument for the duration of the call.
162
Fred Drakee058b4f1998-02-16 06:15:35 +0000163However, a common pitfall is to extract an object from a list and
164hold on to it for a while without incrementing its reference count.
165Some other operation might conceivably remove the object from the
166list, decrementing its reference count and possible deallocating it.
167The real danger is that innocent-looking operations may invoke
168arbitrary Python code which could do this; there is a code path which
169allows control to flow back to the user from a \cfunction{Py_DECREF()},
170so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000171
Guido van Rossum4a944d71997-08-14 20:35:38 +0000172A safe approach is to always use the generic operations (functions
Fred Drakee058b4f1998-02-16 06:15:35 +0000173whose name begins with \samp{PyObject_}, \samp{PyNumber_},
174\samp{PySequence_} or \samp{PyMapping_}). These operations always
Guido van Rossum4a944d71997-08-14 20:35:38 +0000175increment the reference count of the object they return. This leaves
Fred Drakee058b4f1998-02-16 06:15:35 +0000176the caller with the responsibility to call \cfunction{Py_DECREF()}
177when they are done with the result; this soon becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000178
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000179\subsubsection{Reference Count Details}
Fred Drakef39ed671998-02-26 22:01:23 +0000180\label{refcountDetails}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000181
182The reference count behavior of functions in the Python/C API is best
183expelained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000184talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000185shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000186properly --- either by passing ownership on (usually to its caller) or
187by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
188a function passes ownership of a reference on to its caller, the
189caller is said to receive a \emph{new} reference. When no ownership
190is transferred, the caller is said to \emph{borrow} the reference.
191Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000192
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000193Conversely, when calling a function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000194object, there are two possibilities: the function \emph{steals} a
195reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000196references; the two notable exceptions are
197\cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which
198steal a reference to the item (but not to the tuple or list into which
Fred Drake003d8da1998-04-13 00:53:42 +0000199the item is put!). These functions were designed to steal a reference
Fred Drakee058b4f1998-02-16 06:15:35 +0000200because of a common idiom for populating a tuple or list with newly
201created objects; for example, the code to create the tuple \code{(1,
2022, "three")} could look like this (forgetting about error handling for
203the moment; a better way to code this is shown below anyway):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000204
205\begin{verbatim}
206PyObject *t;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000207
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000208t = PyTuple_New(3);
209PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
210PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
211PyTuple_SetItem(t, 2, PyString_FromString("three"));
212\end{verbatim}
213
Fred Drakee058b4f1998-02-16 06:15:35 +0000214Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
215set tuple items; \cfunction{PySequence_SetItem()} and
216\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
217immutable data type. You should only use
218\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000219yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000220
221Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000222\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
223can also use \cfunction{PySequence_SetItem()}; this illustrates the
224difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000225
226\begin{verbatim}
227PyObject *l, *x;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000228
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000229l = PyList_New(3);
230x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000231PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000232x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000233PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000234x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000235PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000236\end{verbatim}
237
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000238You might find it strange that the ``recommended'' approach takes more
239code. However, in practice, you will rarely use these ways of
240creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000241\cfunction{Py_BuildValue()}, that can create most common objects from
242\C{} values, directed by a \dfn{format string}. For example, the
243above two blocks of code could be replaced by the following (which
244also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000245
246\begin{verbatim}
247PyObject *t, *l;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000248
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000249t = Py_BuildValue("(iis)", 1, 2, "three");
250l = Py_BuildValue("[iis]", 1, 2, "three");
251\end{verbatim}
252
Fred Drakee058b4f1998-02-16 06:15:35 +0000253It is much more common to use \cfunction{PyObject_SetItem()} and
254friends with items whose references you are only borrowing, like
255arguments that were passed in to the function you are writing. In
256that case, their behaviour regarding reference counts is much saner,
257since you don't have to increment a reference count so you can give a
258reference away (``have it be stolen''). For example, this function
259sets all items of a list (actually, any mutable sequence) to a given
260item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000261
262\begin{verbatim}
263int set_all(PyObject *target, PyObject *item)
264{
265 int i, n;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000266
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000267 n = PyObject_Length(target);
268 if (n < 0)
269 return -1;
270 for (i = 0; i < n; i++) {
271 if (PyObject_SetItem(target, i, item) < 0)
272 return -1;
273 }
274 return 0;
275}
276\end{verbatim}
277
278The situation is slightly different for function return values.
279While passing a reference to most functions does not change your
280ownership responsibilities for that reference, many functions that
281return a referece to an object give you ownership of the reference.
282The reason is simple: in many cases, the returned object is created
283on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000284object. Therefore, the generic functions that return object
285references, like \cfunction{PyObject_GetItem()} and
286\cfunction{PySequence_GetItem()}, always return a new reference (i.e.,
287the caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000288
289It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000290by a function depends on which function you call only --- \emph{the
291plumage} (i.e., the type of the type of the object passed as an
292argument to the function) \emph{doesn't enter into it!} Thus, if you
293extract an item from a list using \cfunction{PyList_GetItem()}, you
294don't own the reference --- but if you obtain the same item from the
295same list using \cfunction{PySequence_GetItem()} (which happens to
296take exactly the same arguments), you do own a reference to the
297returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000298
Fred Drakee058b4f1998-02-16 06:15:35 +0000299Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000300sum of the items in a list of integers; once using
Fred Drakee058b4f1998-02-16 06:15:35 +0000301\cfunction{PyList_GetItem()}, once using
302\cfunction{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000303
304\begin{verbatim}
305long sum_list(PyObject *list)
306{
307 int i, n;
308 long total = 0;
309 PyObject *item;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000310
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000311 n = PyList_Size(list);
312 if (n < 0)
313 return -1; /* Not a list */
314 for (i = 0; i < n; i++) {
315 item = PyList_GetItem(list, i); /* Can't fail */
316 if (!PyInt_Check(item)) continue; /* Skip non-integers */
317 total += PyInt_AsLong(item);
318 }
319 return total;
320}
321\end{verbatim}
322
323\begin{verbatim}
324long sum_sequence(PyObject *sequence)
325{
326 int i, n;
327 long total = 0;
328 PyObject *item;
329 n = PyObject_Size(list);
330 if (n < 0)
331 return -1; /* Has no length */
332 for (i = 0; i < n; i++) {
333 item = PySequence_GetItem(list, i);
334 if (item == NULL)
335 return -1; /* Not a sequence, or other failure */
336 if (PyInt_Check(item))
337 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000338 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000339 }
340 return total;
341}
342\end{verbatim}
343
344\subsection{Types}
Fred Drakef39ed671998-02-26 22:01:23 +0000345\label{types}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000346
347There are few other data types that play a significant role in
Fred Drakef8830d11998-04-23 14:06:01 +0000348the Python/C API; most are simple \C{} types such as \ctype{int},
349\ctype{long}, \ctype{double} and \ctype{char *}. A few structure types
Guido van Rossum4a944d71997-08-14 20:35:38 +0000350are used to describe static tables used to list the functions exported
351by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000352be discussed together with the functions that use them.
353
354\section{Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000355\label{exceptions}
Guido van Rossum59a61351997-08-14 20:34:33 +0000356
Guido van Rossum4a944d71997-08-14 20:35:38 +0000357The Python programmer only needs to deal with exceptions if specific
358error handling is required; unhandled exceptions are automatically
359propagated to the caller, then to the caller's caller, and so on, till
360they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000361user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000362
Fred Drakeb0a78731998-01-13 18:51:10 +0000363For \C{} programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000364All functions in the Python/C API can raise exceptions, unless an
365explicit claim is made otherwise in a function's documentation. In
366general, when a function encounters an error, it sets an exception,
367discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000368error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000369return a Boolean true/false result, with false indicating an error.
370Very few functions return no explicit error indicator or have an
371ambiguous return value, and require explicit testing for errors with
Fred Drakee058b4f1998-02-16 06:15:35 +0000372\cfunction{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000373
374Exception state is maintained in per-thread storage (this is
375equivalent to using global storage in an unthreaded application). A
Fred Drakec6fa34e1998-04-02 06:47:24 +0000376thread can be in one of two states: an exception has occurred, or not.
Fred Drakee058b4f1998-02-16 06:15:35 +0000377The function \cfunction{PyErr_Occurred()} can be used to check for
378this: it returns a borrowed reference to the exception type object
379when an exception has occurred, and \NULL{} otherwise. There are a
380number of functions to set the exception state:
381\cfunction{PyErr_SetString()} is the most common (though not the most
382general) function to set the exception state, and
383\cfunction{PyErr_Clear()} clears the exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000384
385The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000386be \NULL{}): the exception type, the corresponding exception
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000387value, and the traceback. These have the same meanings as the Python
388object \code{sys.exc_type}, \code{sys.exc_value},
389\code{sys.exc_traceback}; however, they are not the same: the Python
390objects represent the last exception being handled by a Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000391\keyword{try} \ldots\ \keyword{except} statement, while the \C{} level
392exception state only exists while an exception is being passed on
393between \C{} functions until it reaches the Python interpreter, which
394takes care of transferring it to \code{sys.exc_type} and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000395
Fred Drakec6fa34e1998-04-02 06:47:24 +0000396Note that starting with Python 1.5, the preferred, thread-safe way to
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000397access the exception state from Python code is to call the function
Fred Drakee058b4f1998-02-16 06:15:35 +0000398\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000399for Python code. Also, the semantics of both ways to access the
400exception state have changed so that a function which catches an
401exception will save and restore its thread's exception state so as to
402preserve the exception state of its caller. This prevents common bugs
403in exception handling code caused by an innocent-looking function
404overwriting the exception being handled; it also reduces the often
405unwanted lifetime extension for objects that are referenced by the
Fred Drakec6fa34e1998-04-02 06:47:24 +0000406stack frames in the traceback.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000407
408As a general principle, a function that calls another function to
409perform some task should check whether the called function raised an
410exception, and if so, pass the exception state on to its caller. It
Fred Drakee058b4f1998-02-16 06:15:35 +0000411should discard any object references that it owns, and returns an
412error indicator, but it should \emph{not} set another exception ---
413that would overwrite the exception that was just raised, and lose
414important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000415
416A simple example of detecting exceptions and passing them on is shown
Fred Drakee058b4f1998-02-16 06:15:35 +0000417in the \cfunction{sum_sequence()} example above. It so happens that
418that example doesn't need to clean up any owned references when it
419detects an error. The following example function shows some error
420cleanup. First, to remind you why you like Python, we show the
421equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000422
423\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000424def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000425 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000426 item = dict[key]
427 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000428 item = 0
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000429 return item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000430\end{verbatim}
431
Fred Drakeb0a78731998-01-13 18:51:10 +0000432Here is the corresponding \C{} code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000433
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000434\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000435int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000436{
437 /* Objects all initialized to NULL for Py_XDECREF */
438 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000439 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000440
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000441 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000442 if (item == NULL) {
Fred Drakec6fa34e1998-04-02 06:47:24 +0000443 /* Handle KeyError only: */
444 if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000445
446 /* Clear the error and use zero: */
447 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000448 item = PyInt_FromLong(0L);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000449 if (item == NULL) goto error;
450 }
451
452 const_one = PyInt_FromLong(1L);
453 if (const_one == NULL) goto error;
454
455 incremented_item = PyNumber_Add(item, const_one);
456 if (incremented_item == NULL) goto error;
457
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000458 if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000459 rv = 0; /* Success */
460 /* Continue with cleanup code */
461
462 error:
463 /* Cleanup code, shared by success and failure path */
464
465 /* Use Py_XDECREF() to ignore NULL references */
466 Py_XDECREF(item);
467 Py_XDECREF(const_one);
468 Py_XDECREF(incremented_item);
469
470 return rv; /* -1 for error, 0 for success */
471}
472\end{verbatim}
473
Fred Drakef8830d11998-04-23 14:06:01 +0000474This example represents an endorsed use of the \keyword{goto} statement
Fred Drakee058b4f1998-02-16 06:15:35 +0000475in \C{}! It illustrates the use of
476\cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to
477handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to
478dispose of owned references that may be \NULL{} (note the \samp{X} in
479the name; \cfunction{Py_DECREF()} would crash when confronted with a
480\NULL{} reference). It is important that the variables used to hold
481owned references are initialized to \NULL{} for this to work;
482likewise, the proposed return value is initialized to \code{-1}
483(failure) and only set to success after the final call made is
484successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000485
Guido van Rossum59a61351997-08-14 20:34:33 +0000486
487\section{Embedding Python}
Fred Drakef39ed671998-02-26 22:01:23 +0000488\label{embedding}
Guido van Rossum59a61351997-08-14 20:34:33 +0000489
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000490The one important task that only embedders (as opposed to extension
491writers) of the Python interpreter have to worry about is the
492initialization, and possibly the finalization, of the Python
493interpreter. Most functionality of the interpreter can only be used
494after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000495
Fred Drakee058b4f1998-02-16 06:15:35 +0000496The basic initialization function is \cfunction{Py_Initialize()}.
497This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000498fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
499\module{__main__}\refbimodindex{__main__} and
500\module{sys}\refbimodindex{sys}. It also initializes the module
Fred Drakec6fa34e1998-04-02 06:47:24 +0000501search path (\code{sys.path}).%
502\indexiii{module}{search}{path}
Guido van Rossum59a61351997-08-14 20:34:33 +0000503
Fred Drakee058b4f1998-02-16 06:15:35 +0000504\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000505(\code{sys.argv}). If this variable is needed by Python code that
506will be executed later, it must be set explicitly with a call to
507\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Fred Drakee058b4f1998-02-16 06:15:35 +0000508to \cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000509
Fred Drakeb0a78731998-01-13 18:51:10 +0000510On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drakee058b4f1998-02-16 06:15:35 +0000511details are slightly different), \cfunction{Py_Initialize()}
512calculates the module search path based upon its best guess for the
513location of the standard Python interpreter executable, assuming that
514the Python library is found in a fixed location relative to the Python
Guido van Rossum42cefd01997-10-05 15:27:29 +0000515interpreter executable. In particular, it looks for a directory named
Fred Drake2de75ec1998-04-09 14:12:11 +0000516\file{lib/python1.5} (replacing \file{1.5} with the current
Guido van Rossum42cefd01997-10-05 15:27:29 +0000517interpreter version) relative to the parent directory where the
Fred Drakee058b4f1998-02-16 06:15:35 +0000518executable named \file{python} is found on the shell command search
Fred Drakec6fa34e1998-04-02 06:47:24 +0000519path (the environment variable \envvar{PATH}).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000520
521For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000522\file{/usr/local/bin/python}, it will assume that the libraries are in
Fred Drake2de75ec1998-04-09 14:12:11 +0000523\file{/usr/local/lib/python1.5}. (In fact, this particular path
Fred Drakee058b4f1998-02-16 06:15:35 +0000524is also the ``fallback'' location, used when no executable file named
Fred Drakec6fa34e1998-04-02 06:47:24 +0000525\file{python} is found along \envvar{PATH}.) The user can override
526this behavior by setting the environment variable \envvar{PYTHONHOME},
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000527or insert additional directories in front of the standard path by
Fred Drakec6fa34e1998-04-02 06:47:24 +0000528setting \envvar{PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000529
Guido van Rossum4a944d71997-08-14 20:35:38 +0000530The embedding application can steer the search by calling
531\code{Py_SetProgramName(\var{file})} \emph{before} calling
Fred Drakec6fa34e1998-04-02 06:47:24 +0000532\cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still
533overrides this and \envvar{PYTHONPATH} is still inserted in front of
Fred Drakee058b4f1998-02-16 06:15:35 +0000534the standard path. An application that requires total control has to
535provide its own implementation of \cfunction{Py_GetPath()},
536\cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()},
537\cfunction{Py_GetProgramFullPath()} (all defined in
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000538\file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000539
Guido van Rossum4a944d71997-08-14 20:35:38 +0000540Sometimes, it is desirable to ``uninitialize'' Python. For instance,
541the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000542\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000543use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000544can be accomplished by calling \cfunction{Py_Finalize()}. The function
545\cfunction{Py_IsInitialized()} returns true iff Python is currently in the
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000546initialized state. More information about these functions is given in
547a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000548
Guido van Rossum4a944d71997-08-14 20:35:38 +0000549
Fred Drakee5bf8b21998-02-12 21:22:28 +0000550\chapter{The Very High Level Layer}
Fred Drakef39ed671998-02-26 22:01:23 +0000551\label{veryhigh}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000552
Fred Drakee5bf8b21998-02-12 21:22:28 +0000553The functions in this chapter will let you execute Python source code
554given in a file or a buffer, but they will not let you interact in a
555more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000556
Fred Drakec6fa34e1998-04-02 06:47:24 +0000557\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000558\end{cfuncdesc}
559
Fred Drakec6fa34e1998-04-02 06:47:24 +0000560\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000561\end{cfuncdesc}
562
Fred Drakec6fa34e1998-04-02 06:47:24 +0000563\begin{cfuncdesc}{int}{PyRun_SimpleFile}{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_InteractiveOne}{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}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000570\end{cfuncdesc}
571
Fred Drakec6fa34e1998-04-02 06:47:24 +0000572\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str,
573 int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000574\end{cfuncdesc}
575
Fred Drakec6fa34e1998-04-02 06:47:24 +0000576\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp,
577 char *filename, int start}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000578\end{cfuncdesc}
579
Fred Drakec6fa34e1998-04-02 06:47:24 +0000580\begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start,
581 PyObject *globals,
582 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000583\end{cfuncdesc}
584
Fred Drakec6fa34e1998-04-02 06:47:24 +0000585\begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename,
586 int start, PyObject *globals,
587 PyObject *locals}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000588\end{cfuncdesc}
589
Fred Drakec6fa34e1998-04-02 06:47:24 +0000590\begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename,
591 int start}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000592\end{cfuncdesc}
593
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000594
595\chapter{Reference Counting}
Fred Drakef39ed671998-02-26 22:01:23 +0000596\label{countingRefs}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000597
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000598The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000599of Python objects.
600
601\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
Fred Drakec6fa34e1998-04-02 06:47:24 +0000602Increment the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000603not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000604\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000605\end{cfuncdesc}
606
607\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000608Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000609\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000610\end{cfuncdesc}
611
612\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000613Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000614not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000615\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
616object's type's deallocation function (which must not be \NULL{}) is
617invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000618
619\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drakee058b4f1998-02-16 06:15:35 +0000620code to be invoked (e.g. when a class instance with a \method{__del__()}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000621method is deallocated). While exceptions in such code are not
622propagated, the executed code has free access to all Python global
623variables. This means that any object that is reachable from a global
Fred Drakee058b4f1998-02-16 06:15:35 +0000624variable should be in a consistent state before \cfunction{Py_DECREF()} is
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000625invoked. For example, code to delete an object from a list should
626copy a reference to the deleted object in a temporary variable, update
Fred Drakee058b4f1998-02-16 06:15:35 +0000627the list data structure, and then call \cfunction{Py_DECREF()} for the
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000628temporary variable.
629\end{cfuncdesc}
630
631\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000632Decrement the reference count for object \var{o}. The object may be
633\NULL{}, in which case the macro has no effect; otherwise the effect
634is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000635applies.
636\end{cfuncdesc}
637
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000638The following functions or macros are only for internal use:
Fred Drakee058b4f1998-02-16 06:15:35 +0000639\cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()},
640\cfunction{_Py_NewReference()}, as well as the global variable
Fred Drakef8830d11998-04-23 14:06:01 +0000641\cdata{_Py_RefTotal}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000642
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000643XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(),
644PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(),
645PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL().
646
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000647
648\chapter{Exception Handling}
Fred Drakef39ed671998-02-26 22:01:23 +0000649\label{exceptionHandling}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000650
651The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000652exceptions. It is important to understand some of the basics of
Fred Drakeb0a78731998-01-13 18:51:10 +0000653Python exception handling. It works somewhat like the \UNIX{}
Fred Drakef8830d11998-04-23 14:06:01 +0000654\cdata{errno} variable: there is a global indicator (per thread) of the
Guido van Rossumae110af1997-05-22 20:11:52 +0000655last error that occurred. Most functions don't clear this on success,
656but will set it to indicate the cause of the error on failure. Most
657functions also return an error indicator, usually \NULL{} if they are
Fred Drakee058b4f1998-02-16 06:15:35 +0000658supposed to return a pointer, or \code{-1} if they return an integer
Fred Drakef8830d11998-04-23 14:06:01 +0000659(exception: the \cfunction{PyArg_Parse*()} functions return \code{1} for
Fred Drakee058b4f1998-02-16 06:15:35 +0000660success and \code{0} for failure). When a function must fail because
661some function it called failed, it generally doesn't set the error
662indicator; the function it called already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000663
664The error indicator consists of three Python objects corresponding to
665the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
666\code{sys.exc_traceback}. API functions exist to interact with the
667error indicator in various ways. There is a separate error indicator
668for each thread.
669
670% XXX Order of these should be more thoughtful.
671% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000672
673\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000674Print a standard traceback to \code{sys.stderr} and clear the error
675indicator. Call this function only when the error indicator is set.
676(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000677\end{cfuncdesc}
678
Fred Drakec6fa34e1998-04-02 06:47:24 +0000679\begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000680Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000681\emph{type} (the first argument to the last call to one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000682\cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
Fred Drakee058b4f1998-02-16 06:15:35 +0000683not set, return \NULL{}. You do not own a reference to the return
684value, so you do not need to \cfunction{Py_DECREF()} it.
685\strong{Note:} do not compare the return value to a specific
686exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
687below.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000688\end{cfuncdesc}
689
690\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000691Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000692\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000693This should only be called when an exception is actually set.
694\end{cfuncdesc}
695
696\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000697Return true if the \var{given} exception matches the exception in
698\var{exc}. If \var{exc} is a class object, this also returns true
699when \var{given} is a subclass. If \var{exc} is a tuple, all
700exceptions in the tuple (and recursively in subtuples) are searched
701for a match. This should only be called when an exception is actually
702set.
703\end{cfuncdesc}
704
705\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000706Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000707\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
708\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
709instance of the same class. This function can be used to instantiate
710the class in that case. If the values are already normalized, nothing
711happens.
Guido van Rossumae110af1997-05-22 20:11:52 +0000712\end{cfuncdesc}
713
714\begin{cfuncdesc}{void}{PyErr_Clear}{}
715Clear the error indicator. If the error indicator is not set, there
716is no effect.
717\end{cfuncdesc}
718
719\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
720Retrieve the error indicator into three variables whose addresses are
721passed. If the error indicator is not set, set all three variables to
722\NULL{}. If it is set, it will be cleared and you own a reference to
723each object retrieved. The value and traceback object may be \NULL{}
724even when the type object is not. \strong{Note:} this function is
725normally only used by code that needs to handle exceptions or by code
726that needs to save and restore the error indicator temporarily.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
730Set the error indicator from the three objects. If the error
731indicator is already set, it is cleared first. If the objects are
732\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
733and non-\NULL{} value or traceback. The exception type should be a
734string or class; if it is a class, the value should be an instance of
735that class. Do not pass an invalid exception type or value.
736(Violating these rules will cause subtle problems later.) This call
737takes away a reference to each object, i.e. you must own a reference
738to each object before the call and after the call you no longer own
739these references. (If you don't understand this, don't use this
740function. I warned you.) \strong{Note:} this function is normally
741only used by code that needs to save and restore the error indicator
742temporarily.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
746This is the most common way to set the error indicator. The first
747argument specifies the exception type; it is normally one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000748standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not
Guido van Rossumae110af1997-05-22 20:11:52 +0000749increment its reference count. The second argument is an error
750message; it is converted to a string object.
751\end{cfuncdesc}
752
753\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000754This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000755specify an arbitrary Python object for the ``value'' of the exception.
756You need not increment its reference count.
757\end{cfuncdesc}
758
759\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000760This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000761\end{cfuncdesc}
762
763\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000764This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000765\var{message})}, where \var{message} indicates that a built-in operation
766was invoked with an illegal argument. It is mostly for internal use.
767\end{cfuncdesc}
768
Fred Drakec6fa34e1998-04-02 06:47:24 +0000769\begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000770This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000771returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000772\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000773\end{cfuncdesc}
774
Fred Drakec6fa34e1998-04-02 06:47:24 +0000775\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
Fred Drakeb0a78731998-01-13 18:51:10 +0000776This is a convenience function to raise an exception when a \C{} library
Fred Drakef8830d11998-04-23 14:06:01 +0000777function has returned an error and set the \C{} variable \cdata{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000778It constructs a tuple object whose first item is the integer
Fred Drakef8830d11998-04-23 14:06:01 +0000779\cdata{errno} value and whose second item is the corresponding error
Fred Drakee058b4f1998-02-16 06:15:35 +0000780message (gotten from \cfunction{strerror()}), and then calls
781\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
Fred Drakef8830d11998-04-23 14:06:01 +0000782the \cdata{errno} value is \constant{EINTR}, indicating an interrupted
Fred Drakee058b4f1998-02-16 06:15:35 +0000783system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000784the error indicator, leaves it set to that. The function always
785returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000786\samp{return PyErr_SetFromErrno();} when the system call returns an
787error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000788\end{cfuncdesc}
789
790\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000791This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000792\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000793operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000794argument. It is mostly for internal use.
795\end{cfuncdesc}
796
797\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
798This function interacts with Python's signal handling. It checks
799whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +0000800corresponding signal handler. If the
801\module{signal}\refbimodindex{signal} module is supported, this can
802invoke a signal handler written in Python. In all cases, the default
803effect for \constant{SIGINT} is to raise the
804\exception{KeyboadInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +0000805error indicator is set and the function returns \code{1}; otherwise
806the function returns \code{0}. The error indicator may or may not be
807cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +0000808\end{cfuncdesc}
809
810\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
811This function is obsolete (XXX or platform dependent?). It simulates
Fred Drakee058b4f1998-02-16 06:15:35 +0000812the effect of a \constant{SIGINT} signal arriving --- the next time
813\cfunction{PyErr_CheckSignals()} is called,
814\exception{KeyboadInterrupt} will be raised.
Guido van Rossumae110af1997-05-22 20:11:52 +0000815\end{cfuncdesc}
816
Fred Drakec6fa34e1998-04-02 06:47:24 +0000817\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
818 PyObject *base,
819 PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000820This utility function creates and returns a new exception object. The
Fred Drakeb0a78731998-01-13 18:51:10 +0000821\var{name} argument must be the name of the new exception, a \C{} string
Guido van Rossum42cefd01997-10-05 15:27:29 +0000822of the form \code{module.class}. The \var{base} and \var{dict}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000823arguments are normally \NULL{}. Normally, this creates a class
Guido van Rossum42cefd01997-10-05 15:27:29 +0000824object derived from the root for all exceptions, the built-in name
Fred Drakef8830d11998-04-23 14:06:01 +0000825\exception{Exception} (accessible in \C{} as \cdata{PyExc_Exception}).
826In this case the \member{__module__} attribute of the new class is set to the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000827first part (up to the last dot) of the \var{name} argument, and the
828class name is set to the last part (after the last dot). When the
829user has specified the \code{-X} command line option to use string
830exceptions, for backward compatibility, or when the \var{base}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000831argument is not a class object (and not \NULL{}), a string object
Guido van Rossum42cefd01997-10-05 15:27:29 +0000832created from the entire \var{name} argument is returned. The
833\var{base} argument can be used to specify an alternate base class.
834The \var{dict} argument can be used to specify a dictionary of class
835variables and methods.
836\end{cfuncdesc}
837
838
Guido van Rossumae110af1997-05-22 20:11:52 +0000839\section{Standard Exceptions}
Fred Drakef39ed671998-02-26 22:01:23 +0000840\label{standardExceptions}
Guido van Rossumae110af1997-05-22 20:11:52 +0000841
842All standard Python exceptions are available as global variables whose
Fred Drakee058b4f1998-02-16 06:15:35 +0000843names are \samp{PyExc_} followed by the Python exception name.
Fred Drakef8830d11998-04-23 14:06:01 +0000844These have the type \ctype{PyObject *}; they are all either class
Fred Drakee058b4f1998-02-16 06:15:35 +0000845objects or string objects, depending on the use of the \code{-X}
846option to the interpreter. For completeness, here are all the
Fred Drake9d20ac31998-02-16 15:27:08 +0000847variables:
Fred Drakef8830d11998-04-23 14:06:01 +0000848\cdata{PyExc_Exception},
849\cdata{PyExc_StandardError},
850\cdata{PyExc_ArithmeticError},
851\cdata{PyExc_LookupError},
852\cdata{PyExc_AssertionError},
853\cdata{PyExc_AttributeError},
854\cdata{PyExc_EOFError},
855\cdata{PyExc_FloatingPointError},
856\cdata{PyExc_IOError},
857\cdata{PyExc_ImportError},
858\cdata{PyExc_IndexError},
859\cdata{PyExc_KeyError},
860\cdata{PyExc_KeyboardInterrupt},
861\cdata{PyExc_MemoryError},
862\cdata{PyExc_NameError},
863\cdata{PyExc_OverflowError},
864\cdata{PyExc_RuntimeError},
865\cdata{PyExc_SyntaxError},
866\cdata{PyExc_SystemError},
867\cdata{PyExc_SystemExit},
868\cdata{PyExc_TypeError},
869\cdata{PyExc_ValueError},
870\cdata{PyExc_ZeroDivisionError}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000871
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000872
873\chapter{Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000874\label{utilities}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000875
876The functions in this chapter perform various utility tasks, such as
Fred Drakeb0a78731998-01-13 18:51:10 +0000877parsing function arguments and constructing Python values from \C{}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000878values.
879
Guido van Rossum42cefd01997-10-05 15:27:29 +0000880\section{OS Utilities}
Fred Drakef39ed671998-02-26 22:01:23 +0000881\label{os}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000882
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000883\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000884Return true (nonzero) if the standard I/O file \var{fp} with name
885\var{filename} is deemed interactive. This is the case for files for
886which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Fred Drakef8830d11998-04-23 14:06:01 +0000887\cdata{Py_InteractiveFlag} is true, this function also returns true if
Fred Drakee058b4f1998-02-16 06:15:35 +0000888the \var{name} pointer is \NULL{} or if the name is equal to one of
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000889the strings \code{"<stdin>"} or \code{"???"}.
890\end{cfuncdesc}
891
892\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +0000893Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000894The result is encoded in the same way as the timestamp returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000895the standard \C{} library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000896\end{cfuncdesc}
897
898
Fred Drakee5bf8b21998-02-12 21:22:28 +0000899\section{Process Control}
Fred Drakef39ed671998-02-26 22:01:23 +0000900\label{processControl}
Fred Drakee5bf8b21998-02-12 21:22:28 +0000901
902\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
903Print a fatal error message and kill the process. No cleanup is
904performed. This function should only be invoked when a condition is
905detected that would make it dangerous to continue using the Python
906interpreter; e.g., when the object administration appears to be
Fred Drakee058b4f1998-02-16 06:15:35 +0000907corrupted. On \UNIX{}, the standard \C{} library function
908\cfunction{abort()} is called which will attempt to produce a
909\file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000910\end{cfuncdesc}
911
912\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drakee058b4f1998-02-16 06:15:35 +0000913Exit the current process. This calls \cfunction{Py_Finalize()} and
914then calls the standard \C{} library function
915\code{exit(\var{status})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000916\end{cfuncdesc}
917
918\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
919Register a cleanup function to be called by \cfunction{Py_Finalize()}.
920The cleanup function will be called with no arguments and should
921return no value. At most 32 cleanup functions can be registered.
922When the registration is successful, \cfunction{Py_AtExit()} returns
923\code{0}; on failure, it returns \code{-1}. The cleanup function
924registered last is called first. Each cleanup function will be called
925at most once. Since Python's internal finallization will have
926completed before the cleanup function, no Python APIs should be called
927by \var{func}.
928\end{cfuncdesc}
929
930
931\section{Importing Modules}
Fred Drakef39ed671998-02-26 22:01:23 +0000932\label{importing}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000933
Fred Drakec6fa34e1998-04-02 06:47:24 +0000934\begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +0000935This is a simplified interface to \cfunction{PyImport_ImportModuleEx()}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000936below, leaving the \var{globals} and \var{locals} arguments set to
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000937\NULL{}. When the \var{name} argument contains a dot (i.e., when
Guido van Rossum42cefd01997-10-05 15:27:29 +0000938it specifies a submodule of a package), the \var{fromlist} argument is
939set to the list \code{['*']} so that the return value is the named
940module rather than the top-level package containing it as would
941otherwise be the case. (Unfortunately, this has an additional side
942effect when \var{name} in fact specifies a subpackage instead of a
943submodule: the submodules specified in the package's \code{__all__}
944variable are loaded.) Return a new reference to the imported module,
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000945or \NULL{} with an exception set on failure (the module may still
Fred Drakec6fa34e1998-04-02 06:47:24 +0000946be created in this case --- examine \code{sys.modules} to find out).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000947\end{cfuncdesc}
948
Fred Drakec6fa34e1998-04-02 06:47:24 +0000949\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000950Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000951Python function \function{__import__()}\bifuncindex{__import__}, as
952the standard \function{__import__()} function calls this function
953directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000954
Guido van Rossum42cefd01997-10-05 15:27:29 +0000955The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000956top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +0000957(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +0000958\function{__import__()}, the return value when a submodule of a
959package was requested is normally the top-level package, unless a
960non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000961\end{cfuncdesc}
962
Fred Drakec6fa34e1998-04-02 06:47:24 +0000963\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000964This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +0000965function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +0000966\code{__builtins__} of the current globals. This means that the
967import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +0000968current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
969\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000970\end{cfuncdesc}
971
Fred Drakec6fa34e1998-04-02 06:47:24 +0000972\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000973Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +0000974Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +0000975\function{reload()} function calls this function directly. Return a
976new reference to the reloaded module, or \NULL{} with an exception set
977on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000978\end{cfuncdesc}
979
Fred Drakec6fa34e1998-04-02 06:47:24 +0000980\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000981Return the module object corresponding to a module name. The
982\var{name} argument may be of the form \code{package.module}). First
983check the modules dictionary if there's one there, and if not, create
984a new one and insert in in the modules dictionary. Because the former
985action is most common, this does not return a new reference, and you
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000986do not own the returned reference. Return \NULL{} with an
Fred Drakef8830d11998-04-23 14:06:01 +0000987exception set on failure. \strong{Note:} this function returns
Guido van Rossum44475131998-04-21 15:30:01 +0000988a ``borrowed'' reference.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000989\end{cfuncdesc}
990
Fred Drakec6fa34e1998-04-02 06:47:24 +0000991\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000992Given a module name (possibly of the form \code{package.module}) and a
993code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +0000994built-in function \function{compile()}\bifuncindex{compile}, load the
995module. Return a new reference to the module object, or \NULL{} with
996an exception set if an error occurred (the module may still be created
997in this case). (This function would reload the module if it was
998already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000999\end{cfuncdesc}
1000
1001\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001002Return the magic number for Python bytecode files (a.k.a. \file{.pyc}
1003and \file{.pyo} files). The magic number should be present in the
Guido van Rossum42cefd01997-10-05 15:27:29 +00001004first four bytes of the bytecode file, in little-endian byte order.
1005\end{cfuncdesc}
1006
Fred Drakec6fa34e1998-04-02 06:47:24 +00001007\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001008Return the dictionary used for the module administration
1009(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
1010variable.
1011\end{cfuncdesc}
1012
1013\begin{cfuncdesc}{void}{_PyImport_Init}{}
1014Initialize the import mechanism. For internal use only.
1015\end{cfuncdesc}
1016
1017\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
1018Empty the module table. For internal use only.
1019\end{cfuncdesc}
1020
1021\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1022Finalize the import mechanism. For internal use only.
1023\end{cfuncdesc}
1024
Fred Drakec6fa34e1998-04-02 06:47:24 +00001025\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001026For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001027\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001028
Fred Drakec6fa34e1998-04-02 06:47:24 +00001029\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001030For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001031\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001032
1033\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *}
1034Load a frozen module. Return \code{1} for success, \code{0} if the
1035module is not found, and \code{-1} with an exception set if the
1036initialization failed. To access the imported module on a successful
Fred Drakee058b4f1998-02-16 06:15:35 +00001037load, use \cfunction{PyImport_ImportModule()}.
1038(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001039already imported.)
1040\end{cfuncdesc}
1041
1042\begin{ctypedesc}{struct _frozen}
1043This is the structure type definition for frozen module descriptors,
Fred Drakec6fa34e1998-04-02 06:47:24 +00001044as generated by the \program{freeze}\index{freeze utility} utility
1045(see \file{Tools/freeze/} in the Python source distribution). Its
1046definition is:
1047
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001048\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001049struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001050 char *name;
1051 unsigned char *code;
1052 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001053};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001054\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001055\end{ctypedesc}
1056
Fred Drakec6fa34e1998-04-02 06:47:24 +00001057\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
Fred Drakef8830d11998-04-23 14:06:01 +00001058This pointer is initialized to point to an array of \ctype{struct
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001059_frozen} records, terminated by one whose members are all \NULL{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001060or zero. When a frozen module is imported, it is searched in this
Fred Drakec6fa34e1998-04-02 06:47:24 +00001061table. Third-party code could play tricks with this to provide a
Guido van Rossum42cefd01997-10-05 15:27:29 +00001062dynamically created collection of frozen modules.
1063\end{cvardesc}
1064
1065
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001066\chapter{Abstract Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001067\label{abstract}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001068
1069The functions in this chapter interact with Python objects regardless
1070of their type, or with wide classes of object types (e.g. all
1071numerical types, or all sequence types). When used on object types
1072for which they do not apply, they will flag a Python exception.
1073
1074\section{Object Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001075\label{object}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001076
1077\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00001078Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001079The flags argument is used to enable certain printing
Fred Drakee058b4f1998-02-16 06:15:35 +00001080options. The only option currently supported is
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001081\constant{Py_PRINT_RAW}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001082\end{cfuncdesc}
1083
1084\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001085Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1086\code{0} otherwise. This is equivalent to the Python expression
1087\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001088This function always succeeds.
1089\end{cfuncdesc}
1090
1091\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001092Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001093Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001094This is the equivalent of the Python expression
1095\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001096\end{cfuncdesc}
1097
1098
1099\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001100Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1101\code{0} otherwise. This is equivalent to the Python expression
1102\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001103This function always succeeds.
1104\end{cfuncdesc}
1105
1106
1107\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001108Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001109Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001110This is the equivalent of the Python expression
1111\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001112\end{cfuncdesc}
1113
1114
1115\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001116Set the value of the attribute named \var{attr_name}, for object
1117\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1118the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1119\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001120\end{cfuncdesc}
1121
1122
1123\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001124Set the value of the attribute named \var{attr_name}, for
1125object \var{o},
1126to the value \var{v}. Returns \code{-1} on failure. This is
1127the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1128\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001129\end{cfuncdesc}
1130
1131
1132\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *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_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001140Delete attribute named \var{attr_name}, for object \var{o}. Returns
1141\code{-1} on failure. This is the equivalent of the Python
1142statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001143\end{cfuncdesc}
1144
1145
1146\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001147Compare the values of \var{o1} and \var{o2} using a routine provided
1148by \var{o1}, if one exists, otherwise with a routine provided by
1149\var{o2}. The result of the comparison is returned in \var{result}.
1150Returns \code{-1} on failure. This is the equivalent of the Python
1151statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001152\end{cfuncdesc}
1153
1154
1155\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001156Compare the values of \var{o1} and \var{o2} using a routine provided
1157by \var{o1}, if one exists, otherwise with a routine provided by
1158\var{o2}. Returns the result of the comparison on success. On error,
1159the value returned is undefined; use \cfunction{PyErr_Occurred()} to
1160detect an error. This is equivalent to the
1161Python expression \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001162\end{cfuncdesc}
1163
1164
1165\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001166Compute the string representation of object, \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001167string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001168the equivalent of the Python expression \samp{repr(\var{o})}.
1169Called by the \function{repr()}\bifuncindex{repr} built-in function
1170and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001171\end{cfuncdesc}
1172
1173
1174\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001175Compute the string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001176string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001177the equivalent of the Python expression \samp{str(\var{o})}.
1178Called by the \function{str()}\bifuncindex{str} built-in function and
1179by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001180\end{cfuncdesc}
1181
1182
1183\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001184Determine if the object \var{o}, is callable. Return \code{1} if the
1185object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001186This function always succeeds.
1187\end{cfuncdesc}
1188
1189
1190\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001191Call a callable Python object \var{callable_object}, with
1192arguments given by the tuple \var{args}. If no arguments are
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001193needed, then args may be \NULL{}. Returns the result of the
1194call on success, or \NULL{} on failure. This is the equivalent
Fred Drakee058b4f1998-02-16 06:15:35 +00001195of the Python expression \samp{apply(\var{o}, \var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001196\end{cfuncdesc}
1197
1198\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001199Call a callable Python object \var{callable_object}, with a
Fred Drakeb0a78731998-01-13 18:51:10 +00001200variable number of \C{} arguments. The \C{} arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001201using a \cfunction{Py_BuildValue()} style format string. The format may
1202be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001203result of the call on success, or \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001204the equivalent of the Python expression \samp{apply(\var{o},
1205\var{args})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001206\end{cfuncdesc}
1207
1208
1209\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001210Call the method named \var{m} of object \var{o} with a variable number
1211of C arguments. The \C{} arguments are described by a
1212\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1213indicating that no arguments are provided. Returns the result of the
1214call on success, or \NULL{} on failure. This is the equivalent of the
1215Python expression \samp{\var{o}.\var{method}(\var{args})}.
1216Note that Special method names, such as \method{__add__()},
1217\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001218abstract-object routines for these must be used.
1219\end{cfuncdesc}
1220
1221
1222\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001223Compute and return the hash value of an object \var{o}. On
1224failure, return \code{-1}. This is the equivalent of the Python
1225expression \samp{hash(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001226\end{cfuncdesc}
1227
1228
1229\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001230Returns \code{1} if the object \var{o} is considered to be true, and
1231\code{0} otherwise. This is equivalent to the Python expression
1232\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001233This function always succeeds.
1234\end{cfuncdesc}
1235
1236
1237\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
1238On success, returns a type object corresponding to the object
Fred Drakee058b4f1998-02-16 06:15:35 +00001239type of object \var{o}. On failure, returns \NULL{}. This is
1240equivalent to the Python expression \samp{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001241\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001242\end{cfuncdesc}
1243
1244\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001245Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001246both sequence and mapping protocols, the sequence length is
Fred Drakee058b4f1998-02-16 06:15:35 +00001247returned. On error, \code{-1} is returned. This is the equivalent
1248to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001249\end{cfuncdesc}
1250
1251
1252\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001253Return element of \var{o} corresponding to the object \var{key} or
1254\NULL{} on failure. This is the equivalent of the Python expression
1255\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001256\end{cfuncdesc}
1257
1258
1259\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001260Map the object \var{key} to the value \var{v}.
1261Returns \code{-1} on failure. This is the equivalent
1262of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001263\end{cfuncdesc}
1264
1265
1266\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001267Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1268failure. This is the equivalent of the Python statement \samp{del
1269\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001270\end{cfuncdesc}
1271
1272
1273\section{Number Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001274\label{number}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001275
1276\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001277Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001278false otherwise.
1279This function always succeeds.
1280\end{cfuncdesc}
1281
1282
1283\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001284Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1285failure. This is the equivalent of the Python expression
1286\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001287\end{cfuncdesc}
1288
1289
1290\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001291Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
1292on failure. This is the equivalent of the Python expression
1293\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001294\end{cfuncdesc}
1295
1296
1297\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001298Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1299failure. This is the equivalent of the Python expression
1300\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001301\end{cfuncdesc}
1302
1303
1304\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001305Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1306failure.
1307This is the equivalent of the Python expression \samp{\var{o1} /
1308\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001309\end{cfuncdesc}
1310
1311
1312\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001313Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1314failure. This is the equivalent of the Python expression
1315\samp{\var{o1} \% \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001316\end{cfuncdesc}
1317
1318
1319\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001320See the built-in function \function{divmod()}\bifuncindex{divmod}.
1321Returns \NULL{} on failure. This is the equivalent of the Python
1322expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001323\end{cfuncdesc}
1324
1325
1326\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001327See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1328\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001329\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
Fred Drakef8830d11998-04-23 14:06:01 +00001330If \var{o3} is to be ignored, pass \cdata{Py_None} in its place.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001331\end{cfuncdesc}
1332
1333
1334\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001335Returns the negation of \var{o} on success, or \NULL{} on failure.
1336This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001337\end{cfuncdesc}
1338
1339
1340\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001341Returns \var{o} on success, or \NULL{} on failure.
1342This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001343\end{cfuncdesc}
1344
1345
1346\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001347Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1348the equivalent of the Python expression \samp{abs(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001349\end{cfuncdesc}
1350
1351
1352\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001353Returns the bitwise negation of \var{o} on success, or \NULL{} on
1354failure. This is the equivalent of the Python expression
1355\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001356\end{cfuncdesc}
1357
1358
1359\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001360Returns the result of left shifting \var{o1} by \var{o2} on success,
1361or \NULL{} on failure. This is the equivalent of the Python
1362expression \samp{\var{o1} << \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001363\end{cfuncdesc}
1364
1365
1366\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001367Returns the result of right shifting \var{o1} by \var{o2} on success,
1368or \NULL{} on failure. This is the equivalent of the Python
1369expression \samp{\var{o1} >> \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001370\end{cfuncdesc}
1371
1372
1373\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001374Returns the result of ``anding'' \var{o2} and \var{o2} on success and
1375\NULL{} on failure. This is the equivalent of the Python
1376expression \samp{\var{o1} and \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001377\end{cfuncdesc}
1378
1379
1380\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001381Returns the bitwise exclusive or of \var{o1} by \var{o2} on success,
1382or \NULL{} on failure. This is the equivalent of the Python
1383expression \samp{\var{o1} \^{ }\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001384\end{cfuncdesc}
1385
1386\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001387Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on
1388failure. This is the equivalent of the Python expression
1389\samp{\var{o1} or \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001390\end{cfuncdesc}
1391
1392
Fred Drakee058b4f1998-02-16 06:15:35 +00001393\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001394This function takes the addresses of two variables of type
Fred Drakef8830d11998-04-23 14:06:01 +00001395\ctype{PyObject*}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001396
Fred Drakee058b4f1998-02-16 06:15:35 +00001397If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}}
1398have the same type, increment their reference count and return
1399\code{0} (success). If the objects can be converted to a common
1400numeric type, replace \code{*p1} and \code{*p2} by their converted
1401value (with 'new' reference counts), and return \code{0}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001402If no conversion is possible, or if some other error occurs,
Fred Drakee058b4f1998-02-16 06:15:35 +00001403return \code{-1} (failure) and don't increment the reference counts.
1404The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the
1405Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1},
1406\var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001407\end{cfuncdesc}
1408
1409
1410\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001411Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001412\NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001413expression \samp{int(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001414\end{cfuncdesc}
1415
1416
1417\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001418Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001419or \NULL{} on failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001420expression \samp{long(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001421\end{cfuncdesc}
1422
1423
1424\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001425Returns the \var{o} converted to a float object on success, or \NULL{}
1426on failure. This is the equivalent of the Python expression
1427\samp{float(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001428\end{cfuncdesc}
1429
1430
Fred Drakef44617d1998-02-12 20:57:15 +00001431\section{Sequence Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001432\label{sequence}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001433
1434\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001435Return \code{1} if the object provides sequence protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001436otherwise.
1437This function always succeeds.
1438\end{cfuncdesc}
1439
1440
1441\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001442Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001443failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001444expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001445\end{cfuncdesc}
1446
1447
1448\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drakee058b4f1998-02-16 06:15:35 +00001449Return the result of repeating sequence object \var{o} \var{count}
1450times, or \NULL{} on failure. This is the equivalent of the Python
1451expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001452\end{cfuncdesc}
1453
1454
1455\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001456Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
1457is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001458\end{cfuncdesc}
1459
1460
1461\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001462Return the slice of sequence object \var{o} between \var{i1} and
1463\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
1464expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001465\end{cfuncdesc}
1466
1467
1468\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001469Assign object \var{v} to the \var{i}th element of \var{o}.
1470Returns \code{-1} on failure. This is the equivalent of the Python
1471statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001472\end{cfuncdesc}
1473
1474\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00001475Delete the \var{i}th element of object \var{v}. Returns
1476\code{-1} on failure. This is the equivalent of the Python
1477statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001478\end{cfuncdesc}
1479
1480\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001481Assign the sequence object \var{v} to the slice in sequence
1482object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
1483the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001484\end{cfuncdesc}
1485
1486\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001487Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
1488Returns \code{-1} on failure. This is the equivalent of the Python
1489statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001490\end{cfuncdesc}
1491
1492\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001493Returns the \var{o} as a tuple on success, and \NULL{} on failure.
1494This is equivalent to the Python expression \code{tuple(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001495\end{cfuncdesc}
1496
1497\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001498Return the number of occurrences of \var{value} in \var{o}, that is,
1499return the number of keys for which \code{\var{o}[\var{key}] ==
1500\var{value}}. On failure, return \code{-1}. This is equivalent to
1501the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001502\end{cfuncdesc}
1503
1504\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001505Determine if \var{o} contains \var{value}. If an item in \var{o} is
1506equal to \var{value}, return \code{1}, otherwise return \code{0}. On
1507error, return \code{-1}. This is equivalent to the Python expression
1508\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001509\end{cfuncdesc}
1510
1511\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00001512Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
1513\var{value}}. On error, return \code{-1}. This is equivalent to
1514the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001515\end{cfuncdesc}
1516
Fred Drakef39ed671998-02-26 22:01:23 +00001517
Fred Drakef44617d1998-02-12 20:57:15 +00001518\section{Mapping Protocol}
Fred Drakef39ed671998-02-26 22:01:23 +00001519\label{mapping}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001520
1521\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001522Return \code{1} if the object provides mapping protocol, and \code{0}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001523otherwise.
1524This function always succeeds.
1525\end{cfuncdesc}
1526
1527
1528\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001529Returns the number of keys in object \var{o} on success, and \code{-1}
1530on failure. For objects that do not provide sequence protocol,
1531this is equivalent to the Python expression \samp{len(\var{o})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001532\end{cfuncdesc}
1533
1534
1535\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001536Remove the mapping for object \var{key} from the object \var{o}.
1537Return \code{-1} on failure. This is equivalent to
1538the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001539\end{cfuncdesc}
1540
1541
1542\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001543Remove the mapping for object \var{key} from the object \var{o}.
1544Return \code{-1} on failure. This is equivalent to
1545the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001546\end{cfuncdesc}
1547
1548
1549\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001550On success, return \code{1} if the mapping object has the key \var{key}
1551and \code{0} otherwise. This is equivalent to the Python expression
1552\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001553This function always succeeds.
1554\end{cfuncdesc}
1555
1556
1557\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001558Return \code{1} if the mapping object has the key \var{key} and
1559\code{0} otherwise. This is equivalent to the Python expression
1560\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001561This function always succeeds.
1562\end{cfuncdesc}
1563
1564
1565\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001566On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001567failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001568expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001569\end{cfuncdesc}
1570
1571
1572\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001573On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001574failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001575expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001576\end{cfuncdesc}
1577
1578
1579\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001580On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001581each item is a tuple containing a key-value pair. On
1582failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00001583expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001584\end{cfuncdesc}
1585
1586\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001587Make object \var{o} empty. Returns \code{1} on success and \code{0}
1588on failure. This is equivalent to the Python statement
1589\samp{for key in \var{o}.keys(): del \var{o}[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001590\end{cfuncdesc}
1591
1592
1593\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001594Return element of \var{o} corresponding to the object \var{key} or
1595\NULL{} on failure. This is the equivalent of the Python expression
1596\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001597\end{cfuncdesc}
1598
1599\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001600Map the object \var{key} to the value \var{v} in object \var{o}.
1601Returns \code{-1} on failure. This is the equivalent of the Python
1602statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001603\end{cfuncdesc}
1604
1605
1606\section{Constructors}
1607
1608\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1609On success, returns a new file object that is opened on the
Fred Drakee058b4f1998-02-16 06:15:35 +00001610file given by \var{file_name}, with a file mode given by \var{mode},
1611where \var{mode} has the same semantics as the standard \C{} routine
1612\cfunction{fopen()}. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001613\end{cfuncdesc}
1614
1615\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
Fred Drakee058b4f1998-02-16 06:15:35 +00001616Return a new file object for an already opened standard \C{} file
1617pointer, \var{fp}. A file name, \var{file_name}, and open mode,
1618\var{mode}, must be provided as well as a flag, \var{close_on_del},
1619that indicates whether the file is to be closed when the file object
1620is destroyed. On failure, return \code{-1}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001621\end{cfuncdesc}
1622
1623\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001624Returns a new float object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001625\NULL{} on failure.
1626\end{cfuncdesc}
1627
1628\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001629Returns a new int 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
Fred Drakee058b4f1998-02-16 06:15:35 +00001633\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1634Returns a new list of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001635failure.
1636\end{cfuncdesc}
1637
1638\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001639Returns a new long object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001640\NULL{} on failure.
1641\end{cfuncdesc}
1642
1643\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001644Returns a new long 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
1648\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1649Returns a new empty dictionary on success, and \NULL{} on
1650failure.
1651\end{cfuncdesc}
1652
1653\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001654Returns a new string object with the value \var{v} on success, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001655\NULL{} on failure.
1656\end{cfuncdesc}
1657
Fred Drakee058b4f1998-02-16 06:15:35 +00001658\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len}
1659Returns a new string object with the value \var{v} and length
1660\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1661the contents of the string are uninitialized.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001662\end{cfuncdesc}
1663
Fred Drakee058b4f1998-02-16 06:15:35 +00001664\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1665Returns a new tuple of length \var{len} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001666failure.
1667\end{cfuncdesc}
1668
1669
1670\chapter{Concrete Objects Layer}
Fred Drakef39ed671998-02-26 22:01:23 +00001671\label{concrete}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001672
1673The functions in this chapter are specific to certain Python object
1674types. Passing them an object of the wrong type is not a good idea;
1675if you receive an object from a Python program and you are not sure
1676that it has the right type, you must perform a type check first;
1677e.g. to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00001678\cfunction{PyDict_Check()}. The chapter is structured like the
1679``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001680
1681
Fred Drakee5bf8b21998-02-12 21:22:28 +00001682\section{Fundamental Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001683\label{fundamental}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001684
Fred Drakee5bf8b21998-02-12 21:22:28 +00001685This section describes Python type objects and the singleton object
1686\code{None}.
1687
1688
1689\subsection{Type Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001690\label{typeObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001691
1692\begin{ctypedesc}{PyTypeObject}
1693
1694\end{ctypedesc}
1695
1696\begin{cvardesc}{PyObject *}{PyType_Type}
1697
1698\end{cvardesc}
1699
1700
1701\subsection{The None Object}
Fred Drakef39ed671998-02-26 22:01:23 +00001702\label{noneObject}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001703
1704\begin{cvardesc}{PyObject *}{Py_None}
Guido van Rossum44475131998-04-21 15:30:01 +00001705The Python \code{None} object, denoting lack of value. This object has
1706no methods.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001707\end{cvardesc}
1708
1709
1710\section{Sequence Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001711\label{sequenceObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001712
1713Generic operations on sequence objects were discussed in the previous
1714chapter; this section deals with the specific kinds of sequence
1715objects that are intrinsic to the Python language.
1716
1717
1718\subsection{String Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001719\label{stringObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001720
1721\begin{ctypedesc}{PyStringObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001722This subtype of \ctype{PyObject} represents a Python string object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001723\end{ctypedesc}
1724
1725\begin{cvardesc}{PyTypeObject}{PyString_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001726This instance of \ctype{PyTypeObject} represents the Python string type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001727\end{cvardesc}
1728
1729\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001730Returns true if the object \var{o} is a string object.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001731\end{cfuncdesc}
1732
Fred Drakec6fa34e1998-04-02 06:47:24 +00001733\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
1734 int len}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001735Returns a new string object with the value \var{v} and length
1736\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
1737the contents of the string are uninitialized.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001738\end{cfuncdesc}
1739
1740\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001741Returns a new string object with the value \var{v} on success, and
1742\NULL{} on failure.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001743\end{cfuncdesc}
1744
1745\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001746Returns the length of the string in string object \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001747\end{cfuncdesc}
1748
1749\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001750Resturns a \NULL{} terminated representation of the contents of \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001751\end{cfuncdesc}
1752
1753\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
1754 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001755Creates a new string object in \var{*string} containing the contents
1756of \var{newpart} appended to \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001757\end{cfuncdesc}
1758
1759\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
1760 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001761Creates a new string object in \var{*string} containing the contents
Guido van Rossum44475131998-04-21 15:30:01 +00001762of \var{newpart} appended to \var{string}. This version decrements
1763the reference count of \var{newpart}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001764\end{cfuncdesc}
1765
1766\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
Guido van Rossum44475131998-04-21 15:30:01 +00001767A way to resize a string object even though it is ``immutable''.
1768Only use this to build up a brand new string object; don't use this if
1769the string may already be known in other parts of the code.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001770\end{cfuncdesc}
1771
1772\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
1773 PyObject *args}
Guido van Rossum44475131998-04-21 15:30:01 +00001774Returns a new string object from \var{format} and \var{args}. Analogous
1775to \code{\var{format} \% \var{args}}. The \var{args} argument must be
1776a tuple.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001777\end{cfuncdesc}
1778
1779\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
Guido van Rossum44475131998-04-21 15:30:01 +00001780Intern the argument \var{*string} in place. The argument must be the
1781address of a pointer variable pointing to a Python string object.
1782If there is an existing interned string that is the same as
1783\var{*string}, it sets \var{*string} to it (decrementing the reference
1784count of the old string object and incrementing the reference count of
1785the interned string object), otherwise it leaves \var{*string} alone
1786and interns it (incrementing its reference count). (Clarification:
1787even though there is a lot of talk about reference counts, think of
Fred Drakef8830d11998-04-23 14:06:01 +00001788this function as reference-count-neutral; you own the object after
1789the call if and only if you owned it before the call.)
Fred Drakec6fa34e1998-04-02 06:47:24 +00001790\end{cfuncdesc}
1791
1792\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
Fred Drakef8830d11998-04-23 14:06:01 +00001793A combination of \cfunction{PyString_FromString()} and
1794\cfunction{PyString_InternInPlace()}, returning either a new string object
Guido van Rossum44475131998-04-21 15:30:01 +00001795that has been interned, or a new (``owned'') reference to an earlier
1796interned string object with the same value.
Fred Drakec6fa34e1998-04-02 06:47:24 +00001797\end{cfuncdesc}
1798
1799\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
Fred Drakef8830d11998-04-23 14:06:01 +00001800Macro form of \cfunction{PyString_AsString()} but without error checking.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001801\end{cfuncdesc}
1802
Fred Drakec6fa34e1998-04-02 06:47:24 +00001803\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
Fred Drakef8830d11998-04-23 14:06:01 +00001804Macro form of \cfunction{PyString_GetSize()} but without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001805\end{cfuncdesc}
1806
1807
Guido van Rossum44475131998-04-21 15:30:01 +00001808
Fred Drakee5bf8b21998-02-12 21:22:28 +00001809\subsection{Tuple Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001810\label{tupleObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001811
1812\begin{ctypedesc}{PyTupleObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001813This subtype of \ctype{PyObject} represents a Python tuple object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001814\end{ctypedesc}
1815
1816\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001817This instance of \ctype{PyTypeObject} represents the Python tuple type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001818\end{cvardesc}
1819
1820\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1821Return true if the argument is a tuple object.
1822\end{cfuncdesc}
1823
Fred Drakec6fa34e1998-04-02 06:47:24 +00001824\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int s}
Fred Drakee058b4f1998-02-16 06:15:35 +00001825Return a new tuple object of size \var{s}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001826\end{cfuncdesc}
1827
1828\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001829Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00001830of that tuple.
1831\end{cfuncdesc}
1832
Fred Drakec6fa34e1998-04-02 06:47:24 +00001833\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001834Returns the object at position \var{pos} in the tuple pointed
1835to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drakef8830d11998-04-23 14:06:01 +00001836sets an \exception{IndexError} exception. \strong{Note:} this
1837function returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001838\end{cfuncdesc}
1839
Fred Drakec6fa34e1998-04-02 06:47:24 +00001840\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00001841Does the same, but does no checking of its arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001842\end{cfuncdesc}
1843
Fred Drakec6fa34e1998-04-02 06:47:24 +00001844\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001845 int low,
1846 int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00001847Takes a slice of the tuple pointed to by \var{p} from
1848\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001849\end{cfuncdesc}
1850
1851\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1852 int pos,
1853 PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001854Inserts a reference to object \var{o} at position \var{pos} of
1855the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001856\end{cfuncdesc}
1857
1858\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1859 int pos,
1860 PyObject *o}
1861
Fred Drakee058b4f1998-02-16 06:15:35 +00001862Does the same, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00001863should \emph{only} be used to fill in brand new tuples.
1864\end{cfuncdesc}
1865
Fred Drakec6fa34e1998-04-02 06:47:24 +00001866\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyTupleObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00001867 int new,
1868 int last_is_sticky}
Fred Drakee058b4f1998-02-16 06:15:35 +00001869Can be used to resize a tuple. Because tuples are
Fred Drakee5bf8b21998-02-12 21:22:28 +00001870\emph{supposed} to be immutable, this should only be used if there is only
1871one module referencing the object. Do \emph{not} use this if the tuple may
Fred Drakee058b4f1998-02-16 06:15:35 +00001872already be known to some other part of the code. \var{last_is_sticky} is
1873a flag --- if set, the tuple will grow or shrink at the front, otherwise
Fred Drakee5bf8b21998-02-12 21:22:28 +00001874it will grow or shrink at the end. Think of this as destroying the old
1875tuple and creating a new one, only more efficiently.
1876\end{cfuncdesc}
1877
1878
1879\subsection{List Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001880\label{listObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001881
1882\begin{ctypedesc}{PyListObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001883This subtype of \ctype{PyObject} represents a Python list object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001884\end{ctypedesc}
1885
1886\begin{cvardesc}{PyTypeObject}{PyList_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001887This instance of \ctype{PyTypeObject} represents the Python list type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001888\end{cvardesc}
1889
1890\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00001891Returns true if its argument is a \ctype{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001892\end{cfuncdesc}
1893
Fred Drakec6fa34e1998-04-02 06:47:24 +00001894\begin{cfuncdesc}{PyObject*}{PyList_New}{int size}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001895Returns a new list of length \var{len} on success, and \NULL{} on
1896failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001897\end{cfuncdesc}
1898
Fred Drakec6fa34e1998-04-02 06:47:24 +00001899\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001900Returns the length of the list object in \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001901\end{cfuncdesc}
1902
Fred Drakec6fa34e1998-04-02 06:47:24 +00001903\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
Guido van Rossum44475131998-04-21 15:30:01 +00001904Returns the object at position \var{pos} in the list pointed
1905to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drakef8830d11998-04-23 14:06:01 +00001906sets an \exception{IndexError} exception. \strong{Note:} this
1907function returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001908\end{cfuncdesc}
1909
Fred Drakec6fa34e1998-04-02 06:47:24 +00001910\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1911 PyObject *item}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001912Sets the item at index \var{index} in list to \var{item}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001913\end{cfuncdesc}
1914
Fred Drakec6fa34e1998-04-02 06:47:24 +00001915\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
Guido van Rossum44475131998-04-21 15:30:01 +00001916 PyObject *item}
1917Inserts the item \var{item} into list \var{list} in front of index
1918\var{index}. Returns 0 if successful; returns -1 and sets an
1919exception if unsuccessful. Analogous to \code{list.insert(index, item)}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001920\end{cfuncdesc}
1921
Fred Drakec6fa34e1998-04-02 06:47:24 +00001922\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Guido van Rossum44475131998-04-21 15:30:01 +00001923Appends the object \var{item} at the end of list \var{list}. Returns
19240 if successful; returns -1 and sets an exception if unsuccessful.
1925Analogous to \code{list.append(item)}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001926\end{cfuncdesc}
1927
Fred Drakec6fa34e1998-04-02 06:47:24 +00001928\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1929 int low, int high}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001930Returns a list of the objects in \var{list} containing the objects
Guido van Rossum44475131998-04-21 15:30:01 +00001931\emph{between} \var{low} and \var{high}. Returns NULL and sets an
1932exception if unsuccessful.
1933Analogous to \code{list[low:high]}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001934\end{cfuncdesc}
1935
Fred Drakec6fa34e1998-04-02 06:47:24 +00001936\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1937 int low, int high,
1938 PyObject *itemlist}
Guido van Rossum44475131998-04-21 15:30:01 +00001939Sets the slice of \var{list} between \var{low} and \var{high} to the contents
1940of \var{itemlist}. Analogous to \code{list[low:high]=itemlist}. Returns 0
1941on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001942\end{cfuncdesc}
1943
Fred Drakec6fa34e1998-04-02 06:47:24 +00001944\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Guido van Rossum44475131998-04-21 15:30:01 +00001945Sorts the items of \var{list} in place. Returns 0 on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001946\end{cfuncdesc}
1947
Fred Drakec6fa34e1998-04-02 06:47:24 +00001948\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Guido van Rossum44475131998-04-21 15:30:01 +00001949Reverses the items of \var{list} in place. Returns 0 on success, -1 on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001950\end{cfuncdesc}
1951
Fred Drakec6fa34e1998-04-02 06:47:24 +00001952\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00001953Returns a new tuple object containing the contents of \var{list}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001954\end{cfuncdesc}
1955
Fred Drakec6fa34e1998-04-02 06:47:24 +00001956\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
Fred Drakef8830d11998-04-23 14:06:01 +00001957Macro form of \cfunction{PyList_GetItem()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001958\end{cfuncdesc}
1959
Guido van Rossuma937d141998-04-24 18:22:02 +00001960\begin{cfuncdesc}{PyObject*}{PyList_SET_ITEM}{PyObject *list, int i,
1961 PyObject *o}
1962Macro form of \cfunction{PyList_SetItem()} without error checking.
1963\end{cfuncdesc}
1964
Fred Drakee5bf8b21998-02-12 21:22:28 +00001965\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
Fred Drakef8830d11998-04-23 14:06:01 +00001966Macro form of \cfunction{PyList_GetSize()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001967\end{cfuncdesc}
1968
1969
1970\section{Mapping Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001971\label{mapObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001972
1973\subsection{Dictionary Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00001974\label{dictObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001975
1976\begin{ctypedesc}{PyDictObject}
Fred Drakef8830d11998-04-23 14:06:01 +00001977This subtype of \ctype{PyObject} represents a Python dictionary object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001978\end{ctypedesc}
1979
1980\begin{cvardesc}{PyTypeObject}{PyDict_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00001981This instance of \ctype{PyTypeObject} represents the Python dictionary type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001982\end{cvardesc}
1983
1984\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00001985Returns true if its argument is a \ctype{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001986\end{cfuncdesc}
1987
Fred Drakec6fa34e1998-04-02 06:47:24 +00001988\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00001989Returns a new empty dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001990\end{cfuncdesc}
1991
1992\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00001993Empties an existing dictionary of all key/value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001994\end{cfuncdesc}
1995
1996\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1997 PyObject *key,
1998 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00001999Inserts \var{value} into the dictionary with a key of \var{key}. Both
2000\var{key} and \var{value} should be PyObjects, and \var{key} should be
2001hashable.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002002\end{cfuncdesc}
2003
2004\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
2005 char *key,
2006 PyObject *val}
Fred Drakee058b4f1998-02-16 06:15:35 +00002007Inserts \var{value} into the dictionary using \var{key}
Fred Drakef8830d11998-04-23 14:06:01 +00002008as a key. \var{key} should be a \ctype{char *}. The key object is
Fred Drakee058b4f1998-02-16 06:15:35 +00002009created using \code{PyString_FromString(\var{key})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002010\end{cfuncdesc}
2011
2012\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002013Removes the entry in dictionary \var{p} with key \var{key}.
2014\var{key} is a PyObject.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002015\end{cfuncdesc}
2016
2017\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002018Removes the entry in dictionary \var{p} which has a key
Fred Drakef8830d11998-04-23 14:06:01 +00002019specified by the \ctype{char *}\var{key}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002020\end{cfuncdesc}
2021
Fred Drakec6fa34e1998-04-02 06:47:24 +00002022\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002023Returns the object from dictionary \var{p} which has a key
Guido van Rossum44475131998-04-21 15:30:01 +00002024\var{key}. Returns \NULL{} if the key \var{key} is not present, but
Fred Drakef8830d11998-04-23 14:06:01 +00002025without (!) setting an exception. \strong{Note:} this function
2026returns a ``borrowed'' reference.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002027\end{cfuncdesc}
2028
Fred Drakec6fa34e1998-04-02 06:47:24 +00002029\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyDictObject *p, char *key}
Fred Drakef8830d11998-04-23 14:06:01 +00002030This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2031specified as a \ctype{char *}, rather than a \ctype{PyObject *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002032\end{cfuncdesc}
2033
Fred Drakec6fa34e1998-04-02 06:47:24 +00002034\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002035Returns a \ctype{PyListObject} containing all the items
Guido van Rossum44475131998-04-21 15:30:01 +00002036from the dictionary, as in the dictinoary method \method{items()} (see
Fred Drakee058b4f1998-02-16 06:15:35 +00002037the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002038\end{cfuncdesc}
2039
Fred Drakec6fa34e1998-04-02 06:47:24 +00002040\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002041Returns a \ctype{PyListObject} containing all the keys
Guido van Rossum44475131998-04-21 15:30:01 +00002042from the dictionary, as in the dictionary method \method{keys()} (see the
Fred Drakee058b4f1998-02-16 06:15:35 +00002043\emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002044\end{cfuncdesc}
2045
Fred Drakec6fa34e1998-04-02 06:47:24 +00002046\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyDictObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002047Returns a \ctype{PyListObject} containing all the values
Guido van Rossum44475131998-04-21 15:30:01 +00002048from the dictionary \var{p}, as in the dictionary method
Fred Drakee058b4f1998-02-16 06:15:35 +00002049\method{values()} (see the \emph{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002050\end{cfuncdesc}
2051
2052\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002053Returns the number of items in the dictionary.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002054\end{cfuncdesc}
2055
2056\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
2057 int ppos,
2058 PyObject **pkey,
2059 PyObject **pvalue}
2060
2061\end{cfuncdesc}
2062
2063
2064\section{Numeric Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002065\label{numericObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002066
2067\subsection{Plain Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002068\label{intObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002069
2070\begin{ctypedesc}{PyIntObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002071This subtype of \ctype{PyObject} represents a Python integer object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002072\end{ctypedesc}
2073
2074\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002075This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drakee5bf8b21998-02-12 21:22:28 +00002076integer type.
2077\end{cvardesc}
2078
2079\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
2080
2081\end{cfuncdesc}
2082
Fred Drakec6fa34e1998-04-02 06:47:24 +00002083\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Fred Drakee058b4f1998-02-16 06:15:35 +00002084Creates a new integer object with a value of \var{ival}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002085
2086The current implementation keeps an array of integer objects for all
Fred Drakee058b4f1998-02-16 06:15:35 +00002087integers between \code{-1} and \code{100}, when you create an int in
2088that range you actually just get back a reference to the existing
2089object. So it should be possible to change the value of \code{1}. I
Fred Drake7e9d3141998-04-03 05:02:28 +00002090suspect the behaviour of Python in this case is undefined. :-)
Fred Drakee5bf8b21998-02-12 21:22:28 +00002091\end{cfuncdesc}
2092
2093\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
Fred Drakee058b4f1998-02-16 06:15:35 +00002094Returns the value of the object \var{io}. No error checking is
2095performed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002096\end{cfuncdesc}
2097
2098\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
Fred Drakef8830d11998-04-23 14:06:01 +00002099Will first attempt to cast the object to a \ctype{PyIntObject}, if
Fred Drakee058b4f1998-02-16 06:15:35 +00002100it is not already one, and then return its value.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002101\end{cfuncdesc}
2102
2103\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002104Returns the systems idea of the largest integer it can handle
2105(\constant{LONG_MAX}, as defined in the system header files).
Fred Drakee5bf8b21998-02-12 21:22:28 +00002106\end{cfuncdesc}
2107
2108
2109\subsection{Long Integer Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002110\label{longObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002111
2112\begin{ctypedesc}{PyLongObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002113This subtype of \ctype{PyObject} represents a Python long integer
Fred Drakee058b4f1998-02-16 06:15:35 +00002114object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002115\end{ctypedesc}
2116
2117\begin{cvardesc}{PyTypeObject}{PyLong_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002118This instance of \ctype{PyTypeObject} represents the Python long
Fred Drakee058b4f1998-02-16 06:15:35 +00002119integer type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002120\end{cvardesc}
2121
2122\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002123Returns true if its argument is a \ctype{PyLongObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002124\end{cfuncdesc}
2125
Fred Drakec6fa34e1998-04-02 06:47:24 +00002126\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Fred Drakef8830d11998-04-23 14:06:01 +00002127Returns a new \ctype{PyLongObject} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002128\end{cfuncdesc}
2129
Fred Drakec6fa34e1998-04-02 06:47:24 +00002130\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Fred Drakef8830d11998-04-23 14:06:01 +00002131Returns a new \ctype{PyLongObject} object from an unsigned \C{} long.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002132\end{cfuncdesc}
2133
Fred Drakec6fa34e1998-04-02 06:47:24 +00002134\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Fred Drakef8830d11998-04-23 14:06:01 +00002135Returns a new \ctype{PyLongObject} object from the integer part of \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002136\end{cfuncdesc}
2137
Fred Drakec6fa34e1998-04-02 06:47:24 +00002138\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002139Returns a \C{} \ctype{long} representation of the contents of \var{pylong}.
2140WHAT HAPPENS IF \var{pylong} is greater than \constant{LONG_MAX}?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002141\end{cfuncdesc}
2142
Fred Drakec6fa34e1998-04-02 06:47:24 +00002143\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002144Returns a \C{} \ctype{unsigned long} representation of the contents of
2145\var{pylong}. WHAT HAPPENS IF \var{pylong} is greater than
2146\constant{ULONG_MAX}?
Fred Drakee5bf8b21998-02-12 21:22:28 +00002147\end{cfuncdesc}
2148
Fred Drakec6fa34e1998-04-02 06:47:24 +00002149\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Fred Drakef8830d11998-04-23 14:06:01 +00002150Returns a \C{} \ctype{double} representation of the contents of \var{pylong}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002151\end{cfuncdesc}
2152
Fred Drakec6fa34e1998-04-02 06:47:24 +00002153\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
2154 int base}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002155\end{cfuncdesc}
2156
2157
2158\subsection{Floating Point Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002159\label{floatObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002160
2161\begin{ctypedesc}{PyFloatObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002162This subtype of \ctype{PyObject} represents a Python floating point
Fred Drakee058b4f1998-02-16 06:15:35 +00002163object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002164\end{ctypedesc}
2165
2166\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002167This instance of \ctype{PyTypeObject} represents the Python floating
Fred Drakee5bf8b21998-02-12 21:22:28 +00002168point type.
2169\end{cvardesc}
2170
2171\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002172Returns true if its argument is a \ctype{PyFloatObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002173\end{cfuncdesc}
2174
Fred Drakec6fa34e1998-04-02 06:47:24 +00002175\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Fred Drakef8830d11998-04-23 14:06:01 +00002176Creates a \ctype{PyFloatObject} object from \var{v}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002177\end{cfuncdesc}
2178
Fred Drakec6fa34e1998-04-02 06:47:24 +00002179\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Fred Drakef8830d11998-04-23 14:06:01 +00002180Returns a \C{} \ctype{double} representation of the contents of \var{pyfloat}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002181\end{cfuncdesc}
2182
Fred Drakec6fa34e1998-04-02 06:47:24 +00002183\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Fred Drakef8830d11998-04-23 14:06:01 +00002184Returns a \C{} \ctype{double} representation of the contents of
2185\var{pyfloat}, but without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002186\end{cfuncdesc}
2187
2188
2189\subsection{Complex Number Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002190\label{complexObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002191
2192\begin{ctypedesc}{Py_complex}
Fred Drake4de05a91998-02-16 14:25:26 +00002193The \C{} structure which corresponds to the value portion of a Python
2194complex number object. Most of the functions for dealing with complex
2195number objects use structures of this type as input or output values,
2196as appropriate. It is defined as:
2197
Fred Drakee058b4f1998-02-16 06:15:35 +00002198\begin{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002199typedef struct {
2200 double real;
2201 double imag;
Fred Drake4de05a91998-02-16 14:25:26 +00002202} Py_complex;
Fred Drakee058b4f1998-02-16 06:15:35 +00002203\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002204\end{ctypedesc}
2205
2206\begin{ctypedesc}{PyComplexObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002207This subtype of \ctype{PyObject} represents a Python complex number object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002208\end{ctypedesc}
2209
2210\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002211This instance of \ctype{PyTypeObject} represents the Python complex
Fred Drakee5bf8b21998-02-12 21:22:28 +00002212number type.
2213\end{cvardesc}
2214
2215\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002216Returns true if its argument is a \ctype{PyComplexObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002217\end{cfuncdesc}
2218
Fred Drakec6fa34e1998-04-02 06:47:24 +00002219\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002220\end{cfuncdesc}
2221
Fred Drakec6fa34e1998-04-02 06:47:24 +00002222\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002223\end{cfuncdesc}
2224
Fred Drakec6fa34e1998-04-02 06:47:24 +00002225\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002226\end{cfuncdesc}
2227
Fred Drakec6fa34e1998-04-02 06:47:24 +00002228\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002229\end{cfuncdesc}
2230
Fred Drakec6fa34e1998-04-02 06:47:24 +00002231\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
2232 Py_complex divisor}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002233\end{cfuncdesc}
2234
Fred Drakec6fa34e1998-04-02 06:47:24 +00002235\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002236\end{cfuncdesc}
2237
Fred Drakec6fa34e1998-04-02 06:47:24 +00002238\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002239\end{cfuncdesc}
2240
Fred Drakec6fa34e1998-04-02 06:47:24 +00002241\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Fred Drakef8830d11998-04-23 14:06:01 +00002242Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002243\end{cfuncdesc}
2244
2245\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Fred Drakef8830d11998-04-23 14:06:01 +00002246Returns the real part of \var{op} as a \C{} \ctype{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002247\end{cfuncdesc}
2248
2249\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Fred Drakef8830d11998-04-23 14:06:01 +00002250Returns the imaginary part of \var{op} as a \C{} \ctype{double}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002251\end{cfuncdesc}
2252
2253\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002254\end{cfuncdesc}
2255
2256
2257
2258\section{Other Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002259\label{otherObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002260
2261\subsection{File Objects}
Fred Drakef39ed671998-02-26 22:01:23 +00002262\label{fileObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002263
2264\begin{ctypedesc}{PyFileObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002265This subtype of \ctype{PyObject} represents a Python file object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002266\end{ctypedesc}
2267
2268\begin{cvardesc}{PyTypeObject}{PyFile_Type}
Fred Drakef8830d11998-04-23 14:06:01 +00002269This instance of \ctype{PyTypeObject} represents the Python file type.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002270\end{cvardesc}
2271
2272\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002273Returns true if its argument is a \ctype{PyFileObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002274\end{cfuncdesc}
2275
Fred Drakec6fa34e1998-04-02 06:47:24 +00002276\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *name, char *mode}
Fred Drakef8830d11998-04-23 14:06:01 +00002277Creates a new \ctype{PyFileObject} pointing to the file
Fred Drakee058b4f1998-02-16 06:15:35 +00002278specified in \var{name} with the mode specified in \var{mode}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002279\end{cfuncdesc}
2280
Fred Drakec6fa34e1998-04-02 06:47:24 +00002281\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
Fred Drakeb92dce31998-02-25 15:40:22 +00002282 char *name, char *mode, int (*close)}
Fred Drakef8830d11998-04-23 14:06:01 +00002283Creates a new \ctype{PyFileObject} from the already-open \var{fp}.
Fred Drakee058b4f1998-02-16 06:15:35 +00002284The function \var{close} will be called when the file should be
2285closed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002286\end{cfuncdesc}
2287
2288\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00002289Returns the file object associated with \var{p} as a \ctype{FILE *}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002290\end{cfuncdesc}
2291
Fred Drakec6fa34e1998-04-02 06:47:24 +00002292\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002293undocumented as yet
2294\end{cfuncdesc}
2295
Fred Drakec6fa34e1998-04-02 06:47:24 +00002296\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00002297Returns the name of the file specified by \var{p} as a
Fred Drakef8830d11998-04-23 14:06:01 +00002298\ctype{PyStringObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002299\end{cfuncdesc}
2300
2301\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drakee058b4f1998-02-16 06:15:35 +00002302Available on systems with \cfunction{setvbuf()} only. This should
2303only be called immediately after file object creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002304\end{cfuncdesc}
2305
2306\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
Fred Drakef8830d11998-04-23 14:06:01 +00002307Sets the \member{softspace} attribute of \var{p} to \var{newflag}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002308Returns the previous value. This function clears any errors, and will
Fred Drakee058b4f1998-02-16 06:15:35 +00002309return \code{0} as the previous value if the attribute either does not
2310exist or if there were errors in retrieving it. There is no way to
2311detect errors from this function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002312\end{cfuncdesc}
2313
Fred Drakec6fa34e1998-04-02 06:47:24 +00002314\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2315 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002316Writes object \var{obj} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002317\end{cfuncdesc}
2318
Fred Drakec6fa34e1998-04-02 06:47:24 +00002319\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p,
2320 int flags}
Fred Drakee058b4f1998-02-16 06:15:35 +00002321Writes string \var{s} to file object \var{p}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002322\end{cfuncdesc}
2323
2324
2325\subsection{CObjects}
Fred Drakef39ed671998-02-26 22:01:23 +00002326\label{cObjects}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002327
Guido van Rossum44475131998-04-21 15:30:01 +00002328\begin{ctypedesc}{PyCObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002329This subtype of \ctype{PyObject} represents an opaque value, useful for
Guido van Rossum44475131998-04-21 15:30:01 +00002330\C{} extension modules who need to pass an opaque value (as a
Fred Drakef8830d11998-04-23 14:06:01 +00002331\ctype{void *} pointer) through Python code to other \C{} code. It is
Guido van Rossum44475131998-04-21 15:30:01 +00002332often used to make a C function pointer defined in one module
2333available to other modules, so the regular import mechanism can be
2334used to access C APIs defined in dynamically loaded modules.
2335\end{ctypedesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002336
Guido van Rossum44475131998-04-21 15:30:01 +00002337\begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtr}{void* cobj,
2338 void (*destr)(void *)}
Fred Drakef8830d11998-04-23 14:06:01 +00002339Creates a \ctype{PyCObject} from the \code{void *} \var{cobj}. The
2340\var{destr} function will be called when the object is reclaimed.
Guido van Rossum44475131998-04-21 15:30:01 +00002341\end{cfuncdesc}
2342
2343\begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2344 void* desc, void (*destr)(void *, void *) }
Fred Drakef8830d11998-04-23 14:06:01 +00002345Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2346\var{destr} function will be called when the object is reclaimed. The
2347\var{desc} argument can be used to pass extra callback data for the
2348destructor function.
Guido van Rossum44475131998-04-21 15:30:01 +00002349\end{cfuncdesc}
2350
2351\begin{cfuncdesc}{void *}{PyCObject_AsVoidPtr}{PyObject* self}
Fred Drakef8830d11998-04-23 14:06:01 +00002352Returns the object \ctype{void *} that the \ctype{PyCObject} \var{self}
Guido van Rossum44475131998-04-21 15:30:01 +00002353was created with.
2354\end{cfuncdesc}
2355
2356\begin{cfuncdesc}{void *}{PyCObject_GetDesc}{PyObject* self}
Fred Drakef8830d11998-04-23 14:06:01 +00002357Returns the description \ctype{void *} that the \ctype{PyCObject}
Guido van Rossum44475131998-04-21 15:30:01 +00002358\var{self} was created with.
2359\end{cfuncdesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002360
Guido van Rossum4a944d71997-08-14 20:35:38 +00002361\chapter{Initialization, Finalization, and Threads}
Fred Drakef39ed671998-02-26 22:01:23 +00002362\label{initialization}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002363
Guido van Rossum4a944d71997-08-14 20:35:38 +00002364\begin{cfuncdesc}{void}{Py_Initialize}{}
2365Initialize the Python interpreter. In an application embedding
2366Python, this should be called before using any other Python/C API
Fred Drakee058b4f1998-02-16 06:15:35 +00002367functions; with the exception of \cfunction{Py_SetProgramName()},
2368\cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()},
2369and \cfunction{PyEval_AcquireLock()}. This initializes the table of
2370loaded modules (\code{sys.modules}), and creates the fundamental
Fred Drake4de05a91998-02-16 14:25:26 +00002371modules \module{__builtin__}\refbimodindex{__builtin__},
2372\module{__main__}\refbimodindex{__main__} and
2373\module{sys}\refbimodindex{sys}. It also initializes the module
2374search path (\code{sys.path}).%
2375\indexiii{module}{search}{path}
2376It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for
2377that. This is a no-op when called for a second time (without calling
Fred Drakee058b4f1998-02-16 06:15:35 +00002378\cfunction{Py_Finalize()} first). There is no return value; it is a
2379fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00002380\end{cfuncdesc}
2381
2382\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00002383Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00002384initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
2385called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00002386again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002387\end{cfuncdesc}
2388
2389\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002390Undo all initializations made by \cfunction{Py_Initialize()} and
2391subsequent use of Python/C API functions, and destroy all
2392sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
2393created and not yet destroyed since the last call to
2394\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
2395by the Python interpreter. This is a no-op when called for a second
2396time (without calling \cfunction{Py_Initialize()} again first). There
2397is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002398
2399This function is provided for a number of reasons. An embedding
2400application might want to restart Python without having to restart the
2401application itself. An application that has loaded the Python
2402interpreter from a dynamically loadable library (or DLL) might want to
2403free all memory allocated by Python before unloading the DLL. During a
2404hunt for memory leaks in an application a developer might want to free
2405all memory allocated by Python before exiting from the application.
2406
Fred Drakee058b4f1998-02-16 06:15:35 +00002407\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00002408modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00002409(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00002410(even functions) or modules. Dynamically loaded extension modules
2411loaded by Python are not unloaded. Small amounts of memory allocated
2412by the Python interpreter may not be freed (if you find a leak, please
2413report it). Memory tied up in circular references between objects is
2414not freed. Some memory allocated by extension modules may not be
2415freed. Some extension may not work properly if their initialization
2416routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00002417calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
2418than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002419\end{cfuncdesc}
2420
Fred Drakec6fa34e1998-04-02 06:47:24 +00002421\begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002422Create a new sub-interpreter. This is an (almost) totally separate
2423environment for the execution of Python code. In particular, the new
2424interpreter has separate, independent versions of all imported
2425modules, including the fundamental modules
2426\module{__builtin__}\refbimodindex{__builtin__},
2427\module{__main__}\refbimodindex{__main__} and
2428\module{sys}\refbimodindex{sys}. The table of loaded modules
2429(\code{sys.modules}) and the module search path (\code{sys.path}) are
2430also separate. The new environment has no \code{sys.argv} variable.
2431It has new standard I/O stream file objects \code{sys.stdin},
2432\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drakef8830d11998-04-23 14:06:01 +00002433same underlying \ctype{FILE} structures in the \C{} library).
Guido van Rossum4a944d71997-08-14 20:35:38 +00002434
2435The return value points to the first thread state created in the new
2436sub-interpreter. This thread state is made the current thread state.
2437Note that no actual thread is created; see the discussion of thread
2438states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002439\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00002440is stored in the current thread state and there may not be a current
2441thread state. (Like all other Python/C API functions, the global
2442interpreter lock must be held before calling this function and is
2443still held when it returns; however, unlike most other Python/C API
2444functions, there needn't be a current thread state on entry.)
2445
2446Extension modules are shared between (sub-)interpreters as follows:
2447the first time a particular extension is imported, it is initialized
2448normally, and a (shallow) copy of its module's dictionary is
2449squirreled away. When the same extension is imported by another
2450(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00002451contents of this copy; the extension's \code{init} function is not
2452called. Note that this is different from what happens when an
2453extension is imported after the interpreter has been completely
2454re-initialized by calling \cfunction{Py_Finalize()} and
2455\cfunction{Py_Initialize()}; in that case, the extension's \code{init}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002456function \emph{is} called again.
2457
Fred Drakee058b4f1998-02-16 06:15:35 +00002458\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00002459interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00002460isn't perfect --- for example, using low-level file operations like
Fred Drakef8830d11998-04-23 14:06:01 +00002461\function{os.close()} they can (accidentally or maliciously) affect each
Guido van Rossum4a944d71997-08-14 20:35:38 +00002462other's open files. Because of the way extensions are shared between
2463(sub-)interpreters, some extensions may not work properly; this is
2464especially likely when the extension makes use of (static) global
2465variables, or when the extension manipulates its module's dictionary
2466after its initialization. It is possible to insert objects created in
2467one sub-interpreter into a namespace of another sub-interpreter; this
2468should be done with great care to avoid sharing user-defined
2469functions, methods, instances or classes between sub-interpreters,
2470since import operations executed by such objects may affect the
2471wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
2472a hard-to-fix bug that will be addressed in a future release.)
2473\end{cfuncdesc}
2474
2475\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
2476Destroy the (sub-)interpreter represented by the given thread state.
2477The given thread state must be the current thread state. See the
2478discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002479thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00002480interpreted are destroyed. (The global interpreter lock must be held
2481before calling this function and is still held when it returns.)
Fred Drakee058b4f1998-02-16 06:15:35 +00002482\cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't
Guido van Rossum4a944d71997-08-14 20:35:38 +00002483been explicitly destroyed at that point.
2484\end{cfuncdesc}
2485
2486\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drakee058b4f1998-02-16 06:15:35 +00002487This function should be called before \cfunction{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00002488for the first time, if it is called at all. It tells the interpreter
Fred Drakee058b4f1998-02-16 06:15:35 +00002489the value of the \code{argv[0]} argument to the \cfunction{main()} function
2490of the program. This is used by \cfunction{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00002491functions below to find the Python run-time libraries relative to the
2492interpreter executable. The default value is \code{"python"}. The
2493argument should point to a zero-terminated character string in static
2494storage whose contents will not change for the duration of the
2495program's execution. No code in the Python interpreter will change
2496the contents of this storage.
2497\end{cfuncdesc}
2498
Fred Drakec6fa34e1998-04-02 06:47:24 +00002499\begin{cfuncdesc}{char*}{Py_GetProgramName}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002500Return the program name set with \cfunction{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00002501default. The returned string points into static storage; the caller
2502should not modify its value.
2503\end{cfuncdesc}
2504
Fred Drakec6fa34e1998-04-02 06:47:24 +00002505\begin{cfuncdesc}{char*}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002506Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00002507is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00002508set with \cfunction{Py_SetProgramName()} and some environment variables;
Guido van Rossum4a944d71997-08-14 20:35:38 +00002509for example, if the program name is \code{"/usr/local/bin/python"},
2510the prefix is \code{"/usr/local"}. The returned string points into
2511static storage; the caller should not modify its value. This
Fred Drakec94d9341998-04-12 02:39:13 +00002512corresponds to the \makevar{prefix} variable in the top-level
2513\file{Makefile} and the \code{-}\code{-prefix} argument to the
Fred Drakee058b4f1998-02-16 06:15:35 +00002514\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00002515Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00002516also the next function.
2517\end{cfuncdesc}
2518
Fred Drakec6fa34e1998-04-02 06:47:24 +00002519\begin{cfuncdesc}{char*}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00002520Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00002521files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002522program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00002523variables; for example, if the program name is
2524\code{"/usr/local/bin/python"}, the exec-prefix is
2525\code{"/usr/local"}. The returned string points into static storage;
2526the caller should not modify its value. This corresponds to the
Fred Drakec94d9341998-04-12 02:39:13 +00002527\makevar{exec_prefix} variable in the top-level \file{Makefile} and the
2528\code{-}\code{-exec_prefix} argument to the \program{configure} script
2529at build time. The value is available to Python code as
Fred Drakeb0a78731998-01-13 18:51:10 +00002530\code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002531
2532Background: The exec-prefix differs from the prefix when platform
2533dependent files (such as executables and shared libraries) are
2534installed in a different directory tree. In a typical installation,
2535platform dependent files may be installed in the
2536\code{"/usr/local/plat"} subtree while platform independent may be
2537installed in \code{"/usr/local"}.
2538
2539Generally speaking, a platform is a combination of hardware and
2540software families, e.g. Sparc machines running the Solaris 2.x
2541operating system are considered the same platform, but Intel machines
2542running Solaris 2.x are another platform, and Intel machines running
2543Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00002544operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002545operating systems are a different story; the installation strategies
2546on those systems are so different that the prefix and exec-prefix are
2547meaningless, and set to the empty string. Note that compiled Python
2548bytecode files are platform independent (but not independent from the
2549Python version by which they were compiled!).
2550
Fred Drakee058b4f1998-02-16 06:15:35 +00002551System administrators will know how to configure the \program{mount} or
2552\program{automount} programs to share \code{"/usr/local"} between platforms
Guido van Rossum4a944d71997-08-14 20:35:38 +00002553while having \code{"/usr/local/plat"} be a different filesystem for each
2554platform.
2555\end{cfuncdesc}
2556
Fred Drakec6fa34e1998-04-02 06:47:24 +00002557\begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002558Return the full program name of the Python executable; this is
2559computed as a side-effect of deriving the default module search path
Fred Drakee058b4f1998-02-16 06:15:35 +00002560from the program name (set by \cfunction{Py_SetProgramName()} above). The
Guido van Rossum4a944d71997-08-14 20:35:38 +00002561returned string points into static storage; the caller should not
2562modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00002563\code{sys.executable}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002564\end{cfuncdesc}
2565
Fred Drakec6fa34e1998-04-02 06:47:24 +00002566\begin{cfuncdesc}{char*}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00002567\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002568Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00002569program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00002570environment variables. The returned string consists of a series of
2571directory names separated by a platform dependent delimiter character.
Fred Drakef8830d11998-04-23 14:06:01 +00002572The delimiter character is \character{:} on \UNIX{}, \character{;} on
2573DOS/Windows, and \character{\\n} (the \ASCII{} newline character) on
Fred Drakee5bc4971998-02-12 23:36:49 +00002574Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00002575should not modify its value. The value is available to Python code
2576as the list \code{sys.path}, which may be modified to change the
2577future search path for loaded modules.
2578
2579% XXX should give the exact rules
2580\end{cfuncdesc}
2581
Fred Drakec6fa34e1998-04-02 06:47:24 +00002582\begin{cfuncdesc}{const char*}{Py_GetVersion}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002583Return the version of this Python interpreter. This is a string that
2584looks something like
2585
Guido van Rossum09270b51997-08-15 18:57:32 +00002586\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00002587"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00002588\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002589
2590The first word (up to the first space character) is the current Python
2591version; the first three characters are the major and minor version
2592separated by a period. The returned string points into static storage;
2593the caller should not modify its value. The value is available to
2594Python code as the list \code{sys.version}.
2595\end{cfuncdesc}
2596
Fred Drakec6fa34e1998-04-02 06:47:24 +00002597\begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00002598Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00002599this is formed from the ``official'' name of the operating system,
2600converted to lower case, followed by the major revision number; e.g.,
2601for Solaris 2.x, which is also known as SunOS 5.x, the value is
2602\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
2603is \code{"win"}. The returned string points into static storage;
2604the caller should not modify its value. The value is available to
2605Python code as \code{sys.platform}.
2606\end{cfuncdesc}
2607
Fred Drakec6fa34e1998-04-02 06:47:24 +00002608\begin{cfuncdesc}{const char*}{Py_GetCopyright}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002609Return the official copyright string for the current Python version,
2610for example
2611
2612\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
2613
2614The returned string points into static storage; the caller should not
2615modify its value. The value is available to Python code as the list
2616\code{sys.copyright}.
2617\end{cfuncdesc}
2618
Fred Drakec6fa34e1998-04-02 06:47:24 +00002619\begin{cfuncdesc}{const char*}{Py_GetCompiler}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002620Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002621version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00002622
Fred Drakee058b4f1998-02-16 06:15:35 +00002623\begin{verbatim}
2624"[GCC 2.7.2.2]"
2625\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002626
2627The returned string points into static storage; the caller should not
2628modify its value. The value is available to Python code as part of
2629the variable \code{sys.version}.
2630\end{cfuncdesc}
2631
Fred Drakec6fa34e1998-04-02 06:47:24 +00002632\begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002633Return information about the sequence number and build date and time
2634of the current Python interpreter instance, for example
2635
Guido van Rossum09270b51997-08-15 18:57:32 +00002636\begin{verbatim}
2637"#67, Aug 1 1997, 22:34:28"
2638\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002639
2640The returned string points into static storage; the caller should not
2641modify its value. The value is available to Python code as part of
2642the variable \code{sys.version}.
2643\end{cfuncdesc}
2644
2645\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
2646% XXX
2647\end{cfuncdesc}
2648
2649% XXX Other PySys thingies (doesn't really belong in this chapter)
2650
2651\section{Thread State and the Global Interpreter Lock}
Fred Drakef39ed671998-02-26 22:01:23 +00002652\label{threads}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002653
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002654The Python interpreter is not fully thread safe. In order to support
2655multi-threaded Python programs, there's a global lock that must be
2656held by the current thread before it can safely access Python objects.
2657Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00002658a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002659increment the reference count of the same object, the reference count
2660could end up being incremented only once instead of twice.
2661
2662Therefore, the rule exists that only the thread that has acquired the
2663global interpreter lock may operate on Python objects or call Python/C
2664API functions. In order to support multi-threaded Python programs,
Fred Drakee058b4f1998-02-16 06:15:35 +00002665the interpreter regularly release and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002666default, every ten bytecode instructions (this can be changed with
Fred Drakee058b4f1998-02-16 06:15:35 +00002667\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002668reacquired around potentially blocking I/O operations like reading or
2669writing a file, so that other threads can run while the thread that
2670requests the I/O is waiting for the I/O operation to complete.
2671
2672The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00002673separate per thread --- for this it uses a data structure called
Fred Drakef8830d11998-04-23 14:06:01 +00002674\ctype{PyThreadState}. This is new in Python 1.5; in earlier versions,
Fred Drakee058b4f1998-02-16 06:15:35 +00002675such state was stored in global variables, and switching threads could
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002676cause problems. In particular, exception handling is now thread safe,
Fred Drakee058b4f1998-02-16 06:15:35 +00002677when the application uses \function{sys.exc_info()} to access the
2678exception last raised in the current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002679
2680There's one global variable left, however: the pointer to the current
Fred Drakef8830d11998-04-23 14:06:01 +00002681\ctype{PyThreadState} structure. While most thread packages have a way
Fred Drake9d20ac31998-02-16 15:27:08 +00002682to store ``per-thread global data,'' Python's internal platform
Fred Drakee058b4f1998-02-16 06:15:35 +00002683independent thread abstraction doesn't support this yet. Therefore,
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002684the current thread state must be manipulated explicitly.
2685
2686This is easy enough in most cases. Most code manipulating the global
2687interpreter lock has the following simple structure:
2688
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002689\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002690Save the thread state in a local variable.
2691Release the interpreter lock.
2692...Do some blocking I/O operation...
2693Reacquire the interpreter lock.
2694Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002695\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002696
2697This is so common that a pair of macros exists to simplify it:
2698
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002699\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002700Py_BEGIN_ALLOW_THREADS
2701...Do some blocking I/O operation...
2702Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002703\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002704
Fred Drakee058b4f1998-02-16 06:15:35 +00002705The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares
2706a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes
2707the block. Another advantage of using these two macros is that when
2708Python is compiled without thread support, they are defined empty,
2709thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002710
2711When thread support is enabled, the block above expands to the
2712following code:
2713
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002714\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002715{
2716 PyThreadState *_save;
2717 _save = PyEval_SaveThread();
2718 ...Do some blocking I/O operation...
2719 PyEval_RestoreThread(_save);
2720}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002721\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002722
2723Using even lower level primitives, we can get roughly the same effect
2724as follows:
2725
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002726\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002727{
2728 PyThreadState *_save;
2729 _save = PyThreadState_Swap(NULL);
2730 PyEval_ReleaseLock();
2731 ...Do some blocking I/O operation...
2732 PyEval_AcquireLock();
2733 PyThreadState_Swap(_save);
2734}
Guido van Rossum9faf4c51997-10-07 14:38:54 +00002735\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002736
2737There are some subtle differences; in particular,
Fred Drakee058b4f1998-02-16 06:15:35 +00002738\cfunction{PyEval_RestoreThread()} saves and restores the value of the
Fred Drakef8830d11998-04-23 14:06:01 +00002739global variable \cdata{errno}, since the lock manipulation does not
2740guarantee that \cdata{errno} is left alone. Also, when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002741is disabled, \cfunction{PyEval_SaveThread()} and
2742\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
2743case, \cfunction{PyEval_ReleaseLock()} and
2744\cfunction{PyEval_AcquireLock()} are not available. This is done so
2745that dynamically loaded extensions compiled with thread support
2746enabled can be loaded by an interpreter that was compiled with
2747disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002748
2749The global interpreter lock is used to protect the pointer to the
2750current thread state. When releasing the lock and saving the thread
2751state, the current thread state pointer must be retrieved before the
2752lock is released (since another thread could immediately acquire the
2753lock and store its own thread state in the global variable).
2754Reversely, when acquiring the lock and restoring the thread state, the
2755lock must be acquired before storing the thread state pointer.
2756
2757Why am I going on with so much detail about this? Because when
Fred Drakeb0a78731998-01-13 18:51:10 +00002758threads are created from \C{}, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002759lock, nor is there a thread state data structure for them. Such
2760threads must bootstrap themselves into existence, by first creating a
2761thread state data structure, then acquiring the lock, and finally
2762storing their thread state pointer, before they can start using the
2763Python/C API. When they are done, they should reset the thread state
2764pointer, release the lock, and finally free their thread state data
2765structure.
2766
2767When creating a thread data structure, you need to provide an
2768interpreter state data structure. The interpreter state data
2769structure hold global data that is shared by all threads in an
2770interpreter, for example the module administration
2771(\code{sys.modules}). Depending on your needs, you can either create
2772a new interpreter state data structure, or share the interpreter state
2773data structure used by the Python main thread (to access the latter,
Fred Drakef8830d11998-04-23 14:06:01 +00002774you must obtain the thread state and access its \member{interp} member;
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002775this must be done by a thread that is created by Python or by the main
2776thread after Python is initialized).
2777
2778XXX More?
2779
2780\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002781This data structure represents the state shared by a number of
2782cooperating threads. Threads belonging to the same interpreter
2783share their module administration and a few other internal items.
2784There are no public members in this structure.
2785
2786Threads belonging to different interpreters initially share nothing,
2787except process state like available memory, open file descriptors and
2788such. The global interpreter lock is also shared by all threads,
2789regardless of to which interpreter they belong.
2790\end{ctypedesc}
2791
2792\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002793This data structure represents the state of a single thread. The only
Fred Drakef8830d11998-04-23 14:06:01 +00002794public data member is \ctype{PyInterpreterState *}\member{interp},
2795which points to this thread's interpreter state.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002796\end{ctypedesc}
2797
2798\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
2799Initialize and acquire the global interpreter lock. It should be
2800called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00002801in any other thread operations such as
2802\cfunction{PyEval_ReleaseLock()} or
2803\code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before
2804calling \cfunction{PyEval_SaveThread()} or
2805\cfunction{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002806
2807This is a no-op when called for a second time. It is safe to call
Fred Drakee058b4f1998-02-16 06:15:35 +00002808this function before calling \cfunction{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002809
2810When only the main thread exists, no lock operations are needed. This
2811is a common situation (most Python programs do not use threads), and
2812the lock operations slow the interpreter down a bit. Therefore, the
2813lock is not created initially. This situation is equivalent to having
2814acquired the lock: when there is only a single thread, all object
2815accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00002816lock, it also acquires it. Before the Python
2817\module{thread}\refbimodindex{thread} module creates a new thread,
2818knowing that either it has the lock or the lock hasn't been created
2819yet, it calls \cfunction{PyEval_InitThreads()}. When this call
2820returns, it is guaranteed that the lock has been created and that it
2821has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002822
2823It is \strong{not} safe to call this function when it is unknown which
2824thread (if any) currently has the global interpreter lock.
2825
2826This function is not available when thread support is disabled at
2827compile time.
2828\end{cfuncdesc}
2829
Guido van Rossum4a944d71997-08-14 20:35:38 +00002830\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002831Acquire the global interpreter lock. The lock must have been created
2832earlier. If this thread already has the lock, a deadlock ensues.
2833This function is not available when thread support is disabled at
2834compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002835\end{cfuncdesc}
2836
2837\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002838Release the global interpreter lock. The lock must have been created
2839earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00002840disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002841\end{cfuncdesc}
2842
2843\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002844Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002845state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002846have been created earlier. If this thread already has the lock,
2847deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00002848is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002849\end{cfuncdesc}
2850
2851\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002852Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002853interpreter lock. The lock must have been created earlier and must be
2854held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002855be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00002856thread state --- if it isn't, a fatal error is reported. This
2857function is not available when thread support is disabled at compile
2858time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002859\end{cfuncdesc}
2860
Fred Drakec6fa34e1998-04-02 06:47:24 +00002861\begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002862Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002863support is enabled) and reset the thread state to \NULL{},
2864returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002865the lock has been created, the current thread must have acquired it.
2866(This function is available even when thread support is disabled at
2867compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002868\end{cfuncdesc}
2869
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002870\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002871Acquire the interpreter lock (if it has been created and thread
2872support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002873must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002874thread must not have acquired it, otherwise deadlock ensues. (This
2875function is available even when thread support is disabled at compile
2876time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00002877\end{cfuncdesc}
2878
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002879% XXX These aren't really C types, but the ctypedesc macro is the simplest!
2880\begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS}
2881This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002882\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002883Note that it contains an opening brace; it must be matched with a
2884following \code{Py_END_ALLOW_THREADS} macro. See above for further
2885discussion of this macro. It is a no-op when thread support is
2886disabled at compile time.
2887\end{ctypedesc}
2888
2889\begin{ctypedesc}{Py_END_ALLOW_THREADS}
2890This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00002891\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002892Note that it contains a closing brace; it must be matched with an
2893earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
2894discussion of this macro. It is a no-op when thread support is
2895disabled at compile time.
2896\end{ctypedesc}
2897
2898\begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002899This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002900is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
2901brace. It is a no-op when thread support is disabled at compile
2902time.
2903\end{ctypedesc}
2904
2905\begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS}
Fred Drakee058b4f1998-02-16 06:15:35 +00002906This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002907equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
2908and variable declaration. It is a no-op when thread support is
2909disabled at compile time.
2910\end{ctypedesc}
2911
2912All of the following functions are only available when thread support
2913is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00002914interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002915
Fred Drakec6fa34e1998-04-02 06:47:24 +00002916\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{}
Guido van Rossumed9dcc11998-08-07 18:28:03 +00002917Create a new interpreter state object. The interpreter lock need not
2918be held, but may be held if it is necessary to serialize calls to this
2919function.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002920\end{cfuncdesc}
2921
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002922\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
2923Reset all information in an interpreter state object. The interpreter
2924lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00002925\end{cfuncdesc}
2926
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002927\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
2928Destroy an interpreter state object. The interpreter lock need not be
2929held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002930call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002931\end{cfuncdesc}
2932
Fred Drakec6fa34e1998-04-02 06:47:24 +00002933\begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002934Create a new thread state object belonging to the given interpreter
Guido van Rossumed9dcc11998-08-07 18:28:03 +00002935object. The interpreter lock need not be held, but may be held if it
2936is necessary to serialize calls to this function.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002937\end{cfuncdesc}
2938
2939\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
2940Reset all information in a thread state object. The interpreter lock
2941must be held.
2942\end{cfuncdesc}
2943
2944\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
2945Destroy a thread state object. The interpreter lock need not be
2946held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00002947call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002948\end{cfuncdesc}
2949
Fred Drakec6fa34e1998-04-02 06:47:24 +00002950\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002951Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002952When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00002953error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002954\end{cfuncdesc}
2955
Fred Drakec6fa34e1998-04-02 06:47:24 +00002956\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002957Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00002958argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00002959must be held.
2960\end{cfuncdesc}
2961
2962
Fred Drakee058b4f1998-02-16 06:15:35 +00002963\chapter{Defining New Object Types}
Fred Drakef39ed671998-02-26 22:01:23 +00002964\label{newTypes}
Guido van Rossum4a944d71997-08-14 20:35:38 +00002965
Fred Drakec6fa34e1998-04-02 06:47:24 +00002966\begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +00002967\end{cfuncdesc}
2968
Fred Drakec6fa34e1998-04-02 06:47:24 +00002969\begin{cfuncdesc}{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
Fred Drakee058b4f1998-02-16 06:15:35 +00002970\end{cfuncdesc}
2971
2972\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
2973\end{cfuncdesc}
2974
2975\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
2976\end{cfuncdesc}
2977
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002978Py_InitModule (!!!)
2979
2980PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse
2981
2982Py_BuildValue
Guido van Rossumae110af1997-05-22 20:11:52 +00002983
2984PyObject, PyVarObject
2985
2986PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
2987
2988Typedefs:
2989unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
2990intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
2991getreadbufferproc, getwritebufferproc, getsegcountproc,
2992destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
2993setattrofunc, cmpfunc, reprfunc, hashfunc
2994
2995PyNumberMethods
2996
2997PySequenceMethods
2998
2999PyMappingMethods
3000
3001PyBufferProcs
3002
3003PyTypeObject
3004
3005DL_IMPORT
3006
3007PyType_Type
3008
3009Py*_Check
3010
3011Py_None, _Py_NoneStruct
3012
Guido van Rossumae110af1997-05-22 20:11:52 +00003013
Fred Drakee5bf8b21998-02-12 21:22:28 +00003014\chapter{Debugging}
Fred Drakef39ed671998-02-26 22:01:23 +00003015\label{debugging}
Guido van Rossumae110af1997-05-22 20:11:52 +00003016
Fred Drakee5bf8b21998-02-12 21:22:28 +00003017XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00003018
3019
Fred Drakef3aa0e01998-03-17 06:23:13 +00003020\input{api.ind} % Index -- must be last
Guido van Rossum9231c8f1997-05-15 21:43:21 +00003021
3022\end{document}