blob: a7270411cfc0dc18c314975c330d7f564c165672 [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
Marc-André Lemburga544ea22001-01-17 18:04:31 +00007\makeindex % tell \index to actually write the .idx file
Guido van Rossum9231c8f1997-05-15 21:43:21 +00008
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 Drake659ebfa2000-04-03 15:42:13 +000023This manual documents the API used by C and \Cpp{} programmers who
Fred Drakee058b4f1998-02-16 06:15:35 +000024want to write extension modules or embed Python. It is a companion to
Fred Drakebe486461999-11-09 17:03:03 +000025\citetitle[../ext/ext.html]{Extending and Embedding the Python
26Interpreter}, which describes the general principles of extension
27writing but does not document the API functions in detail.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000028
Guido van Rossum5b8a5231997-12-30 04:38:44 +000029\strong{Warning:} The current version of this document is incomplete.
30I hope that it is nevertheless useful. I will continue to work on it,
31and release new versions from time to time, independent from Python
32source code releases.
33
Guido van Rossum9231c8f1997-05-15 21:43:21 +000034\end{abstract}
35
Fred Drake4d4f9e71998-01-13 22:25:02 +000036\tableofcontents
Guido van Rossum9231c8f1997-05-15 21:43:21 +000037
Guido van Rossum5060b3b1997-08-17 18:02:23 +000038% XXX Consider moving all this back to ext.tex and giving api.tex
39% XXX a *really* short intro only.
Guido van Rossum9231c8f1997-05-15 21:43:21 +000040
Fred Drakeefd146c1999-02-15 15:30:45 +000041\chapter{Introduction \label{intro}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000042
Fred Drake659ebfa2000-04-03 15:42:13 +000043The Application Programmer's Interface to Python gives C and
44\Cpp{} programmers access to the Python interpreter at a variety of
45levels. The API is equally usable from \Cpp{}, but for brevity it is
46generally referred to as the Python/C API. There are two
47fundamentally different reasons for using the Python/C API. The first
48reason is to write \emph{extension modules} for specific purposes;
49these are C modules that extend the Python interpreter. This is
50probably the most common use. The second reason is to use Python as a
51component in a larger application; this technique is generally
52referred to as \dfn{embedding} Python in an application.
Guido van Rossum59a61351997-08-14 20:34:33 +000053
Guido van Rossum4a944d71997-08-14 20:35:38 +000054Writing an extension module is a relatively well-understood process,
55where a ``cookbook'' approach works well. There are several tools
56that automate the process to some extent. While people have embedded
57Python in other applications since its early existence, the process of
Fred Drakefc43d002001-05-21 15:03:35 +000058embedding Python is less straightforward than writing an extension.
Guido van Rossum59a61351997-08-14 20:34:33 +000059
Guido van Rossum4a944d71997-08-14 20:35:38 +000060Many API functions are useful independent of whether you're embedding
61or extending Python; moreover, most applications that embed Python
62will need to provide a custom extension as well, so it's probably a
63good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000064attempting to embed Python in a real application.
65
Fred Drakeefd146c1999-02-15 15:30:45 +000066
67\section{Include Files \label{includes}}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000068
69All function, type and macro definitions needed to use the Python/C
70API are included in your code by the following line:
71
Fred Drakee058b4f1998-02-16 06:15:35 +000072\begin{verbatim}
73#include "Python.h"
74\end{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +000075
Fred Drakee058b4f1998-02-16 06:15:35 +000076This implies inclusion of the following standard headers:
Fred Drake0b71cea2000-09-26 05:51:50 +000077\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>},
78\code{<limits.h>}, and \code{<stdlib.h>} (if available).
Fred Drake396ca572001-09-06 16:30:30 +000079Since Python may define some pre-processor definitions which affect
80the standard headers on some systems, you must include \file{Python.h}
81before any standard headers are included.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000082
83All user visible names defined by Python.h (except those defined by
Fred Drakee058b4f1998-02-16 06:15:35 +000084the included standard headers) have one of the prefixes \samp{Py} or
Fred Drake659ebfa2000-04-03 15:42:13 +000085\samp{_Py}. Names beginning with \samp{_Py} are for internal use by
86the Python implementation and should not be used by extension writers.
87Structure member names do not have a reserved prefix.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000088
Fred Drakee058b4f1998-02-16 06:15:35 +000089\strong{Important:} user code should never define names that begin
90with \samp{Py} or \samp{_Py}. This confuses the reader, and
91jeopardizes the portability of the user code to future Python
92versions, which may define additional names beginning with one of
93these prefixes.
Guido van Rossum580aa8d1997-11-25 15:34:51 +000094
Fred Drake659ebfa2000-04-03 15:42:13 +000095The header files are typically installed with Python. On \UNIX, these
96are located in the directories
97\file{\envvar{prefix}/include/python\var{version}/} and
98\file{\envvar{exec_prefix}/include/python\var{version}/}, where
99\envvar{prefix} and \envvar{exec_prefix} are defined by the
100corresponding parameters to Python's \program{configure} script and
101\var{version} is \code{sys.version[:3]}. On Windows, the headers are
102installed in \file{\envvar{prefix}/include}, where \envvar{prefix} is
103the installation directory specified to the installer.
104
105To include the headers, place both directories (if different) on your
106compiler's search path for includes. Do \emph{not} place the parent
107directories on the search path and then use
Fred Draked5d04352000-09-14 20:24:17 +0000108\samp{\#include <python\shortversion/Python.h>}; this will break on
Fred Drake659ebfa2000-04-03 15:42:13 +0000109multi-platform builds since the platform independent headers under
110\envvar{prefix} include the platform specific headers from
111\envvar{exec_prefix}.
112
Fred Drakebab29652001-07-10 16:10:08 +0000113\Cpp{} users should note that though the API is defined entirely using
114C, the header files do properly declare the entry points to be
115\code{extern "C"}, so there is no need to do anything special to use
116the API from \Cpp.
117
Fred Drakeefd146c1999-02-15 15:30:45 +0000118
119\section{Objects, Types and Reference Counts \label{objects}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000120
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000121Most Python/C API functions have one or more arguments as well as a
Fred Drake659ebfa2000-04-03 15:42:13 +0000122return value of type \ctype{PyObject*}. This type is a pointer
Fred Drakee058b4f1998-02-16 06:15:35 +0000123to an opaque data type representing an arbitrary Python
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000124object. Since all Python object types are treated the same way by the
125Python language in most situations (e.g., assignments, scope rules,
126and argument passing), it is only fitting that they should be
Fred Drake659ebfa2000-04-03 15:42:13 +0000127represented by a single C type. Almost all Python objects live on the
128heap: you never declare an automatic or static variable of type
129\ctype{PyObject}, only pointer variables of type \ctype{PyObject*} can
130be declared. The sole exception are the type objects\obindex{type};
131since these must never be deallocated, they are typically static
132\ctype{PyTypeObject} objects.
Guido van Rossum59a61351997-08-14 20:34:33 +0000133
Fred Drakee058b4f1998-02-16 06:15:35 +0000134All Python objects (even Python integers) have a \dfn{type} and a
135\dfn{reference count}. An object's type determines what kind of object
Guido van Rossum4a944d71997-08-14 20:35:38 +0000136it is (e.g., an integer, a list, or a user-defined function; there are
Fred Drakebe486461999-11-09 17:03:03 +0000137many more as explained in the \citetitle[../ref/ref.html]{Python
138Reference Manual}). For each of the well-known types there is a macro
139to check whether an object is of that type; for instance,
Fred Drake659ebfa2000-04-03 15:42:13 +0000140\samp{PyList_Check(\var{a})} is true if (and only if) the object
141pointed to by \var{a} is a Python list.
Guido van Rossum59a61351997-08-14 20:34:33 +0000142
Fred Drakeefd146c1999-02-15 15:30:45 +0000143
144\subsection{Reference Counts \label{refcounts}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000145
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000146The reference count is important because today's computers have a
Fred Drake003d8da1998-04-13 00:53:42 +0000147finite (and often severely limited) memory size; it counts how many
Guido van Rossum4a944d71997-08-14 20:35:38 +0000148different places there are that have a reference to an object. Such a
Fred Drake659ebfa2000-04-03 15:42:13 +0000149place could be another object, or a global (or static) C variable, or
150a local variable in some C function. When an object's reference count
Guido van Rossum4a944d71997-08-14 20:35:38 +0000151becomes zero, the object is deallocated. If it contains references to
152other objects, their reference count is decremented. Those other
153objects may be deallocated in turn, if this decrement makes their
154reference count become zero, and so on. (There's an obvious problem
155with objects that reference each other here; for now, the solution is
Fred Drake659ebfa2000-04-03 15:42:13 +0000156``don't do that.'')
Guido van Rossum59a61351997-08-14 20:34:33 +0000157
Guido van Rossum4a944d71997-08-14 20:35:38 +0000158Reference counts are always manipulated explicitly. The normal way is
Fred Drake659ebfa2000-04-03 15:42:13 +0000159to use the macro \cfunction{Py_INCREF()}\ttindex{Py_INCREF()} to
160increment an object's reference count by one, and
161\cfunction{Py_DECREF()}\ttindex{Py_DECREF()} to decrement it by
162one. The \cfunction{Py_DECREF()} macro is considerably more complex
163than the incref one, since it must check whether the reference count
164becomes zero and then cause the object's deallocator to be called.
165The deallocator is a function pointer contained in the object's type
166structure. The type-specific deallocator takes care of decrementing
167the reference counts for other objects contained in the object if this
168is a compound object type, such as a list, as well as performing any
169additional finalization that's needed. There's no chance that the
170reference count can overflow; at least as many bits are used to hold
171the reference count as there are distinct memory locations in virtual
172memory (assuming \code{sizeof(long) >= sizeof(char*)}). Thus, the
173reference count increment is a simple operation.
Guido van Rossum59a61351997-08-14 20:34:33 +0000174
Guido van Rossum4a944d71997-08-14 20:35:38 +0000175It is not necessary to increment an object's reference count for every
176local variable that contains a pointer to an object. In theory, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000177object's reference count goes up by one when the variable is made to
Guido van Rossum4a944d71997-08-14 20:35:38 +0000178point to it and it goes down by one when the variable goes out of
179scope. However, these two cancel each other out, so at the end the
180reference count hasn't changed. The only real reason to use the
181reference count is to prevent the object from being deallocated as
182long as our variable is pointing to it. If we know that there is at
183least one other reference to the object that lives at least as long as
184our variable, there is no need to increment the reference count
185temporarily. An important situation where this arises is in objects
Fred Drake659ebfa2000-04-03 15:42:13 +0000186that are passed as arguments to C functions in an extension module
Guido van Rossum4a944d71997-08-14 20:35:38 +0000187that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000188reference to every argument for the duration of the call.
189
Fred Drakee058b4f1998-02-16 06:15:35 +0000190However, a common pitfall is to extract an object from a list and
191hold on to it for a while without incrementing its reference count.
192Some other operation might conceivably remove the object from the
193list, decrementing its reference count and possible deallocating it.
194The real danger is that innocent-looking operations may invoke
195arbitrary Python code which could do this; there is a code path which
196allows control to flow back to the user from a \cfunction{Py_DECREF()},
197so almost any operation is potentially dangerous.
Guido van Rossum59a61351997-08-14 20:34:33 +0000198
Guido van Rossum4a944d71997-08-14 20:35:38 +0000199A safe approach is to always use the generic operations (functions
Fred Drake659ebfa2000-04-03 15:42:13 +0000200whose name begins with \samp{PyObject_}, \samp{PyNumber_},
201\samp{PySequence_} or \samp{PyMapping_}). These operations always
202increment the reference count of the object they return. This leaves
203the caller with the responsibility to call
204\cfunction{Py_DECREF()} when they are done with the result; this soon
205becomes second nature.
Guido van Rossum59a61351997-08-14 20:34:33 +0000206
Fred Drakeefd146c1999-02-15 15:30:45 +0000207
208\subsubsection{Reference Count Details \label{refcountDetails}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000209
210The reference count behavior of functions in the Python/C API is best
Fred Drake659ebfa2000-04-03 15:42:13 +0000211explained in terms of \emph{ownership of references}. Note that we
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000212talk of owning references, never of owning objects; objects are always
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000213shared! When a function owns a reference, it has to dispose of it
Fred Drakee058b4f1998-02-16 06:15:35 +0000214properly --- either by passing ownership on (usually to its caller) or
215by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When
216a function passes ownership of a reference on to its caller, the
217caller is said to receive a \emph{new} reference. When no ownership
218is transferred, the caller is said to \emph{borrow} the reference.
219Nothing needs to be done for a borrowed reference.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000220
Fred Drakea8455ab2000-06-16 19:58:42 +0000221Conversely, when a calling function passes it a reference to an
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000222object, there are two possibilities: the function \emph{steals} a
223reference to the object, or it does not. Few functions steal
Fred Drakee058b4f1998-02-16 06:15:35 +0000224references; the two notable exceptions are
Fred Drake659ebfa2000-04-03 15:42:13 +0000225\cfunction{PyList_SetItem()}\ttindex{PyList_SetItem()} and
226\cfunction{PyTuple_SetItem()}\ttindex{PyTuple_SetItem()}, which
Fred Drakee058b4f1998-02-16 06:15:35 +0000227steal a reference to the item (but not to the tuple or list into which
Fred Drake003d8da1998-04-13 00:53:42 +0000228the item is put!). These functions were designed to steal a reference
Fred Drakee058b4f1998-02-16 06:15:35 +0000229because of a common idiom for populating a tuple or list with newly
230created objects; for example, the code to create the tuple \code{(1,
2312, "three")} could look like this (forgetting about error handling for
Fred Drake659ebfa2000-04-03 15:42:13 +0000232the moment; a better way to code this is shown below):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000233
234\begin{verbatim}
235PyObject *t;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000236
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000237t = PyTuple_New(3);
238PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
239PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
240PyTuple_SetItem(t, 2, PyString_FromString("three"));
241\end{verbatim}
242
Fred Drakee058b4f1998-02-16 06:15:35 +0000243Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to
244set tuple items; \cfunction{PySequence_SetItem()} and
245\cfunction{PyObject_SetItem()} refuse to do this since tuples are an
246immutable data type. You should only use
247\cfunction{PyTuple_SetItem()} for tuples that you are creating
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000248yourself.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000249
250Equivalent code for populating a list can be written using
Fred Drakee058b4f1998-02-16 06:15:35 +0000251\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code
252can also use \cfunction{PySequence_SetItem()}; this illustrates the
253difference between the two (the extra \cfunction{Py_DECREF()} calls):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000254
255\begin{verbatim}
256PyObject *l, *x;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000257
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000258l = PyList_New(3);
259x = PyInt_FromLong(1L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000260PySequence_SetItem(l, 0, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000261x = PyInt_FromLong(2L);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000262PySequence_SetItem(l, 1, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000263x = PyString_FromString("three");
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000264PySequence_SetItem(l, 2, x); Py_DECREF(x);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000265\end{verbatim}
266
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000267You might find it strange that the ``recommended'' approach takes more
268code. However, in practice, you will rarely use these ways of
269creating and populating a tuple or list. There's a generic function,
Fred Drakee058b4f1998-02-16 06:15:35 +0000270\cfunction{Py_BuildValue()}, that can create most common objects from
Fred Drake659ebfa2000-04-03 15:42:13 +0000271C values, directed by a \dfn{format string}. For example, the
Fred Drakee058b4f1998-02-16 06:15:35 +0000272above two blocks of code could be replaced by the following (which
273also takes care of the error checking):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000274
275\begin{verbatim}
276PyObject *t, *l;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000277
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000278t = Py_BuildValue("(iis)", 1, 2, "three");
279l = Py_BuildValue("[iis]", 1, 2, "three");
280\end{verbatim}
281
Fred Drakee058b4f1998-02-16 06:15:35 +0000282It is much more common to use \cfunction{PyObject_SetItem()} and
283friends with items whose references you are only borrowing, like
284arguments that were passed in to the function you are writing. In
285that case, their behaviour regarding reference counts is much saner,
286since you don't have to increment a reference count so you can give a
287reference away (``have it be stolen''). For example, this function
288sets all items of a list (actually, any mutable sequence) to a given
289item:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000290
291\begin{verbatim}
292int set_all(PyObject *target, PyObject *item)
293{
294 int i, n;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000295
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000296 n = PyObject_Length(target);
297 if (n < 0)
298 return -1;
299 for (i = 0; i < n; i++) {
300 if (PyObject_SetItem(target, i, item) < 0)
301 return -1;
302 }
303 return 0;
304}
305\end{verbatim}
Fred Drake659ebfa2000-04-03 15:42:13 +0000306\ttindex{set_all()}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000307
308The situation is slightly different for function return values.
309While passing a reference to most functions does not change your
310ownership responsibilities for that reference, many functions that
311return a referece to an object give you ownership of the reference.
312The reason is simple: in many cases, the returned object is created
313on the fly, and the reference you get is the only reference to the
Fred Drakee058b4f1998-02-16 06:15:35 +0000314object. Therefore, the generic functions that return object
315references, like \cfunction{PyObject_GetItem()} and
Fred Drakebab29652001-07-10 16:10:08 +0000316\cfunction{PySequence_GetItem()}, always return a new reference (the
317caller becomes the owner of the reference).
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000318
319It is important to realize that whether you own a reference returned
Fred Drakee058b4f1998-02-16 06:15:35 +0000320by a function depends on which function you call only --- \emph{the
Fred Drakebab29652001-07-10 16:10:08 +0000321plumage} (the type of the type of the object passed as an
Fred Drakee058b4f1998-02-16 06:15:35 +0000322argument to the function) \emph{doesn't enter into it!} Thus, if you
323extract an item from a list using \cfunction{PyList_GetItem()}, you
324don't own the reference --- but if you obtain the same item from the
325same list using \cfunction{PySequence_GetItem()} (which happens to
326take exactly the same arguments), you do own a reference to the
327returned object.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000328
Fred Drakee058b4f1998-02-16 06:15:35 +0000329Here is an example of how you could write a function that computes the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000330sum of the items in a list of integers; once using
Fred Drake659ebfa2000-04-03 15:42:13 +0000331\cfunction{PyList_GetItem()}\ttindex{PyList_GetItem()}, and once using
332\cfunction{PySequence_GetItem()}\ttindex{PySequence_GetItem()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000333
334\begin{verbatim}
335long sum_list(PyObject *list)
336{
337 int i, n;
338 long total = 0;
339 PyObject *item;
Fred Drakec6fa34e1998-04-02 06:47:24 +0000340
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000341 n = PyList_Size(list);
342 if (n < 0)
343 return -1; /* Not a list */
344 for (i = 0; i < n; i++) {
345 item = PyList_GetItem(list, i); /* Can't fail */
346 if (!PyInt_Check(item)) continue; /* Skip non-integers */
347 total += PyInt_AsLong(item);
348 }
349 return total;
350}
351\end{verbatim}
Fred Drake659ebfa2000-04-03 15:42:13 +0000352\ttindex{sum_list()}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000353
354\begin{verbatim}
355long sum_sequence(PyObject *sequence)
356{
357 int i, n;
358 long total = 0;
359 PyObject *item;
Fred Drake659ebfa2000-04-03 15:42:13 +0000360 n = PySequence_Length(sequence);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000361 if (n < 0)
362 return -1; /* Has no length */
363 for (i = 0; i < n; i++) {
Fred Drake659ebfa2000-04-03 15:42:13 +0000364 item = PySequence_GetItem(sequence, i);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000365 if (item == NULL)
366 return -1; /* Not a sequence, or other failure */
367 if (PyInt_Check(item))
368 total += PyInt_AsLong(item);
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000369 Py_DECREF(item); /* Discard reference ownership */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000370 }
371 return total;
372}
373\end{verbatim}
Fred Drake659ebfa2000-04-03 15:42:13 +0000374\ttindex{sum_sequence()}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000375
Fred Drakeefd146c1999-02-15 15:30:45 +0000376
377\subsection{Types \label{types}}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000378
379There are few other data types that play a significant role in
Fred Drake659ebfa2000-04-03 15:42:13 +0000380the Python/C API; most are simple C types such as \ctype{int},
381\ctype{long}, \ctype{double} and \ctype{char*}. A few structure types
Guido van Rossum4a944d71997-08-14 20:35:38 +0000382are used to describe static tables used to list the functions exported
Fred Drake659ebfa2000-04-03 15:42:13 +0000383by a module or the data attributes of a new object type, and another
384is used to describe the value of a complex number. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000385be discussed together with the functions that use them.
386
Fred Drakeefd146c1999-02-15 15:30:45 +0000387
388\section{Exceptions \label{exceptions}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000389
Guido van Rossum4a944d71997-08-14 20:35:38 +0000390The Python programmer only needs to deal with exceptions if specific
391error handling is required; unhandled exceptions are automatically
Fred Drake659ebfa2000-04-03 15:42:13 +0000392propagated to the caller, then to the caller's caller, and so on, until
Guido van Rossum4a944d71997-08-14 20:35:38 +0000393they reach the top-level interpreter, where they are reported to the
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000394user accompanied by a stack traceback.
Guido van Rossum59a61351997-08-14 20:34:33 +0000395
Fred Drake659ebfa2000-04-03 15:42:13 +0000396For C programmers, however, error checking always has to be explicit.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000397All functions in the Python/C API can raise exceptions, unless an
398explicit claim is made otherwise in a function's documentation. In
399general, when a function encounters an error, it sets an exception,
400discards any object references that it owns, and returns an
Fred Drakee058b4f1998-02-16 06:15:35 +0000401error indicator --- usually \NULL{} or \code{-1}. A few functions
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000402return a Boolean true/false result, with false indicating an error.
403Very few functions return no explicit error indicator or have an
404ambiguous return value, and require explicit testing for errors with
Fred Drake659ebfa2000-04-03 15:42:13 +0000405\cfunction{PyErr_Occurred()}\ttindex{PyErr_Occurred()}.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000406
407Exception state is maintained in per-thread storage (this is
408equivalent to using global storage in an unthreaded application). A
Fred Drakec6fa34e1998-04-02 06:47:24 +0000409thread can be in one of two states: an exception has occurred, or not.
Fred Drakee058b4f1998-02-16 06:15:35 +0000410The function \cfunction{PyErr_Occurred()} can be used to check for
411this: it returns a borrowed reference to the exception type object
412when an exception has occurred, and \NULL{} otherwise. There are a
413number of functions to set the exception state:
Fred Drake659ebfa2000-04-03 15:42:13 +0000414\cfunction{PyErr_SetString()}\ttindex{PyErr_SetString()} is the most
415common (though not the most general) function to set the exception
416state, and \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} clears the
417exception state.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000418
419The full exception state consists of three objects (all of which can
Fred Drakee058b4f1998-02-16 06:15:35 +0000420be \NULL{}): the exception type, the corresponding exception
Fred Drake659ebfa2000-04-03 15:42:13 +0000421value, and the traceback. These have the same meanings as the Python
422\withsubitem{(in module sys)}{
423 \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}}
424objects \code{sys.exc_type}, \code{sys.exc_value}, and
425\code{sys.exc_traceback}; however, they are not the same: the Python
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000426objects represent the last exception being handled by a Python
Fred Drake659ebfa2000-04-03 15:42:13 +0000427\keyword{try} \ldots\ \keyword{except} statement, while the C level
Fred Drakee058b4f1998-02-16 06:15:35 +0000428exception state only exists while an exception is being passed on
Fred Drake659ebfa2000-04-03 15:42:13 +0000429between C functions until it reaches the Python bytecode interpreter's
430main loop, which takes care of transferring it to \code{sys.exc_type}
431and friends.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000432
Fred Drakec6fa34e1998-04-02 06:47:24 +0000433Note that starting with Python 1.5, the preferred, thread-safe way to
Fred Drake659ebfa2000-04-03 15:42:13 +0000434access the exception state from Python code is to call the function
435\withsubitem{(in module sys)}{\ttindex{exc_info()}}
Fred Drakee058b4f1998-02-16 06:15:35 +0000436\function{sys.exc_info()}, which returns the per-thread exception state
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000437for Python code. Also, the semantics of both ways to access the
438exception state have changed so that a function which catches an
439exception will save and restore its thread's exception state so as to
440preserve the exception state of its caller. This prevents common bugs
441in exception handling code caused by an innocent-looking function
442overwriting the exception being handled; it also reduces the often
443unwanted lifetime extension for objects that are referenced by the
Fred Drakec6fa34e1998-04-02 06:47:24 +0000444stack frames in the traceback.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000445
446As a general principle, a function that calls another function to
447perform some task should check whether the called function raised an
448exception, and if so, pass the exception state on to its caller. It
Fred Drake659ebfa2000-04-03 15:42:13 +0000449should discard any object references that it owns, and return an
Fred Drakee058b4f1998-02-16 06:15:35 +0000450error indicator, but it should \emph{not} set another exception ---
451that would overwrite the exception that was just raised, and lose
452important information about the exact cause of the error.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000453
Fred Drake659ebfa2000-04-03 15:42:13 +0000454A simple example of detecting exceptions and passing them on is shown
455in the \cfunction{sum_sequence()}\ttindex{sum_sequence()} example
456above. It so happens that that example doesn't need to clean up any
457owned references when it detects an error. The following example
458function shows some error cleanup. First, to remind you why you like
459Python, we show the equivalent Python code:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000460
461\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000462def incr_item(dict, key):
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000463 try:
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000464 item = dict[key]
465 except KeyError:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000466 item = 0
Fred Drake6b3f3f22000-11-29 15:48:22 +0000467 dict[key] = item + 1
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000468\end{verbatim}
Fred Drake659ebfa2000-04-03 15:42:13 +0000469\ttindex{incr_item()}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000470
Fred Drake659ebfa2000-04-03 15:42:13 +0000471Here is the corresponding C code, in all its glory:
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000472
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000473\begin{verbatim}
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000474int incr_item(PyObject *dict, PyObject *key)
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000475{
476 /* Objects all initialized to NULL for Py_XDECREF */
477 PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
Guido van Rossum5b8a5231997-12-30 04:38:44 +0000478 int rv = -1; /* Return value initialized to -1 (failure) */
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000479
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000480 item = PyObject_GetItem(dict, key);
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000481 if (item == NULL) {
Fred Drakec6fa34e1998-04-02 06:47:24 +0000482 /* Handle KeyError only: */
Fred Drake6b3f3f22000-11-29 15:48:22 +0000483 if (!PyErr_ExceptionMatches(PyExc_KeyError))
484 goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000485
486 /* Clear the error and use zero: */
487 PyErr_Clear();
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000488 item = PyInt_FromLong(0L);
Fred Drake6b3f3f22000-11-29 15:48:22 +0000489 if (item == NULL)
490 goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000491 }
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000492 const_one = PyInt_FromLong(1L);
Fred Drake6b3f3f22000-11-29 15:48:22 +0000493 if (const_one == NULL)
494 goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000495
496 incremented_item = PyNumber_Add(item, const_one);
Fred Drake6b3f3f22000-11-29 15:48:22 +0000497 if (incremented_item == NULL)
498 goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000499
Fred Drake6b3f3f22000-11-29 15:48:22 +0000500 if (PyObject_SetItem(dict, key, incremented_item) < 0)
501 goto error;
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000502 rv = 0; /* Success */
503 /* Continue with cleanup code */
504
505 error:
506 /* Cleanup code, shared by success and failure path */
507
508 /* Use Py_XDECREF() to ignore NULL references */
509 Py_XDECREF(item);
510 Py_XDECREF(const_one);
511 Py_XDECREF(incremented_item);
512
513 return rv; /* -1 for error, 0 for success */
514}
515\end{verbatim}
Fred Drake659ebfa2000-04-03 15:42:13 +0000516\ttindex{incr_item()}
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000517
Fred Drakef8830d11998-04-23 14:06:01 +0000518This example represents an endorsed use of the \keyword{goto} statement
Fred Drake659ebfa2000-04-03 15:42:13 +0000519in C! It illustrates the use of
520\cfunction{PyErr_ExceptionMatches()}\ttindex{PyErr_ExceptionMatches()} and
521\cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} to
522handle specific exceptions, and the use of
523\cfunction{Py_XDECREF()}\ttindex{Py_XDECREF()} to
524dispose of owned references that may be \NULL{} (note the
525\character{X} in the name; \cfunction{Py_DECREF()} would crash when
526confronted with a \NULL{} reference). It is important that the
527variables used to hold owned references are initialized to \NULL{} for
528this to work; likewise, the proposed return value is initialized to
529\code{-1} (failure) and only set to success after the final call made
530is successful.
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000531
Guido van Rossum59a61351997-08-14 20:34:33 +0000532
Fred Drakeefd146c1999-02-15 15:30:45 +0000533\section{Embedding Python \label{embedding}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000534
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000535The one important task that only embedders (as opposed to extension
536writers) of the Python interpreter have to worry about is the
537initialization, and possibly the finalization, of the Python
538interpreter. Most functionality of the interpreter can only be used
539after the interpreter has been initialized.
Guido van Rossum59a61351997-08-14 20:34:33 +0000540
Fred Drake659ebfa2000-04-03 15:42:13 +0000541The basic initialization function is
542\cfunction{Py_Initialize()}\ttindex{Py_Initialize()}.
Fred Drakee058b4f1998-02-16 06:15:35 +0000543This initializes the table of loaded modules, and creates the
Fred Drake4de05a91998-02-16 14:25:26 +0000544fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
Fred Drake680cabb2001-08-14 15:32:16 +0000545\module{__main__}\refbimodindex{__main__}, \module{sys}\refbimodindex{sys},
546and \module{exceptions}.\refbimodindex{exceptions} It also initializes
547the module search path (\code{sys.path}).%
Fred Drakec6fa34e1998-04-02 06:47:24 +0000548\indexiii{module}{search}{path}
Fred Drake659ebfa2000-04-03 15:42:13 +0000549\withsubitem{(in module sys)}{\ttindex{path}}
Guido van Rossum59a61351997-08-14 20:34:33 +0000550
Fred Drakee058b4f1998-02-16 06:15:35 +0000551\cfunction{Py_Initialize()} does not set the ``script argument list''
Guido van Rossum4a944d71997-08-14 20:35:38 +0000552(\code{sys.argv}). If this variable is needed by Python code that
553will be executed later, it must be set explicitly with a call to
Fred Drake659ebfa2000-04-03 15:42:13 +0000554\code{PySys_SetArgv(\var{argc},
555\var{argv})}\ttindex{PySys_SetArgv()} subsequent to the call to
556\cfunction{Py_Initialize()}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000557
Fred Drakeb0a78731998-01-13 18:51:10 +0000558On most systems (in particular, on \UNIX{} and Windows, although the
Fred Drake659ebfa2000-04-03 15:42:13 +0000559details are slightly different),
560\cfunction{Py_Initialize()} calculates the module search path based
561upon its best guess for the location of the standard Python
562interpreter executable, assuming that the Python library is found in a
563fixed location relative to the Python interpreter executable. In
564particular, it looks for a directory named
Fred Draked5d04352000-09-14 20:24:17 +0000565\file{lib/python\shortversion} relative to the parent directory where
566the executable named \file{python} is found on the shell command
567search path (the environment variable \envvar{PATH}).
Guido van Rossum42cefd01997-10-05 15:27:29 +0000568
569For instance, if the Python executable is found in
Fred Drakee058b4f1998-02-16 06:15:35 +0000570\file{/usr/local/bin/python}, it will assume that the libraries are in
Fred Draked5d04352000-09-14 20:24:17 +0000571\file{/usr/local/lib/python\shortversion}. (In fact, this particular path
Fred Drakee058b4f1998-02-16 06:15:35 +0000572is also the ``fallback'' location, used when no executable file named
Fred Drakec6fa34e1998-04-02 06:47:24 +0000573\file{python} is found along \envvar{PATH}.) The user can override
574this behavior by setting the environment variable \envvar{PYTHONHOME},
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000575or insert additional directories in front of the standard path by
Fred Drakec6fa34e1998-04-02 06:47:24 +0000576setting \envvar{PYTHONPATH}.
Guido van Rossum59a61351997-08-14 20:34:33 +0000577
Guido van Rossum4a944d71997-08-14 20:35:38 +0000578The embedding application can steer the search by calling
Fred Drake659ebfa2000-04-03 15:42:13 +0000579\code{Py_SetProgramName(\var{file})}\ttindex{Py_SetProgramName()} \emph{before} calling
Fred Drakec6fa34e1998-04-02 06:47:24 +0000580\cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still
581overrides this and \envvar{PYTHONPATH} is still inserted in front of
Fred Drakee058b4f1998-02-16 06:15:35 +0000582the standard path. An application that requires total control has to
Fred Drake659ebfa2000-04-03 15:42:13 +0000583provide its own implementation of
584\cfunction{Py_GetPath()}\ttindex{Py_GetPath()},
585\cfunction{Py_GetPrefix()}\ttindex{Py_GetPrefix()},
586\cfunction{Py_GetExecPrefix()}\ttindex{Py_GetExecPrefix()}, and
587\cfunction{Py_GetProgramFullPath()}\ttindex{Py_GetProgramFullPath()} (all
588defined in \file{Modules/getpath.c}).
Guido van Rossum59a61351997-08-14 20:34:33 +0000589
Guido van Rossum4a944d71997-08-14 20:35:38 +0000590Sometimes, it is desirable to ``uninitialize'' Python. For instance,
591the application may want to start over (make another call to
Fred Drakee058b4f1998-02-16 06:15:35 +0000592\cfunction{Py_Initialize()}) or the application is simply done with its
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000593use of Python and wants to free all memory allocated by Python. This
Fred Drakee058b4f1998-02-16 06:15:35 +0000594can be accomplished by calling \cfunction{Py_Finalize()}. The function
Fred Drake659ebfa2000-04-03 15:42:13 +0000595\cfunction{Py_IsInitialized()}\ttindex{Py_IsInitialized()} returns
596true if Python is currently in the initialized state. More
597information about these functions is given in a later chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000598
Guido van Rossum4a944d71997-08-14 20:35:38 +0000599
Fred Drakeefd146c1999-02-15 15:30:45 +0000600\chapter{The Very High Level Layer \label{veryhigh}}
Guido van Rossum4a944d71997-08-14 20:35:38 +0000601
Fred Drakee5bf8b21998-02-12 21:22:28 +0000602The functions in this chapter will let you execute Python source code
603given in a file or a buffer, but they will not let you interact in a
604more detailed way with the interpreter.
Guido van Rossum4a944d71997-08-14 20:35:38 +0000605
Fred Drake659ebfa2000-04-03 15:42:13 +0000606Several of these functions accept a start symbol from the grammar as a
607parameter. The available start symbols are \constant{Py_eval_input},
608\constant{Py_file_input}, and \constant{Py_single_input}. These are
609described following the functions which accept them as parameters.
610
Fred Drake510d08b2000-08-14 02:50:21 +0000611Note also that several of these functions take \ctype{FILE*}
612parameters. On particular issue which needs to be handled carefully
613is that the \ctype{FILE} structure for different C libraries can be
614different and incompatible. Under Windows (at least), it is possible
615for dynamically linked extensions to actually use different libraries,
616so care should be taken that \ctype{FILE*} parameters are only passed
617to these functions if it is certain that they were created by the same
618library that the Python runtime is using.
619
Fred Drake24e62192001-05-21 15:56:55 +0000620\begin{cfuncdesc}{int}{Py_Main}{int argc, char **argv}
621 The main program for the standard interpreter. This is made
622 available for programs which embed Python. The \var{argc} and
623 \var{argv} parameters should be prepared exactly as those which are
624 passed to a C program's \cfunction{main()} function. It is
625 important to note that the argument list may be modified (but the
626 contents of the strings pointed to by the argument list are not).
627 The return value will be the integer passed to the
628 \function{sys.exit()} function, \code{1} if the interpreter exits
629 due to an exception, or \code{2} if the parameter list does not
630 represent a valid Python command line.
631\end{cfuncdesc}
632
Fred Drakec6fa34e1998-04-02 06:47:24 +0000633\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename}
Fred Drake0041a941999-04-29 04:20:46 +0000634 If \var{fp} refers to a file associated with an interactive device
635 (console or terminal input or \UNIX{} pseudo-terminal), return the
636 value of \cfunction{PyRun_InteractiveLoop()}, otherwise return the
637 result of \cfunction{PyRun_SimpleFile()}. If \var{filename} is
Fred Drakea8d73412000-08-11 20:39:29 +0000638 \NULL{}, this function uses \code{"???"} as the filename.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000639\end{cfuncdesc}
640
Fred Drakec6fa34e1998-04-02 06:47:24 +0000641\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command}
Fred Drake0041a941999-04-29 04:20:46 +0000642 Executes the Python source code from \var{command} in the
643 \module{__main__} module. If \module{__main__} does not already
644 exist, it is created. Returns \code{0} on success or \code{-1} if
645 an exception was raised. If there was an error, there is no way to
646 get the exception information.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000647\end{cfuncdesc}
648
Fred Drakec6fa34e1998-04-02 06:47:24 +0000649\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename}
Fred Drake0041a941999-04-29 04:20:46 +0000650 Similar to \cfunction{PyRun_SimpleString()}, but the Python source
651 code is read from \var{fp} instead of an in-memory string.
652 \var{filename} should be the name of the file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000653\end{cfuncdesc}
654
Fred Drakec6fa34e1998-04-02 06:47:24 +0000655\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename}
Fred Drakea8d73412000-08-11 20:39:29 +0000656 Read and execute a single statement from a file associated with an
657 interactive device. If \var{filename} is \NULL, \code{"???"} is
658 used instead. The user will be prompted using \code{sys.ps1} and
659 \code{sys.ps2}. Returns \code{0} when the input was executed
660 successfully, \code{-1} if there was an exception, or an error code
661 from the \file{errcode.h} include file distributed as part of Python
662 in case of a parse error. (Note that \file{errcode.h} is not
663 included by \file{Python.h}, so must be included specifically if
664 needed.)
Fred Drakee5bf8b21998-02-12 21:22:28 +0000665\end{cfuncdesc}
666
Fred Drakec6fa34e1998-04-02 06:47:24 +0000667\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename}
Fred Drakea8d73412000-08-11 20:39:29 +0000668 Read and execute statements from a file associated with an
669 interactive device until \EOF{} is reached. If \var{filename} is
670 \NULL, \code{"???"} is used instead. The user will be prompted
671 using \code{sys.ps1} and \code{sys.ps2}. Returns \code{0} at \EOF.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000672\end{cfuncdesc}
673
Fred Drakec6fa34e1998-04-02 06:47:24 +0000674\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str,
675 int start}
Fred Drake0041a941999-04-29 04:20:46 +0000676 Parse Python source code from \var{str} using the start token
677 \var{start}. The result can be used to create a code object which
678 can be evaluated efficiently. This is useful if a code fragment
679 must be evaluated many times.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000680\end{cfuncdesc}
681
Fred Drakec6fa34e1998-04-02 06:47:24 +0000682\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp,
683 char *filename, int start}
Fred Drake0041a941999-04-29 04:20:46 +0000684 Similar to \cfunction{PyParser_SimpleParseString()}, but the Python
685 source code is read from \var{fp} instead of an in-memory string.
686 \var{filename} should be the name of the file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000687\end{cfuncdesc}
688
Fred Drakec6fa34e1998-04-02 06:47:24 +0000689\begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start,
690 PyObject *globals,
691 PyObject *locals}
Fred Drake0041a941999-04-29 04:20:46 +0000692 Execute Python source code from \var{str} in the context specified
693 by the dictionaries \var{globals} and \var{locals}. The parameter
694 \var{start} specifies the start token that should be used to parse
695 the source code.
696
697 Returns the result of executing the code as a Python object, or
698 \NULL{} if an exception was raised.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000699\end{cfuncdesc}
700
Fred Drakec6fa34e1998-04-02 06:47:24 +0000701\begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename,
702 int start, PyObject *globals,
703 PyObject *locals}
Fred Drake0041a941999-04-29 04:20:46 +0000704 Similar to \cfunction{PyRun_String()}, but the Python source code is
Fred Drake659ebfa2000-04-03 15:42:13 +0000705 read from \var{fp} instead of an in-memory string.
706 \var{filename} should be the name of the file.
Fred Drakee5bf8b21998-02-12 21:22:28 +0000707\end{cfuncdesc}
708
Fred Drakec6fa34e1998-04-02 06:47:24 +0000709\begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename,
710 int start}
Fred Drake0041a941999-04-29 04:20:46 +0000711 Parse and compile the Python source code in \var{str}, returning the
712 resulting code object. The start token is given by \var{start};
Fred Drakec924b8d1999-08-23 18:57:25 +0000713 this can be used to constrain the code which can be compiled and should
714 be \constant{Py_eval_input}, \constant{Py_file_input}, or
715 \constant{Py_single_input}. The filename specified by
716 \var{filename} is used to construct the code object and may appear
717 in tracebacks or \exception{SyntaxError} exception messages. This
718 returns \NULL{} if the code cannot be parsed or compiled.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000719\end{cfuncdesc}
720
Fred Drakec924b8d1999-08-23 18:57:25 +0000721\begin{cvardesc}{int}{Py_eval_input}
722 The start symbol from the Python grammar for isolated expressions;
Fred Drake659ebfa2000-04-03 15:42:13 +0000723 for use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}.
Fred Drakec924b8d1999-08-23 18:57:25 +0000724\end{cvardesc}
725
726\begin{cvardesc}{int}{Py_file_input}
727 The start symbol from the Python grammar for sequences of statements
728 as read from a file or other source; for use with
Fred Drake659ebfa2000-04-03 15:42:13 +0000729 \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. This is
730 the symbol to use when compiling arbitrarily long Python source code.
Fred Drakec924b8d1999-08-23 18:57:25 +0000731\end{cvardesc}
732
733\begin{cvardesc}{int}{Py_single_input}
734 The start symbol from the Python grammar for a single statement; for
Fred Drake659ebfa2000-04-03 15:42:13 +0000735 use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}.
736 This is the symbol used for the interactive interpreter loop.
Fred Drakec924b8d1999-08-23 18:57:25 +0000737\end{cvardesc}
738
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000739
Fred Drakeefd146c1999-02-15 15:30:45 +0000740\chapter{Reference Counting \label{countingRefs}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000741
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000742The macros in this section are used for managing reference counts
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000743of Python objects.
744
745\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
Fred Drakec6fa34e1998-04-02 06:47:24 +0000746Increment the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000747not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000748\cfunction{Py_XINCREF()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000749\end{cfuncdesc}
750
751\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000752Increment the reference count for object \var{o}. The object may be
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000753\NULL{}, in which case the macro has no effect.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000754\end{cfuncdesc}
755
756\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000757Decrement the reference count for object \var{o}. The object must
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000758not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
Fred Drakee058b4f1998-02-16 06:15:35 +0000759\cfunction{Py_XDECREF()}. If the reference count reaches zero, the
760object's type's deallocation function (which must not be \NULL{}) is
761invoked.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000762
763\strong{Warning:} The deallocation function can cause arbitrary Python
Fred Drake659ebfa2000-04-03 15:42:13 +0000764code to be invoked (e.g. when a class instance with a
765\method{__del__()} method is deallocated). While exceptions in such
766code are not propagated, the executed code has free access to all
767Python global variables. This means that any object that is reachable
768from a global variable should be in a consistent state before
769\cfunction{Py_DECREF()} is invoked. For example, code to delete an
770object from a list should copy a reference to the deleted object in a
771temporary variable, update the list data structure, and then call
772\cfunction{Py_DECREF()} for the temporary variable.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000773\end{cfuncdesc}
774
775\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +0000776Decrement the reference count for object \var{o}. The object may be
777\NULL{}, in which case the macro has no effect; otherwise the effect
778is the same as for \cfunction{Py_DECREF()}, and the same warning
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000779applies.
780\end{cfuncdesc}
781
Fred Drake659ebfa2000-04-03 15:42:13 +0000782The following functions or macros are only for use within the
783interpreter core: \cfunction{_Py_Dealloc()},
784\cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as
785well as the global variable \cdata{_Py_RefTotal}.
Guido van Rossum580aa8d1997-11-25 15:34:51 +0000786
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000787
Fred Drakeefd146c1999-02-15 15:30:45 +0000788\chapter{Exception Handling \label{exceptionHandling}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000789
Fred Drake659ebfa2000-04-03 15:42:13 +0000790The functions described in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000791exceptions. It is important to understand some of the basics of
Fred Drake659ebfa2000-04-03 15:42:13 +0000792Python exception handling. It works somewhat like the
793\UNIX{} \cdata{errno} variable: there is a global indicator (per
794thread) of the last error that occurred. Most functions don't clear
795this on success, but will set it to indicate the cause of the error on
796failure. Most functions also return an error indicator, usually
797\NULL{} if they are supposed to return a pointer, or \code{-1} if they
798return an integer (exception: the \cfunction{PyArg_Parse*()} functions
799return \code{1} for success and \code{0} for failure). When a
800function must fail because some function it called failed, it
801generally doesn't set the error indicator; the function it called
802already set it.
Guido van Rossumae110af1997-05-22 20:11:52 +0000803
804The error indicator consists of three Python objects corresponding to
Fred Drake659ebfa2000-04-03 15:42:13 +0000805\withsubitem{(in module sys)}{
806 \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}}
Guido van Rossumae110af1997-05-22 20:11:52 +0000807the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
808\code{sys.exc_traceback}. API functions exist to interact with the
809error indicator in various ways. There is a separate error indicator
810for each thread.
811
812% XXX Order of these should be more thoughtful.
813% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000814
815\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000816Print a standard traceback to \code{sys.stderr} and clear the error
817indicator. Call this function only when the error indicator is set.
818(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000819\end{cfuncdesc}
820
Fred Drakec6fa34e1998-04-02 06:47:24 +0000821\begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000822Test whether the error indicator is set. If set, return the exception
Fred Drakee058b4f1998-02-16 06:15:35 +0000823\emph{type} (the first argument to the last call to one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000824\cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If
Fred Drakee058b4f1998-02-16 06:15:35 +0000825not set, return \NULL{}. You do not own a reference to the return
826value, so you do not need to \cfunction{Py_DECREF()} it.
Fred Drake659ebfa2000-04-03 15:42:13 +0000827\strong{Note:} Do not compare the return value to a specific
Fred Drakee058b4f1998-02-16 06:15:35 +0000828exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown
Fred Drake659ebfa2000-04-03 15:42:13 +0000829below. (The comparison could easily fail since the exception may be
830an instance instead of a class, in the case of a class exception, or
831it may the a subclass of the expected exception.)
Guido van Rossum42cefd01997-10-05 15:27:29 +0000832\end{cfuncdesc}
833
834\begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000835Equivalent to
Fred Drakee058b4f1998-02-16 06:15:35 +0000836\samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}.
Fred Drake659ebfa2000-04-03 15:42:13 +0000837This should only be called when an exception is actually set; a memory
838access violation will occur if no exception has been raised.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000839\end{cfuncdesc}
840
841\begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000842Return true if the \var{given} exception matches the exception in
843\var{exc}. If \var{exc} is a class object, this also returns true
Fred Drake659ebfa2000-04-03 15:42:13 +0000844when \var{given} is an instance of a subclass. If \var{exc} is a tuple, all
Guido van Rossum42cefd01997-10-05 15:27:29 +0000845exceptions in the tuple (and recursively in subtuples) are searched
Fred Drake659ebfa2000-04-03 15:42:13 +0000846for a match. If \var{given} is \NULL, a memory access violation will
847occur.
Guido van Rossum42cefd01997-10-05 15:27:29 +0000848\end{cfuncdesc}
849
850\begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
Guido van Rossum42cefd01997-10-05 15:27:29 +0000851Under certain circumstances, the values returned by
Fred Drakee058b4f1998-02-16 06:15:35 +0000852\cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that
853\code{*\var{exc}} is a class object but \code{*\var{val}} is not an
854instance of the same class. This function can be used to instantiate
855the class in that case. If the values are already normalized, nothing
Fred Drake659ebfa2000-04-03 15:42:13 +0000856happens. The delayed normalization is implemented to improve
857performance.
Guido van Rossumae110af1997-05-22 20:11:52 +0000858\end{cfuncdesc}
859
860\begin{cfuncdesc}{void}{PyErr_Clear}{}
861Clear the error indicator. If the error indicator is not set, there
862is no effect.
863\end{cfuncdesc}
864
Fred Drake659ebfa2000-04-03 15:42:13 +0000865\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue,
866 PyObject **ptraceback}
Guido van Rossumae110af1997-05-22 20:11:52 +0000867Retrieve the error indicator into three variables whose addresses are
868passed. If the error indicator is not set, set all three variables to
869\NULL{}. If it is set, it will be cleared and you own a reference to
Fred Drake659ebfa2000-04-03 15:42:13 +0000870each object retrieved. The value and traceback object may be
871\NULL{} even when the type object is not. \strong{Note:} This
872function is normally only used by code that needs to handle exceptions
873or by code that needs to save and restore the error indicator
874temporarily.
Guido van Rossumae110af1997-05-22 20:11:52 +0000875\end{cfuncdesc}
876
Fred Drake17e63432000-08-31 05:50:40 +0000877\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value,
878 PyObject *traceback}
Guido van Rossumae110af1997-05-22 20:11:52 +0000879Set the error indicator from the three objects. If the error
880indicator is already set, it is cleared first. If the objects are
881\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
882and non-\NULL{} value or traceback. The exception type should be a
883string or class; if it is a class, the value should be an instance of
884that class. Do not pass an invalid exception type or value.
885(Violating these rules will cause subtle problems later.) This call
Fred Drakebab29652001-07-10 16:10:08 +0000886takes away a reference to each object: you must own a reference
Guido van Rossumae110af1997-05-22 20:11:52 +0000887to each object before the call and after the call you no longer own
888these references. (If you don't understand this, don't use this
Fred Drake659ebfa2000-04-03 15:42:13 +0000889function. I warned you.) \strong{Note:} This function is normally
Guido van Rossumae110af1997-05-22 20:11:52 +0000890only used by code that needs to save and restore the error indicator
891temporarily.
892\end{cfuncdesc}
893
894\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
895This is the most common way to set the error indicator. The first
896argument specifies the exception type; it is normally one of the
Fred Drakef8830d11998-04-23 14:06:01 +0000897standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not
Guido van Rossumae110af1997-05-22 20:11:52 +0000898increment its reference count. The second argument is an error
899message; it is converted to a string object.
900\end{cfuncdesc}
901
902\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +0000903This function is similar to \cfunction{PyErr_SetString()} but lets you
Guido van Rossumae110af1997-05-22 20:11:52 +0000904specify an arbitrary Python object for the ``value'' of the exception.
905You need not increment its reference count.
906\end{cfuncdesc}
907
Fred Drake73577702000-04-10 18:50:14 +0000908\begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception,
Moshe Zadka57a59322000-09-01 09:47:20 +0000909 const char *format, \moreargs}
Fred Drake89fb0352000-10-14 05:49:30 +0000910This function sets the error indicator. \var{exception} should be a
911Python exception (string or class, not an instance).
Fred Drake5566c1c2001-01-19 22:48:33 +0000912\var{format} should be a string, containing format codes, similar to
Moshe Zadka57a59322000-09-01 09:47:20 +0000913\cfunction{printf}. The \code{width.precision} before a format code
914is parsed, but the width part is ignored.
915
916\begin{tableii}{c|l}{character}{Character}{Meaning}
917 \lineii{c}{Character, as an \ctype{int} parameter}
918 \lineii{d}{Number in decimal, as an \ctype{int} parameter}
919 \lineii{x}{Number in hexadecimal, as an \ctype{int} parameter}
920 \lineii{x}{A string, as a \ctype{char *} parameter}
921\end{tableii}
922
923An unrecognized format character causes all the rest of
924the format string to be copied as-is to the result string,
925and any extra arguments discarded.
926
927A new reference is returned, which is owned by the caller.
Jeremy Hylton98605b52000-04-10 18:40:57 +0000928\end{cfuncdesc}
929
Guido van Rossumae110af1997-05-22 20:11:52 +0000930\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +0000931This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000932\end{cfuncdesc}
933
934\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000935This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000936\var{message})}, where \var{message} indicates that a built-in operation
937was invoked with an illegal argument. It is mostly for internal use.
938\end{cfuncdesc}
939
Fred Drakec6fa34e1998-04-02 06:47:24 +0000940\begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000941This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
Guido van Rossumae110af1997-05-22 20:11:52 +0000942returns \NULL{} so an object allocation function can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000943\samp{return PyErr_NoMemory();} when it runs out of memory.
Guido van Rossumae110af1997-05-22 20:11:52 +0000944\end{cfuncdesc}
945
Fred Drakec6fa34e1998-04-02 06:47:24 +0000946\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
Fred Drake659ebfa2000-04-03 15:42:13 +0000947This is a convenience function to raise an exception when a C library
948function has returned an error and set the C variable \cdata{errno}.
Guido van Rossumae110af1997-05-22 20:11:52 +0000949It constructs a tuple object whose first item is the integer
Fred Drakef8830d11998-04-23 14:06:01 +0000950\cdata{errno} value and whose second item is the corresponding error
Fred Drake659ebfa2000-04-03 15:42:13 +0000951message (gotten from \cfunction{strerror()}\ttindex{strerror()}), and
952then calls
Fred Drakee058b4f1998-02-16 06:15:35 +0000953\samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
Fred Drakef8830d11998-04-23 14:06:01 +0000954the \cdata{errno} value is \constant{EINTR}, indicating an interrupted
Fred Drakee058b4f1998-02-16 06:15:35 +0000955system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set
Guido van Rossumae110af1997-05-22 20:11:52 +0000956the error indicator, leaves it set to that. The function always
957returns \NULL{}, so a wrapper function around a system call can write
Fred Drakee058b4f1998-02-16 06:15:35 +0000958\samp{return PyErr_SetFromErrno();} when the system call returns an
959error.
Guido van Rossumae110af1997-05-22 20:11:52 +0000960\end{cfuncdesc}
961
Fred Drake490d34d2001-06-20 21:39:12 +0000962\begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrnoWithFilename}{PyObject *type,
963 char *filename}
964Similar to \cfunction{PyErr_SetFromErrno()}, with the additional
965behavior that if \var{filename} is not \NULL, it is passed to the
966constructor of \var{type} as a third parameter. In the case of
967exceptions such as \exception{IOError} and \exception{OSError}, this
968is used to define the \member{filename} attribute of the exception
969instance.
970\end{cfuncdesc}
971
Guido van Rossumae110af1997-05-22 20:11:52 +0000972\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
Fred Drakee058b4f1998-02-16 06:15:35 +0000973This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
Guido van Rossumae110af1997-05-22 20:11:52 +0000974\var{message})}, where \var{message} indicates that an internal
Guido van Rossum5060b3b1997-08-17 18:02:23 +0000975operation (e.g. a Python/C API function) was invoked with an illegal
Guido van Rossumae110af1997-05-22 20:11:52 +0000976argument. It is mostly for internal use.
977\end{cfuncdesc}
978
Guido van Rossum3dbb4062000-12-19 03:53:01 +0000979\begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message}
980Issue a warning message. The \var{category} argument is a warning
Fred Drake5566c1c2001-01-19 22:48:33 +0000981category (see below) or \NULL; the \var{message} argument is a message
Guido van Rossum3dbb4062000-12-19 03:53:01 +0000982string.
983
984This function normally prints a warning message to \var{sys.stderr};
985however, it is also possible that the user has specified that warnings
986are to be turned into errors, and in that case this will raise an
987exception. It is also possible that the function raises an exception
988because of a problem with the warning machinery (the implementation
989imports the \module{warnings} module to do the heavy lifting). The
990return value is \code{0} if no exception is raised, or \code{-1} if
991an exception is raised. (It is not possible to determine whether a
992warning message is actually printed, nor what the reason is for the
993exception; this is intentional.) If an exception is raised, the
Fred Drake5566c1c2001-01-19 22:48:33 +0000994caller should do its normal exception handling
995(e.g. \cfunction{Py_DECREF()} owned references and return an error
996value).
Guido van Rossum3dbb4062000-12-19 03:53:01 +0000997
998Warning categories must be subclasses of \cdata{Warning}; the default
999warning category is \cdata{RuntimeWarning}. The standard Python
1000warning categories are available as global variables whose names are
1001\samp{PyExc_} followed by the Python exception name. These have the
1002type \ctype{PyObject*}; they are all class objects. Their names are
1003\cdata{PyExc_Warning}, \cdata{PyExc_UserWarning},
1004\cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning}, and
1005\cdata{PyExc_RuntimeWarning}. \cdata{PyExc_Warning} is a subclass of
1006\cdata{PyExc_Exception}; the other warning categories are subclasses
1007of \cdata{PyExc_Warning}.
1008
1009For information about warning control, see the documentation for the
Fred Drake316ef7c2001-01-04 05:56:34 +00001010\module{warnings} module and the \programopt{-W} option in the command
1011line documentation. There is no C API for warning control.
Guido van Rossum3dbb4062000-12-19 03:53:01 +00001012\end{cfuncdesc}
1013
Guido van Rossum1874c8f2001-02-28 23:46:44 +00001014\begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category, char *message,
1015char *filename, int lineno, char *module, PyObject *registry}
1016Issue a warning message with explicit control over all warning
1017attributes. This is a straightforward wrapper around the Python
1018function \function{warnings.warn_explicit()}, see there for more
1019information. The \var{module} and \var{registry} arguments may be
1020set to \code{NULL} to get the default effect described there.
1021\end{cfuncdesc}
1022
Guido van Rossumae110af1997-05-22 20:11:52 +00001023\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
1024This function interacts with Python's signal handling. It checks
1025whether a signal has been sent to the processes and if so, invokes the
Fred Drake4de05a91998-02-16 14:25:26 +00001026corresponding signal handler. If the
1027\module{signal}\refbimodindex{signal} module is supported, this can
1028invoke a signal handler written in Python. In all cases, the default
Fred Drake659ebfa2000-04-03 15:42:13 +00001029effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the
1030\withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
1031\exception{KeyboardInterrupt} exception. If an exception is raised the
Fred Drakee058b4f1998-02-16 06:15:35 +00001032error indicator is set and the function returns \code{1}; otherwise
1033the function returns \code{0}. The error indicator may or may not be
1034cleared if it was previously set.
Guido van Rossumae110af1997-05-22 20:11:52 +00001035\end{cfuncdesc}
1036
1037\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
Fred Drake659ebfa2000-04-03 15:42:13 +00001038This function is obsolete. It simulates the effect of a
1039\constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time
Fred Drakee058b4f1998-02-16 06:15:35 +00001040\cfunction{PyErr_CheckSignals()} is called,
Fred Drake659ebfa2000-04-03 15:42:13 +00001041\withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
1042\exception{KeyboardInterrupt} will be raised.
1043It may be called without holding the interpreter lock.
Guido van Rossumae110af1997-05-22 20:11:52 +00001044\end{cfuncdesc}
1045
Fred Drakec6fa34e1998-04-02 06:47:24 +00001046\begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
1047 PyObject *base,
1048 PyObject *dict}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001049This utility function creates and returns a new exception object. The
Fred Drake659ebfa2000-04-03 15:42:13 +00001050\var{name} argument must be the name of the new exception, a C string
1051of the form \code{module.class}. The \var{base} and
Fred Draked04038d2000-06-29 20:15:14 +00001052\var{dict} arguments are normally \NULL{}. This creates a
Fred Drake659ebfa2000-04-03 15:42:13 +00001053class object derived from the root for all exceptions, the built-in
1054name \exception{Exception} (accessible in C as
Fred Draked04038d2000-06-29 20:15:14 +00001055\cdata{PyExc_Exception}). The \member{__module__} attribute of the
1056new class is set to the first part (up to the last dot) of the
1057\var{name} argument, and the class name is set to the last part (after
1058the last dot). The \var{base} argument can be used to specify an
1059alternate base class. The \var{dict} argument can be used to specify
1060a dictionary of class variables and methods.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001061\end{cfuncdesc}
1062
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001063\begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj}
1064This utility function prints a warning message to \var{sys.stderr}
1065when an exception has been set but it is impossible for the
1066interpreter to actually raise the exception. It is used, for example,
1067when an exception occurs in an \member{__del__} method.
1068
1069The function is called with a single argument \var{obj} that
1070identifies where the context in which the unraisable exception
1071occurred. The repr of \var{obj} will be printed in the warning
1072message.
1073\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001074
Fred Drakeefd146c1999-02-15 15:30:45 +00001075\section{Standard Exceptions \label{standardExceptions}}
Guido van Rossumae110af1997-05-22 20:11:52 +00001076
1077All standard Python exceptions are available as global variables whose
Fred Drake659ebfa2000-04-03 15:42:13 +00001078names are \samp{PyExc_} followed by the Python exception name. These
1079have the type \ctype{PyObject*}; they are all class objects. For
1080completeness, here are all the variables:
1081
1082\begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes}
1083 \lineiii{PyExc_Exception}{\exception{Exception}}{(1)}
1084 \lineiii{PyExc_StandardError}{\exception{StandardError}}{(1)}
1085 \lineiii{PyExc_ArithmeticError}{\exception{ArithmeticError}}{(1)}
1086 \lineiii{PyExc_LookupError}{\exception{LookupError}}{(1)}
1087 \lineiii{PyExc_AssertionError}{\exception{AssertionError}}{}
1088 \lineiii{PyExc_AttributeError}{\exception{AttributeError}}{}
1089 \lineiii{PyExc_EOFError}{\exception{EOFError}}{}
1090 \lineiii{PyExc_EnvironmentError}{\exception{EnvironmentError}}{(1)}
1091 \lineiii{PyExc_FloatingPointError}{\exception{FloatingPointError}}{}
1092 \lineiii{PyExc_IOError}{\exception{IOError}}{}
1093 \lineiii{PyExc_ImportError}{\exception{ImportError}}{}
1094 \lineiii{PyExc_IndexError}{\exception{IndexError}}{}
1095 \lineiii{PyExc_KeyError}{\exception{KeyError}}{}
1096 \lineiii{PyExc_KeyboardInterrupt}{\exception{KeyboardInterrupt}}{}
1097 \lineiii{PyExc_MemoryError}{\exception{MemoryError}}{}
1098 \lineiii{PyExc_NameError}{\exception{NameError}}{}
1099 \lineiii{PyExc_NotImplementedError}{\exception{NotImplementedError}}{}
1100 \lineiii{PyExc_OSError}{\exception{OSError}}{}
1101 \lineiii{PyExc_OverflowError}{\exception{OverflowError}}{}
1102 \lineiii{PyExc_RuntimeError}{\exception{RuntimeError}}{}
1103 \lineiii{PyExc_SyntaxError}{\exception{SyntaxError}}{}
1104 \lineiii{PyExc_SystemError}{\exception{SystemError}}{}
1105 \lineiii{PyExc_SystemExit}{\exception{SystemExit}}{}
1106 \lineiii{PyExc_TypeError}{\exception{TypeError}}{}
1107 \lineiii{PyExc_ValueError}{\exception{ValueError}}{}
Fred Drakea8d73412000-08-11 20:39:29 +00001108 \lineiii{PyExc_WindowsError}{\exception{WindowsError}}{(2)}
Fred Drake659ebfa2000-04-03 15:42:13 +00001109 \lineiii{PyExc_ZeroDivisionError}{\exception{ZeroDivisionError}}{}
1110\end{tableiii}
1111
1112\noindent
Fred Drakea8d73412000-08-11 20:39:29 +00001113Notes:
Fred Drake659ebfa2000-04-03 15:42:13 +00001114\begin{description}
1115\item[(1)]
Fred Draked04038d2000-06-29 20:15:14 +00001116 This is a base class for other standard exceptions.
Fred Drakea8d73412000-08-11 20:39:29 +00001117
1118\item[(2)]
1119 Only defined on Windows; protect code that uses this by testing that
1120 the preprocessor macro \code{MS_WINDOWS} is defined.
Fred Drake659ebfa2000-04-03 15:42:13 +00001121\end{description}
1122
1123
1124\section{Deprecation of String Exceptions}
1125
Fred Draked04038d2000-06-29 20:15:14 +00001126All exceptions built into Python or provided in the standard library
1127are derived from \exception{Exception}.
Fred Drake659ebfa2000-04-03 15:42:13 +00001128\withsubitem{(built-in exception)}{\ttindex{Exception}}
Fred Drake659ebfa2000-04-03 15:42:13 +00001129
Fred Draked04038d2000-06-29 20:15:14 +00001130String exceptions are still supported in the interpreter to allow
Fred Drake659ebfa2000-04-03 15:42:13 +00001131existing code to run unmodified, but this will also change in a future
1132release.
Guido van Rossumae110af1997-05-22 20:11:52 +00001133
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001134
Fred Drakeefd146c1999-02-15 15:30:45 +00001135\chapter{Utilities \label{utilities}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001136
Fred Drake88fdaa72001-07-20 20:56:11 +00001137The functions in this chapter perform various utility tasks, ranging
1138from helping C code be more portable across platforms, using Python
1139modules from C, and parsing function arguments and constructing Python
1140values from C values.
1141
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001142
Fred Drake377fb1e2001-07-14 03:01:48 +00001143\section{Operating System Utilities \label{os}}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001144
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001145\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +00001146Return true (nonzero) if the standard I/O file \var{fp} with name
1147\var{filename} is deemed interactive. This is the case for files for
1148which \samp{isatty(fileno(\var{fp}))} is true. If the global flag
Fred Drakef8830d11998-04-23 14:06:01 +00001149\cdata{Py_InteractiveFlag} is true, this function also returns true if
Fred Drake5566c1c2001-01-19 22:48:33 +00001150the \var{filename} pointer is \NULL{} or if the name is equal to one of
Fred Drakea8455ab2000-06-16 19:58:42 +00001151the strings \code{'<stdin>'} or \code{'???'}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001152\end{cfuncdesc}
1153
1154\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
Fred Drakee058b4f1998-02-16 06:15:35 +00001155Return the time of last modification of the file \var{filename}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001156The result is encoded in the same way as the timestamp returned by
Fred Drake659ebfa2000-04-03 15:42:13 +00001157the standard C library function \cfunction{time()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001158\end{cfuncdesc}
1159
Fred Drakecabbc3b2000-06-28 15:53:13 +00001160\begin{cfuncdesc}{void}{PyOS_AfterFork}{}
1161Function to update some internal state after a process fork; this
1162should be called in the new process if the Python interpreter will
1163continue to be used. If a new executable is loaded into the new
1164process, this function does not need to be called.
1165\end{cfuncdesc}
1166
Fred Drake17e63432000-08-31 05:50:40 +00001167\begin{cfuncdesc}{int}{PyOS_CheckStack}{}
1168Return true when the interpreter runs out of stack space. This is a
1169reliable check, but is only available when \code{USE_STACKCHECK} is
1170defined (currently on Windows using the Microsoft Visual C++ compiler
1171and on the Macintosh). \code{USE_CHECKSTACK} will be defined
1172automatically; you should never change the definition in your own
1173code.
1174\end{cfuncdesc}
1175
Guido van Rossumc96ec6e2000-09-16 16:30:48 +00001176\begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_getsig}{int i}
1177Return the current signal handler for signal \var{i}.
1178This is a thin wrapper around either \cfunction{sigaction} or
1179\cfunction{signal}. Do not call those functions directly!
1180\ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}.
1181\end{cfuncdesc}
1182
1183\begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_setsig}{int i, PyOS_sighandler_t h}
1184Set the signal handler for signal \var{i} to be \var{h};
1185return the old signal handler.
1186This is a thin wrapper around either \cfunction{sigaction} or
1187\cfunction{signal}. Do not call those functions directly!
1188\ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}.
1189\end{cfuncdesc}
1190
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001191
Fred Drakeefd146c1999-02-15 15:30:45 +00001192\section{Process Control \label{processControl}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00001193
1194\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
1195Print a fatal error message and kill the process. No cleanup is
1196performed. This function should only be invoked when a condition is
1197detected that would make it dangerous to continue using the Python
1198interpreter; e.g., when the object administration appears to be
Fred Drake659ebfa2000-04-03 15:42:13 +00001199corrupted. On \UNIX{}, the standard C library function
1200\cfunction{abort()}\ttindex{abort()} is called which will attempt to
1201produce a \file{core} file.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001202\end{cfuncdesc}
1203
1204\begin{cfuncdesc}{void}{Py_Exit}{int status}
Fred Drake659ebfa2000-04-03 15:42:13 +00001205Exit the current process. This calls
1206\cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and
1207then calls the standard C library function
1208\code{exit(\var{status})}\ttindex{exit()}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001209\end{cfuncdesc}
1210
1211\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
Fred Drake659ebfa2000-04-03 15:42:13 +00001212Register a cleanup function to be called by
1213\cfunction{Py_Finalize()}\ttindex{Py_Finalize()}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001214The cleanup function will be called with no arguments and should
Fred Drake659ebfa2000-04-03 15:42:13 +00001215return no value. At most 32 \index{cleanup functions}cleanup
1216functions can be registered.
Fred Drakee5bf8b21998-02-12 21:22:28 +00001217When the registration is successful, \cfunction{Py_AtExit()} returns
1218\code{0}; on failure, it returns \code{-1}. The cleanup function
1219registered last is called first. Each cleanup function will be called
1220at most once. Since Python's internal finallization will have
1221completed before the cleanup function, no Python APIs should be called
1222by \var{func}.
1223\end{cfuncdesc}
1224
1225
Fred Drakeefd146c1999-02-15 15:30:45 +00001226\section{Importing Modules \label{importing}}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001227
Fred Drakec6fa34e1998-04-02 06:47:24 +00001228\begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name}
Fred Drake659ebfa2000-04-03 15:42:13 +00001229This is a simplified interface to
1230\cfunction{PyImport_ImportModuleEx()} below, leaving the
1231\var{globals} and \var{locals} arguments set to \NULL{}. When the
Fred Drakebab29652001-07-10 16:10:08 +00001232\var{name} argument contains a dot (when it specifies a
Fred Drake659ebfa2000-04-03 15:42:13 +00001233submodule of a package), the \var{fromlist} argument is set to the
1234list \code{['*']} so that the return value is the named module rather
1235than the top-level package containing it as would otherwise be the
1236case. (Unfortunately, this has an additional side effect when
1237\var{name} in fact specifies a subpackage instead of a submodule: the
1238submodules specified in the package's \code{__all__} variable are
1239\index{package variable!\code{__all__}}
1240\withsubitem{(package variable)}{\ttindex{__all__}}loaded.) Return a
1241new reference to the imported module, or
1242\NULL{} with an exception set on failure (the module may still be
1243created in this case --- examine \code{sys.modules} to find out).
1244\withsubitem{(in module sys)}{\ttindex{modules}}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001245\end{cfuncdesc}
1246
Fred Drake01978582001-08-08 19:14:53 +00001247\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name,
1248 PyObject *globals, PyObject *locals, PyObject *fromlist}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001249Import a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +00001250Python function \function{__import__()}\bifuncindex{__import__}, as
1251the standard \function{__import__()} function calls this function
1252directly.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001253
Guido van Rossum42cefd01997-10-05 15:27:29 +00001254The return value is a new reference to the imported module or
Guido van Rossum580aa8d1997-11-25 15:34:51 +00001255top-level package, or \NULL{} with an exception set on failure
Guido van Rossumc44d3d61997-10-06 05:10:47 +00001256(the module may still be created in this case). Like for
Fred Drakee058b4f1998-02-16 06:15:35 +00001257\function{__import__()}, the return value when a submodule of a
1258package was requested is normally the top-level package, unless a
1259non-empty \var{fromlist} was given.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001260\end{cfuncdesc}
1261
Fred Drakec6fa34e1998-04-02 06:47:24 +00001262\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001263This is a higher-level interface that calls the current ``import hook
Fred Drakee058b4f1998-02-16 06:15:35 +00001264function''. It invokes the \function{__import__()} function from the
Guido van Rossum42cefd01997-10-05 15:27:29 +00001265\code{__builtins__} of the current globals. This means that the
1266import is done using whatever import hooks are installed in the
Fred Drake4de05a91998-02-16 14:25:26 +00001267current environment, e.g. by \module{rexec}\refstmodindex{rexec} or
1268\module{ihooks}\refstmodindex{ihooks}.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001269\end{cfuncdesc}
1270
Fred Drakec6fa34e1998-04-02 06:47:24 +00001271\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001272Reload a module. This is best described by referring to the built-in
Fred Drake53fb7721998-02-16 06:23:20 +00001273Python function \function{reload()}\bifuncindex{reload}, as the standard
Fred Drakee058b4f1998-02-16 06:15:35 +00001274\function{reload()} function calls this function directly. Return a
1275new reference to the reloaded module, or \NULL{} with an exception set
1276on failure (the module still exists in this case).
Guido van Rossum42cefd01997-10-05 15:27:29 +00001277\end{cfuncdesc}
1278
Fred Drakec6fa34e1998-04-02 06:47:24 +00001279\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001280Return the module object corresponding to a module name. The
1281\var{name} argument may be of the form \code{package.module}). First
1282check the modules dictionary if there's one there, and if not, create
Fred Drake659ebfa2000-04-03 15:42:13 +00001283a new one and insert in in the modules dictionary.
Guido van Rossuma096a2e1998-11-02 17:02:42 +00001284Warning: this function does not load or import the module; if the
1285module wasn't already loaded, you will get an empty module object.
1286Use \cfunction{PyImport_ImportModule()} or one of its variants to
1287import a module.
Fred Drake659ebfa2000-04-03 15:42:13 +00001288Return \NULL{} with an exception set on failure.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001289\end{cfuncdesc}
1290
Fred Drakec6fa34e1998-04-02 06:47:24 +00001291\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001292Given a module name (possibly of the form \code{package.module}) and a
1293code object read from a Python bytecode file or obtained from the
Fred Drake53fb7721998-02-16 06:23:20 +00001294built-in function \function{compile()}\bifuncindex{compile}, load the
1295module. Return a new reference to the module object, or \NULL{} with
1296an exception set if an error occurred (the module may still be created
1297in this case). (This function would reload the module if it was
1298already imported.)
Guido van Rossum42cefd01997-10-05 15:27:29 +00001299\end{cfuncdesc}
1300
1301\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
Fred Drake659ebfa2000-04-03 15:42:13 +00001302Return the magic number for Python bytecode files (a.k.a.
1303\file{.pyc} and \file{.pyo} files). The magic number should be
1304present in the first four bytes of the bytecode file, in little-endian
1305byte order.
Guido van Rossum42cefd01997-10-05 15:27:29 +00001306\end{cfuncdesc}
1307
Fred Drakec6fa34e1998-04-02 06:47:24 +00001308\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001309Return the dictionary used for the module administration
1310(a.k.a. \code{sys.modules}). Note that this is a per-interpreter
1311variable.
1312\end{cfuncdesc}
1313
1314\begin{cfuncdesc}{void}{_PyImport_Init}{}
1315Initialize the import mechanism. For internal use only.
1316\end{cfuncdesc}
1317
1318\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
1319Empty the module table. For internal use only.
1320\end{cfuncdesc}
1321
1322\begin{cfuncdesc}{void}{_PyImport_Fini}{}
1323Finalize the import mechanism. For internal use only.
1324\end{cfuncdesc}
1325
Fred Drakec6fa34e1998-04-02 06:47:24 +00001326\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001327For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001328\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001329
Fred Drakec6fa34e1998-04-02 06:47:24 +00001330\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001331For internal use only.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00001332\end{cfuncdesc}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001333
Fred Drake1d158692000-06-18 05:21:21 +00001334\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name}
1335Load a frozen module named \var{name}. Return \code{1} for success,
1336\code{0} if the module is not found, and \code{-1} with an exception
1337set if the initialization failed. To access the imported module on a
1338successful load, use \cfunction{PyImport_ImportModule()}.
Fred Drakee058b4f1998-02-16 06:15:35 +00001339(Note the misnomer --- this function would reload the module if it was
Guido van Rossum42cefd01997-10-05 15:27:29 +00001340already imported.)
1341\end{cfuncdesc}
1342
Fred Drake659ebfa2000-04-03 15:42:13 +00001343\begin{ctypedesc}[_frozen]{struct _frozen}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001344This is the structure type definition for frozen module descriptors,
Fred Drakec6fa34e1998-04-02 06:47:24 +00001345as generated by the \program{freeze}\index{freeze utility} utility
1346(see \file{Tools/freeze/} in the Python source distribution). Its
Fred Drakee0d9a832000-09-01 05:30:00 +00001347definition, found in \file{Include/import.h}, is:
Fred Drakec6fa34e1998-04-02 06:47:24 +00001348
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001349\begin{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001350struct _frozen {
Fred Drake36fbe761997-10-13 18:18:33 +00001351 char *name;
1352 unsigned char *code;
1353 int size;
Guido van Rossum42cefd01997-10-05 15:27:29 +00001354};
Guido van Rossum9faf4c51997-10-07 14:38:54 +00001355\end{verbatim}
Guido van Rossum42cefd01997-10-05 15:27:29 +00001356\end{ctypedesc}
1357
Fred Drakec6fa34e1998-04-02 06:47:24 +00001358\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
Fred Drakef8830d11998-04-23 14:06:01 +00001359This pointer is initialized to point to an array of \ctype{struct
Fred Drake659ebfa2000-04-03 15:42:13 +00001360_frozen} records, terminated by one whose members are all
1361\NULL{} or zero. When a frozen module is imported, it is searched in
1362this table. Third-party code could play tricks with this to provide a
Guido van Rossum42cefd01997-10-05 15:27:29 +00001363dynamically created collection of frozen modules.
1364\end{cvardesc}
1365
Fred Drakee0d9a832000-09-01 05:30:00 +00001366\begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name,
1367 void (*initfunc)(void)}
1368Add a single module to the existing table of built-in modules. This
1369is a convenience wrapper around \cfunction{PyImport_ExtendInittab()},
1370returning \code{-1} if the table could not be extended. The new
1371module can be imported by the name \var{name}, and uses the function
1372\var{initfunc} as the initialization function called on the first
1373attempted import. This should be called before
1374\cfunction{Py_Initialize()}.
1375\end{cfuncdesc}
1376
1377\begin{ctypedesc}[_inittab]{struct _inittab}
1378Structure describing a single entry in the list of built-in modules.
1379Each of these structures gives the name and initialization function
1380for a module built into the interpreter. Programs which embed Python
1381may use an array of these structures in conjunction with
1382\cfunction{PyImport_ExtendInittab()} to provide additional built-in
1383modules. The structure is defined in \file{Include/import.h} as:
1384
1385\begin{verbatim}
1386struct _inittab {
1387 char *name;
1388 void (*initfunc)(void);
1389};
1390\end{verbatim}
1391\end{ctypedesc}
1392
1393\begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab}
1394Add a collection of modules to the table of built-in modules. The
1395\var{newtab} array must end with a sentinel entry which contains
1396\NULL{} for the \member{name} field; failure to provide the sentinel
1397value can result in a memory fault. Returns \code{0} on success or
1398\code{-1} if insufficient memory could be allocated to extend the
1399internal table. In the event of failure, no modules are added to the
1400internal table. This should be called before
1401\cfunction{Py_Initialize()}.
1402\end{cfuncdesc}
1403
Guido van Rossum42cefd01997-10-05 15:27:29 +00001404
Fred Drake88fdaa72001-07-20 20:56:11 +00001405\section{Parsing arguements and building values
1406 \label{arg-parsing}}
1407
1408These functions are useful when creating your own extensions functions
1409and methods. Additional information and examples are available in
1410\citetitle[../ext/ext.html]{Extending and Embedding the Python
1411Interpreter}.
1412
1413\begin{cfuncdesc}{int}{PyArg_ParseTuple}{PyObject *args, char *format,
1414 \moreargs}
1415 Parse the parameters of a function that takes only positional
1416 parameters into local variables. Returns true on success; on
1417 failure, it returns false and raises the appropriate exception. See
1418 \citetitle[../ext/parseTuple.html]{Extending and Embedding the
1419 Python Interpreter} for more information.
1420\end{cfuncdesc}
1421
1422\begin{cfuncdesc}{int}{PyArg_ParseTupleAndKeywords}{PyObject *args,
1423 PyObject *kw, char *format, char *keywords[],
1424 \moreargs}
1425 Parse the parameters of a function that takes both positional and
1426 keyword parameters into local variables. Returns true on success;
1427 on failure, it returns false and raises the appropriate exception.
1428 See \citetitle[../ext/parseTupleAndKeywords.html]{Extending and
1429 Embedding the Python Interpreter} for more information.
1430\end{cfuncdesc}
1431
1432\begin{cfuncdesc}{int}{PyArg_Parse}{PyObject *args, char *format,
1433 \moreargs}
1434 Function used to deconstruct the argument lists of ``old-style''
1435 functions --- these are functions which use the
1436 \constant{METH_OLDARGS} parameter parsing method. This is not
1437 recommended for use in parameter parsing in new code, and most code
1438 in the standard interpreter has been modified to no longer use this
1439 for that purpose. It does remain a convenient way to decompose
1440 other tuples, however, and may continue to be used for that
1441 purpose.
1442\end{cfuncdesc}
1443
1444\begin{cfuncdesc}{PyObject*}{Py_BuildValue}{char *format,
1445 \moreargs}
1446 Create a new value based on a format string similar to those
1447 accepted by the \cfunction{PyArg_Parse*()} family of functions and a
1448 sequence of values. Returns the value or \NULL{} in the case of an
1449 error; an exception will be raised if \NULL{} is returned. For more
1450 information on the format string and additional parameters, see
1451 \citetitle[../ext/buildValue.html]{Extending and Embedding the
1452 Python Interpreter}.
1453\end{cfuncdesc}
1454
1455
1456
Fred Drakeefd146c1999-02-15 15:30:45 +00001457\chapter{Abstract Objects Layer \label{abstract}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001458
1459The functions in this chapter interact with Python objects regardless
1460of their type, or with wide classes of object types (e.g. all
1461numerical types, or all sequence types). When used on object types
Fred Drake659ebfa2000-04-03 15:42:13 +00001462for which they do not apply, they will raise a Python exception.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001463
Fred Drakeefd146c1999-02-15 15:30:45 +00001464\section{Object Protocol \label{object}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001465
1466\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
Fred Drake659ebfa2000-04-03 15:42:13 +00001467Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error.
1468The flags argument is used to enable certain printing options. The
1469only option currently supported is \constant{Py_PRINT_RAW}; if given,
1470the \function{str()} of the object is written instead of the
1471\function{repr()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001472\end{cfuncdesc}
1473
1474\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001475Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1476\code{0} otherwise. This is equivalent to the Python expression
1477\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001478This function always succeeds.
1479\end{cfuncdesc}
1480
Fred Drake659ebfa2000-04-03 15:42:13 +00001481\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o,
1482 char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001483Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001484Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001485This is the equivalent of the Python expression
1486\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001487\end{cfuncdesc}
1488
1489
1490\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001491Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
1492\code{0} otherwise. This is equivalent to the Python expression
1493\samp{hasattr(\var{o}, \var{attr_name})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001494This function always succeeds.
1495\end{cfuncdesc}
1496
1497
Fred Drake659ebfa2000-04-03 15:42:13 +00001498\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o,
1499 PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001500Retrieve an attribute named \var{attr_name} from object \var{o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001501Returns the attribute value on success, or \NULL{} on failure.
Fred Drakee058b4f1998-02-16 06:15:35 +00001502This is the equivalent of the Python expression
1503\samp{\var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001504\end{cfuncdesc}
1505
1506
Fred Drake01978582001-08-08 19:14:53 +00001507\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o,
1508 char *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001509Set the value of the attribute named \var{attr_name}, for object
1510\var{o}, to the value \var{v}. Returns \code{-1} on failure. This is
1511the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1512\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001513\end{cfuncdesc}
1514
1515
Fred Drake01978582001-08-08 19:14:53 +00001516\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o,
1517 PyObject *attr_name, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001518Set the value of the attribute named \var{attr_name}, for
1519object \var{o},
1520to the value \var{v}. Returns \code{-1} on failure. This is
1521the equivalent of the Python statement \samp{\var{o}.\var{attr_name} =
1522\var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001523\end{cfuncdesc}
1524
1525
1526\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001527Delete attribute named \var{attr_name}, for object \var{o}. Returns
1528\code{-1} on failure. This is the equivalent of the Python
1529statement: \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001530\end{cfuncdesc}
1531
1532
1533\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
Fred Drakee058b4f1998-02-16 06:15:35 +00001534Delete attribute named \var{attr_name}, for object \var{o}. Returns
1535\code{-1} on failure. This is the equivalent of the Python
1536statement \samp{del \var{o}.\var{attr_name}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001537\end{cfuncdesc}
1538
1539
1540\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
Fred Drakee058b4f1998-02-16 06:15:35 +00001541Compare the values of \var{o1} and \var{o2} using a routine provided
1542by \var{o1}, if one exists, otherwise with a routine provided by
1543\var{o2}. The result of the comparison is returned in \var{result}.
1544Returns \code{-1} on failure. This is the equivalent of the Python
Fred Drake659ebfa2000-04-03 15:42:13 +00001545statement\bifuncindex{cmp} \samp{\var{result} = cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001546\end{cfuncdesc}
1547
1548
1549\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001550Compare the values of \var{o1} and \var{o2} using a routine provided
1551by \var{o1}, if one exists, otherwise with a routine provided by
1552\var{o2}. Returns the result of the comparison on success. On error,
1553the value returned is undefined; use \cfunction{PyErr_Occurred()} to
Fred Drake659ebfa2000-04-03 15:42:13 +00001554detect an error. This is equivalent to the Python
1555expression\bifuncindex{cmp} \samp{cmp(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001556\end{cfuncdesc}
1557
1558
1559\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00001560Compute a string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001561string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001562the equivalent of the Python expression \samp{repr(\var{o})}.
1563Called by the \function{repr()}\bifuncindex{repr} built-in function
1564and by reverse quotes.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001565\end{cfuncdesc}
1566
1567
1568\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00001569Compute a string representation of object \var{o}. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001570string representation on success, \NULL{} on failure. This is
Fred Drakee058b4f1998-02-16 06:15:35 +00001571the equivalent of the Python expression \samp{str(\var{o})}.
1572Called by the \function{str()}\bifuncindex{str} built-in function and
1573by the \keyword{print} statement.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001574\end{cfuncdesc}
1575
1576
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +00001577\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
1578Compute a Unicode string representation of object \var{o}. Returns the
1579Unicode string representation on success, \NULL{} on failure. This is
1580the equivalent of the Python expression \samp{unistr(\var{o})}.
1581Called by the \function{unistr()}\bifuncindex{unistr} built-in function.
1582\end{cfuncdesc}
1583
Fred Drake58c8f9f2001-03-28 21:14:32 +00001584\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
1585Return \code{1} if \var{inst} is an instance of the class \var{cls} or
1586a subclass of \var{cls}. If \var{cls} is a type object rather than a
1587class object, \cfunction{PyObject_IsInstance()} returns \code{1} if
1588\var{inst} is of type \var{cls}. If \var{inst} is not a class
1589instance and \var{cls} is neither a type object or class object,
1590\var{inst} must have a \member{__class__} attribute --- the class
1591relationship of the value of that attribute with \var{cls} will be
1592used to determine the result of this function.
1593\versionadded{2.1}
1594\end{cfuncdesc}
1595
1596Subclass determination is done in a fairly straightforward way, but
1597includes a wrinkle that implementors of extensions to the class system
1598may want to be aware of. If \class{A} and \class{B} are class
1599objects, \class{B} is a subclass of \class{A} if it inherits from
1600\class{A} either directly or indirectly. If either is not a class
1601object, a more general mechanism is used to determine the class
1602relationship of the two objects. When testing if \var{B} is a
1603subclass of \var{A}, if \var{A} is \var{B},
1604\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
1605\var{B} are different objects, \var{B}'s \member{__bases__} attribute
1606is searched in a depth-first fashion for \var{A} --- the presence of
1607the \member{__bases__} attribute is considered sufficient for this
1608determination.
1609
1610\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
1611 PyObject *cls}
1612Returns \code{1} if the class \var{derived} is identical to or derived
1613from the class \var{cls}, otherwise returns \code{0}. In case of an
1614error, returns \code{-1}. If either \var{derived} or \var{cls} is not
1615an actual class object, this function uses the generic algorithm
1616described above.
1617\versionadded{2.1}
1618\end{cfuncdesc}
1619
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +00001620
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001621\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00001622Determine if the object \var{o} is callable. Return \code{1} if the
Fred Drakee058b4f1998-02-16 06:15:35 +00001623object is callable and \code{0} otherwise.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001624This function always succeeds.
1625\end{cfuncdesc}
1626
1627
Fred Drake659ebfa2000-04-03 15:42:13 +00001628\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
1629 PyObject *args}
Fred Drakee058b4f1998-02-16 06:15:35 +00001630Call a callable Python object \var{callable_object}, with
1631arguments given by the tuple \var{args}. If no arguments are
Fred Drake659ebfa2000-04-03 15:42:13 +00001632needed, then \var{args} may be \NULL{}. Returns the result of the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001633call on success, or \NULL{} on failure. This is the equivalent
Fred Drakef90490e2001-08-02 18:00:28 +00001634of the Python expression \samp{apply(\var{callable_object},
1635\var{args})} or \samp{\var{callable_object}(*\var{args})}.
Fred Drake659ebfa2000-04-03 15:42:13 +00001636\bifuncindex{apply}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001637\end{cfuncdesc}
1638
Fred Drake5566c1c2001-01-19 22:48:33 +00001639\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object,
1640 char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001641Call a callable Python object \var{callable_object}, with a
Fred Drake659ebfa2000-04-03 15:42:13 +00001642variable number of C arguments. The C arguments are described
Fred Drakee058b4f1998-02-16 06:15:35 +00001643using a \cfunction{Py_BuildValue()} style format string. The format may
1644be \NULL{}, indicating that no arguments are provided. Returns the
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001645result of the call on success, or \NULL{} on failure. This is
Fred Drakef90490e2001-08-02 18:00:28 +00001646the equivalent of the Python expression
1647\samp{apply(\var{callable_object}\var{args})} or
1648\samp{\var{callable_object}(*\var{args})}.
1649\bifuncindex{apply}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001650\end{cfuncdesc}
1651
1652
Fred Drake5566c1c2001-01-19 22:48:33 +00001653\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
1654 char *method, char *format, ...}
Fred Drakee058b4f1998-02-16 06:15:35 +00001655Call the method named \var{m} of object \var{o} with a variable number
Fred Drake659ebfa2000-04-03 15:42:13 +00001656of C arguments. The C arguments are described by a
Fred Drakee058b4f1998-02-16 06:15:35 +00001657\cfunction{Py_BuildValue()} format string. The format may be \NULL{},
1658indicating that no arguments are provided. Returns the result of the
1659call on success, or \NULL{} on failure. This is the equivalent of the
1660Python expression \samp{\var{o}.\var{method}(\var{args})}.
Fred Drake659ebfa2000-04-03 15:42:13 +00001661Note that special method names, such as \method{__add__()},
1662\method{__getitem__()}, and so on are not supported. The specific
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001663abstract-object routines for these must be used.
1664\end{cfuncdesc}
1665
1666
1667\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001668Compute and return the hash value of an object \var{o}. On
1669failure, return \code{-1}. This is the equivalent of the Python
Fred Drake659ebfa2000-04-03 15:42:13 +00001670expression \samp{hash(\var{o})}.\bifuncindex{hash}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001671\end{cfuncdesc}
1672
1673
1674\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001675Returns \code{1} if the object \var{o} is considered to be true, and
1676\code{0} otherwise. This is equivalent to the Python expression
1677\samp{not not \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001678This function always succeeds.
1679\end{cfuncdesc}
1680
1681
1682\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
Fred Drakef47d8ef2001-09-20 19:18:52 +00001683When \var{o} is non-\NULL, returns a type object corresponding to the
1684object type of object \var{o}. On failure, raises
1685\exception{SystemError} and returns \NULL. This is equivalent to the
1686Python expression \code{type(\var{o})}.
Fred Drake53fb7721998-02-16 06:23:20 +00001687\bifuncindex{type}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001688\end{cfuncdesc}
1689
Fred Drakef47d8ef2001-09-20 19:18:52 +00001690\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
1691Return true if the object \var{o} is of type \var{type} or a subtype
1692of \var{type}. Both parameters must be non-\NULL.
1693\end{cfuncdesc}
1694
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001695\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001696Return the length of object \var{o}. If the object \var{o} provides
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001697both sequence and mapping protocols, the sequence length is
Fred Drake659ebfa2000-04-03 15:42:13 +00001698returned. On error, \code{-1} is returned. This is the equivalent
1699to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001700\end{cfuncdesc}
1701
1702
1703\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001704Return element of \var{o} corresponding to the object \var{key} or
1705\NULL{} on failure. This is the equivalent of the Python expression
1706\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001707\end{cfuncdesc}
1708
1709
Fred Drake01978582001-08-08 19:14:53 +00001710\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
1711 PyObject *key, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00001712Map the object \var{key} to the value \var{v}.
1713Returns \code{-1} on failure. This is the equivalent
1714of the Python statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001715\end{cfuncdesc}
1716
1717
Guido van Rossumd1dbf631999-01-22 20:10:49 +00001718\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00001719Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
1720failure. This is the equivalent of the Python statement \samp{del
1721\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001722\end{cfuncdesc}
1723
Andrew M. Kuchling8c46b302000-07-13 23:58:16 +00001724\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
1725Derives a file-descriptor from a Python object. If the object
1726is an integer or long integer, its value is returned. If not, the
1727object's \method{fileno()} method is called if it exists; the method
1728must return an integer or long integer, which is returned as the file
1729descriptor value. Returns \code{-1} on failure.
1730\end{cfuncdesc}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001731
Tim Peters7eea37e2001-09-04 22:08:56 +00001732\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
1733This is equivalent to the Python expression \samp{dir(\var{o})},
1734returning a (possibly empty) list of strings appropriate for the
1735object argument, or \NULL{} in case of error.
1736If the argument is \NULL{}, this is like the Python \samp{dir()},
1737returning the names of the current locals; in this case, if no
1738execution frame is active then \NULL{} is returned but
1739\cfunction{PyErr_Occurred()} will return false.
1740\end{cfuncdesc}
1741
Fred Drake01978582001-08-08 19:14:53 +00001742
Fred Drakeefd146c1999-02-15 15:30:45 +00001743\section{Number Protocol \label{number}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001744
1745\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001746Returns \code{1} if the object \var{o} provides numeric protocols, and
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001747false otherwise.
1748This function always succeeds.
1749\end{cfuncdesc}
1750
1751
1752\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001753Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1754failure. This is the equivalent of the Python expression
1755\samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001756\end{cfuncdesc}
1757
1758
1759\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
Fred Drake659ebfa2000-04-03 15:42:13 +00001760Returns the result of subtracting \var{o2} from \var{o1}, or
1761\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001762\samp{\var{o1} - \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001763\end{cfuncdesc}
1764
1765
1766\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001767Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1768failure. This is the equivalent of the Python expression
1769\samp{\var{o1} * \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001770\end{cfuncdesc}
1771
1772
1773\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001774Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1775failure.
1776This is the equivalent of the Python expression \samp{\var{o1} /
1777\var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001778\end{cfuncdesc}
1779
1780
Fred Drake01978582001-08-08 19:14:53 +00001781\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
1782Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
1783failure. This is equivalent to the ``classic'' division of integers.
1784\versionadded{2.2}
1785\end{cfuncdesc}
1786
1787
1788\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
1789Return a reasonable approximation for the mathematical value of
1790\var{o1} divided by \var{o2}, or \NULL{} on failure. The return value
1791is ``approximate'' because binary floating point numbers are
1792approximate; it is not possible to represent all real numbers in base
1793two. This function can return a floating point value when passed two
1794integers.
1795\versionadded{2.2}
1796\end{cfuncdesc}
1797
1798
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001799\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001800Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1801failure. This is the equivalent of the Python expression
Fred Drake659ebfa2000-04-03 15:42:13 +00001802\samp{\var{o1} \%\ \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001803\end{cfuncdesc}
1804
1805
1806\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
Fred Drake53fb7721998-02-16 06:23:20 +00001807See the built-in function \function{divmod()}\bifuncindex{divmod}.
1808Returns \NULL{} on failure. This is the equivalent of the Python
1809expression \samp{divmod(\var{o1}, \var{o2})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001810\end{cfuncdesc}
1811
1812
Fred Drake01978582001-08-08 19:14:53 +00001813\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
1814 PyObject *o2, PyObject *o3}
Fred Drake53fb7721998-02-16 06:23:20 +00001815See the built-in function \function{pow()}\bifuncindex{pow}. Returns
1816\NULL{} on failure. This is the equivalent of the Python expression
Fred Drakee058b4f1998-02-16 06:15:35 +00001817\samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional.
Fred Drake659ebfa2000-04-03 15:42:13 +00001818If \var{o3} is to be ignored, pass \cdata{Py_None} in its place
1819(passing \NULL{} for \var{o3} would cause an illegal memory access).
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001820\end{cfuncdesc}
1821
1822
1823\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001824Returns the negation of \var{o} on success, or \NULL{} on failure.
1825This is the equivalent of the Python expression \samp{-\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001826\end{cfuncdesc}
1827
1828
1829\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001830Returns \var{o} on success, or \NULL{} on failure.
1831This is the equivalent of the Python expression \samp{+\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001832\end{cfuncdesc}
1833
1834
1835\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001836Returns the absolute value of \var{o}, or \NULL{} on failure. This is
1837the equivalent of the Python expression \samp{abs(\var{o})}.
Fred Drake659ebfa2000-04-03 15:42:13 +00001838\bifuncindex{abs}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001839\end{cfuncdesc}
1840
1841
1842\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00001843Returns the bitwise negation of \var{o} on success, or \NULL{} on
1844failure. This is the equivalent of the Python expression
1845\samp{\~\var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001846\end{cfuncdesc}
1847
1848
1849\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001850Returns the result of left shifting \var{o1} by \var{o2} on success,
1851or \NULL{} on failure. This is the equivalent of the Python
Fred Draked20d8b32001-04-13 14:52:39 +00001852expression \samp{\var{o1} <\code{<} \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001853\end{cfuncdesc}
1854
1855
1856\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00001857Returns the result of right shifting \var{o1} by \var{o2} on success,
1858or \NULL{} on failure. This is the equivalent of the Python
Fred Draked20d8b32001-04-13 14:52:39 +00001859expression \samp{\var{o1} >\code{>} \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001860\end{cfuncdesc}
1861
1862
1863\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001864Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
1865\NULL{} on failure. This is the equivalent of the Python expression
Fred Drake5566c1c2001-01-19 22:48:33 +00001866\samp{\var{o1} \&\ \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001867\end{cfuncdesc}
1868
1869
1870\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001871Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on success,
Fred Drakee058b4f1998-02-16 06:15:35 +00001872or \NULL{} on failure. This is the equivalent of the Python
Fred Drake755c23d2001-07-14 03:05:53 +00001873expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001874\end{cfuncdesc}
1875
1876\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001877Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
1878\NULL{} on failure. This is the equivalent of the Python expression
1879\samp{\var{o1} | \var{o2}}.
1880\end{cfuncdesc}
1881
1882
1883\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
Fred Drake01978582001-08-08 19:14:53 +00001884Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
1885failure. The operation is done \emph{in-place} when \var{o1} supports
1886it. This is the equivalent of the Python statement \samp{\var{o1} +=
1887\var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001888\end{cfuncdesc}
1889
1890
Fred Drake01978582001-08-08 19:14:53 +00001891\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
1892 PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001893Returns the result of subtracting \var{o2} from \var{o1}, or
Fred Drake01978582001-08-08 19:14:53 +00001894\NULL{} on failure. The operation is done \emph{in-place} when
1895\var{o1} supports it. This is the equivalent of the Python statement
1896\samp{\var{o1} -= \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001897\end{cfuncdesc}
1898
1899
Fred Drake01978582001-08-08 19:14:53 +00001900\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
1901 PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001902Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on
1903failure. The operation is done \emph{in-place} when \var{o1} supports it.
Fred Drake01978582001-08-08 19:14:53 +00001904This is the equivalent of the Python statement \samp{\var{o1} *= \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001905\end{cfuncdesc}
1906
1907
Fred Drake01978582001-08-08 19:14:53 +00001908\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
1909 PyObject *o2}
1910Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
1911failure. The operation is done \emph{in-place} when \var{o1} supports
1912it. This is the equivalent of the Python statement \samp{\var{o1} /=
1913\var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001914\end{cfuncdesc}
1915
1916
Fred Drake01978582001-08-08 19:14:53 +00001917\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
1918 PyObject *o2}
1919Returns the mathematical of dividing \var{o1} by \var{o2}, or \NULL{}
1920on failure. The operation is done \emph{in-place} when \var{o1}
1921supports it. This is the equivalent of the Python statement
1922\samp{\var{o1} //= \var{o2}}.
1923\versionadded{2.2}
1924\end{cfuncdesc}
1925
1926
1927\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
1928 PyObject *o2}
1929Return a reasonable approximation for the mathematical value of
1930\var{o1} divided by \var{o2}, or \NULL{} on failure. The return value
1931is ``approximate'' because binary floating point numbers are
1932approximate; it is not possible to represent all real numbers in base
1933two. This function can return a floating point value when passed two
1934integers. The operation is done \emph{in-place} when \var{o1}
1935supports it.
1936\versionadded{2.2}
1937\end{cfuncdesc}
1938
1939
1940\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
1941 PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00001942Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on
1943failure. The operation is done \emph{in-place} when \var{o1} supports it.
Fred Drake01978582001-08-08 19:14:53 +00001944This is the equivalent of the Python statement \samp{\var{o1} \%= \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001945\end{cfuncdesc}
1946
1947
Fred Drake01978582001-08-08 19:14:53 +00001948\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
1949 PyObject *o2, PyObject *o3}
1950See the built-in function \function{pow()}.\bifuncindex{pow} Returns
1951\NULL{} on failure. The operation is done \emph{in-place} when
1952\var{o1} supports it. This is the equivalent of the Python statement
1953\samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None}, or an
1954in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
1955otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
1956place (passing \NULL{} for \var{o3} would cause an illegal memory
1957access).
Fred Drake7740a012000-09-12 20:27:05 +00001958\end{cfuncdesc}
1959
Fred Drake01978582001-08-08 19:14:53 +00001960\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
1961 PyObject *o2}
1962Returns the result of left shifting \var{o1} by \var{o2} on success,
1963or \NULL{} on failure. The operation is done \emph{in-place} when
1964\var{o1} supports it. This is the equivalent of the Python statement
1965\samp{\var{o1} <\code{<=} \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001966\end{cfuncdesc}
1967
1968
Fred Drake01978582001-08-08 19:14:53 +00001969\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
1970 PyObject *o2}
1971Returns the result of right shifting \var{o1} by \var{o2} on success,
1972or \NULL{} on failure. The operation is done \emph{in-place} when
1973\var{o1} supports it. This is the equivalent of the Python statement
1974\samp{\var{o1} >\code{>=} \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001975\end{cfuncdesc}
1976
1977
1978\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
Fred Drake5566c1c2001-01-19 22:48:33 +00001979Returns the ``bitwise and'' of \var{o1} and \var{o2} on success
1980and \NULL{} on failure. The operation is done \emph{in-place} when
Fred Drake01978582001-08-08 19:14:53 +00001981\var{o1} supports it. This is the equivalent of the Python statement
Fred Drake5566c1c2001-01-19 22:48:33 +00001982\samp{\var{o1} \&= \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001983\end{cfuncdesc}
1984
1985
1986\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
Fred Drake01978582001-08-08 19:14:53 +00001987Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
1988success, or \NULL{} on failure. The operation is done \emph{in-place}
1989when \var{o1} supports it. This is the equivalent of the Python
1990statement \samp{\var{o1} \textasciicircum= \var{o2}}.
Fred Drake7740a012000-09-12 20:27:05 +00001991\end{cfuncdesc}
1992
1993\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
Fred Drake01978582001-08-08 19:14:53 +00001994Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
1995\NULL{} on failure. The operation is done \emph{in-place} when
1996\var{o1} supports it. This is the equivalent of the Python statement
1997\samp{\var{o1} |= \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001998\end{cfuncdesc}
1999
Fred Drakec0e6c5b2000-09-22 18:17:49 +00002000\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002001This function takes the addresses of two variables of type
Fred Drake659ebfa2000-04-03 15:42:13 +00002002\ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}} and
2003\code{*\var{p2}} have the same type, increment their reference count
2004and return \code{0} (success). If the objects can be converted to a
2005common numeric type, replace \code{*p1} and \code{*p2} by their
2006converted value (with 'new' reference counts), and return \code{0}.
2007If no conversion is possible, or if some other error occurs, return
2008\code{-1} (failure) and don't increment the reference counts. The
2009call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
2010statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
2011\bifuncindex{coerce}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002012\end{cfuncdesc}
2013
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002014\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002015Returns the \var{o} converted to an integer object on success, or
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002016\NULL{} on failure. This is the equivalent of the Python
Fred Drake659ebfa2000-04-03 15:42:13 +00002017expression \samp{int(\var{o})}.\bifuncindex{int}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002018\end{cfuncdesc}
2019
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002020\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002021Returns the \var{o} converted to a long integer object on success,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002022or \NULL{} on failure. This is the equivalent of the Python
Fred Drake659ebfa2000-04-03 15:42:13 +00002023expression \samp{long(\var{o})}.\bifuncindex{long}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002024\end{cfuncdesc}
2025
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002026\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00002027Returns the \var{o} converted to a float object on success, or
2028\NULL{} on failure. This is the equivalent of the Python expression
2029\samp{float(\var{o})}.\bifuncindex{float}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002030\end{cfuncdesc}
2031
2032
Fred Drakeefd146c1999-02-15 15:30:45 +00002033\section{Sequence Protocol \label{sequence}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002034
2035\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00002036Return \code{1} if the object provides sequence protocol, and
2037\code{0} otherwise. This function always succeeds.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002038\end{cfuncdesc}
2039
Fred Drakec6a3cb42001-04-04 01:25:17 +00002040\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00002041Returns the number of objects in sequence \var{o} on success, and
2042\code{-1} on failure. For objects that do not provide sequence
2043protocol, this is equivalent to the Python expression
2044\samp{len(\var{o})}.\bifuncindex{len}
2045\end{cfuncdesc}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002046
Fred Drakec6a3cb42001-04-04 01:25:17 +00002047\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
2048Alternate name for \cfunction{PySequence_Size()}.
2049\end{cfuncdesc}
2050
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002051\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
Fred Drakee058b4f1998-02-16 06:15:35 +00002052Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002053failure. This is the equivalent of the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002054expression \samp{\var{o1} + \var{o2}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002055\end{cfuncdesc}
2056
2057
2058\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Fred Drake659ebfa2000-04-03 15:42:13 +00002059Return the result of repeating sequence object
2060\var{o} \var{count} times, or \NULL{} on failure. This is the
2061equivalent of the Python expression \samp{\var{o} * \var{count}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002062\end{cfuncdesc}
2063
Fred Drake01978582001-08-08 19:14:53 +00002064\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
2065 PyObject *o2}
Fred Drake7740a012000-09-12 20:27:05 +00002066Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on
2067failure. The operation is done \emph{in-place} when \var{o1} supports it.
2068This is the equivalent of the Python expression \samp{\var{o1} += \var{o2}}.
2069\end{cfuncdesc}
2070
2071
2072\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
2073Return the result of repeating sequence object \var{o} \var{count} times, or
2074\NULL{} on failure. The operation is done \emph{in-place} when \var{o}
2075supports it. This is the equivalent of the Python expression \samp{\var{o}
2076*= \var{count}}.
2077\end{cfuncdesc}
2078
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002079
2080\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
Fred Drakee058b4f1998-02-16 06:15:35 +00002081Return the \var{i}th element of \var{o}, or \NULL{} on failure. This
2082is the equivalent of the Python expression \samp{\var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002083\end{cfuncdesc}
2084
2085
2086\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00002087Return the slice of sequence object \var{o} between \var{i1} and
2088\var{i2}, or \NULL{} on failure. This is the equivalent of the Python
2089expression \samp{\var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002090\end{cfuncdesc}
2091
2092
2093\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00002094Assign object \var{v} to the \var{i}th element of \var{o}.
2095Returns \code{-1} on failure. This is the equivalent of the Python
2096statement \samp{\var{o}[\var{i}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002097\end{cfuncdesc}
2098
2099\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
Fred Drake5566c1c2001-01-19 22:48:33 +00002100Delete the \var{i}th element of object \var{o}. Returns
Fred Drakee058b4f1998-02-16 06:15:35 +00002101\code{-1} on failure. This is the equivalent of the Python
2102statement \samp{del \var{o}[\var{i}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002103\end{cfuncdesc}
2104
Fred Drake659ebfa2000-04-03 15:42:13 +00002105\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
2106 int i2, PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00002107Assign the sequence object \var{v} to the slice in sequence
2108object \var{o} from \var{i1} to \var{i2}. This is the equivalent of
2109the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002110\end{cfuncdesc}
2111
2112\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
Fred Drakee058b4f1998-02-16 06:15:35 +00002113Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}.
2114Returns \code{-1} on failure. This is the equivalent of the Python
2115statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002116\end{cfuncdesc}
2117
2118\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002119Returns the \var{o} as a tuple on success, and \NULL{} on failure.
Fred Drake659ebfa2000-04-03 15:42:13 +00002120This is equivalent to the Python expression \samp{tuple(\var{o})}.
2121\bifuncindex{tuple}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002122\end{cfuncdesc}
2123
2124\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00002125Return the number of occurrences of \var{value} in \var{o}, that is,
2126return the number of keys for which \code{\var{o}[\var{key}] ==
2127\var{value}}. On failure, return \code{-1}. This is equivalent to
2128the Python expression \samp{\var{o}.count(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002129\end{cfuncdesc}
2130
Fred Drake659ebfa2000-04-03 15:42:13 +00002131\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00002132Determine if \var{o} contains \var{value}. If an item in \var{o} is
2133equal to \var{value}, return \code{1}, otherwise return \code{0}. On
2134error, return \code{-1}. This is equivalent to the Python expression
2135\samp{\var{value} in \var{o}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002136\end{cfuncdesc}
2137
2138\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drakee058b4f1998-02-16 06:15:35 +00002139Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
2140\var{value}}. On error, return \code{-1}. This is equivalent to
2141the Python expression \samp{\var{o}.index(\var{value})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002142\end{cfuncdesc}
2143
Fred Drakea8455ab2000-06-16 19:58:42 +00002144\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
2145Return a list object with the same contents as the arbitrary sequence
2146\var{o}. The returned list is guaranteed to be new.
2147\end{cfuncdesc}
2148
2149\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
2150Return a tuple object with the same contents as the arbitrary sequence
2151\var{o}. If \var{o} is a tuple, a new reference will be returned,
2152otherwise a tuple will be constructed with the appropriate contents.
2153\end{cfuncdesc}
2154
Fred Drakef39ed671998-02-26 22:01:23 +00002155
Fred Drake81cccb72000-09-12 15:22:05 +00002156\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
2157Returns the sequence \var{o} as a tuple, unless it is already a
2158tuple or list, in which case \var{o} is returned. Use
2159\cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
2160result. Returns \NULL{} on failure. If the object is not a sequence,
2161raises \exception{TypeError} with \var{m} as the message text.
2162\end{cfuncdesc}
2163
2164\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
2165Return the \var{i}th element of \var{o}, assuming that \var{o} was
2166returned by \cfunction{PySequence_Fast()}, and that \var{i} is within
2167bounds. The caller is expected to get the length of the sequence by
Fred Drake96a2a802001-05-29 18:51:41 +00002168calling \cfunction{PySequence_Size()} on \var{o}, since lists and tuples
Fred Drake81cccb72000-09-12 15:22:05 +00002169are guaranteed to always return their true length.
2170\end{cfuncdesc}
2171
2172
Fred Drakeefd146c1999-02-15 15:30:45 +00002173\section{Mapping Protocol \label{mapping}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002174
2175\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00002176Return \code{1} if the object provides mapping protocol, and
2177\code{0} otherwise. This function always succeeds.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002178\end{cfuncdesc}
2179
2180
2181\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
Fred Drake659ebfa2000-04-03 15:42:13 +00002182Returns the number of keys in object \var{o} on success, and
2183\code{-1} on failure. For objects that do not provide mapping
2184protocol, this is equivalent to the Python expression
2185\samp{len(\var{o})}.\bifuncindex{len}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002186\end{cfuncdesc}
2187
2188
2189\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002190Remove the mapping for object \var{key} from the object \var{o}.
2191Return \code{-1} on failure. This is equivalent to
2192the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002193\end{cfuncdesc}
2194
2195
2196\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002197Remove the mapping for object \var{key} from the object \var{o}.
2198Return \code{-1} on failure. This is equivalent to
2199the Python statement \samp{del \var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002200\end{cfuncdesc}
2201
2202
2203\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
Fred Drake659ebfa2000-04-03 15:42:13 +00002204On success, return \code{1} if the mapping object has the key
2205\var{key} and \code{0} otherwise. This is equivalent to the Python
2206expression \samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002207This function always succeeds.
2208\end{cfuncdesc}
2209
2210
2211\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002212Return \code{1} if the mapping object has the key \var{key} and
2213\code{0} otherwise. This is equivalent to the Python expression
2214\samp{\var{o}.has_key(\var{key})}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002215This function always succeeds.
2216\end{cfuncdesc}
2217
2218
2219\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002220On success, return a list of the keys in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002221failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002222expression \samp{\var{o}.keys()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002223\end{cfuncdesc}
2224
2225
2226\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002227On success, return a list of the values in object \var{o}. On
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002228failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002229expression \samp{\var{o}.values()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002230\end{cfuncdesc}
2231
2232
2233\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00002234On success, return a list of the items in object \var{o}, where
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002235each item is a tuple containing a key-value pair. On
2236failure, return \NULL{}. This is equivalent to the Python
Fred Drakee058b4f1998-02-16 06:15:35 +00002237expression \samp{\var{o}.items()}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002238\end{cfuncdesc}
2239
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002240
2241\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00002242Return element of \var{o} corresponding to the object \var{key} or
2243\NULL{} on failure. This is the equivalent of the Python expression
2244\samp{\var{o}[\var{key}]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002245\end{cfuncdesc}
2246
Fred Drakedbcaeda2001-05-07 17:42:18 +00002247\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
2248 PyObject *v}
Fred Drakee058b4f1998-02-16 06:15:35 +00002249Map the object \var{key} to the value \var{v} in object \var{o}.
2250Returns \code{-1} on failure. This is the equivalent of the Python
2251statement \samp{\var{o}[\var{key}] = \var{v}}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002252\end{cfuncdesc}
2253
2254
Fred Drakedbcaeda2001-05-07 17:42:18 +00002255\section{Iterator Protocol \label{iterator}}
2256
Fred Drakea8e08272001-05-07 17:47:07 +00002257\versionadded{2.2}
2258
Fred Drakedbcaeda2001-05-07 17:42:18 +00002259There are only a couple of functions specifically for working with
2260iterators.
2261
2262\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
2263 Return true if the object \var{o} supports the iterator protocol.
2264\end{cfuncdesc}
2265
2266\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
2267 Return the next value from the iteration \var{o}. If the object is
2268 an iterator, this retrieves the next value from the iteration, and
2269 returns \NULL{} with no exception set if there are no remaining
2270 items. If the object is not an iterator, \exception{TypeError} is
2271 raised, or if there is an error in retrieving the item, returns
2272 \NULL{} and passes along the exception.
2273\end{cfuncdesc}
2274
2275To write a loop which iterates over an iterator, the C code should
2276look something like this:
2277
2278\begin{verbatim}
2279PyObject *iterator = ...;
2280PyObject *item;
2281
2282while (item = PyIter_Next(iter)) {
2283 /* do something with item */
2284}
2285if (PyErr_Occurred()) {
2286 /* propogate error */
2287}
2288else {
2289 /* continue doing useful work */
2290}
2291\end{verbatim}
2292
2293
Fred Drakeefd146c1999-02-15 15:30:45 +00002294\chapter{Concrete Objects Layer \label{concrete}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002295
2296The functions in this chapter are specific to certain Python object
2297types. Passing them an object of the wrong type is not a good idea;
2298if you receive an object from a Python program and you are not sure
2299that it has the right type, you must perform a type check first;
Fred Drake5566c1c2001-01-19 22:48:33 +00002300for example, to check that an object is a dictionary, use
Fred Drakee5bf8b21998-02-12 21:22:28 +00002301\cfunction{PyDict_Check()}. The chapter is structured like the
2302``family tree'' of Python object types.
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002303
Fred Drake89024422000-10-23 16:00:54 +00002304\strong{Warning:}
2305While the functions described in this chapter carefully check the type
2306of the objects which are passed in, many of them do not check for
2307\NULL{} being passed instead of a valid object. Allowing \NULL{} to
2308be passed in can cause memory access violations and immediate
2309termination of the interpreter.
2310
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002311
Fred Drakeefd146c1999-02-15 15:30:45 +00002312\section{Fundamental Objects \label{fundamental}}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002313
Fred Drakee5bf8b21998-02-12 21:22:28 +00002314This section describes Python type objects and the singleton object
2315\code{None}.
2316
2317
Fred Drakeefd146c1999-02-15 15:30:45 +00002318\subsection{Type Objects \label{typeObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002319
Fred Drake659ebfa2000-04-03 15:42:13 +00002320\obindex{type}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002321\begin{ctypedesc}{PyTypeObject}
Fred Drake659ebfa2000-04-03 15:42:13 +00002322The C structure of the objects used to describe built-in types.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002323\end{ctypedesc}
2324
Fred Drake659ebfa2000-04-03 15:42:13 +00002325\begin{cvardesc}{PyObject*}{PyType_Type}
Fred Drakeefd146c1999-02-15 15:30:45 +00002326This is the type object for type objects; it is the same object as
2327\code{types.TypeType} in the Python layer.
Fred Drake659ebfa2000-04-03 15:42:13 +00002328\withsubitem{(in module types)}{\ttindex{TypeType}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002329\end{cvardesc}
2330
Fred Drake659ebfa2000-04-03 15:42:13 +00002331\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
2332Returns true is the object \var{o} is a type object.
2333\end{cfuncdesc}
2334
2335\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
2336Returns true if the type object \var{o} sets the feature
Fred Drakef0e08ef2001-02-03 01:11:26 +00002337\var{feature}. Type features are denoted by single bit flags.
Fred Drake659ebfa2000-04-03 15:42:13 +00002338\end{cfuncdesc}
2339
Fred Draked61d0d32001-09-23 02:05:26 +00002340\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
2341Returns true if \var{a} is a subtype of \var{b}.
2342\end{cfuncdesc}
2343
2344\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
2345 int nitems}
2346\end{cfuncdesc}
2347
2348\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
2349 PyObject *args, PyObject *kwds}
2350\end{cfuncdesc}
2351
Fred Drakee5bf8b21998-02-12 21:22:28 +00002352
Fred Drakeefd146c1999-02-15 15:30:45 +00002353\subsection{The None Object \label{noneObject}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002354
Fred Drake659ebfa2000-04-03 15:42:13 +00002355\obindex{None@\texttt{None}}
2356Note that the \ctype{PyTypeObject} for \code{None} is not directly
2357exposed in the Python/C API. Since \code{None} is a singleton,
2358testing for object identity (using \samp{==} in C) is sufficient.
2359There is no \cfunction{PyNone_Check()} function for the same reason.
2360
2361\begin{cvardesc}{PyObject*}{Py_None}
Guido van Rossum44475131998-04-21 15:30:01 +00002362The Python \code{None} object, denoting lack of value. This object has
2363no methods.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002364\end{cvardesc}
2365
2366
Fred Drakefa774872001-07-11 20:35:37 +00002367\section{Numeric Objects \label{numericObjects}}
2368
2369\obindex{numeric}
2370
2371
2372\subsection{Plain Integer Objects \label{intObjects}}
2373
2374\obindex{integer}
2375\begin{ctypedesc}{PyIntObject}
2376This subtype of \ctype{PyObject} represents a Python integer object.
2377\end{ctypedesc}
2378
2379\begin{cvardesc}{PyTypeObject}{PyInt_Type}
2380This instance of \ctype{PyTypeObject} represents the Python plain
2381integer type. This is the same object as \code{types.IntType}.
2382\withsubitem{(in modules types)}{\ttindex{IntType}}
2383\end{cvardesc}
2384
2385\begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002386Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype of
2387\cdata{PyInt_Type}.
2388\versionchanged[Allowed subtypes to be accepted]{2.2}
2389\end{cfuncdesc}
2390
2391\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o}
2392Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a
2393subtype of \cdata{PyInt_Type}.
2394\versionadded{2.2}
Fred Drakefa774872001-07-11 20:35:37 +00002395\end{cfuncdesc}
2396
2397\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
2398Creates a new integer object with a value of \var{ival}.
2399
2400The current implementation keeps an array of integer objects for all
2401integers between \code{-1} and \code{100}, when you create an int in
2402that range you actually just get back a reference to the existing
2403object. So it should be possible to change the value of \code{1}. I
2404suspect the behaviour of Python in this case is undefined. :-)
2405\end{cfuncdesc}
2406
2407\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
2408Will first attempt to cast the object to a \ctype{PyIntObject}, if
2409it is not already one, and then return its value.
2410\end{cfuncdesc}
2411
2412\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
2413Returns the value of the object \var{io}. No error checking is
2414performed.
2415\end{cfuncdesc}
2416
2417\begin{cfuncdesc}{long}{PyInt_GetMax}{}
2418Returns the system's idea of the largest integer it can handle
2419(\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
2420header files).
2421\end{cfuncdesc}
2422
2423
2424\subsection{Long Integer Objects \label{longObjects}}
2425
2426\obindex{long integer}
2427\begin{ctypedesc}{PyLongObject}
2428This subtype of \ctype{PyObject} represents a Python long integer
2429object.
2430\end{ctypedesc}
2431
2432\begin{cvardesc}{PyTypeObject}{PyLong_Type}
2433This instance of \ctype{PyTypeObject} represents the Python long
2434integer type. This is the same object as \code{types.LongType}.
2435\withsubitem{(in modules types)}{\ttindex{LongType}}
2436\end{cvardesc}
2437
2438\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002439Returns true if its argument is a \ctype{PyLongObject} or a subtype of
2440\ctype{PyLongObject}.
2441\versionchanged[Allowed subtypes to be accepted]{2.2}
2442\end{cfuncdesc}
2443
2444\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
2445Returns true if its argument is a \ctype{PyLongObject}, but not a
2446subtype of \ctype{PyLongObject}.
2447\versionadded{2.2}
Fred Drakefa774872001-07-11 20:35:37 +00002448\end{cfuncdesc}
2449
2450\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
2451Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{} on
2452failure.
2453\end{cfuncdesc}
2454
2455\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
2456Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
2457long}, or \NULL{} on failure.
2458\end{cfuncdesc}
2459
Fred Drakef47d8ef2001-09-20 19:18:52 +00002460\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
2461Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
2462or \NULL{} on failure.
2463\end{cfuncdesc}
2464
2465\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
2466Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
2467long long}, or \NULL{} on failure.
2468\end{cfuncdesc}
2469
Fred Drakefa774872001-07-11 20:35:37 +00002470\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
2471Returns a new \ctype{PyLongObject} object from the integer part of
2472\var{v}, or \NULL{} on failure.
2473\end{cfuncdesc}
2474
Fred Drakef47d8ef2001-09-20 19:18:52 +00002475\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
2476 int base}
2477Return a new \ctype{PyLongObject} based on the string value in
2478\var{str}, which is interpreted according to the radix in \var{base}.
2479If \var{pend} is non-\NULL, \code{*\var{pend}} will point to the first
2480character in \var{str} which follows the representation of the
2481number. If \var{base} is \code{0}, the radix will be determined base
2482on the leading characters of \var{str}: if \var{str} starts with
2483\code{'0x'} or \code{'0X'}, radix 16 will be used; if \var{str} starts
2484with \code{'0'}, radix 8 will be used; otherwise radix 10 will be
2485used. If \var{base} is not \code{0}, it must be between \code{2} and
2486\code{36}, inclusive. Leading spaces are ignored. If there are no
2487digits, \exception{ValueError} will be raised.
2488\end{cfuncdesc}
2489
2490\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
2491 int length, int base}
2492Convert a sequence of Unicode digits to a Python long integer value.
2493The first parameter, \var{u}, points to the first character of the
2494Unicode string, \var{length} gives the number of characters, and
2495\var{base} is the radix for the conversion. The radix must be in the
2496range [2, 36]; if it is out of range, \exception{ValueError} will be
2497raised.
2498\versionadded{1.6}
2499\end{cfuncdesc}
2500
2501\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
2502Create a Python integer or long integer from the pointer \var{p}. The
2503pointer value can be retrieved from the resulting value using
2504\cfunction{PyLong_AsVoidPtr()}.
2505\versionadded{1.5.2}
2506\end{cfuncdesc}
2507
Fred Drakefa774872001-07-11 20:35:37 +00002508\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
2509Returns a C \ctype{long} representation of the contents of
2510\var{pylong}. If \var{pylong} is greater than
2511\constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError} is
2512raised.\withsubitem{(built-in exception)}{\ttindex{OverflowError}}
2513\end{cfuncdesc}
2514
2515\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
2516Returns a C \ctype{unsigned long} representation of the contents of
2517\var{pylong}. If \var{pylong} is greater than
2518\constant{ULONG_MAX}\ttindex{ULONG_MAX}, an \exception{OverflowError}
2519is raised.\withsubitem{(built-in exception)}{\ttindex{OverflowError}}
2520\end{cfuncdesc}
2521
Fred Drakef47d8ef2001-09-20 19:18:52 +00002522\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
2523Return a C \ctype{long long} from a Python long integer. If
2524\var{pylong} cannot be represented as a \ctype{long long}, an
2525\exception{OverflowError} will be raised.
2526\versionadded{2.2}
Fred Drakefa774872001-07-11 20:35:37 +00002527\end{cfuncdesc}
2528
Fred Drakef47d8ef2001-09-20 19:18:52 +00002529\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
2530 *pylong}
2531Return a C \ctype{unsigned long long} from a Python long integer. If
2532\var{pylong} cannot be represented as an \ctype{unsigned long long},
2533an \exception{OverflowError} will be raised if the value is positive,
2534or a \exception{TypeError} will be raised if the value is negative.
2535\versionadded{2.2}
2536\end{cfuncdesc}
2537
2538\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
2539Returns a C \ctype{double} representation of the contents of
2540\var{pylong}. If \var{pylong} cannot be approximately represented as
2541a \ctype{double}, an \exception{OverflowError} exception is raised and
2542\code{-1.0} will be returned.
2543\end{cfuncdesc}
2544
2545\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
2546Convert a Python integer or long integer \var{pylong} to a C
2547\ctype{void} pointer. If \var{pylong} cannot be converted, an
2548\exception{OverflowError} will be raised. This is only assured to
2549produce a usable \ctype{void} pointer for values created with
2550\cfunction{PyLong_FromVoidPtr()}.
2551\versionadded{1.5.2}
Fred Drakefa774872001-07-11 20:35:37 +00002552\end{cfuncdesc}
2553
2554
2555\subsection{Floating Point Objects \label{floatObjects}}
2556
2557\obindex{floating point}
2558\begin{ctypedesc}{PyFloatObject}
2559This subtype of \ctype{PyObject} represents a Python floating point
2560object.
2561\end{ctypedesc}
2562
2563\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
2564This instance of \ctype{PyTypeObject} represents the Python floating
2565point type. This is the same object as \code{types.FloatType}.
2566\withsubitem{(in modules types)}{\ttindex{FloatType}}
2567\end{cvardesc}
2568
2569\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002570Returns true if its argument is a \ctype{PyFloatObject} or a subtype
2571of \ctype{PyFloatObject}.
2572\versionchanged[Allowed subtypes to be accepted]{2.2}
2573\end{cfuncdesc}
2574
2575\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
2576Returns true if its argument is a \ctype{PyFloatObject}, but not a
2577subtype of \ctype{PyFloatObject}.
2578\versionadded{2.2}
Fred Drakefa774872001-07-11 20:35:37 +00002579\end{cfuncdesc}
2580
2581\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
2582Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
2583failure.
2584\end{cfuncdesc}
2585
2586\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
2587Returns a C \ctype{double} representation of the contents of \var{pyfloat}.
2588\end{cfuncdesc}
2589
2590\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
2591Returns a C \ctype{double} representation of the contents of
2592\var{pyfloat}, but without error checking.
2593\end{cfuncdesc}
2594
2595
2596\subsection{Complex Number Objects \label{complexObjects}}
2597
2598\obindex{complex number}
2599Python's complex number objects are implemented as two distinct types
2600when viewed from the C API: one is the Python object exposed to
2601Python programs, and the other is a C structure which represents the
2602actual complex number value. The API provides functions for working
2603with both.
2604
2605\subsubsection{Complex Numbers as C Structures}
2606
2607Note that the functions which accept these structures as parameters
2608and return them as results do so \emph{by value} rather than
2609dereferencing them through pointers. This is consistent throughout
2610the API.
2611
2612\begin{ctypedesc}{Py_complex}
2613The C structure which corresponds to the value portion of a Python
2614complex number object. Most of the functions for dealing with complex
2615number objects use structures of this type as input or output values,
2616as appropriate. It is defined as:
2617
2618\begin{verbatim}
2619typedef struct {
2620 double real;
2621 double imag;
2622} Py_complex;
2623\end{verbatim}
2624\end{ctypedesc}
2625
2626\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
2627Return the sum of two complex numbers, using the C
2628\ctype{Py_complex} representation.
2629\end{cfuncdesc}
2630
2631\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
2632Return the difference between two complex numbers, using the C
2633\ctype{Py_complex} representation.
2634\end{cfuncdesc}
2635
2636\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
2637Return the negation of the complex number \var{complex}, using the C
2638\ctype{Py_complex} representation.
2639\end{cfuncdesc}
2640
2641\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
2642Return the product of two complex numbers, using the C
2643\ctype{Py_complex} representation.
2644\end{cfuncdesc}
2645
2646\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
2647 Py_complex divisor}
2648Return the quotient of two complex numbers, using the C
2649\ctype{Py_complex} representation.
2650\end{cfuncdesc}
2651
2652\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
2653Return the exponentiation of \var{num} by \var{exp}, using the C
2654\ctype{Py_complex} representation.
2655\end{cfuncdesc}
2656
2657
2658\subsubsection{Complex Numbers as Python Objects}
2659
2660\begin{ctypedesc}{PyComplexObject}
2661This subtype of \ctype{PyObject} represents a Python complex number object.
2662\end{ctypedesc}
2663
2664\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2665This instance of \ctype{PyTypeObject} represents the Python complex
2666number type.
2667\end{cvardesc}
2668
2669\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002670Returns true if its argument is a \ctype{PyComplexObject} or a subtype
2671of \ctype{PyComplexObject}.
2672\versionchanged[Allowed subtypes to be accepted]{2.2}
2673\end{cfuncdesc}
2674
2675\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
2676Returns true if its argument is a \ctype{PyComplexObject}, but not a
2677subtype of \ctype{PyComplexObject}.
2678\versionadded{2.2}
Fred Drakefa774872001-07-11 20:35:37 +00002679\end{cfuncdesc}
2680
2681\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
2682Create a new Python complex number object from a C
2683\ctype{Py_complex} value.
2684\end{cfuncdesc}
2685
2686\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
2687Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}.
2688\end{cfuncdesc}
2689
2690\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
2691Returns the real part of \var{op} as a C \ctype{double}.
2692\end{cfuncdesc}
2693
2694\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
2695Returns the imaginary part of \var{op} as a C \ctype{double}.
2696\end{cfuncdesc}
2697
2698\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
2699Returns the \ctype{Py_complex} value of the complex number \var{op}.
2700\end{cfuncdesc}
2701
2702
2703
Fred Drakeefd146c1999-02-15 15:30:45 +00002704\section{Sequence Objects \label{sequenceObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002705
Fred Drake659ebfa2000-04-03 15:42:13 +00002706\obindex{sequence}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002707Generic operations on sequence objects were discussed in the previous
2708chapter; this section deals with the specific kinds of sequence
2709objects that are intrinsic to the Python language.
2710
2711
Fred Drakeefd146c1999-02-15 15:30:45 +00002712\subsection{String Objects \label{stringObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002713
Fred Drake89024422000-10-23 16:00:54 +00002714These functions raise \exception{TypeError} when expecting a string
2715parameter and are called with a non-string parameter.
2716
Fred Drake659ebfa2000-04-03 15:42:13 +00002717\obindex{string}
Fred Drakee5bf8b21998-02-12 21:22:28 +00002718\begin{ctypedesc}{PyStringObject}
Fred Drakef8830d11998-04-23 14:06:01 +00002719This subtype of \ctype{PyObject} represents a Python string object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002720\end{ctypedesc}
2721
2722\begin{cvardesc}{PyTypeObject}{PyString_Type}
Fred Drake659ebfa2000-04-03 15:42:13 +00002723This instance of \ctype{PyTypeObject} represents the Python string
2724type; it is the same object as \code{types.TypeType} in the Python
2725layer.\withsubitem{(in module types)}{\ttindex{StringType}}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00002726\end{cvardesc}
2727
2728\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002729Returns true if the object \var{o} is a string object or an instance
2730of a subtype of the string type.
2731\versionchanged[Allowed subtypes to be accepted]{2.2}
2732\end{cfuncdesc}
2733
2734\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
2735Returns true if the object \var{o} is a string object, but not an
2736instance of a subtype of the string type.
2737\versionadded{2.2}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002738\end{cfuncdesc}
2739
Fred Drakec6fa34e1998-04-02 06:47:24 +00002740\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002741Returns a new string object with the value \var{v} on success, and
2742\NULL{} on failure.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002743\end{cfuncdesc}
2744
Fred Drake659ebfa2000-04-03 15:42:13 +00002745\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
2746 int len}
2747Returns a new string object with the value \var{v} and length
2748\var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{},
2749the contents of the string are uninitialized.
2750\end{cfuncdesc}
2751
Barry Warsawc86aa572001-08-28 02:31:28 +00002752\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
2753Takes a C \code{printf}-style \var{format} string and a variable
2754number of arguments, calculates the size of the resulting Python
2755string and returns a string with the values formatted into it. The
2756variable arguments must be C types and must correspond exactly to the
2757format characters in the \var{format} string. The following format
2758characters are allowed:
2759\begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
2760 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
2761 \lineiii{\%c}{int}{A single character, represented as an C int.}
2762 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
2763 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
2764 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
2765 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
2766 \lineiii{\%s}{char*}{A null-terminated C character array.}
2767 \lineiii{\%p}{void*}{The hex representation of a C pointer.
2768 Mostly equivalent to \code{printf("\%p")} except that it is
2769 guaranteed to start with the literal \code{0x} regardless of
2770 what the platform's \code{printf} yields.}
2771\end{tableiii}
2772\end{cfuncdesc}
2773
2774\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
2775 va_list vargs}
2776Identical to \function{PyString_FromFormat()} except that it takes
2777exactly two arguments.
2778\end{cfuncdesc}
2779
Fred Drakec6fa34e1998-04-02 06:47:24 +00002780\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002781Returns the length of the string in string object \var{string}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002782\end{cfuncdesc}
2783
Fred Drake659ebfa2000-04-03 15:42:13 +00002784\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
Fred Drake5d644212000-10-07 12:31:50 +00002785Macro form of \cfunction{PyString_Size()} but without error
Fred Drake659ebfa2000-04-03 15:42:13 +00002786checking.
2787\end{cfuncdesc}
2788
Fred Drakec6fa34e1998-04-02 06:47:24 +00002789\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake659ebfa2000-04-03 15:42:13 +00002790Returns a null-terminated representation of the contents of
2791\var{string}. The pointer refers to the internal buffer of
Fred Drake89024422000-10-23 16:00:54 +00002792\var{string}, not a copy. The data must not be modified in any way,
2793unless the string was just created using
2794\code{PyString_FromStringAndSize(NULL, \var{size})}.
2795It must not be deallocated.
Fred Drake659ebfa2000-04-03 15:42:13 +00002796\end{cfuncdesc}
2797
2798\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
2799Macro form of \cfunction{PyString_AsString()} but without error
2800checking.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002801\end{cfuncdesc}
2802
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00002803\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
2804 char **buffer,
2805 int *length}
2806Returns a null-terminated representation of the contents of the object
2807\var{obj} through the output variables \var{buffer} and \var{length}.
2808
2809The function accepts both string and Unicode objects as input. For
2810Unicode objects it returns the default encoded version of the object.
2811If \var{length} is set to \NULL{}, the resulting buffer may not contain
2812null characters; if it does, the function returns -1 and a
2813TypeError is raised.
2814
2815The buffer refers to an internal string buffer of \var{obj}, not a
Fred Drake89024422000-10-23 16:00:54 +00002816copy. The data must not be modified in any way, unless the string was
2817just created using \code{PyString_FromStringAndSize(NULL,
2818\var{size})}. It must not be deallocated.
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00002819\end{cfuncdesc}
2820
Fred Drakec6fa34e1998-04-02 06:47:24 +00002821\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
2822 PyObject *newpart}
Fred Drake66b989c1999-02-15 20:15:39 +00002823Creates a new string object in \var{*string} containing the
Fred Drakeddc6c272000-03-31 18:22:38 +00002824contents of \var{newpart} appended to \var{string}; the caller will
2825own the new reference. The reference to the old value of \var{string}
2826will be stolen. If the new string
Fred Drake66b989c1999-02-15 20:15:39 +00002827cannot be created, the old reference to \var{string} will still be
2828discarded and the value of \var{*string} will be set to
2829\NULL{}; the appropriate exception will be set.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002830\end{cfuncdesc}
2831
2832\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
2833 PyObject *newpart}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00002834Creates a new string object in \var{*string} containing the contents
Guido van Rossum44475131998-04-21 15:30:01 +00002835of \var{newpart} appended to \var{string}. This version decrements
2836the reference count of \var{newpart}.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002837\end{cfuncdesc}
2838
2839\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
Guido van Rossum44475131998-04-21 15:30:01 +00002840A way to resize a string object even though it is ``immutable''.
2841Only use this to build up a brand new string object; don't use this if
2842the string may already be known in other parts of the code.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002843\end{cfuncdesc}
2844
2845\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
2846 PyObject *args}
Guido van Rossum44475131998-04-21 15:30:01 +00002847Returns a new string object from \var{format} and \var{args}. Analogous
Fred Drake659ebfa2000-04-03 15:42:13 +00002848to \code{\var{format} \%\ \var{args}}. The \var{args} argument must be
Guido van Rossum44475131998-04-21 15:30:01 +00002849a tuple.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002850\end{cfuncdesc}
2851
2852\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
Guido van Rossum44475131998-04-21 15:30:01 +00002853Intern the argument \var{*string} in place. The argument must be the
2854address of a pointer variable pointing to a Python string object.
2855If there is an existing interned string that is the same as
2856\var{*string}, it sets \var{*string} to it (decrementing the reference
2857count of the old string object and incrementing the reference count of
2858the interned string object), otherwise it leaves \var{*string} alone
2859and interns it (incrementing its reference count). (Clarification:
2860even though there is a lot of talk about reference counts, think of
Fred Drakef8830d11998-04-23 14:06:01 +00002861this function as reference-count-neutral; you own the object after
2862the call if and only if you owned it before the call.)
Fred Drakec6fa34e1998-04-02 06:47:24 +00002863\end{cfuncdesc}
2864
2865\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
Fred Drakef8830d11998-04-23 14:06:01 +00002866A combination of \cfunction{PyString_FromString()} and
2867\cfunction{PyString_InternInPlace()}, returning either a new string object
Guido van Rossum44475131998-04-21 15:30:01 +00002868that has been interned, or a new (``owned'') reference to an earlier
2869interned string object with the same value.
Fred Drakec6fa34e1998-04-02 06:47:24 +00002870\end{cfuncdesc}
2871
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002872\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
2873 int size,
2874 const char *encoding,
2875 const char *errors}
Marc-André Lemburg2d920412001-05-15 12:00:02 +00002876Creates an object by decoding \var{size} bytes of the encoded
2877buffer \var{s} using the codec registered
2878for \var{encoding}. \var{encoding} and \var{errors} have the same meaning
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002879as the parameters of the same name in the unicode() builtin
2880function. The codec to be used is looked up using the Python codec
2881registry. Returns \NULL{} in case an exception was raised by the
2882codec.
2883\end{cfuncdesc}
2884
Marc-André Lemburg2d920412001-05-15 12:00:02 +00002885\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
2886 const char *encoding,
2887 const char *errors}
2888Decodes a string object by passing it to the codec registered
2889for \var{encoding} and returns the result as Python
2890object. \var{encoding} and \var{errors} have the same meaning as the
2891parameters of the same name in the string .encode() method. The codec
2892to be used is looked up using the Python codec registry. Returns
2893\NULL{} in case an exception was raised by the codec.
2894\end{cfuncdesc}
2895
2896\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002897 int size,
2898 const char *encoding,
2899 const char *errors}
Marc-André Lemburg2d920412001-05-15 12:00:02 +00002900Encodes the \ctype{char} buffer of the given size by passing it to
2901the codec registered for \var{encoding} and returns a Python object.
2902\var{encoding} and \var{errors} have the same
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002903meaning as the parameters of the same name in the string .encode()
2904method. The codec to be used is looked up using the Python codec
2905registry. Returns \NULL{} in case an exception was raised by the
2906codec.
2907\end{cfuncdesc}
2908
Marc-André Lemburg2d920412001-05-15 12:00:02 +00002909\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002910 const char *encoding,
2911 const char *errors}
Marc-André Lemburg2d920412001-05-15 12:00:02 +00002912Encodes a string object using the codec registered
2913for \var{encoding} and returns the result as Python
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00002914object. \var{encoding} and \var{errors} have the same meaning as the
2915parameters of the same name in the string .encode() method. The codec
2916to be used is looked up using the Python codec registry. Returns
2917\NULL{} in case an exception was raised by the codec.
2918\end{cfuncdesc}
2919
Fred Drakee5bf8b21998-02-12 21:22:28 +00002920
Fred Drakea4cd2612000-04-06 14:10:29 +00002921\subsection{Unicode Objects \label{unicodeObjects}}
2922\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
2923
2924%--- Unicode Type -------------------------------------------------------
2925
2926These are the basic Unicode object types used for the Unicode
2927implementation in Python:
2928
2929\begin{ctypedesc}{Py_UNICODE}
2930This type represents a 16-bit unsigned storage type which is used by
2931Python internally as basis for holding Unicode ordinals. On platforms
2932where \ctype{wchar_t} is available and also has 16-bits,
2933\ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
2934native platform compatibility. On all other platforms,
2935\ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
2936\end{ctypedesc}
2937
2938\begin{ctypedesc}{PyUnicodeObject}
2939This subtype of \ctype{PyObject} represents a Python Unicode object.
2940\end{ctypedesc}
2941
2942\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
2943This instance of \ctype{PyTypeObject} represents the Python Unicode type.
2944\end{cvardesc}
2945
2946%--- These are really C macros... is there a macrodesc TeX macro ?
2947
2948The following APIs are really C macros and can be used to do fast
2949checks and to access internal read-only data of Unicode objects:
2950
2951\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Fred Drakef47d8ef2001-09-20 19:18:52 +00002952Returns true if the object \var{o} is a Unicode object or an instance
2953of a Unicode subtype.
2954\versionchanged[Allowed subtypes to be accepted]{2.2}
2955\end{cfuncdesc}
2956
2957\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
2958Returns true if the object \var{o} is a Unicode object, but not an
2959instance of a subtype.
2960\versionadded{2.2}
Fred Drakea4cd2612000-04-06 14:10:29 +00002961\end{cfuncdesc}
2962
2963\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
2964Returns the size of the object. o has to be a
2965PyUnicodeObject (not checked).
2966\end{cfuncdesc}
2967
2968\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
2969Returns the size of the object's internal buffer in bytes. o has to be
2970a PyUnicodeObject (not checked).
2971\end{cfuncdesc}
2972
Fred Drake992fe5a2000-06-16 21:04:15 +00002973\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Fred Drakea4cd2612000-04-06 14:10:29 +00002974Returns a pointer to the internal Py_UNICODE buffer of the object. o
2975has to be a PyUnicodeObject (not checked).
2976\end{cfuncdesc}
2977
Fred Drake992fe5a2000-06-16 21:04:15 +00002978\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Fred Drakea4cd2612000-04-06 14:10:29 +00002979Returns a (const char *) pointer to the internal buffer of the object.
2980o has to be a PyUnicodeObject (not checked).
2981\end{cfuncdesc}
2982
2983% --- Unicode character properties ---------------------------------------
2984
2985Unicode provides many different character properties. The most often
2986needed ones are available through these macros which are mapped to C
2987functions depending on the Python configuration.
2988
2989\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
2990Returns 1/0 depending on whether \var{ch} is a whitespace character.
2991\end{cfuncdesc}
2992
2993\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
2994Returns 1/0 depending on whether \var{ch} is a lowercase character.
2995\end{cfuncdesc}
2996
2997\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Fred Drakeae96aab2000-07-03 13:38:10 +00002998Returns 1/0 depending on whether \var{ch} is an uppercase character.
Fred Drakea4cd2612000-04-06 14:10:29 +00002999\end{cfuncdesc}
3000
3001\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
3002Returns 1/0 depending on whether \var{ch} is a titlecase character.
3003\end{cfuncdesc}
3004
3005\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
3006Returns 1/0 depending on whether \var{ch} is a linebreak character.
3007\end{cfuncdesc}
3008
3009\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
3010Returns 1/0 depending on whether \var{ch} is a decimal character.
3011\end{cfuncdesc}
3012
3013\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
3014Returns 1/0 depending on whether \var{ch} is a digit character.
3015\end{cfuncdesc}
3016
3017\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
3018Returns 1/0 depending on whether \var{ch} is a numeric character.
3019\end{cfuncdesc}
3020
Fred Drakeae96aab2000-07-03 13:38:10 +00003021\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
3022Returns 1/0 depending on whether \var{ch} is an alphabetic character.
3023\end{cfuncdesc}
3024
3025\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
3026Returns 1/0 depending on whether \var{ch} is an alphanumeric character.
3027\end{cfuncdesc}
3028
Fred Drakea4cd2612000-04-06 14:10:29 +00003029These APIs can be used for fast direct character conversions:
3030
3031\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
3032Returns the character \var{ch} converted to lower case.
3033\end{cfuncdesc}
3034
3035\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
3036Returns the character \var{ch} converted to upper case.
3037\end{cfuncdesc}
3038
3039\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
3040Returns the character \var{ch} converted to title case.
3041\end{cfuncdesc}
3042
3043\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
3044Returns the character \var{ch} converted to a decimal positive integer.
3045Returns -1 in case this is not possible. Does not raise exceptions.
3046\end{cfuncdesc}
3047
3048\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
3049Returns the character \var{ch} converted to a single digit integer.
3050Returns -1 in case this is not possible. Does not raise exceptions.
3051\end{cfuncdesc}
3052
3053\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
3054Returns the character \var{ch} converted to a (positive) double.
3055Returns -1.0 in case this is not possible. Does not raise exceptions.
3056\end{cfuncdesc}
3057
3058% --- Plain Py_UNICODE ---------------------------------------------------
3059
3060To create Unicode objects and access their basic sequence properties,
3061use these APIs:
3062
3063\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
3064 int size}
3065
3066Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
3067given size. \var{u} may be \NULL{} which causes the contents to be
3068undefined. It is the user's responsibility to fill in the needed data.
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +00003069The buffer is copied into the new object. If the buffer is not \NULL{},
3070the return value might be a shared object. Therefore, modification of
3071the resulting Unicode Object is only allowed when \var{u} is \NULL{}.
Fred Drakea4cd2612000-04-06 14:10:29 +00003072\end{cfuncdesc}
3073
Fred Drake1d158692000-06-18 05:21:21 +00003074\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003075Return a read-only pointer to the Unicode object's internal
3076\ctype{Py_UNICODE} buffer.
3077\end{cfuncdesc}
3078
3079\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
3080Return the length of the Unicode object.
3081\end{cfuncdesc}
3082
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003083\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
3084 const char *encoding,
3085 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003086
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003087Coerce an encoded object obj to an Unicode object and return a
3088reference with incremented refcount.
Fred Drakea4cd2612000-04-06 14:10:29 +00003089
3090Coercion is done in the following way:
3091\begin{enumerate}
3092\item Unicode objects are passed back as-is with incremented
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003093 refcount. Note: these cannot be decoded; passing a non-NULL
3094 value for encoding will result in a TypeError.
Fred Drakea4cd2612000-04-06 14:10:29 +00003095
3096\item String and other char buffer compatible objects are decoded
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003097 according to the given encoding and using the error handling
3098 defined by errors. Both can be NULL to have the interface use
3099 the default values (see the next section for details).
Fred Drakea4cd2612000-04-06 14:10:29 +00003100
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003101\item All other objects cause an exception.
Fred Drakea4cd2612000-04-06 14:10:29 +00003102\end{enumerate}
3103The API returns NULL in case of an error. The caller is responsible
3104for decref'ing the returned objects.
3105\end{cfuncdesc}
3106
Marc-André Lemburg5a20b212000-07-07 15:47:06 +00003107\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
3108
3109Shortcut for PyUnicode_FromEncodedObject(obj, NULL, ``strict'')
3110which is used throughout the interpreter whenever coercion to
3111Unicode is needed.
3112\end{cfuncdesc}
3113
Fred Drakea4cd2612000-04-06 14:10:29 +00003114% --- wchar_t support for platforms which support it ---------------------
3115
3116If the platform supports \ctype{wchar_t} and provides a header file
3117wchar.h, Python can interface directly to this type using the
3118following functions. Support is optimized if Python's own
3119\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
3120
3121\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
3122 int size}
3123Create a Unicode Object from the \ctype{whcar_t} buffer \var{w} of the
3124given size. Returns \NULL{} on failure.
3125\end{cfuncdesc}
3126
3127\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
3128 wchar_t *w,
3129 int size}
Fred Drakea4cd2612000-04-06 14:10:29 +00003130Copies the Unicode Object contents into the \ctype{whcar_t} buffer
3131\var{w}. At most \var{size} \ctype{whcar_t} characters are copied.
3132Returns the number of \ctype{whcar_t} characters copied or -1 in case
3133of an error.
3134\end{cfuncdesc}
3135
3136
3137\subsubsection{Builtin Codecs \label{builtinCodecs}}
3138
3139Python provides a set of builtin codecs which are written in C
3140for speed. All of these codecs are directly usable via the
3141following functions.
3142
3143Many of the following APIs take two arguments encoding and
3144errors. These parameters encoding and errors have the same semantics
3145as the ones of the builtin unicode() Unicode object constructor.
3146
3147Setting encoding to NULL causes the default encoding to be used which
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +00003148is \ASCII{}. The file system calls should use
3149\var{Py_FileSystemDefaultEncoding} as the encoding for file
3150names. This variable should be treated as read-only: On some systems,
3151it will be a pointer to a static string, on others, it will change at
3152run-time, e.g. when the application invokes setlocale.
Fred Drakea4cd2612000-04-06 14:10:29 +00003153
3154Error handling is set by errors which may also be set to NULL meaning
3155to use the default handling defined for the codec. Default error
3156handling for all builtin codecs is ``strict'' (ValueErrors are raised).
3157
3158The codecs all use a similar interface. Only deviation from the
3159following generic ones are documented for simplicity.
3160
3161% --- Generic Codecs -----------------------------------------------------
3162
3163These are the generic codec APIs:
3164
3165\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
3166 int size,
3167 const char *encoding,
3168 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003169Create a Unicode object by decoding \var{size} bytes of the encoded
3170string \var{s}. \var{encoding} and \var{errors} have the same meaning
3171as the parameters of the same name in the unicode() builtin
3172function. The codec to be used is looked up using the Python codec
3173registry. Returns \NULL{} in case an exception was raised by the
3174codec.
3175\end{cfuncdesc}
3176
3177\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
3178 int size,
3179 const char *encoding,
3180 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003181Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a
3182Python string object. \var{encoding} and \var{errors} have the same
3183meaning as the parameters of the same name in the Unicode .encode()
3184method. The codec to be used is looked up using the Python codec
3185registry. Returns \NULL{} in case an exception was raised by the
3186codec.
3187\end{cfuncdesc}
3188
3189\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
3190 const char *encoding,
3191 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003192Encodes a Unicode object and returns the result as Python string
3193object. \var{encoding} and \var{errors} have the same meaning as the
3194parameters of the same name in the Unicode .encode() method. The codec
3195to be used is looked up using the Python codec registry. Returns
3196\NULL{} in case an exception was raised by the codec.
3197\end{cfuncdesc}
3198
3199% --- UTF-8 Codecs -------------------------------------------------------
3200
3201These are the UTF-8 codec APIs:
3202
3203\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
3204 int size,
3205 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003206Creates a Unicode object by decoding \var{size} bytes of the UTF-8
3207encoded string \var{s}. Returns \NULL{} in case an exception was
3208raised by the codec.
3209\end{cfuncdesc}
3210
3211\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
3212 int size,
3213 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003214Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
3215and returns a Python string object. Returns \NULL{} in case an
3216exception was raised by the codec.
3217\end{cfuncdesc}
3218
3219\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003220Encodes a Unicode objects using UTF-8 and returns the result as Python
3221string object. Error handling is ``strict''. Returns
3222\NULL{} in case an exception was raised by the codec.
3223\end{cfuncdesc}
3224
3225% --- UTF-16 Codecs ------------------------------------------------------ */
3226
3227These are the UTF-16 codec APIs:
3228
3229\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
3230 int size,
3231 const char *errors,
3232 int *byteorder}
Fred Drakea4cd2612000-04-06 14:10:29 +00003233Decodes \var{length} bytes from a UTF-16 encoded buffer string and
3234returns the corresponding Unicode object.
3235
3236\var{errors} (if non-NULL) defines the error handling. It defaults
3237to ``strict''.
3238
3239If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
3240the given byte order:
3241
3242\begin{verbatim}
3243 *byteorder == -1: little endian
3244 *byteorder == 0: native order
3245 *byteorder == 1: big endian
3246\end{verbatim}
3247
3248and then switches according to all byte order marks (BOM) it finds in
3249the input data. BOM marks are not copied into the resulting Unicode
3250string. After completion, \var{*byteorder} is set to the current byte
3251order at the end of input data.
3252
3253If \var{byteorder} is \NULL{}, the codec starts in native order mode.
3254
3255Returns \NULL{} in case an exception was raised by the codec.
3256\end{cfuncdesc}
3257
3258\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
3259 int size,
3260 const char *errors,
3261 int byteorder}
Fred Drakea4cd2612000-04-06 14:10:29 +00003262Returns a Python string object holding the UTF-16 encoded value of the
3263Unicode data in \var{s}.
3264
Fred Drakea8455ab2000-06-16 19:58:42 +00003265If \var{byteorder} is not \code{0}, output is written according to the
Fred Drakea4cd2612000-04-06 14:10:29 +00003266following byte order:
3267
3268\begin{verbatim}
3269 byteorder == -1: little endian
3270 byteorder == 0: native byte order (writes a BOM mark)
3271 byteorder == 1: big endian
3272\end{verbatim}
3273
Fred Drakea8455ab2000-06-16 19:58:42 +00003274If byteorder is \code{0}, the output string will always start with the
Fred Drakea4cd2612000-04-06 14:10:29 +00003275Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
3276prepended.
3277
3278Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16
3279reduced to UCS-2. This trick makes it possible to add full UTF-16
3280capabilities at a later point without comprimising the APIs.
3281
3282Returns \NULL{} in case an exception was raised by the codec.
3283\end{cfuncdesc}
3284
3285\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003286Returns a Python string using the UTF-16 encoding in native byte
3287order. The string always starts with a BOM mark. Error handling is
3288``strict''. Returns \NULL{} in case an exception was raised by the
3289codec.
3290\end{cfuncdesc}
3291
3292% --- Unicode-Escape Codecs ----------------------------------------------
3293
3294These are the ``Unicode Esacpe'' codec APIs:
3295
3296\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
3297 int size,
3298 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003299Creates a Unicode object by decoding \var{size} bytes of the Unicode-Esacpe
3300encoded string \var{s}. Returns \NULL{} in case an exception was
3301raised by the codec.
3302\end{cfuncdesc}
3303
3304\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
3305 int size,
3306 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003307Encodes the \ctype{Py_UNICODE} buffer of the given size using Unicode-Escape
3308and returns a Python string object. Returns \NULL{} in case an
3309exception was raised by the codec.
3310\end{cfuncdesc}
3311
3312\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003313Encodes a Unicode objects using Unicode-Escape and returns the result
3314as Python string object. Error handling is ``strict''. Returns
3315\NULL{} in case an exception was raised by the codec.
3316\end{cfuncdesc}
3317
3318% --- Raw-Unicode-Escape Codecs ------------------------------------------
3319
3320These are the ``Raw Unicode Esacpe'' codec APIs:
3321
3322\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
3323 int size,
3324 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003325Creates a Unicode object by decoding \var{size} bytes of the Raw-Unicode-Esacpe
3326encoded string \var{s}. Returns \NULL{} in case an exception was
3327raised by the codec.
3328\end{cfuncdesc}
3329
3330\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
3331 int size,
3332 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003333Encodes the \ctype{Py_UNICODE} buffer of the given size using Raw-Unicode-Escape
3334and returns a Python string object. Returns \NULL{} in case an
3335exception was raised by the codec.
3336\end{cfuncdesc}
3337
3338\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003339Encodes a Unicode objects using Raw-Unicode-Escape and returns the result
3340as Python string object. Error handling is ``strict''. Returns
3341\NULL{} in case an exception was raised by the codec.
3342\end{cfuncdesc}
3343
3344% --- Latin-1 Codecs -----------------------------------------------------
3345
3346These are the Latin-1 codec APIs:
3347
3348Latin-1 corresponds to the first 256 Unicode ordinals and only these
3349are accepted by the codecs during encoding.
3350
3351\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Fred Drake1d158692000-06-18 05:21:21 +00003352 int size,
3353 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003354Creates a Unicode object by decoding \var{size} bytes of the Latin-1
3355encoded string \var{s}. Returns \NULL{} in case an exception was
3356raised by the codec.
3357\end{cfuncdesc}
3358
3359\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Fred Drake1d158692000-06-18 05:21:21 +00003360 int size,
3361 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003362Encodes the \ctype{Py_UNICODE} buffer of the given size using Latin-1
3363and returns a Python string object. Returns \NULL{} in case an
3364exception was raised by the codec.
3365\end{cfuncdesc}
3366
3367\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003368Encodes a Unicode objects using Latin-1 and returns the result as
3369Python string object. Error handling is ``strict''. Returns
3370\NULL{} in case an exception was raised by the codec.
3371\end{cfuncdesc}
3372
3373% --- ASCII Codecs -------------------------------------------------------
3374
Fred Drake1d158692000-06-18 05:21:21 +00003375These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
3376accepted. All other codes generate errors.
Fred Drakea4cd2612000-04-06 14:10:29 +00003377
3378\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Fred Drake1d158692000-06-18 05:21:21 +00003379 int size,
3380 const char *errors}
3381Creates a Unicode object by decoding \var{size} bytes of the
3382\ASCII{} encoded string \var{s}. Returns \NULL{} in case an exception
3383was raised by the codec.
Fred Drakea4cd2612000-04-06 14:10:29 +00003384\end{cfuncdesc}
3385
3386\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Fred Drake1d158692000-06-18 05:21:21 +00003387 int size,
3388 const char *errors}
3389Encodes the \ctype{Py_UNICODE} buffer of the given size using
3390\ASCII{} and returns a Python string object. Returns \NULL{} in case
3391an exception was raised by the codec.
Fred Drakea4cd2612000-04-06 14:10:29 +00003392\end{cfuncdesc}
3393
3394\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Fred Drake1d158692000-06-18 05:21:21 +00003395Encodes a Unicode objects using \ASCII{} and returns the result as Python
Fred Drakea4cd2612000-04-06 14:10:29 +00003396string object. Error handling is ``strict''. Returns
3397\NULL{} in case an exception was raised by the codec.
3398\end{cfuncdesc}
3399
3400% --- Character Map Codecs -----------------------------------------------
3401
3402These are the mapping codec APIs:
3403
3404This codec is special in that it can be used to implement many
3405different codecs (and this is in fact what was done to obtain most of
3406the standard codecs included in the \module{encodings} package). The
3407codec uses mapping to encode and decode characters.
3408
3409Decoding mappings must map single string characters to single Unicode
3410characters, integers (which are then interpreted as Unicode ordinals)
3411or None (meaning "undefined mapping" and causing an error).
3412
3413Encoding mappings must map single Unicode characters to single string
3414characters, integers (which are then interpreted as Latin-1 ordinals)
3415or None (meaning "undefined mapping" and causing an error).
3416
3417The mapping objects provided must only support the __getitem__ mapping
3418interface.
3419
3420If a character lookup fails with a LookupError, the character is
3421copied as-is meaning that its ordinal value will be interpreted as
3422Unicode or Latin-1 ordinal resp. Because of this, mappings only need
3423to contain those mappings which map characters to different code
3424points.
3425
3426\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
3427 int size,
3428 PyObject *mapping,
3429 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003430Creates a Unicode object by decoding \var{size} bytes of the encoded
3431string \var{s} using the given \var{mapping} object. Returns \NULL{}
3432in case an exception was raised by the codec.
3433\end{cfuncdesc}
3434
3435\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
3436 int size,
3437 PyObject *mapping,
3438 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003439Encodes the \ctype{Py_UNICODE} buffer of the given size using the
3440given \var{mapping} object and returns a Python string object.
3441Returns \NULL{} in case an exception was raised by the codec.
3442\end{cfuncdesc}
3443
3444\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
3445 PyObject *mapping}
Fred Drakea4cd2612000-04-06 14:10:29 +00003446Encodes a Unicode objects using the given \var{mapping} object and
3447returns the result as Python string object. Error handling is
3448``strict''. Returns \NULL{} in case an exception was raised by the
3449codec.
3450\end{cfuncdesc}
3451
3452The following codec API is special in that maps Unicode to Unicode.
3453
3454\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
3455 int size,
3456 PyObject *table,
3457 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003458Translates a \ctype{Py_UNICODE} buffer of the given length by applying
3459a character mapping \var{table} to it and returns the resulting
Fred Drake1d158692000-06-18 05:21:21 +00003460Unicode object. Returns \NULL{} when an exception was raised by the
3461codec.
Fred Drakea4cd2612000-04-06 14:10:29 +00003462
3463The \var{mapping} table must map Unicode ordinal integers to Unicode
3464ordinal integers or None (causing deletion of the character).
3465
3466Mapping tables must only provide the __getitem__ interface,
3467e.g. dictionaries or sequences. Unmapped character ordinals (ones
3468which cause a LookupError) are left untouched and are copied as-is.
Fred Drakea4cd2612000-04-06 14:10:29 +00003469\end{cfuncdesc}
3470
3471% --- MBCS codecs for Windows --------------------------------------------
3472
Fred Drake1d158692000-06-18 05:21:21 +00003473These are the MBCS codec APIs. They are currently only available on
Fred Drakea4cd2612000-04-06 14:10:29 +00003474Windows and use the Win32 MBCS converters to implement the
Fred Drake1d158692000-06-18 05:21:21 +00003475conversions. Note that MBCS (or DBCS) is a class of encodings, not
3476just one. The target encoding is defined by the user settings on the
3477machine running the codec.
Fred Drakea4cd2612000-04-06 14:10:29 +00003478
3479\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
3480 int size,
3481 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003482Creates a Unicode object by decoding \var{size} bytes of the MBCS
Fred Drake1d158692000-06-18 05:21:21 +00003483encoded string \var{s}. Returns \NULL{} in case an exception was
Fred Drakea4cd2612000-04-06 14:10:29 +00003484raised by the codec.
3485\end{cfuncdesc}
3486
3487\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
3488 int size,
3489 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003490Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
3491and returns a Python string object. Returns \NULL{} in case an
3492exception was raised by the codec.
3493\end{cfuncdesc}
3494
3495\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Fred Drakea4cd2612000-04-06 14:10:29 +00003496Encodes a Unicode objects using MBCS and returns the result as Python
Fred Drake1d158692000-06-18 05:21:21 +00003497string object. Error handling is ``strict''. Returns \NULL{} in case
3498an exception was raised by the codec.
Fred Drakea4cd2612000-04-06 14:10:29 +00003499\end{cfuncdesc}
3500
3501% --- Methods & Slots ----------------------------------------------------
3502
3503\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
3504
3505The following APIs are capable of handling Unicode objects and strings
3506on input (we refer to them as strings in the descriptions) and return
3507Unicode objects or integers as apporpriate.
3508
3509They all return \NULL{} or -1 in case an exception occurrs.
3510
3511\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
3512 PyObject *right}
Fred Drakea4cd2612000-04-06 14:10:29 +00003513Concat two strings giving a new Unicode string.
3514\end{cfuncdesc}
3515
3516\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
3517 PyObject *sep,
3518 int maxsplit}
Fred Drakea4cd2612000-04-06 14:10:29 +00003519Split a string giving a list of Unicode strings.
3520
3521If sep is NULL, splitting will be done at all whitespace
3522substrings. Otherwise, splits occur at the given separator.
3523
3524At most maxsplit splits will be done. If negative, no limit is set.
3525
3526Separators are not included in the resulting list.
3527\end{cfuncdesc}
3528
3529\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
3530 int maxsplit}
Fred Drake1d158692000-06-18 05:21:21 +00003531Split a Unicode string at line breaks, returning a list of Unicode
3532strings. CRLF is considered to be one line break. The Line break
3533characters are not included in the resulting strings.
Fred Drakea4cd2612000-04-06 14:10:29 +00003534\end{cfuncdesc}
3535
3536\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
3537 PyObject *table,
3538 const char *errors}
Fred Drakea4cd2612000-04-06 14:10:29 +00003539Translate a string by applying a character mapping table to it and
3540return the resulting Unicode object.
3541
3542The mapping table must map Unicode ordinal integers to Unicode ordinal
3543integers or None (causing deletion of the character).
3544
3545Mapping tables must only provide the __getitem__ interface,
3546e.g. dictionaries or sequences. Unmapped character ordinals (ones
3547which cause a LookupError) are left untouched and are copied as-is.
3548
3549\var{errors} has the usual meaning for codecs. It may be \NULL{}
3550which indicates to use the default error handling.
Fred Drakea4cd2612000-04-06 14:10:29 +00003551\end{cfuncdesc}
3552
3553\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
3554 PyObject *seq}
Fred Drakea4cd2612000-04-06 14:10:29 +00003555Join a sequence of strings using the given separator and return
3556the resulting Unicode string.
3557\end{cfuncdesc}
3558
3559\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
3560 PyObject *substr,
3561 int start,
3562 int end,
3563 int direction}
Fred Drakea4cd2612000-04-06 14:10:29 +00003564Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
3565the given tail end (\var{direction} == -1 means to do a prefix match,
3566\var{direction} == 1 a suffix match), 0 otherwise.
3567\end{cfuncdesc}
3568
3569\begin{cfuncdesc}{PyObject*}{PyUnicode_Find}{PyObject *str,
3570 PyObject *substr,
3571 int start,
3572 int end,
3573 int direction}
Fred Drakea4cd2612000-04-06 14:10:29 +00003574Return the first position of \var{substr} in
3575\var{str}[\var{start}:\var{end}] using the given \var{direction}
3576(\var{direction} == 1 means to do a forward search,
3577\var{direction} == -1 a backward search), 0 otherwise.
3578\end{cfuncdesc}
3579
3580\begin{cfuncdesc}{PyObject*}{PyUnicode_Count}{PyObject *str,
3581 PyObject *substr,
3582 int start,
3583 int end}
Fred Drakea4cd2612000-04-06 14:10:29 +00003584Count the number of occurrences of \var{substr} in
3585\var{str}[\var{start}:\var{end}]
3586\end{cfuncdesc}
3587
3588\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
3589 PyObject *substr,
3590 PyObject *replstr,
3591 int maxcount}
Fred Drakea4cd2612000-04-06 14:10:29 +00003592Replace at most \var{maxcount} occurrences of \var{substr} in
3593\var{str} with \var{replstr} and return the resulting Unicode object.
3594\var{maxcount} == -1 means: replace all occurrences.
3595\end{cfuncdesc}
3596
Fred Drake1d158692000-06-18 05:21:21 +00003597\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
Fred Drakea4cd2612000-04-06 14:10:29 +00003598Compare two strings and return -1, 0, 1 for less than, equal,
3599greater than resp.
3600\end{cfuncdesc}
3601
3602\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
3603 PyObject *args}
Fred Drake1d158692000-06-18 05:21:21 +00003604Returns a new string object from \var{format} and \var{args}; this is
3605analogous to \code{\var{format} \%\ \var{args}}. The
3606\var{args} argument must be a tuple.
Fred Drakea4cd2612000-04-06 14:10:29 +00003607\end{cfuncdesc}
3608
3609\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
3610 PyObject *element}
Fred Drakea4cd2612000-04-06 14:10:29 +00003611Checks whether \var{element} is contained in \var{container} and
Fred Drake1d158692000-06-18 05:21:21 +00003612returns true or false accordingly.
Fred Drakea4cd2612000-04-06 14:10:29 +00003613
Fred Drake1d158692000-06-18 05:21:21 +00003614\var{element} has to coerce to a one element Unicode string. \code{-1} is
Fred Drakea4cd2612000-04-06 14:10:29 +00003615returned in case of an error.
3616\end{cfuncdesc}
3617
3618
Fred Drake58c5a2a1999-08-04 13:13:24 +00003619\subsection{Buffer Objects \label{bufferObjects}}
Fred Drake659ebfa2000-04-03 15:42:13 +00003620\sectionauthor{Greg Stein}{gstein@lyra.org}
Fred Drake58c5a2a1999-08-04 13:13:24 +00003621
Fred Drake659ebfa2000-04-03 15:42:13 +00003622\obindex{buffer}
3623Python objects implemented in C can export a group of functions called
3624the ``buffer\index{buffer interface} interface.'' These functions can
3625be used by an object to expose its data in a raw, byte-oriented
3626format. Clients of the object can use the buffer interface to access
3627the object data directly, without needing to copy it first.
3628
3629Two examples of objects that support
3630the buffer interface are strings and arrays. The string object exposes
3631the character contents in the buffer interface's byte-oriented
3632form. An array can also expose its contents, but it should be noted
3633that array elements may be multi-byte values.
3634
3635An example user of the buffer interface is the file object's
3636\method{write()} method. Any object that can export a series of bytes
3637through the buffer interface can be written to a file. There are a
Fred Drake88fdaa72001-07-20 20:56:11 +00003638number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake659ebfa2000-04-03 15:42:13 +00003639against an object's buffer interface, returning data from the target
3640object.
3641
3642More information on the buffer interface is provided in the section
3643``Buffer Object Structures'' (section \ref{buffer-structs}), under
3644the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
3645
3646A ``buffer object'' is defined in the \file{bufferobject.h} header
3647(included by \file{Python.h}). These objects look very similar to
3648string objects at the Python programming level: they support slicing,
3649indexing, concatenation, and some other standard string
3650operations. However, their data can come from one of two sources: from
3651a block of memory, or from another object which exports the buffer
3652interface.
3653
3654Buffer objects are useful as a way to expose the data from another
3655object's buffer interface to the Python programmer. They can also be
3656used as a zero-copy slicing mechanism. Using their ability to
3657reference a block of memory, it is possible to expose any data to the
3658Python programmer quite easily. The memory could be a large, constant
3659array in a C extension, it could be a raw block of memory for
3660manipulation before passing to an operating system library, or it
3661could be used to pass around structured data in its native, in-memory
3662format.
3663
3664\begin{ctypedesc}{PyBufferObject}
3665This subtype of \ctype{PyObject} represents a buffer object.
3666\end{ctypedesc}
Fred Drake58c5a2a1999-08-04 13:13:24 +00003667
3668\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
3669The instance of \ctype{PyTypeObject} which represents the Python
Fred Drake659ebfa2000-04-03 15:42:13 +00003670buffer type; it is the same object as \code{types.BufferType} in the
3671Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003672\end{cvardesc}
3673
3674\begin{cvardesc}{int}{Py_END_OF_BUFFER}
Fred Drake659ebfa2000-04-03 15:42:13 +00003675This constant may be passed as the \var{size} parameter to
3676\cfunction{PyBuffer_FromObject()} or
3677\cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the new
3678\ctype{PyBufferObject} should refer to \var{base} object from the
3679specified \var{offset} to the end of its exported buffer. Using this
3680enables the caller to avoid querying the \var{base} object for its
3681length.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003682\end{cvardesc}
3683
3684\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
3685Return true if the argument has type \cdata{PyBuffer_Type}.
3686\end{cfuncdesc}
3687
3688\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
3689 int offset, int size}
Fred Drake659ebfa2000-04-03 15:42:13 +00003690Return a new read-only buffer object. This raises
3691\exception{TypeError} if \var{base} doesn't support the read-only
3692buffer protocol or doesn't provide exactly one buffer segment, or it
3693raises \exception{ValueError} if \var{offset} is less than zero. The
3694buffer will hold a reference to the \var{base} object, and the
3695buffer's contents will refer to the \var{base} object's buffer
3696interface, starting as position \var{offset} and extending for
3697\var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
3698the new buffer's contents extend to the length of the
3699\var{base} object's exported buffer data.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003700\end{cfuncdesc}
3701
3702\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
3703 int offset,
3704 int size}
3705Return a new writable buffer object. Parameters and exceptions are
3706similar to those for \cfunction{PyBuffer_FromObject()}.
Fred Drake659ebfa2000-04-03 15:42:13 +00003707If the \var{base} object does not export the writeable buffer
3708protocol, then \exception{TypeError} is raised.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003709\end{cfuncdesc}
3710
3711\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
Fred Drake659ebfa2000-04-03 15:42:13 +00003712Return a new read-only buffer object that reads from a specified
3713location in memory, with a specified size.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003714The caller is responsible for ensuring that the memory buffer, passed
3715in as \var{ptr}, is not deallocated while the returned buffer object
3716exists. Raises \exception{ValueError} if \var{size} is less than
Fred Drake659ebfa2000-04-03 15:42:13 +00003717zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be passed
3718for the \var{size} parameter; \exception{ValueError} will be raised in
3719that case.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003720\end{cfuncdesc}
3721
3722\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
Fred Drake659ebfa2000-04-03 15:42:13 +00003723Similar to \cfunction{PyBuffer_FromMemory()}, but the returned buffer
3724is writable.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003725\end{cfuncdesc}
3726
3727\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
3728Returns a new writable buffer object that maintains its own memory
Fred Drake659ebfa2000-04-03 15:42:13 +00003729buffer of \var{size} bytes. \exception{ValueError} is returned if
3730\var{size} is not zero or positive.
Fred Drake58c5a2a1999-08-04 13:13:24 +00003731\end{cfuncdesc}
3732
Guido van Rossum44475131998-04-21 15:30:01 +00003733
Fred Drakeefd146c1999-02-15 15:30:45 +00003734\subsection{Tuple Objects \label{tupleObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003735
Fred Drake659ebfa2000-04-03 15:42:13 +00003736\obindex{tuple}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003737\begin{ctypedesc}{PyTupleObject}
Fred Drakef8830d11998-04-23 14:06:01 +00003738This subtype of \ctype{PyObject} represents a Python tuple object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003739\end{ctypedesc}
3740
3741\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
Fred Drake659ebfa2000-04-03 15:42:13 +00003742This instance of \ctype{PyTypeObject} represents the Python tuple
3743type; it is the same object as \code{types.TupleType} in the Python
3744layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003745\end{cvardesc}
3746
3747\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00003748Return true if \var{p} is a tuple object or an instance of a subtype
3749of the tuple type.
3750\versionchanged[Allowed subtypes to be accepted]{2.2}
3751\end{cfuncdesc}
3752
3753\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
3754Return true if \var{p} is a tuple object, but not an instance of
3755a subtype of the tuple type.
3756\versionadded{2.2}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003757\end{cfuncdesc}
3758
Fred Drake659ebfa2000-04-03 15:42:13 +00003759\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
3760Return a new tuple object of size \var{len}, or \NULL{} on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003761\end{cfuncdesc}
3762
Fred Drakea05460c2001-02-12 17:38:18 +00003763\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Fred Drakee058b4f1998-02-16 06:15:35 +00003764Takes a pointer to a tuple object, and returns the size
Fred Drakee5bf8b21998-02-12 21:22:28 +00003765of that tuple.
3766\end{cfuncdesc}
3767
Fred Drake0e40c3d2001-08-20 16:48:59 +00003768\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
3769Return the size of the tuple \var{p}, which must be non-\NULL{} and
3770point to a tuple; no error checking is performed.
3771\end{cfuncdesc}
3772
Fred Drakea05460c2001-02-12 17:38:18 +00003773\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
Fred Drakee058b4f1998-02-16 06:15:35 +00003774Returns the object at position \var{pos} in the tuple pointed
3775to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drake659ebfa2000-04-03 15:42:13 +00003776sets an \exception{IndexError} exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003777\end{cfuncdesc}
3778
Fred Drakea05460c2001-02-12 17:38:18 +00003779\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
Fred Drakefac312f2001-05-29 15:13:00 +00003780Like \cfunction{PyTuple_GetItem()}, but does no checking of its
3781arguments.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003782\end{cfuncdesc}
3783
Fred Drakea05460c2001-02-12 17:38:18 +00003784\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
3785 int low, int high}
Fred Drakee058b4f1998-02-16 06:15:35 +00003786Takes a slice of the tuple pointed to by \var{p} from
3787\var{low} to \var{high} and returns it as a new tuple.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003788\end{cfuncdesc}
3789
Fred Drake659ebfa2000-04-03 15:42:13 +00003790\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
3791 int pos, PyObject *o}
Fred Drakee058b4f1998-02-16 06:15:35 +00003792Inserts a reference to object \var{o} at position \var{pos} of
3793the tuple pointed to by \var{p}. It returns \code{0} on success.
Fred Drake659ebfa2000-04-03 15:42:13 +00003794\strong{Note:} This function ``steals'' a reference to \var{o}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003795\end{cfuncdesc}
3796
Fred Drake659ebfa2000-04-03 15:42:13 +00003797\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
3798 int pos, PyObject *o}
Fred Drakefac312f2001-05-29 15:13:00 +00003799Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
Fred Drakee5bf8b21998-02-12 21:22:28 +00003800should \emph{only} be used to fill in brand new tuples.
Fred Drake659ebfa2000-04-03 15:42:13 +00003801\strong{Note:} This function ``steals'' a reference to \var{o}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003802\end{cfuncdesc}
3803
Fred Drakefac312f2001-05-29 15:13:00 +00003804\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
Fred Drake659ebfa2000-04-03 15:42:13 +00003805Can be used to resize a tuple. \var{newsize} will be the new length
3806of the tuple. Because tuples are \emph{supposed} to be immutable,
3807this should only be used if there is only one reference to the object.
3808Do \emph{not} use this if the tuple may already be known to some other
Fred Drakefac312f2001-05-29 15:13:00 +00003809part of the code. The tuple will always grow or shrink at the end.
3810Think of this as destroying the old tuple and creating a new one, only
3811more efficiently. Returns \code{0} on success. Client code should
3812never assume that the resulting value of \code{*\var{p}} will be the
3813same as before calling this function. If the object referenced by
3814\code{*\var{p}} is replaced, the original \code{*\var{p}} is
3815destroyed. On failure, returns \code{-1} and sets \code{*\var{p}} to
3816\NULL, and raises \exception{MemoryError} or \exception{SystemError}.
3817\versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003818\end{cfuncdesc}
3819
3820
Fred Drakeefd146c1999-02-15 15:30:45 +00003821\subsection{List Objects \label{listObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003822
Fred Drake659ebfa2000-04-03 15:42:13 +00003823\obindex{list}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003824\begin{ctypedesc}{PyListObject}
Fred Drakef8830d11998-04-23 14:06:01 +00003825This subtype of \ctype{PyObject} represents a Python list object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003826\end{ctypedesc}
3827
3828\begin{cvardesc}{PyTypeObject}{PyList_Type}
Fred Drake659ebfa2000-04-03 15:42:13 +00003829This instance of \ctype{PyTypeObject} represents the Python list
3830type. This is the same object as \code{types.ListType}.
3831\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003832\end{cvardesc}
3833
3834\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00003835Returns true if its argument is a \ctype{PyListObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003836\end{cfuncdesc}
3837
Fred Drake659ebfa2000-04-03 15:42:13 +00003838\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
3839Returns a new list of length \var{len} on success, or \NULL{} on
Guido van Rossum3c4378b1998-04-14 20:21:10 +00003840failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003841\end{cfuncdesc}
3842
Fred Drakec6fa34e1998-04-02 06:47:24 +00003843\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
Fred Drake659ebfa2000-04-03 15:42:13 +00003844Returns the length of the list object in \var{list}; this is
3845equivalent to \samp{len(\var{list})} on a list object.
3846\bifuncindex{len}
3847\end{cfuncdesc}
3848
3849\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
Fred Drake5d644212000-10-07 12:31:50 +00003850Macro form of \cfunction{PyList_Size()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003851\end{cfuncdesc}
3852
Fred Drakec6fa34e1998-04-02 06:47:24 +00003853\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
Guido van Rossum44475131998-04-21 15:30:01 +00003854Returns the object at position \var{pos} in the list pointed
3855to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and
Fred Drake659ebfa2000-04-03 15:42:13 +00003856sets an \exception{IndexError} exception.
3857\end{cfuncdesc}
3858
3859\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
3860Macro form of \cfunction{PyList_GetItem()} without error checking.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003861\end{cfuncdesc}
3862
Fred Drakec6fa34e1998-04-02 06:47:24 +00003863\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
3864 PyObject *item}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00003865Sets the item at index \var{index} in list to \var{item}.
Fred Drakebab29652001-07-10 16:10:08 +00003866Returns \code{0} on success or \code{-1} on failure.
Fred Drake00d0cb62001-06-03 03:12:57 +00003867\strong{Note:} This function ``steals'' a reference to \var{item} and
3868discards a reference to an item already in the list at the affected
3869position.
Fred Drake659ebfa2000-04-03 15:42:13 +00003870\end{cfuncdesc}
3871
Fred Drakebab29652001-07-10 16:10:08 +00003872\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
Fred Drake659ebfa2000-04-03 15:42:13 +00003873 PyObject *o}
3874Macro form of \cfunction{PyList_SetItem()} without error checking.
Fred Drake00d0cb62001-06-03 03:12:57 +00003875\strong{Note:} This function ``steals'' a reference to \var{item},
3876and, unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
Fred Drakebab29652001-07-10 16:10:08 +00003877reference to any item that it being replaced; any reference in
3878\var{list} at position \var{i} will be leaked. This is normally only
3879used to fill in new lists where there is no previous content.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003880\end{cfuncdesc}
3881
Fred Drakec6fa34e1998-04-02 06:47:24 +00003882\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
Guido van Rossum44475131998-04-21 15:30:01 +00003883 PyObject *item}
3884Inserts the item \var{item} into list \var{list} in front of index
Fred Drake659ebfa2000-04-03 15:42:13 +00003885\var{index}. Returns \code{0} if successful; returns \code{-1} and
3886raises an exception if unsuccessful. Analogous to
3887\code{\var{list}.insert(\var{index}, \var{item})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003888\end{cfuncdesc}
3889
Fred Drakec6fa34e1998-04-02 06:47:24 +00003890\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Guido van Rossum44475131998-04-21 15:30:01 +00003891Appends the object \var{item} at the end of list \var{list}. Returns
Fred Drake659ebfa2000-04-03 15:42:13 +00003892\code{0} if successful; returns \code{-1} and sets an exception if
3893unsuccessful. Analogous to \code{\var{list}.append(\var{item})}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003894\end{cfuncdesc}
3895
Fred Drakec6fa34e1998-04-02 06:47:24 +00003896\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
3897 int low, int high}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00003898Returns a list of the objects in \var{list} containing the objects
Guido van Rossum44475131998-04-21 15:30:01 +00003899\emph{between} \var{low} and \var{high}. Returns NULL and sets an
3900exception if unsuccessful.
Fred Drake659ebfa2000-04-03 15:42:13 +00003901Analogous to \code{\var{list}[\var{low}:\var{high}]}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003902\end{cfuncdesc}
3903
Fred Drakec6fa34e1998-04-02 06:47:24 +00003904\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
3905 int low, int high,
3906 PyObject *itemlist}
Fred Drake659ebfa2000-04-03 15:42:13 +00003907Sets the slice of \var{list} between \var{low} and \var{high} to the
3908contents of \var{itemlist}. Analogous to
3909\code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns
3910\code{0} on success, \code{-1} on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003911\end{cfuncdesc}
3912
Fred Drakec6fa34e1998-04-02 06:47:24 +00003913\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Fred Drake659ebfa2000-04-03 15:42:13 +00003914Sorts the items of \var{list} in place. Returns \code{0} on success,
3915\code{-1} on failure. This is equivalent to
3916\samp{\var{list}.sort()}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003917\end{cfuncdesc}
3918
Fred Drakec6fa34e1998-04-02 06:47:24 +00003919\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Fred Drake659ebfa2000-04-03 15:42:13 +00003920Reverses the items of \var{list} in place. Returns \code{0} on
3921success, \code{-1} on failure. This is the equivalent of
3922\samp{\var{list}.reverse()}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003923\end{cfuncdesc}
3924
Fred Drakec6fa34e1998-04-02 06:47:24 +00003925\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Fred Drake659ebfa2000-04-03 15:42:13 +00003926Returns a new tuple object containing the contents of \var{list};
3927equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003928\end{cfuncdesc}
3929
3930
Fred Drakeefd146c1999-02-15 15:30:45 +00003931\section{Mapping Objects \label{mapObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003932
Fred Drake659ebfa2000-04-03 15:42:13 +00003933\obindex{mapping}
3934
3935
Fred Drakeefd146c1999-02-15 15:30:45 +00003936\subsection{Dictionary Objects \label{dictObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003937
Fred Drake659ebfa2000-04-03 15:42:13 +00003938\obindex{dictionary}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003939\begin{ctypedesc}{PyDictObject}
Fred Drakef8830d11998-04-23 14:06:01 +00003940This subtype of \ctype{PyObject} represents a Python dictionary object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003941\end{ctypedesc}
3942
3943\begin{cvardesc}{PyTypeObject}{PyDict_Type}
Fred Drake659ebfa2000-04-03 15:42:13 +00003944This instance of \ctype{PyTypeObject} represents the Python dictionary
3945type. This is exposed to Python programs as \code{types.DictType} and
3946\code{types.DictionaryType}.
3947\withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003948\end{cvardesc}
3949
3950\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00003951Returns true if its argument is a \ctype{PyDictObject}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003952\end{cfuncdesc}
3953
Fred Drakec6fa34e1998-04-02 06:47:24 +00003954\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Fred Drake659ebfa2000-04-03 15:42:13 +00003955Returns a new empty dictionary, or \NULL{} on failure.
3956\end{cfuncdesc}
3957
3958\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
3959Empties an existing dictionary of all key-value pairs.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003960\end{cfuncdesc}
3961
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00003962\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Fred Drake0e40c3d2001-08-20 16:48:59 +00003963Returns a new dictionary that contains the same key-value pairs as
3964\var{p}.
Fred Drake11ee9022001-08-10 21:31:12 +00003965\versionadded{1.6}
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00003966\end{cfuncdesc}
3967
Fred Drake659ebfa2000-04-03 15:42:13 +00003968\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
3969 PyObject *val}
Fred Drakebab29652001-07-10 16:10:08 +00003970Inserts \var{value} into the dictionary \var{p} with a key of \var{key}.
Fred Drake659ebfa2000-04-03 15:42:13 +00003971\var{key} must be hashable; if it isn't, \exception{TypeError} will be
3972raised.
Fred Drakebab29652001-07-10 16:10:08 +00003973Returns \code{0} on success or \code{-1} on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003974\end{cfuncdesc}
3975
Fred Drake83e01bf2001-03-16 15:41:29 +00003976\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Fred Drakee5bf8b21998-02-12 21:22:28 +00003977 char *key,
3978 PyObject *val}
Fred Drakebab29652001-07-10 16:10:08 +00003979Inserts \var{value} into the dictionary \var{p} using \var{key}
Fred Drake1d158692000-06-18 05:21:21 +00003980as a key. \var{key} should be a \ctype{char*}. The key object is
Fred Drakee058b4f1998-02-16 06:15:35 +00003981created using \code{PyString_FromString(\var{key})}.
Fred Drakebab29652001-07-10 16:10:08 +00003982Returns \code{0} on success or \code{-1} on failure.
Fred Drake659ebfa2000-04-03 15:42:13 +00003983\ttindex{PyString_FromString()}
Fred Drakee5bf8b21998-02-12 21:22:28 +00003984\end{cfuncdesc}
3985
Fred Drake659ebfa2000-04-03 15:42:13 +00003986\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00003987Removes the entry in dictionary \var{p} with key \var{key}.
Fred Drake659ebfa2000-04-03 15:42:13 +00003988\var{key} must be hashable; if it isn't, \exception{TypeError} is
3989raised.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003990\end{cfuncdesc}
3991
Fred Drake659ebfa2000-04-03 15:42:13 +00003992\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00003993Removes the entry in dictionary \var{p} which has a key
Fred Drake659ebfa2000-04-03 15:42:13 +00003994specified by the string \var{key}.
Fred Drakebab29652001-07-10 16:10:08 +00003995Returns \code{0} on success or \code{-1} on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00003996\end{cfuncdesc}
3997
Fred Drake659ebfa2000-04-03 15:42:13 +00003998\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Fred Drakee058b4f1998-02-16 06:15:35 +00003999Returns the object from dictionary \var{p} which has a key
Guido van Rossum44475131998-04-21 15:30:01 +00004000\var{key}. Returns \NULL{} if the key \var{key} is not present, but
Fred Drake659ebfa2000-04-03 15:42:13 +00004001\emph{without} setting an exception.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004002\end{cfuncdesc}
4003
Fred Drake659ebfa2000-04-03 15:42:13 +00004004\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
Fred Drakef8830d11998-04-23 14:06:01 +00004005This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
Fred Drake659ebfa2000-04-03 15:42:13 +00004006specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004007\end{cfuncdesc}
4008
Fred Drake659ebfa2000-04-03 15:42:13 +00004009\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00004010Returns a \ctype{PyListObject} containing all the items
Guido van Rossum44475131998-04-21 15:30:01 +00004011from the dictionary, as in the dictinoary method \method{items()} (see
Fred Drakebe486461999-11-09 17:03:03 +00004012the \citetitle[../lib/lib.html]{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00004013\end{cfuncdesc}
4014
Fred Drake659ebfa2000-04-03 15:42:13 +00004015\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00004016Returns a \ctype{PyListObject} containing all the keys
Guido van Rossum44475131998-04-21 15:30:01 +00004017from the dictionary, as in the dictionary method \method{keys()} (see the
Fred Drakebe486461999-11-09 17:03:03 +00004018\citetitle[../lib/lib.html]{Python Library Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00004019\end{cfuncdesc}
4020
Fred Drake659ebfa2000-04-03 15:42:13 +00004021\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Fred Drakef8830d11998-04-23 14:06:01 +00004022Returns a \ctype{PyListObject} containing all the values
Guido van Rossum44475131998-04-21 15:30:01 +00004023from the dictionary \var{p}, as in the dictionary method
Fred Drakebe486461999-11-09 17:03:03 +00004024\method{values()} (see the \citetitle[../lib/lib.html]{Python Library
4025Reference}).
Fred Drakee5bf8b21998-02-12 21:22:28 +00004026\end{cfuncdesc}
4027
Fred Drake659ebfa2000-04-03 15:42:13 +00004028\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
4029Returns the number of items in the dictionary. This is equivalent to
4030\samp{len(\var{p})} on a dictionary.\bifuncindex{len}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004031\end{cfuncdesc}
4032
Fred Drake83e01bf2001-03-16 15:41:29 +00004033\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
Fred Drake7d45d342000-08-11 17:07:32 +00004034 PyObject **pkey, PyObject **pvalue}
Fred Drake83e01bf2001-03-16 15:41:29 +00004035Iterate over all key-value pairs in the dictionary \var{p}. The
4036\ctype{int} referred to by \var{ppos} must be initialized to \code{0}
4037prior to the first call to this function to start the iteration; the
4038function returns true for each pair in the dictionary, and false once
4039all pairs have been reported. The parameters \var{pkey} and
4040\var{pvalue} should either point to \ctype{PyObject*} variables that
4041will be filled in with each key and value, respectively, or may be
Fred Drake8d00a0f2001-04-13 17:55:02 +00004042\NULL.
4043
Fred Drake83e01bf2001-03-16 15:41:29 +00004044For example:
Fred Drakee5bf8b21998-02-12 21:22:28 +00004045
Fred Drake83e01bf2001-03-16 15:41:29 +00004046\begin{verbatim}
4047PyObject *key, *value;
4048int pos = 0;
4049
4050while (PyDict_Next(self->dict, &pos, &key, &value)) {
4051 /* do something interesting with the values... */
4052 ...
4053}
4054\end{verbatim}
Fred Drake8d00a0f2001-04-13 17:55:02 +00004055
4056The dictionary \var{p} should not be mutated during iteration. It is
4057safe (since Python 2.1) to modify the values of the keys as you
Fred Drake11ee9022001-08-10 21:31:12 +00004058iterate over the dictionary, but only so long as the set of keys does
4059not change. For example:
Fred Drake8d00a0f2001-04-13 17:55:02 +00004060
4061\begin{verbatim}
4062PyObject *key, *value;
4063int pos = 0;
4064
4065while (PyDict_Next(self->dict, &pos, &key, &value)) {
4066 int i = PyInt_AS_LONG(value) + 1;
4067 PyObject *o = PyInt_FromLong(i);
4068 if (o == NULL)
4069 return -1;
4070 if (PyDict_SetItem(self->dict, key, o) < 0) {
4071 Py_DECREF(o);
4072 return -1;
4073 }
4074 Py_DECREF(o);
4075}
4076\end{verbatim}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004077\end{cfuncdesc}
4078
Fred Drake11ee9022001-08-10 21:31:12 +00004079\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
4080Iterate over dictionary \var{b} adding key-value pairs to dictionary
4081\var{a}. If \var{override} is true, existing pairs in \var{a} will be
4082replaced if a matching key is found in \var{b}, otherwise pairs will
4083only be added if there is not a matching key in \var{a}. Returns
4084\code{0} on success or \code{-1} if an exception was raised.
4085\versionadded{2.2}
4086\end{cfuncdesc}
4087
4088\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
4089This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C, or
4090\code{\var{a}.update(\var{b})} in Python. Returns \code{0} on success
4091or \code{-1} if an exception was raised.
4092\versionadded{2.2}
4093\end{cfuncdesc}
4094
Fred Drakee5bf8b21998-02-12 21:22:28 +00004095
Fred Drakeefd146c1999-02-15 15:30:45 +00004096\section{Other Objects \label{otherObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004097
Fred Drakeefd146c1999-02-15 15:30:45 +00004098\subsection{File Objects \label{fileObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004099
Fred Drake659ebfa2000-04-03 15:42:13 +00004100\obindex{file}
4101Python's built-in file objects are implemented entirely on the
4102\ctype{FILE*} support from the C standard library. This is an
4103implementation detail and may change in future releases of Python.
4104
Fred Drakee5bf8b21998-02-12 21:22:28 +00004105\begin{ctypedesc}{PyFileObject}
Fred Drakef8830d11998-04-23 14:06:01 +00004106This subtype of \ctype{PyObject} represents a Python file object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004107\end{ctypedesc}
4108
4109\begin{cvardesc}{PyTypeObject}{PyFile_Type}
Fred Drake659ebfa2000-04-03 15:42:13 +00004110This instance of \ctype{PyTypeObject} represents the Python file
4111type. This is exposed to Python programs as \code{types.FileType}.
4112\withsubitem{(in module types)}{\ttindex{FileType}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004113\end{cvardesc}
4114
4115\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00004116Returns true if its argument is a \ctype{PyFileObject} or a subtype of
4117\ctype{PyFileObject}.
4118\versionchanged[Allowed subtypes to be accepted]{2.2}
4119\end{cfuncdesc}
4120
4121\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
4122Returns true if its argument is a \ctype{PyFileObject}, but not a
4123subtype of \ctype{PyFileObject}.
4124\versionadded{2.2}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004125\end{cfuncdesc}
4126
Fred Drake659ebfa2000-04-03 15:42:13 +00004127\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
4128On success, returns a new file object that is opened on the
4129file given by \var{filename}, with a file mode given by \var{mode},
4130where \var{mode} has the same semantics as the standard C routine
4131\cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004132\end{cfuncdesc}
4133
Fred Drakec6fa34e1998-04-02 06:47:24 +00004134\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
Fred Drake659ebfa2000-04-03 15:42:13 +00004135 char *name, char *mode,
4136 int (*close)(FILE*)}
4137Creates a new \ctype{PyFileObject} from the already-open standard C
4138file pointer, \var{fp}. The function \var{close} will be called when
4139the file should be closed. Returns \NULL{} on failure.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004140\end{cfuncdesc}
4141
Fred Drake659ebfa2000-04-03 15:42:13 +00004142\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
4143Returns the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004144\end{cfuncdesc}
4145
Fred Drakec6fa34e1998-04-02 06:47:24 +00004146\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
Fred Drake659ebfa2000-04-03 15:42:13 +00004147Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
4148function reads one line from the object \var{p}. \var{p} may be a
4149file object or any object with a \method{readline()} method. If
4150\var{n} is \code{0}, exactly one line is read, regardless of the
4151length of the line. If \var{n} is greater than \code{0}, no more than
4152\var{n} bytes will be read from the file; a partial line can be
4153returned. In both cases, an empty string is returned if the end of
4154the file is reached immediately. If \var{n} is less than \code{0},
4155however, one line is read regardless of length, but
4156\exception{EOFError} is raised if the end of the file is reached
4157immediately.
4158\withsubitem{(built-in exception)}{\ttindex{EOFError}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004159\end{cfuncdesc}
4160
Fred Drakec6fa34e1998-04-02 06:47:24 +00004161\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Fred Drake659ebfa2000-04-03 15:42:13 +00004162Returns the name of the file specified by \var{p} as a string object.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004163\end{cfuncdesc}
4164
4165\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
Fred Drake659ebfa2000-04-03 15:42:13 +00004166Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
4167only. This should only be called immediately after file object
4168creation.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004169\end{cfuncdesc}
4170
Fred Drake659ebfa2000-04-03 15:42:13 +00004171\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
4172This function exists for internal use by the interpreter.
4173Sets the \member{softspace} attribute of \var{p} to \var{newflag} and
4174\withsubitem{(file attribute)}{\ttindex{softspace}}returns the
4175previous value. \var{p} does not have to be a file object
4176for this function to work properly; any object is supported (thought
4177its only interesting if the \member{softspace} attribute can be set).
4178This function clears any errors, and will return \code{0} as the
4179previous value if the attribute either does not exist or if there were
4180errors in retrieving it. There is no way to detect errors from this
4181function, but doing so should not be needed.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004182\end{cfuncdesc}
4183
Fred Drakec6fa34e1998-04-02 06:47:24 +00004184\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
4185 int flags}
Fred Drake659ebfa2000-04-03 15:42:13 +00004186Writes object \var{obj} to file object \var{p}. The only supported
4187flag for \var{flags} is \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW};
4188if given, the \function{str()} of the object is written instead of the
4189\function{repr()}. Returns \code{0} on success or \code{-1} on
4190failure; the appropriate exception will be set.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004191\end{cfuncdesc}
4192
Fred Drake024ef6f2001-08-10 14:27:38 +00004193\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
Fred Drake659ebfa2000-04-03 15:42:13 +00004194Writes string \var{s} to file object \var{p}. Returns \code{0} on
4195success or \code{-1} on failure; the appropriate exception will be
4196set.
Fred Drakee5bf8b21998-02-12 21:22:28 +00004197\end{cfuncdesc}
4198
4199
Fred Drake5838d0f2001-01-28 06:39:35 +00004200\subsection{Instance Objects \label{instanceObjects}}
4201
4202\obindex{instance}
4203There are very few functions specific to instance objects.
4204
4205\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
4206 Type object for class instances.
4207\end{cvardesc}
4208
4209\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
4210 Returns true if \var{obj} is an instance.
4211\end{cfuncdesc}
4212
4213\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
4214 PyObject *arg,
4215 PyObject *kw}
4216 Create a new instance of a specific class. The parameters \var{arg}
4217 and \var{kw} are used as the positional and keyword parameters to
4218 the object's constructor.
4219\end{cfuncdesc}
4220
4221\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
4222 PyObject *dict}
4223 Create a new instance of a specific class without calling it's
4224 constructor. \var{class} is the class of new object. The
4225 \var{dict} parameter will be used as the object's \member{__dict__};
4226 if \NULL, a new dictionary will be created for the instance.
4227\end{cfuncdesc}
4228
4229
Fred Drakef8d7a5d2001-09-06 17:12:44 +00004230\subsection{Method Objects \label{method-objects}}
4231
4232\obindex{method}
4233There are some useful functions that are useful for working with
4234method objects.
4235
4236\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
4237 This instance of \ctype{PyTypeObject} represents the Python method
4238 type. This is exposed to Python programs as \code{types.MethodType}.
4239 \withsubitem{(in module types)}{\ttindex{MethodType}}
4240\end{cvardesc}
4241
4242\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
4243 Return true if \var{o} is a method object (has type
4244 \cdata{PyMethod_Type}). The parameter must not be \NULL.
4245\end{cfuncdesc}
4246
4247\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
4248 PyObject *self, PyObject *class}
4249 Return a new method object, with \var{func} being any callable
4250 object; this is the function that will be called when the method is
4251 called. If this method should be bound to an instance, \var{self}
4252 should be the instance and \var{class} should be the class of
4253 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
4254 should be the class which provides the unbound method..
4255\end{cfuncdesc}
4256
4257\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
4258 Return the class object from which the method \var{meth} was
4259 created; if this was created from an instance, it will be the class
4260 of the instance.
4261\end{cfuncdesc}
4262
4263\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
4264 Macro version of \cfunction{PyMethod_Class()} which avoids error
4265 checking.
4266\end{cfuncdesc}
4267
4268\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
4269 Return the function object associated with the method \var{meth}.
4270\end{cfuncdesc}
4271
4272\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
4273 Macro version of \cfunction{PyMethod_Function()} which avoids error
4274 checking.
4275\end{cfuncdesc}
4276
4277\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
4278 Return the instance associated with the method \var{meth} if it is
4279 bound, otherwise return \NULL.
4280\end{cfuncdesc}
4281
4282\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
4283 Macro version of \cfunction{PyMethod_Self()} which avoids error
4284 checking.
4285\end{cfuncdesc}
4286
4287
Fred Drakeefd146c1999-02-15 15:30:45 +00004288\subsection{Module Objects \label{moduleObjects}}
4289
4290\obindex{module}
4291There are only a few functions special to module objects.
4292
Fred Drake659ebfa2000-04-03 15:42:13 +00004293\begin{cvardesc}{PyTypeObject}{PyModule_Type}
4294This instance of \ctype{PyTypeObject} represents the Python module
4295type. This is exposed to Python programs as \code{types.ModuleType}.
4296\withsubitem{(in module types)}{\ttindex{ModuleType}}
4297\end{cvardesc}
4298
4299\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Fred Drakef47d8ef2001-09-20 19:18:52 +00004300Returns true if \var{p} is a module object, or a subtype of a
4301module object.
4302\versionchanged[Allowed subtypes to be accepted]{2.2}
4303\end{cfuncdesc}
4304
4305\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
4306Returns true if \var{p} is a module object, but not a subtype of
4307\cdata{PyModule_Type}.
4308\versionadded{2.2}
Fred Drakeefd146c1999-02-15 15:30:45 +00004309\end{cfuncdesc}
4310
Fred Drake659ebfa2000-04-03 15:42:13 +00004311\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
4312Return a new module object with the \member{__name__} attribute set to
4313\var{name}. Only the module's \member{__doc__} and
4314\member{__name__} attributes are filled in; the caller is responsible
4315for providing a \member{__file__} attribute.
4316\withsubitem{(module attribute)}{
4317 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
4318\end{cfuncdesc}
4319
4320\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
Fred Drakeefd146c1999-02-15 15:30:45 +00004321Return the dictionary object that implements \var{module}'s namespace;
4322this object is the same as the \member{__dict__} attribute of the
4323module object. This function never fails.
Fred Drake659ebfa2000-04-03 15:42:13 +00004324\withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakeefd146c1999-02-15 15:30:45 +00004325\end{cfuncdesc}
4326
Fred Drake659ebfa2000-04-03 15:42:13 +00004327\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
Fred Drakeefd146c1999-02-15 15:30:45 +00004328Return \var{module}'s \member{__name__} value. If the module does not
Fred Drake659ebfa2000-04-03 15:42:13 +00004329provide one, or if it is not a string, \exception{SystemError} is
4330raised and \NULL{} is returned.
4331\withsubitem{(module attribute)}{\ttindex{__name__}}
4332\withsubitem{(built-in exception)}{\ttindex{SystemError}}
Fred Drakeefd146c1999-02-15 15:30:45 +00004333\end{cfuncdesc}
4334
Fred Drake659ebfa2000-04-03 15:42:13 +00004335\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
Fred Drakeefd146c1999-02-15 15:30:45 +00004336Return the name of the file from which \var{module} was loaded using
4337\var{module}'s \member{__file__} attribute. If this is not defined,
Fred Drake659ebfa2000-04-03 15:42:13 +00004338or if it is not a string, raise \exception{SystemError} and return
4339\NULL.
4340\withsubitem{(module attribute)}{\ttindex{__file__}}
4341\withsubitem{(built-in exception)}{\ttindex{SystemError}}
Fred Drakeefd146c1999-02-15 15:30:45 +00004342\end{cfuncdesc}
4343
Fred Drake891150b2000-09-23 03:25:42 +00004344\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
4345 char *name, PyObject *value}
4346Add an object to \var{module} as \var{name}. This is a convenience
4347function which can be used from the module's initialization function.
4348This steals a reference to \var{value}. Returns \code{-1} on error,
4349\code{0} on success.
4350\versionadded{2.0}
4351\end{cfuncdesc}
4352
4353\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
4354 char *name, int value}
4355Add an integer constant to \var{module} as \var{name}. This convenience
4356function can be used from the module's initialization function.
4357Returns \code{-1} on error, \code{0} on success.
4358\versionadded{2.0}
4359\end{cfuncdesc}
4360
4361\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
4362 char *name, char *value}
4363Add a string constant to \var{module} as \var{name}. This convenience
4364function can be used from the module's initialization function. The
4365string \var{value} must be null-terminated. Returns \code{-1} on
4366error, \code{0} on success.
4367\versionadded{2.0}
4368\end{cfuncdesc}
4369
Fred Drakeefd146c1999-02-15 15:30:45 +00004370
Fred Draked61d0d32001-09-23 02:05:26 +00004371\subsection{Iterator Objects \label{iterator-objects}}
4372
4373Python provides two general-purpose iterator objects. The first, a
4374sequence iterator, works with an arbitrary sequence supporting the
4375\method{__getitem__()} method. The second works with a callable
4376object and a sentinel value, calling the callable for each item in the
4377sequence, and ending the iteration when the sentinel value is
4378returned.
4379
4380\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
4381 Type object for iterator objects returned by
4382 \cfunction{PySeqIter_New()} and the one-argument form of the
4383 \function{iter()} built-in function for built-in sequence types.
4384\end{cvardesc}
4385
4386\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
4387 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
4388\end{cfuncdesc}
4389
4390\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
4391 Return an iterator that works with a general sequence object,
4392 \var{seq}. The iteration ends when the sequence raises
4393 \exception{IndexError} for the subscripting operation.
4394\end{cfuncdesc}
4395
4396
4397\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
4398 Type object for iterator objects returned by
4399 \cfunction{PyCallIter_New()} and the two-argument form of the
4400 \function{iter()} built-in function.
4401\end{cvardesc}
4402
4403\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
4404 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
4405\end{cfuncdesc}
4406
4407\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
4408 PyObject *sentinel}
4409 Return a new iterator. The first parameter, \var{callable}, can be
4410 any Python callable object that can be called with no parameters;
4411 each call to it should return the next item in the iteration. When
4412 \var{callable} returns a value equal to \var{sentinel}, the
4413 iteration will be terminated.
4414\end{cfuncdesc}
4415
4416
Fred Drakeefd146c1999-02-15 15:30:45 +00004417\subsection{CObjects \label{cObjects}}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004418
Fred Drake659ebfa2000-04-03 15:42:13 +00004419\obindex{CObject}
4420Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Draked61d0d32001-09-23 02:05:26 +00004421section 1.12 (``Providing a C API for an Extension Module), for more
Fred Drake659ebfa2000-04-03 15:42:13 +00004422information on using these objects.
4423
4424
Guido van Rossum44475131998-04-21 15:30:01 +00004425\begin{ctypedesc}{PyCObject}
Fred Drakef8830d11998-04-23 14:06:01 +00004426This subtype of \ctype{PyObject} represents an opaque value, useful for
Fred Drake659ebfa2000-04-03 15:42:13 +00004427C extension modules who need to pass an opaque value (as a
4428\ctype{void*} pointer) through Python code to other C code. It is
Guido van Rossum44475131998-04-21 15:30:01 +00004429often used to make a C function pointer defined in one module
4430available to other modules, so the regular import mechanism can be
4431used to access C APIs defined in dynamically loaded modules.
4432\end{ctypedesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004433
Fred Drake659ebfa2000-04-03 15:42:13 +00004434\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
4435Returns true if its argument is a \ctype{PyCObject}.
4436\end{cfuncdesc}
4437
4438\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Marc-André Lemburga544ea22001-01-17 18:04:31 +00004439 void (*destr)(void *)}
Fred Drake1d158692000-06-18 05:21:21 +00004440Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drakedab44681999-05-13 18:41:14 +00004441\var{destr} function will be called when the object is reclaimed, unless
4442it is \NULL.
Guido van Rossum44475131998-04-21 15:30:01 +00004443\end{cfuncdesc}
4444
Fred Drake659ebfa2000-04-03 15:42:13 +00004445\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
Marc-André Lemburga544ea22001-01-17 18:04:31 +00004446 void* desc, void (*destr)(void *, void *) }
Fred Drakef8830d11998-04-23 14:06:01 +00004447Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
4448\var{destr} function will be called when the object is reclaimed. The
4449\var{desc} argument can be used to pass extra callback data for the
4450destructor function.
Guido van Rossum44475131998-04-21 15:30:01 +00004451\end{cfuncdesc}
4452
Fred Drake659ebfa2000-04-03 15:42:13 +00004453\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
4454Returns the object \ctype{void *} that the
4455\ctype{PyCObject} \var{self} was created with.
Guido van Rossum44475131998-04-21 15:30:01 +00004456\end{cfuncdesc}
4457
Fred Drake659ebfa2000-04-03 15:42:13 +00004458\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
4459Returns the description \ctype{void *} that the
4460\ctype{PyCObject} \var{self} was created with.
Guido van Rossum44475131998-04-21 15:30:01 +00004461\end{cfuncdesc}
Fred Drakee5bf8b21998-02-12 21:22:28 +00004462
Fred Drake659ebfa2000-04-03 15:42:13 +00004463
Fred Drakeefd146c1999-02-15 15:30:45 +00004464\chapter{Initialization, Finalization, and Threads
4465 \label{initialization}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004466
Guido van Rossum4a944d71997-08-14 20:35:38 +00004467\begin{cfuncdesc}{void}{Py_Initialize}{}
4468Initialize the Python interpreter. In an application embedding
4469Python, this should be called before using any other Python/C API
Fred Drake659ebfa2000-04-03 15:42:13 +00004470functions; with the exception of
4471\cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()},
4472\cfunction{PyEval_InitThreads()}\ttindex{PyEval_InitThreads()},
4473\cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()},
4474and \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()}.
4475This initializes the table of loaded modules (\code{sys.modules}), and
4476\withsubitem{(in module sys)}{\ttindex{modules}\ttindex{path}}creates the
4477fundamental modules \module{__builtin__}\refbimodindex{__builtin__},
Fred Drake4de05a91998-02-16 14:25:26 +00004478\module{__main__}\refbimodindex{__main__} and
4479\module{sys}\refbimodindex{sys}. It also initializes the module
Fred Drake659ebfa2000-04-03 15:42:13 +00004480search\indexiii{module}{search}{path} path (\code{sys.path}).
4481It does not set \code{sys.argv}; use
4482\cfunction{PySys_SetArgv()}\ttindex{PySys_SetArgv()} for that. This
4483is a no-op when called for a second time (without calling
4484\cfunction{Py_Finalize()}\ttindex{Py_Finalize()} first). There is no
4485return value; it is a fatal error if the initialization fails.
Guido van Rossum42cefd01997-10-05 15:27:29 +00004486\end{cfuncdesc}
4487
4488\begin{cfuncdesc}{int}{Py_IsInitialized}{}
Guido van Rossum42cefd01997-10-05 15:27:29 +00004489Return true (nonzero) when the Python interpreter has been
Fred Drakee058b4f1998-02-16 06:15:35 +00004490initialized, false (zero) if not. After \cfunction{Py_Finalize()} is
4491called, this returns false until \cfunction{Py_Initialize()} is called
Guido van Rossum42cefd01997-10-05 15:27:29 +00004492again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004493\end{cfuncdesc}
4494
4495\begin{cfuncdesc}{void}{Py_Finalize}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00004496Undo all initializations made by \cfunction{Py_Initialize()} and
4497subsequent use of Python/C API functions, and destroy all
4498sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were
4499created and not yet destroyed since the last call to
4500\cfunction{Py_Initialize()}. Ideally, this frees all memory allocated
4501by the Python interpreter. This is a no-op when called for a second
4502time (without calling \cfunction{Py_Initialize()} again first). There
4503is no return value; errors during finalization are ignored.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004504
4505This function is provided for a number of reasons. An embedding
4506application might want to restart Python without having to restart the
4507application itself. An application that has loaded the Python
4508interpreter from a dynamically loadable library (or DLL) might want to
4509free all memory allocated by Python before unloading the DLL. During a
4510hunt for memory leaks in an application a developer might want to free
4511all memory allocated by Python before exiting from the application.
4512
Fred Drakee058b4f1998-02-16 06:15:35 +00004513\strong{Bugs and caveats:} The destruction of modules and objects in
Guido van Rossum4a944d71997-08-14 20:35:38 +00004514modules is done in random order; this may cause destructors
Fred Drakee058b4f1998-02-16 06:15:35 +00004515(\method{__del__()} methods) to fail when they depend on other objects
Guido van Rossum4a944d71997-08-14 20:35:38 +00004516(even functions) or modules. Dynamically loaded extension modules
4517loaded by Python are not unloaded. Small amounts of memory allocated
4518by the Python interpreter may not be freed (if you find a leak, please
4519report it). Memory tied up in circular references between objects is
4520not freed. Some memory allocated by extension modules may not be
4521freed. Some extension may not work properly if their initialization
4522routine is called more than once; this can happen if an applcation
Fred Drakee058b4f1998-02-16 06:15:35 +00004523calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more
4524than once.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004525\end{cfuncdesc}
4526
Fred Drakec6fa34e1998-04-02 06:47:24 +00004527\begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{}
Fred Drake4de05a91998-02-16 14:25:26 +00004528Create a new sub-interpreter. This is an (almost) totally separate
4529environment for the execution of Python code. In particular, the new
4530interpreter has separate, independent versions of all imported
4531modules, including the fundamental modules
4532\module{__builtin__}\refbimodindex{__builtin__},
4533\module{__main__}\refbimodindex{__main__} and
4534\module{sys}\refbimodindex{sys}. The table of loaded modules
4535(\code{sys.modules}) and the module search path (\code{sys.path}) are
4536also separate. The new environment has no \code{sys.argv} variable.
4537It has new standard I/O stream file objects \code{sys.stdin},
4538\code{sys.stdout} and \code{sys.stderr} (however these refer to the
Fred Drake659ebfa2000-04-03 15:42:13 +00004539same underlying \ctype{FILE} structures in the C library).
4540\withsubitem{(in module sys)}{
4541 \ttindex{stdout}\ttindex{stderr}\ttindex{stdin}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004542
4543The return value points to the first thread state created in the new
4544sub-interpreter. This thread state is made the current thread state.
4545Note that no actual thread is created; see the discussion of thread
4546states below. If creation of the new interpreter is unsuccessful,
Guido van Rossum580aa8d1997-11-25 15:34:51 +00004547\NULL{} is returned; no exception is set since the exception state
Guido van Rossum4a944d71997-08-14 20:35:38 +00004548is stored in the current thread state and there may not be a current
4549thread state. (Like all other Python/C API functions, the global
4550interpreter lock must be held before calling this function and is
4551still held when it returns; however, unlike most other Python/C API
4552functions, there needn't be a current thread state on entry.)
4553
4554Extension modules are shared between (sub-)interpreters as follows:
4555the first time a particular extension is imported, it is initialized
4556normally, and a (shallow) copy of its module's dictionary is
4557squirreled away. When the same extension is imported by another
4558(sub-)interpreter, a new module is initialized and filled with the
Fred Drakee058b4f1998-02-16 06:15:35 +00004559contents of this copy; the extension's \code{init} function is not
4560called. Note that this is different from what happens when an
4561extension is imported after the interpreter has been completely
Fred Drake659ebfa2000-04-03 15:42:13 +00004562re-initialized by calling
4563\cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and
4564\cfunction{Py_Initialize()}\ttindex{Py_Initialize()}; in that case,
4565the extension's \code{init\var{module}} function \emph{is} called
4566again.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004567
Fred Drakee058b4f1998-02-16 06:15:35 +00004568\strong{Bugs and caveats:} Because sub-interpreters (and the main
Guido van Rossum4a944d71997-08-14 20:35:38 +00004569interpreter) are part of the same process, the insulation between them
Fred Drakee058b4f1998-02-16 06:15:35 +00004570isn't perfect --- for example, using low-level file operations like
Fred Drake659ebfa2000-04-03 15:42:13 +00004571\withsubitem{(in module os)}{\ttindex{close()}}
Fred Drakef8830d11998-04-23 14:06:01 +00004572\function{os.close()} they can (accidentally or maliciously) affect each
Guido van Rossum4a944d71997-08-14 20:35:38 +00004573other's open files. Because of the way extensions are shared between
4574(sub-)interpreters, some extensions may not work properly; this is
4575especially likely when the extension makes use of (static) global
4576variables, or when the extension manipulates its module's dictionary
4577after its initialization. It is possible to insert objects created in
4578one sub-interpreter into a namespace of another sub-interpreter; this
4579should be done with great care to avoid sharing user-defined
4580functions, methods, instances or classes between sub-interpreters,
4581since import operations executed by such objects may affect the
4582wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
4583a hard-to-fix bug that will be addressed in a future release.)
4584\end{cfuncdesc}
4585
4586\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
4587Destroy the (sub-)interpreter represented by the given thread state.
4588The given thread state must be the current thread state. See the
4589discussion of thread states below. When the call returns, the current
Guido van Rossum580aa8d1997-11-25 15:34:51 +00004590thread state is \NULL{}. All thread states associated with this
Guido van Rossum4a944d71997-08-14 20:35:38 +00004591interpreted are destroyed. (The global interpreter lock must be held
4592before calling this function and is still held when it returns.)
Fred Drake659ebfa2000-04-03 15:42:13 +00004593\cfunction{Py_Finalize()}\ttindex{Py_Finalize()} will destroy all
4594sub-interpreters that haven't been explicitly destroyed at that point.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004595\end{cfuncdesc}
4596
4597\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
Fred Drake659ebfa2000-04-03 15:42:13 +00004598This function should be called before
4599\cfunction{Py_Initialize()}\ttindex{Py_Initialize()} is called
Guido van Rossum4a944d71997-08-14 20:35:38 +00004600for the first time, if it is called at all. It tells the interpreter
Fred Drake659ebfa2000-04-03 15:42:13 +00004601the value of the \code{argv[0]} argument to the
4602\cfunction{main()}\ttindex{main()} function of the program. This is
4603used by \cfunction{Py_GetPath()}\ttindex{Py_GetPath()} and some other
Guido van Rossum4a944d71997-08-14 20:35:38 +00004604functions below to find the Python run-time libraries relative to the
Fred Drakea8455ab2000-06-16 19:58:42 +00004605interpreter executable. The default value is \code{'python'}. The
Guido van Rossum4a944d71997-08-14 20:35:38 +00004606argument should point to a zero-terminated character string in static
4607storage whose contents will not change for the duration of the
4608program's execution. No code in the Python interpreter will change
4609the contents of this storage.
4610\end{cfuncdesc}
4611
Fred Drakec6fa34e1998-04-02 06:47:24 +00004612\begin{cfuncdesc}{char*}{Py_GetProgramName}{}
Fred Drake659ebfa2000-04-03 15:42:13 +00004613Return the program name set with
4614\cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, or the
Guido van Rossum4a944d71997-08-14 20:35:38 +00004615default. The returned string points into static storage; the caller
4616should not modify its value.
4617\end{cfuncdesc}
4618
Fred Drakec6fa34e1998-04-02 06:47:24 +00004619\begin{cfuncdesc}{char*}{Py_GetPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00004620Return the \emph{prefix} for installed platform-independent files. This
Guido van Rossum4a944d71997-08-14 20:35:38 +00004621is derived through a number of complicated rules from the program name
Fred Drakee058b4f1998-02-16 06:15:35 +00004622set with \cfunction{Py_SetProgramName()} and some environment variables;
Fred Drakea8455ab2000-06-16 19:58:42 +00004623for example, if the program name is \code{'/usr/local/bin/python'},
4624the prefix is \code{'/usr/local'}. The returned string points into
Guido van Rossum4a944d71997-08-14 20:35:38 +00004625static storage; the caller should not modify its value. This
Fred Drakec94d9341998-04-12 02:39:13 +00004626corresponds to the \makevar{prefix} variable in the top-level
Fred Drakea8455ab2000-06-16 19:58:42 +00004627\file{Makefile} and the \longprogramopt{prefix} argument to the
Fred Drakee058b4f1998-02-16 06:15:35 +00004628\program{configure} script at build time. The value is available to
Fred Drakeb0a78731998-01-13 18:51:10 +00004629Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See
Guido van Rossum4a944d71997-08-14 20:35:38 +00004630also the next function.
4631\end{cfuncdesc}
4632
Fred Drakec6fa34e1998-04-02 06:47:24 +00004633\begin{cfuncdesc}{char*}{Py_GetExecPrefix}{}
Fred Drakee058b4f1998-02-16 06:15:35 +00004634Return the \emph{exec-prefix} for installed platform-\emph{de}pendent
Guido van Rossum4a944d71997-08-14 20:35:38 +00004635files. This is derived through a number of complicated rules from the
Fred Drakee058b4f1998-02-16 06:15:35 +00004636program name set with \cfunction{Py_SetProgramName()} and some environment
Guido van Rossum4a944d71997-08-14 20:35:38 +00004637variables; for example, if the program name is
Fred Drakea8455ab2000-06-16 19:58:42 +00004638\code{'/usr/local/bin/python'}, the exec-prefix is
4639\code{'/usr/local'}. The returned string points into static storage;
Guido van Rossum4a944d71997-08-14 20:35:38 +00004640the caller should not modify its value. This corresponds to the
Fred Drakec94d9341998-04-12 02:39:13 +00004641\makevar{exec_prefix} variable in the top-level \file{Makefile} and the
Fred Drakea8455ab2000-06-16 19:58:42 +00004642\longprogramopt{exec-prefix} argument to the
Fred Drake310ee611999-11-09 17:31:42 +00004643\program{configure} script at build time. The value is available to
4644Python code as \code{sys.exec_prefix}. It is only useful on \UNIX{}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004645
4646Background: The exec-prefix differs from the prefix when platform
4647dependent files (such as executables and shared libraries) are
4648installed in a different directory tree. In a typical installation,
4649platform dependent files may be installed in the
Fred Drakea8455ab2000-06-16 19:58:42 +00004650\file{/usr/local/plat} subtree while platform independent may be
4651installed in \file{/usr/local}.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004652
4653Generally speaking, a platform is a combination of hardware and
4654software families, e.g. Sparc machines running the Solaris 2.x
4655operating system are considered the same platform, but Intel machines
4656running Solaris 2.x are another platform, and Intel machines running
4657Linux are yet another platform. Different major revisions of the same
Fred Drakeb0a78731998-01-13 18:51:10 +00004658operating system generally also form different platforms. Non-\UNIX{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004659operating systems are a different story; the installation strategies
4660on those systems are so different that the prefix and exec-prefix are
4661meaningless, and set to the empty string. Note that compiled Python
4662bytecode files are platform independent (but not independent from the
4663Python version by which they were compiled!).
4664
Fred Drakee058b4f1998-02-16 06:15:35 +00004665System administrators will know how to configure the \program{mount} or
Fred Drakea8455ab2000-06-16 19:58:42 +00004666\program{automount} programs to share \file{/usr/local} between platforms
4667while having \file{/usr/local/plat} be a different filesystem for each
Guido van Rossum4a944d71997-08-14 20:35:38 +00004668platform.
4669\end{cfuncdesc}
4670
Fred Drakec6fa34e1998-04-02 06:47:24 +00004671\begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004672Return the full program name of the Python executable; this is
4673computed as a side-effect of deriving the default module search path
Fred Drake659ebfa2000-04-03 15:42:13 +00004674from the program name (set by
4675\cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()} above).
4676The returned string points into static storage; the caller should not
Guido van Rossum4a944d71997-08-14 20:35:38 +00004677modify its value. The value is available to Python code as
Guido van Rossum42cefd01997-10-05 15:27:29 +00004678\code{sys.executable}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004679\withsubitem{(in module sys)}{\ttindex{executable}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004680\end{cfuncdesc}
4681
Fred Drakec6fa34e1998-04-02 06:47:24 +00004682\begin{cfuncdesc}{char*}{Py_GetPath}{}
Fred Drake4de05a91998-02-16 14:25:26 +00004683\indexiii{module}{search}{path}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004684Return the default module search path; this is computed from the
Fred Drakee058b4f1998-02-16 06:15:35 +00004685program name (set by \cfunction{Py_SetProgramName()} above) and some
Guido van Rossum4a944d71997-08-14 20:35:38 +00004686environment variables. The returned string consists of a series of
4687directory names separated by a platform dependent delimiter character.
Fred Drakef8830d11998-04-23 14:06:01 +00004688The delimiter character is \character{:} on \UNIX{}, \character{;} on
Fred Drake659ebfa2000-04-03 15:42:13 +00004689DOS/Windows, and \character{\e n} (the \ASCII{} newline character) on
Fred Drakee5bc4971998-02-12 23:36:49 +00004690Macintosh. The returned string points into static storage; the caller
Guido van Rossum4a944d71997-08-14 20:35:38 +00004691should not modify its value. The value is available to Python code
Fred Drake659ebfa2000-04-03 15:42:13 +00004692as the list \code{sys.path}\withsubitem{(in module sys)}{\ttindex{path}},
4693which may be modified to change the future search path for loaded
4694modules.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004695
4696% XXX should give the exact rules
4697\end{cfuncdesc}
4698
Fred Drakec6fa34e1998-04-02 06:47:24 +00004699\begin{cfuncdesc}{const char*}{Py_GetVersion}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004700Return the version of this Python interpreter. This is a string that
4701looks something like
4702
Guido van Rossum09270b51997-08-15 18:57:32 +00004703\begin{verbatim}
Fred Drakee058b4f1998-02-16 06:15:35 +00004704"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
Guido van Rossum09270b51997-08-15 18:57:32 +00004705\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004706
4707The first word (up to the first space character) is the current Python
4708version; the first three characters are the major and minor version
4709separated by a period. The returned string points into static storage;
4710the caller should not modify its value. The value is available to
4711Python code as the list \code{sys.version}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004712\withsubitem{(in module sys)}{\ttindex{version}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004713\end{cfuncdesc}
4714
Fred Drakec6fa34e1998-04-02 06:47:24 +00004715\begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
Fred Drakeb0a78731998-01-13 18:51:10 +00004716Return the platform identifier for the current platform. On \UNIX{},
Guido van Rossum4a944d71997-08-14 20:35:38 +00004717this is formed from the ``official'' name of the operating system,
4718converted to lower case, followed by the major revision number; e.g.,
4719for Solaris 2.x, which is also known as SunOS 5.x, the value is
Fred Drakea8455ab2000-06-16 19:58:42 +00004720\code{'sunos5'}. On Macintosh, it is \code{'mac'}. On Windows, it
4721is \code{'win'}. The returned string points into static storage;
Guido van Rossum4a944d71997-08-14 20:35:38 +00004722the caller should not modify its value. The value is available to
4723Python code as \code{sys.platform}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004724\withsubitem{(in module sys)}{\ttindex{platform}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004725\end{cfuncdesc}
4726
Fred Drakec6fa34e1998-04-02 06:47:24 +00004727\begin{cfuncdesc}{const char*}{Py_GetCopyright}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004728Return the official copyright string for the current Python version,
4729for example
4730
Fred Drakea8455ab2000-06-16 19:58:42 +00004731\code{'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004732
4733The returned string points into static storage; the caller should not
4734modify its value. The value is available to Python code as the list
4735\code{sys.copyright}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004736\withsubitem{(in module sys)}{\ttindex{copyright}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004737\end{cfuncdesc}
4738
Fred Drakec6fa34e1998-04-02 06:47:24 +00004739\begin{cfuncdesc}{const char*}{Py_GetCompiler}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004740Return an indication of the compiler used to build the current Python
Fred Drakee058b4f1998-02-16 06:15:35 +00004741version, in square brackets, for example:
Guido van Rossum4a944d71997-08-14 20:35:38 +00004742
Fred Drakee058b4f1998-02-16 06:15:35 +00004743\begin{verbatim}
4744"[GCC 2.7.2.2]"
4745\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004746
4747The returned string points into static storage; the caller should not
4748modify its value. The value is available to Python code as part of
4749the variable \code{sys.version}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004750\withsubitem{(in module sys)}{\ttindex{version}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004751\end{cfuncdesc}
4752
Fred Drakec6fa34e1998-04-02 06:47:24 +00004753\begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004754Return information about the sequence number and build date and time
4755of the current Python interpreter instance, for example
4756
Guido van Rossum09270b51997-08-15 18:57:32 +00004757\begin{verbatim}
4758"#67, Aug 1 1997, 22:34:28"
4759\end{verbatim}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004760
4761The returned string points into static storage; the caller should not
4762modify its value. The value is available to Python code as part of
4763the variable \code{sys.version}.
Fred Drake659ebfa2000-04-03 15:42:13 +00004764\withsubitem{(in module sys)}{\ttindex{version}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004765\end{cfuncdesc}
4766
4767\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
Fred Drake659ebfa2000-04-03 15:42:13 +00004768Set \code{sys.argv} based on \var{argc} and \var{argv}. These
4769parameters are similar to those passed to the program's
4770\cfunction{main()}\ttindex{main()} function with the difference that
4771the first entry should refer to the script file to be executed rather
4772than the executable hosting the Python interpreter. If there isn't a
4773script that will be run, the first entry in \var{argv} can be an empty
4774string. If this function fails to initialize \code{sys.argv}, a fatal
4775condition is signalled using
4776\cfunction{Py_FatalError()}\ttindex{Py_FatalError()}.
4777\withsubitem{(in module sys)}{\ttindex{argv}}
4778% XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
4779% check w/ Guido.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004780\end{cfuncdesc}
4781
4782% XXX Other PySys thingies (doesn't really belong in this chapter)
4783
Fred Drakeefd146c1999-02-15 15:30:45 +00004784\section{Thread State and the Global Interpreter Lock
4785 \label{threads}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00004786
Fred Drake659ebfa2000-04-03 15:42:13 +00004787\index{global interpreter lock}
4788\index{interpreter lock}
4789\index{lock, interpreter}
4790
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004791The Python interpreter is not fully thread safe. In order to support
4792multi-threaded Python programs, there's a global lock that must be
4793held by the current thread before it can safely access Python objects.
4794Without the lock, even the simplest operations could cause problems in
Fred Drake7baf3d41998-02-20 00:45:52 +00004795a multi-threaded program: for example, when two threads simultaneously
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004796increment the reference count of the same object, the reference count
4797could end up being incremented only once instead of twice.
4798
4799Therefore, the rule exists that only the thread that has acquired the
4800global interpreter lock may operate on Python objects or call Python/C
4801API functions. In order to support multi-threaded Python programs,
Fred Drake659ebfa2000-04-03 15:42:13 +00004802the interpreter regularly releases and reacquires the lock --- by
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004803default, every ten bytecode instructions (this can be changed with
Fred Drake659ebfa2000-04-03 15:42:13 +00004804\withsubitem{(in module sys)}{\ttindex{setcheckinterval()}}
Fred Drakee058b4f1998-02-16 06:15:35 +00004805\function{sys.setcheckinterval()}). The lock is also released and
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004806reacquired around potentially blocking I/O operations like reading or
4807writing a file, so that other threads can run while the thread that
4808requests the I/O is waiting for the I/O operation to complete.
4809
4810The Python interpreter needs to keep some bookkeeping information
Fred Drakee058b4f1998-02-16 06:15:35 +00004811separate per thread --- for this it uses a data structure called
Fred Drake659ebfa2000-04-03 15:42:13 +00004812\ctype{PyThreadState}\ttindex{PyThreadState}. This is new in Python
48131.5; in earlier versions, such state was stored in global variables,
4814and switching threads could cause problems. In particular, exception
4815handling is now thread safe, when the application uses
4816\withsubitem{(in module sys)}{\ttindex{exc_info()}}
4817\function{sys.exc_info()} to access the exception last raised in the
4818current thread.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004819
4820There's one global variable left, however: the pointer to the current
Fred Drake659ebfa2000-04-03 15:42:13 +00004821\ctype{PyThreadState}\ttindex{PyThreadState} structure. While most
4822thread packages have a way to store ``per-thread global data,''
4823Python's internal platform independent thread abstraction doesn't
4824support this yet. Therefore, the current thread state must be
4825manipulated explicitly.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004826
4827This is easy enough in most cases. Most code manipulating the global
4828interpreter lock has the following simple structure:
4829
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004830\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004831Save the thread state in a local variable.
4832Release the interpreter lock.
4833...Do some blocking I/O operation...
4834Reacquire the interpreter lock.
4835Restore the thread state from the local variable.
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004836\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004837
4838This is so common that a pair of macros exists to simplify it:
4839
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004840\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004841Py_BEGIN_ALLOW_THREADS
4842...Do some blocking I/O operation...
4843Py_END_ALLOW_THREADS
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004844\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004845
Fred Drake659ebfa2000-04-03 15:42:13 +00004846The \code{Py_BEGIN_ALLOW_THREADS}\ttindex{Py_BEGIN_ALLOW_THREADS} macro
4847opens a new block and declares a hidden local variable; the
4848\code{Py_END_ALLOW_THREADS}\ttindex{Py_END_ALLOW_THREADS} macro closes
Fred Drakee058b4f1998-02-16 06:15:35 +00004849the block. Another advantage of using these two macros is that when
4850Python is compiled without thread support, they are defined empty,
4851thus saving the thread state and lock manipulations.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004852
4853When thread support is enabled, the block above expands to the
4854following code:
4855
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004856\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004857 PyThreadState *_save;
Fred Drake659ebfa2000-04-03 15:42:13 +00004858
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004859 _save = PyEval_SaveThread();
4860 ...Do some blocking I/O operation...
4861 PyEval_RestoreThread(_save);
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004862\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004863
4864Using even lower level primitives, we can get roughly the same effect
4865as follows:
4866
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004867\begin{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004868 PyThreadState *_save;
Fred Drake659ebfa2000-04-03 15:42:13 +00004869
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004870 _save = PyThreadState_Swap(NULL);
4871 PyEval_ReleaseLock();
4872 ...Do some blocking I/O operation...
4873 PyEval_AcquireLock();
4874 PyThreadState_Swap(_save);
Guido van Rossum9faf4c51997-10-07 14:38:54 +00004875\end{verbatim}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004876
4877There are some subtle differences; in particular,
Fred Drake659ebfa2000-04-03 15:42:13 +00004878\cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()} saves
4879and restores the value of the global variable
4880\cdata{errno}\ttindex{errno}, since the lock manipulation does not
Fred Drakef8830d11998-04-23 14:06:01 +00004881guarantee that \cdata{errno} is left alone. Also, when thread support
Fred Drake659ebfa2000-04-03 15:42:13 +00004882is disabled,
4883\cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} and
Fred Drakee058b4f1998-02-16 06:15:35 +00004884\cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this
Fred Drake659ebfa2000-04-03 15:42:13 +00004885case, \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} and
4886\cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()} are not
4887available. This is done so that dynamically loaded extensions
4888compiled with thread support enabled can be loaded by an interpreter
4889that was compiled with disabled thread support.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004890
4891The global interpreter lock is used to protect the pointer to the
4892current thread state. When releasing the lock and saving the thread
4893state, the current thread state pointer must be retrieved before the
4894lock is released (since another thread could immediately acquire the
4895lock and store its own thread state in the global variable).
Fred Drakeffe58ca2000-09-29 17:31:54 +00004896Conversely, when acquiring the lock and restoring the thread state,
4897the lock must be acquired before storing the thread state pointer.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004898
4899Why am I going on with so much detail about this? Because when
Fred Drake659ebfa2000-04-03 15:42:13 +00004900threads are created from C, they don't have the global interpreter
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004901lock, nor is there a thread state data structure for them. Such
4902threads must bootstrap themselves into existence, by first creating a
4903thread state data structure, then acquiring the lock, and finally
4904storing their thread state pointer, before they can start using the
4905Python/C API. When they are done, they should reset the thread state
4906pointer, release the lock, and finally free their thread state data
4907structure.
4908
4909When creating a thread data structure, you need to provide an
4910interpreter state data structure. The interpreter state data
4911structure hold global data that is shared by all threads in an
4912interpreter, for example the module administration
4913(\code{sys.modules}). Depending on your needs, you can either create
4914a new interpreter state data structure, or share the interpreter state
4915data structure used by the Python main thread (to access the latter,
Fred Drakef8830d11998-04-23 14:06:01 +00004916you must obtain the thread state and access its \member{interp} member;
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004917this must be done by a thread that is created by Python or by the main
4918thread after Python is initialized).
4919
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004920
4921\begin{ctypedesc}{PyInterpreterState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004922This data structure represents the state shared by a number of
4923cooperating threads. Threads belonging to the same interpreter
4924share their module administration and a few other internal items.
4925There are no public members in this structure.
4926
4927Threads belonging to different interpreters initially share nothing,
4928except process state like available memory, open file descriptors and
4929such. The global interpreter lock is also shared by all threads,
4930regardless of to which interpreter they belong.
4931\end{ctypedesc}
4932
4933\begin{ctypedesc}{PyThreadState}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004934This data structure represents the state of a single thread. The only
Fred Drakef8830d11998-04-23 14:06:01 +00004935public data member is \ctype{PyInterpreterState *}\member{interp},
4936which points to this thread's interpreter state.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004937\end{ctypedesc}
4938
4939\begin{cfuncdesc}{void}{PyEval_InitThreads}{}
4940Initialize and acquire the global interpreter lock. It should be
4941called in the main thread before creating a second thread or engaging
Fred Drakee058b4f1998-02-16 06:15:35 +00004942in any other thread operations such as
Fred Drake659ebfa2000-04-03 15:42:13 +00004943\cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} or
4944\code{PyEval_ReleaseThread(\var{tstate})}\ttindex{PyEval_ReleaseThread()}.
4945It is not needed before calling
4946\cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} or
4947\cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004948
4949This is a no-op when called for a second time. It is safe to call
Fred Drake659ebfa2000-04-03 15:42:13 +00004950this function before calling
4951\cfunction{Py_Initialize()}\ttindex{Py_Initialize()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004952
4953When only the main thread exists, no lock operations are needed. This
4954is a common situation (most Python programs do not use threads), and
4955the lock operations slow the interpreter down a bit. Therefore, the
4956lock is not created initially. This situation is equivalent to having
4957acquired the lock: when there is only a single thread, all object
4958accesses are safe. Therefore, when this function initializes the
Fred Drake4de05a91998-02-16 14:25:26 +00004959lock, it also acquires it. Before the Python
4960\module{thread}\refbimodindex{thread} module creates a new thread,
4961knowing that either it has the lock or the lock hasn't been created
4962yet, it calls \cfunction{PyEval_InitThreads()}. When this call
4963returns, it is guaranteed that the lock has been created and that it
4964has acquired it.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004965
4966It is \strong{not} safe to call this function when it is unknown which
4967thread (if any) currently has the global interpreter lock.
4968
4969This function is not available when thread support is disabled at
4970compile time.
4971\end{cfuncdesc}
4972
Guido van Rossum4a944d71997-08-14 20:35:38 +00004973\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004974Acquire the global interpreter lock. The lock must have been created
4975earlier. If this thread already has the lock, a deadlock ensues.
4976This function is not available when thread support is disabled at
4977compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004978\end{cfuncdesc}
4979
4980\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004981Release the global interpreter lock. The lock must have been created
4982earlier. This function is not available when thread support is
Fred Drakee058b4f1998-02-16 06:15:35 +00004983disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004984\end{cfuncdesc}
4985
4986\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004987Acquire the global interpreter lock and then set the current thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00004988state to \var{tstate}, which should not be \NULL{}. The lock must
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004989have been created earlier. If this thread already has the lock,
4990deadlock ensues. This function is not available when thread support
Fred Drakee058b4f1998-02-16 06:15:35 +00004991is disabled at compile time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00004992\end{cfuncdesc}
4993
4994\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
Guido van Rossum580aa8d1997-11-25 15:34:51 +00004995Reset the current thread state to \NULL{} and release the global
Guido van Rossumc44d3d61997-10-06 05:10:47 +00004996interpreter lock. The lock must have been created earlier and must be
4997held by the current thread. The \var{tstate} argument, which must not
Guido van Rossum580aa8d1997-11-25 15:34:51 +00004998be \NULL{}, is only used to check that it represents the current
Fred Drakee058b4f1998-02-16 06:15:35 +00004999thread state --- if it isn't, a fatal error is reported. This
5000function is not available when thread support is disabled at compile
5001time.
Guido van Rossum4a944d71997-08-14 20:35:38 +00005002\end{cfuncdesc}
5003
Fred Drakec6fa34e1998-04-02 06:47:24 +00005004\begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005005Release the interpreter lock (if it has been created and thread
Guido van Rossum580aa8d1997-11-25 15:34:51 +00005006support is enabled) and reset the thread state to \NULL{},
5007returning the previous thread state (which is not \NULL{}). If
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005008the lock has been created, the current thread must have acquired it.
5009(This function is available even when thread support is disabled at
5010compile time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00005011\end{cfuncdesc}
5012
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005013\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005014Acquire the interpreter lock (if it has been created and thread
5015support is enabled) and set the thread state to \var{tstate}, which
Guido van Rossum580aa8d1997-11-25 15:34:51 +00005016must not be \NULL{}. If the lock has been created, the current
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005017thread must not have acquired it, otherwise deadlock ensues. (This
5018function is available even when thread support is disabled at compile
5019time.)
Guido van Rossum4a944d71997-08-14 20:35:38 +00005020\end{cfuncdesc}
5021
Fred Drake659ebfa2000-04-03 15:42:13 +00005022The following macros are normally used without a trailing semicolon;
5023look for example usage in the Python source distribution.
5024
5025\begin{csimplemacrodesc}{Py_BEGIN_ALLOW_THREADS}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005026This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00005027\samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005028Note that it contains an opening brace; it must be matched with a
5029following \code{Py_END_ALLOW_THREADS} macro. See above for further
5030discussion of this macro. It is a no-op when thread support is
5031disabled at compile time.
Fred Drake659ebfa2000-04-03 15:42:13 +00005032\end{csimplemacrodesc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005033
Fred Drake659ebfa2000-04-03 15:42:13 +00005034\begin{csimplemacrodesc}{Py_END_ALLOW_THREADS}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005035This macro expands to
Fred Drakee058b4f1998-02-16 06:15:35 +00005036\samp{PyEval_RestoreThread(_save); \}}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005037Note that it contains a closing brace; it must be matched with an
5038earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further
5039discussion of this macro. It is a no-op when thread support is
5040disabled at compile time.
Fred Drake659ebfa2000-04-03 15:42:13 +00005041\end{csimplemacrodesc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005042
Thomas Wouterse30ac572001-07-09 14:35:01 +00005043\begin{csimplemacrodesc}{Py_BLOCK_THREADS}
Fred Drakebab29652001-07-10 16:10:08 +00005044This macro expands to \samp{PyEval_RestoreThread(_save);}: it
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005045is equivalent to \code{Py_END_ALLOW_THREADS} without the closing
5046brace. It is a no-op when thread support is disabled at compile
5047time.
Fred Drake659ebfa2000-04-03 15:42:13 +00005048\end{csimplemacrodesc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005049
Thomas Wouterse30ac572001-07-09 14:35:01 +00005050\begin{csimplemacrodesc}{Py_UNBLOCK_THREADS}
Fred Drakebab29652001-07-10 16:10:08 +00005051This macro expands to \samp{_save = PyEval_SaveThread();}: it is
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005052equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace
5053and variable declaration. It is a no-op when thread support is
5054disabled at compile time.
Fred Drake659ebfa2000-04-03 15:42:13 +00005055\end{csimplemacrodesc}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005056
5057All of the following functions are only available when thread support
5058is enabled at compile time, and must be called only when the
Fred Drake9d20ac31998-02-16 15:27:08 +00005059interpreter lock has been created.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005060
Fred Drakec6fa34e1998-04-02 06:47:24 +00005061\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{}
Guido van Rossumed9dcc11998-08-07 18:28:03 +00005062Create a new interpreter state object. The interpreter lock need not
5063be held, but may be held if it is necessary to serialize calls to this
5064function.
Guido van Rossum4a944d71997-08-14 20:35:38 +00005065\end{cfuncdesc}
5066
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005067\begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp}
5068Reset all information in an interpreter state object. The interpreter
5069lock must be held.
Guido van Rossum4a944d71997-08-14 20:35:38 +00005070\end{cfuncdesc}
5071
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005072\begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp}
5073Destroy an interpreter state object. The interpreter lock need not be
5074held. The interpreter state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00005075call to \cfunction{PyInterpreterState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005076\end{cfuncdesc}
5077
Fred Drakec6fa34e1998-04-02 06:47:24 +00005078\begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005079Create a new thread state object belonging to the given interpreter
Guido van Rossumed9dcc11998-08-07 18:28:03 +00005080object. The interpreter lock need not be held, but may be held if it
5081is necessary to serialize calls to this function.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005082\end{cfuncdesc}
5083
5084\begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate}
5085Reset all information in a thread state object. The interpreter lock
5086must be held.
5087\end{cfuncdesc}
5088
5089\begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate}
5090Destroy a thread state object. The interpreter lock need not be
5091held. The thread state must have been reset with a previous
Fred Drakee058b4f1998-02-16 06:15:35 +00005092call to \cfunction{PyThreadState_Clear()}.
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005093\end{cfuncdesc}
5094
Fred Drakec6fa34e1998-04-02 06:47:24 +00005095\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005096Return the current thread state. The interpreter lock must be held.
Guido van Rossum580aa8d1997-11-25 15:34:51 +00005097When the current thread state is \NULL{}, this issues a fatal
Guido van Rossum5b8a5231997-12-30 04:38:44 +00005098error (so that the caller needn't check for \NULL{}).
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005099\end{cfuncdesc}
5100
Fred Drakec6fa34e1998-04-02 06:47:24 +00005101\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate}
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005102Swap the current thread state with the thread state given by the
Guido van Rossum580aa8d1997-11-25 15:34:51 +00005103argument \var{tstate}, which may be \NULL{}. The interpreter lock
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005104must be held.
5105\end{cfuncdesc}
5106
Fred Drake24e62192001-05-21 15:56:55 +00005107\begin{cfuncdesc}{PyObject*}{PyThreadState_GetDict}{}
5108Return a dictionary in which extensions can store thread-specific
5109state information. Each extension should use a unique key to use to
5110store state in the dictionary. If this function returns \NULL, an
5111exception has been raised and the caller should allow it to
5112propogate.
5113\end{cfuncdesc}
5114
Guido van Rossumc44d3d61997-10-06 05:10:47 +00005115
Fred Drake68db7302001-07-17 19:48:30 +00005116\section{Profiling and Tracing \label{profiling}}
5117
5118\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
5119
5120The Python interpreter provides some low-level support for attaching
5121profiling and execution tracing facilities. These are used for
5122profiling, debugging, and coverage analysis tools.
5123
5124Starting with Python 2.2, the implementation of this facility was
5125substantially revised, and an interface from C was added. This C
5126interface allows the profiling or tracing code to avoid the overhead
5127of calling through Python-level callable objects, making a direct C
5128function call instead. The essential attributes of the facility have
5129not changed; the interface allows trace functions to be installed
5130per-thread, and the basic events reported to the trace function are
5131the same as had been reported to the Python-level trace functions in
5132previous versions.
5133
5134\begin{ctypedesc}[Py_tracefunc]{int (*Py_tracefunc)(PyObject *obj,
5135 PyFrameObject *frame, int what,
5136 PyObject *arg)}
5137 The type of the trace function registered using
5138 \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}.
5139 The first parameter is the object passed to the registration
5140 function,
5141\end{ctypedesc}
5142
5143\begin{cvardesc}{int}{PyTrace_CALL}
5144 The value of the \var{what} parameter to a \ctype{Py_tracefunc}
5145 function when a new function or method call is being reported.
5146\end{cvardesc}
5147
5148\begin{cvardesc}{int}{PyTrace_EXCEPT}
5149\end{cvardesc}
5150
5151\begin{cvardesc}{int}{PyTrace_LINE}
5152 The value passed as the \var{what} parameter to a trace function
5153 (but not a profiling function) when a line-number event is being
5154 reported.
5155\end{cvardesc}
5156
5157\begin{cvardesc}{int}{PyTrace_RETURN}
5158 The value for the \var{what} parameter to \ctype{Py_tracefunc}
5159 functions when a call is returning without propogating an exception.
5160\end{cvardesc}
5161
5162\begin{cfuncdesc}{void}{PyEval_SetProfile}{Py_tracefunc func, PyObject *obj}
Fred Drakef90490e2001-08-02 18:00:28 +00005163 Set the profiler function to \var{func}. The \var{obj} parameter is
5164 passed to the function as its first parameter, and may be any Python
5165 object, or \NULL. If the profile function needs to maintain state,
5166 using a different value for \var{obj} for each thread provides a
5167 convenient and thread-safe place to store it. The profile function
5168 is called for all monitored events except the line-number events.
Fred Drake68db7302001-07-17 19:48:30 +00005169\end{cfuncdesc}
5170
5171\begin{cfuncdesc}{void}{PyEval_SetTrace}{Py_tracefunc func, PyObject *obj}
Fred Drakef90490e2001-08-02 18:00:28 +00005172 Set the the tracing function to \var{func}. This is similar to
5173 \cfunction{PyEval_SetProfile()}, except the tracing function does
5174 receive line-number events.
Fred Drake68db7302001-07-17 19:48:30 +00005175\end{cfuncdesc}
5176
5177
Fred Drake01978582001-08-08 19:14:53 +00005178\section{Advanced Debugger Support \label{advanced-debugging}}
5179\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
5180
5181These functions are only intended to be used by advanced debugging
5182tools.
5183
5184\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_Head}{}
5185Return the interpreter state object at the head of the list of all
5186such objects.
5187\versionadded{2.2}
5188\end{cfuncdesc}
5189
5190\begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_Next}{PyInterpreterState *interp}
5191Return the next interpreter state object after \var{interp} from the
5192list of all such objects.
5193\versionadded{2.2}
5194\end{cfuncdesc}
5195
5196\begin{cfuncdesc}{PyThreadState *}{PyInterpreterState_ThreadHead}{PyInterpreterState *interp}
5197Return the a pointer to the first \ctype{PyThreadState} object in the
5198list of threads associated with the interpreter \var{interp}.
5199\versionadded{2.2}
5200\end{cfuncdesc}
5201
5202\begin{cfuncdesc}{PyThreadState*}{PyThreadState_Next}{PyThreadState *tstate}
5203Return the next thread state object after \var{tstate} from the list
5204of all such objects belonging to the same \ctype{PyInterpreterState}
5205object.
5206\versionadded{2.2}
5207\end{cfuncdesc}
5208
5209
Fred Drake659ebfa2000-04-03 15:42:13 +00005210\chapter{Memory Management \label{memory}}
5211\sectionauthor{Vladimir Marangozov}{Vladimir.Marangozov@inrialpes.fr}
5212
5213
5214\section{Overview \label{memoryOverview}}
5215
5216Memory management in Python involves a private heap containing all
5217Python objects and data structures. The management of this private
5218heap is ensured internally by the \emph{Python memory manager}. The
5219Python memory manager has different components which deal with various
5220dynamic storage management aspects, like sharing, segmentation,
5221preallocation or caching.
5222
5223At the lowest level, a raw memory allocator ensures that there is
5224enough room in the private heap for storing all Python-related data
5225by interacting with the memory manager of the operating system. On top
5226of the raw memory allocator, several object-specific allocators
5227operate on the same heap and implement distinct memory management
5228policies adapted to the peculiarities of every object type. For
5229example, integer objects are managed differently within the heap than
5230strings, tuples or dictionaries because integers imply different
5231storage requirements and speed/space tradeoffs. The Python memory
5232manager thus delegates some of the work to the object-specific
5233allocators, but ensures that the latter operate within the bounds of
5234the private heap.
5235
5236It is important to understand that the management of the Python heap
5237is performed by the interpreter itself and that the user has no
5238control on it, even if she regularly manipulates object pointers to
5239memory blocks inside that heap. The allocation of heap space for
5240Python objects and other internal buffers is performed on demand by
5241the Python memory manager through the Python/C API functions listed in
5242this document.
5243
5244To avoid memory corruption, extension writers should never try to
5245operate on Python objects with the functions exported by the C
5246library: \cfunction{malloc()}\ttindex{malloc()},
5247\cfunction{calloc()}\ttindex{calloc()},
5248\cfunction{realloc()}\ttindex{realloc()} and
5249\cfunction{free()}\ttindex{free()}. This will result in
5250mixed calls between the C allocator and the Python memory manager
5251with fatal consequences, because they implement different algorithms
5252and operate on different heaps. However, one may safely allocate and
5253release memory blocks with the C library allocator for individual
5254purposes, as shown in the following example:
5255
5256\begin{verbatim}
5257 PyObject *res;
5258 char *buf = (char *) malloc(BUFSIZ); /* for I/O */
5259
5260 if (buf == NULL)
5261 return PyErr_NoMemory();
5262 ...Do some I/O operation involving buf...
5263 res = PyString_FromString(buf);
5264 free(buf); /* malloc'ed */
5265 return res;
5266\end{verbatim}
5267
5268In this example, the memory request for the I/O buffer is handled by
5269the C library allocator. The Python memory manager is involved only
5270in the allocation of the string object returned as a result.
5271
5272In most situations, however, it is recommended to allocate memory from
5273the Python heap specifically because the latter is under control of
5274the Python memory manager. For example, this is required when the
5275interpreter is extended with new object types written in C. Another
5276reason for using the Python heap is the desire to \emph{inform} the
5277Python memory manager about the memory needs of the extension module.
5278Even when the requested memory is used exclusively for internal,
5279highly-specific purposes, delegating all memory requests to the Python
5280memory manager causes the interpreter to have a more accurate image of
5281its memory footprint as a whole. Consequently, under certain
5282circumstances, the Python memory manager may or may not trigger
5283appropriate actions, like garbage collection, memory compaction or
5284other preventive procedures. Note that by using the C library
5285allocator as shown in the previous example, the allocated memory for
5286the I/O buffer escapes completely the Python memory manager.
5287
5288
5289\section{Memory Interface \label{memoryInterface}}
5290
5291The following function sets, modeled after the ANSI C standard, are
5292available for allocating and releasing memory from the Python heap:
5293
5294
Fred Drake7d45d342000-08-11 17:07:32 +00005295\begin{cfuncdesc}{void*}{PyMem_Malloc}{size_t n}
5296Allocates \var{n} bytes and returns a pointer of type \ctype{void*} to
Fred Drakebab29652001-07-10 16:10:08 +00005297the allocated memory, or \NULL{} if the request fails. Requesting zero
Fred Drake659ebfa2000-04-03 15:42:13 +00005298bytes returns a non-\NULL{} pointer.
Fred Drakebab29652001-07-10 16:10:08 +00005299The memory will not have been initialized in any way.
Fred Drake659ebfa2000-04-03 15:42:13 +00005300\end{cfuncdesc}
5301
Fred Drake7d45d342000-08-11 17:07:32 +00005302\begin{cfuncdesc}{void*}{PyMem_Realloc}{void *p, size_t n}
Fred Drake659ebfa2000-04-03 15:42:13 +00005303Resizes the memory block pointed to by \var{p} to \var{n} bytes. The
5304contents will be unchanged to the minimum of the old and the new
5305sizes. If \var{p} is \NULL{}, the call is equivalent to
Fred Drakebab29652001-07-10 16:10:08 +00005306\cfunction{PyMem_Malloc(\var{n})}; if \var{n} is equal to zero, the
5307memory block is resized but is not freed, and the returned pointer is
5308non-\NULL{}. Unless \var{p} is \NULL{}, it must have been returned by
5309a previous call to \cfunction{PyMem_Malloc()} or
5310\cfunction{PyMem_Realloc()}.
Fred Drake659ebfa2000-04-03 15:42:13 +00005311\end{cfuncdesc}
5312
Fred Drake7d45d342000-08-11 17:07:32 +00005313\begin{cfuncdesc}{void}{PyMem_Free}{void *p}
Fred Drake659ebfa2000-04-03 15:42:13 +00005314Frees the memory block pointed to by \var{p}, which must have been
5315returned by a previous call to \cfunction{PyMem_Malloc()} or
5316\cfunction{PyMem_Realloc()}. Otherwise, or if
5317\cfunction{PyMem_Free(p)} has been called before, undefined behaviour
5318occurs. If \var{p} is \NULL{}, no operation is performed.
5319\end{cfuncdesc}
5320
Fred Drake659ebfa2000-04-03 15:42:13 +00005321The following type-oriented macros are provided for convenience. Note
5322that \var{TYPE} refers to any C type.
5323
Fred Drakef913e542000-09-12 20:17:17 +00005324\begin{cfuncdesc}{\var{TYPE}*}{PyMem_New}{TYPE, size_t n}
Fred Drake659ebfa2000-04-03 15:42:13 +00005325Same as \cfunction{PyMem_Malloc()}, but allocates \code{(\var{n} *
5326sizeof(\var{TYPE}))} bytes of memory. Returns a pointer cast to
5327\ctype{\var{TYPE}*}.
Fred Drakebab29652001-07-10 16:10:08 +00005328The memory will not have been initialized in any way.
Fred Drake659ebfa2000-04-03 15:42:13 +00005329\end{cfuncdesc}
5330
Fred Drakef913e542000-09-12 20:17:17 +00005331\begin{cfuncdesc}{\var{TYPE}*}{PyMem_Resize}{void *p, TYPE, size_t n}
Fred Drake659ebfa2000-04-03 15:42:13 +00005332Same as \cfunction{PyMem_Realloc()}, but the memory block is resized
5333to \code{(\var{n} * sizeof(\var{TYPE}))} bytes. Returns a pointer
5334cast to \ctype{\var{TYPE}*}.
5335\end{cfuncdesc}
5336
Fred Drakef913e542000-09-12 20:17:17 +00005337\begin{cfuncdesc}{void}{PyMem_Del}{void *p}
Fred Drake659ebfa2000-04-03 15:42:13 +00005338Same as \cfunction{PyMem_Free()}.
5339\end{cfuncdesc}
5340
Fred Drakef913e542000-09-12 20:17:17 +00005341In addition, the following macro sets are provided for calling the
5342Python memory allocator directly, without involving the C API functions
5343listed above. However, note that their use does not preserve binary
5344compatibility accross Python versions and is therefore deprecated in
5345extension modules.
5346
5347\cfunction{PyMem_MALLOC()}, \cfunction{PyMem_REALLOC()}, \cfunction{PyMem_FREE()}.
5348
5349\cfunction{PyMem_NEW()}, \cfunction{PyMem_RESIZE()}, \cfunction{PyMem_DEL()}.
5350
Fred Drake659ebfa2000-04-03 15:42:13 +00005351
5352\section{Examples \label{memoryExamples}}
5353
5354Here is the example from section \ref{memoryOverview}, rewritten so
5355that the I/O buffer is allocated from the Python heap by using the
5356first function set:
5357
5358\begin{verbatim}
5359 PyObject *res;
5360 char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
5361
5362 if (buf == NULL)
5363 return PyErr_NoMemory();
5364 /* ...Do some I/O operation involving buf... */
5365 res = PyString_FromString(buf);
5366 PyMem_Free(buf); /* allocated with PyMem_Malloc */
5367 return res;
5368\end{verbatim}
5369
Fred Drakef913e542000-09-12 20:17:17 +00005370The same code using the type-oriented function set:
Fred Drake659ebfa2000-04-03 15:42:13 +00005371
5372\begin{verbatim}
5373 PyObject *res;
Fred Drakef913e542000-09-12 20:17:17 +00005374 char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
Fred Drake659ebfa2000-04-03 15:42:13 +00005375
5376 if (buf == NULL)
5377 return PyErr_NoMemory();
5378 /* ...Do some I/O operation involving buf... */
5379 res = PyString_FromString(buf);
Fred Drakef913e542000-09-12 20:17:17 +00005380 PyMem_Del(buf); /* allocated with PyMem_New */
Fred Drake659ebfa2000-04-03 15:42:13 +00005381 return res;
5382\end{verbatim}
5383
Fred Drakef913e542000-09-12 20:17:17 +00005384Note that in the two examples above, the buffer is always
5385manipulated via functions belonging to the same set. Indeed, it
Fred Drake659ebfa2000-04-03 15:42:13 +00005386is required to use the same memory API family for a given
5387memory block, so that the risk of mixing different allocators is
5388reduced to a minimum. The following code sequence contains two errors,
5389one of which is labeled as \emph{fatal} because it mixes two different
5390allocators operating on different heaps.
5391
5392\begin{verbatim}
Fred Drakef913e542000-09-12 20:17:17 +00005393char *buf1 = PyMem_New(char, BUFSIZ);
Fred Drake659ebfa2000-04-03 15:42:13 +00005394char *buf2 = (char *) malloc(BUFSIZ);
5395char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
5396...
Fred Drakef913e542000-09-12 20:17:17 +00005397PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */
Fred Drake659ebfa2000-04-03 15:42:13 +00005398free(buf2); /* Right -- allocated via malloc() */
Fred Drakef913e542000-09-12 20:17:17 +00005399free(buf1); /* Fatal -- should be PyMem_Del() */
Fred Drake659ebfa2000-04-03 15:42:13 +00005400\end{verbatim}
5401
5402In addition to the functions aimed at handling raw memory blocks from
5403the Python heap, objects in Python are allocated and released with
Fred Drakef913e542000-09-12 20:17:17 +00005404\cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()} and
5405\cfunction{PyObject_Del()}, or with their corresponding macros
5406\cfunction{PyObject_NEW()}, \cfunction{PyObject_NEW_VAR()} and
Fred Drakee06f0f92000-06-30 15:52:39 +00005407\cfunction{PyObject_DEL()}.
Fred Drake659ebfa2000-04-03 15:42:13 +00005408
Fred Drakee06f0f92000-06-30 15:52:39 +00005409These will be explained in the next chapter on defining and
5410implementing new object types in C.
Fred Drake659ebfa2000-04-03 15:42:13 +00005411
5412
Fred Drakeefd146c1999-02-15 15:30:45 +00005413\chapter{Defining New Object Types \label{newTypes}}
Guido van Rossum4a944d71997-08-14 20:35:38 +00005414
Fred Drake88fdaa72001-07-20 20:56:11 +00005415
5416\section{Allocating Objects on the Heap
5417 \label{allocating-objects}}
5418
Fred Drakec6fa34e1998-04-02 06:47:24 +00005419\begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type}
Fred Drakee058b4f1998-02-16 06:15:35 +00005420\end{cfuncdesc}
5421
Fred Drakef913e542000-09-12 20:17:17 +00005422\begin{cfuncdesc}{PyVarObject*}{_PyObject_NewVar}{PyTypeObject *type, int size}
Fred Drakee058b4f1998-02-16 06:15:35 +00005423\end{cfuncdesc}
5424
Fred Drakef913e542000-09-12 20:17:17 +00005425\begin{cfuncdesc}{void}{_PyObject_Del}{PyObject *op}
Fred Drakee058b4f1998-02-16 06:15:35 +00005426\end{cfuncdesc}
5427
Fred Drakef913e542000-09-12 20:17:17 +00005428\begin{cfuncdesc}{PyObject*}{PyObject_Init}{PyObject *op,
Fred Drakebab29652001-07-10 16:10:08 +00005429 PyTypeObject *type}
5430 Initialize a newly-allocated object \var{op} with its type and
5431 initial reference. Returns the initialized object. If \var{type}
5432 indicates that the object participates in the cyclic garbage
5433 detector, it it added to the detector's set of observed objects.
5434 Other fields of the object are not affected.
Fred Drakef913e542000-09-12 20:17:17 +00005435\end{cfuncdesc}
5436
5437\begin{cfuncdesc}{PyVarObject*}{PyObject_InitVar}{PyVarObject *op,
Fred Drakebab29652001-07-10 16:10:08 +00005438 PyTypeObject *type, int size}
5439 This does everything \cfunction{PyObject_Init()} does, and also
5440 initializes the length information for a variable-size object.
Fred Drakef913e542000-09-12 20:17:17 +00005441\end{cfuncdesc}
5442
5443\begin{cfuncdesc}{\var{TYPE}*}{PyObject_New}{TYPE, PyTypeObject *type}
Fred Drakebab29652001-07-10 16:10:08 +00005444 Allocate a new Python object using the C structure type \var{TYPE}
5445 and the Python type object \var{type}. Fields not defined by the
5446 Python object header are not initialized; the object's reference
5447 count will be one. The size of the memory
5448 allocation is determined from the \member{tp_basicsize} field of the
5449 type object.
Fred Drakef913e542000-09-12 20:17:17 +00005450\end{cfuncdesc}
5451
5452\begin{cfuncdesc}{\var{TYPE}*}{PyObject_NewVar}{TYPE, PyTypeObject *type,
5453 int size}
Fred Drakebab29652001-07-10 16:10:08 +00005454 Allocate a new Python object using the C structure type \var{TYPE}
5455 and the Python type object \var{type}. Fields not defined by the
5456 Python object header are not initialized. The allocated memory
5457 allows for the \var{TYPE} structure plus \var{size} fields of the
5458 size given by the \member{tp_itemsize} field of \var{type}. This is
5459 useful for implementing objects like tuples, which are able to
5460 determine their size at construction time. Embedding the array of
5461 fields into the same allocation decreases the number of allocations,
5462 improving the memory management efficiency.
Fred Drakef913e542000-09-12 20:17:17 +00005463\end{cfuncdesc}
5464
5465\begin{cfuncdesc}{void}{PyObject_Del}{PyObject *op}
Fred Drakebab29652001-07-10 16:10:08 +00005466 Releases memory allocated to an object using
5467 \cfunction{PyObject_New()} or \cfunction{PyObject_NewVar()}. This
5468 is normally called from the \member{tp_dealloc} handler specified in
5469 the object's type. The fields of the object should not be accessed
5470 after this call as the memory is no longer a valid Python object.
Fred Drakef913e542000-09-12 20:17:17 +00005471\end{cfuncdesc}
5472
5473\begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW}{TYPE, PyTypeObject *type}
Fred Drakebab29652001-07-10 16:10:08 +00005474 Macro version of \cfunction{PyObject_New()}, to gain performance at
5475 the expense of safety. This does not check \var{type} for a \NULL{}
5476 value.
Fred Drakef913e542000-09-12 20:17:17 +00005477\end{cfuncdesc}
5478
5479\begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW_VAR}{TYPE, PyTypeObject *type,
5480 int size}
Fred Drakebab29652001-07-10 16:10:08 +00005481 Macro version of \cfunction{PyObject_NewVar()}, to gain performance
5482 at the expense of safety. This does not check \var{type} for a
5483 \NULL{} value.
Fred Drakef913e542000-09-12 20:17:17 +00005484\end{cfuncdesc}
5485
5486\begin{cfuncdesc}{void}{PyObject_DEL}{PyObject *op}
Fred Drakebab29652001-07-10 16:10:08 +00005487 Macro version of \cfunction{PyObject_Del()}.
Fred Drakee058b4f1998-02-16 06:15:35 +00005488\end{cfuncdesc}
5489
Fred Drakeee814bf2000-11-28 22:34:32 +00005490\begin{cfuncdesc}{PyObject*}{Py_InitModule}{char *name,
5491 PyMethodDef *methods}
5492 Create a new module object based on a name and table of functions,
5493 returning the new module object.
5494\end{cfuncdesc}
5495
5496\begin{cfuncdesc}{PyObject*}{Py_InitModule3}{char *name,
5497 PyMethodDef *methods,
5498 char *doc}
5499 Create a new module object based on a name and table of functions,
5500 returning the new module object. If \var{doc} is non-\NULL, it will
5501 be used to define the docstring for the module.
5502\end{cfuncdesc}
5503
5504\begin{cfuncdesc}{PyObject*}{Py_InitModule4}{char *name,
5505 PyMethodDef *methods,
5506 char *doc, PyObject *self,
5507 int apiver}
5508 Create a new module object based on a name and table of functions,
5509 returning the new module object. If \var{doc} is non-\NULL, it will
5510 be used to define the docstring for the module. If \var{self} is
5511 non-\NULL, it will passed to the functions of the module as their
5512 (otherwise \NULL) first parameter. (This was added as an
5513 experimental feature, and there are no known uses in the current
5514 version of Python.) For \var{apiver}, the only value which should
5515 be passed is defined by the constant \constant{PYTHON_API_VERSION}.
5516
5517 \strong{Note:} Most uses of this function should probably be using
5518 the \cfunction{Py_InitModule3()} instead; only use this if you are
5519 sure you need it.
5520\end{cfuncdesc}
Guido van Rossum3c4378b1998-04-14 20:21:10 +00005521
Fred Drake659ebfa2000-04-03 15:42:13 +00005522DL_IMPORT
5523
Fred Drakebab29652001-07-10 16:10:08 +00005524\begin{cvardesc}{PyObject}{_Py_NoneStruct}
5525 Object which is visible in Python as \code{None}. This should only
5526 be accessed using the \code{Py_None} macro, which evaluates to a
5527 pointer to this object.
5528\end{cvardesc}
Fred Drake659ebfa2000-04-03 15:42:13 +00005529
5530
5531\section{Common Object Structures \label{common-structs}}
5532
Guido van Rossumae110af1997-05-22 20:11:52 +00005533PyObject, PyVarObject
5534
5535PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
5536
5537Typedefs:
5538unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
5539intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
Guido van Rossumae110af1997-05-22 20:11:52 +00005540destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
5541setattrofunc, cmpfunc, reprfunc, hashfunc
5542
Fred Drakea8455ab2000-06-16 19:58:42 +00005543\begin{ctypedesc}{PyCFunction}
5544Type of the functions used to implement most Python callables in C.
5545\end{ctypedesc}
5546
5547\begin{ctypedesc}{PyMethodDef}
5548Structure used to describe a method of an extension type. This
5549structure has four fields:
5550
5551\begin{tableiii}{l|l|l}{member}{Field}{C Type}{Meaning}
5552 \lineiii{ml_name}{char *}{name of the method}
5553 \lineiii{ml_meth}{PyCFunction}{pointer to the C implementation}
5554 \lineiii{ml_flags}{int}{flag bits indicating how the call should be
5555 constructed}
5556 \lineiii{ml_doc}{char *}{points to the contents of the docstring}
5557\end{tableiii}
5558\end{ctypedesc}
5559
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00005560The \var{ml_meth} is a C function pointer. The functions may be of
5561different types, but they always return \ctype{PyObject*}. If the
5562function is not of the \ctype{PyCFunction}, the compiler will require
5563a cast in the method table. Even though \ctype{PyCFunction} defines
5564the first parameter as \ctype{PyObject*}, it is common that the method
5565implementation uses a the specific C type of the \var{self} object.
5566
5567The flags can have the following values. Only METH_VARARGS and
5568METH_KEYWORDS can be combined; the others can't.
5569
5570\begin{datadesc}{METH_VARARGS}
5571
5572This is the typical calling convention, where the methods have the
5573type \ctype{PyMethodDef}. The function expects two \ctype{PyObject*}.
5574The first one is the \var{self} object for methods; for module
5575functions, it has the value given to \cfunction{PyInitModule4} (or
5576\NULL{} if \cfunction{PyInitModule} was used). The second parameter
5577(often called \var{args}) is a tuple object representing all
5578arguments. This parameter is typically processed using
5579\cfunction{PyArg_ParseTuple}.
5580
5581\end{datadesc}
5582
5583\begin{datadesc}{METH_KEYWORDS}
5584
5585Methods with these flags must be of type
5586\ctype{PyCFunctionWithKeywords}. The function expects three
5587parameters: \var{self}, \var{args}, and a dictionary of all the keyword
5588arguments. The flag is typically combined with METH_VARARGS, and the
5589parameters are typically processed using
5590\cfunction{PyArg_ParseTupleAndKeywords}.
5591
5592\end{datadesc}
5593
5594\begin{datadesc}{METH_NOARGS}
5595
5596Methods without parameters don't need to check whether arguments are
5597given if they are listed with the \code{METH_NOARGS} flag. They need
5598to be of type \ctype{PyNoArgsFunction}, i.e. they expect a single
5599\var{self} parameter.
5600
5601\end{datadesc}
5602
5603\begin{datadesc}{METH_O}
5604
5605Methods with a single object argument can be listed with the
5606\code{METH_O} flag, instead of invoking \cfunction{PyArg_ParseTuple}
5607with a \code{``O''} argument. They have the type \ctype{PyCFunction},
5608with the \var{self} parameter, and a \ctype{PyObject*} parameter
5609representing the single argument.
5610
5611\end{datadesc}
5612
5613\begin{datadesc}{METH_OLDARGS}
5614
5615This calling convention is deprecated. The method must be of type
5616\ctype{PyCFunction}. The second argument is \NULL{} if no arguments
5617are given, a single object if exactly one argument is given, and a
5618tuple of objects if more than one argument is given.
5619
5620\end{datadesc}
5621
Fred Drakea8455ab2000-06-16 19:58:42 +00005622\begin{cfuncdesc}{PyObject*}{Py_FindMethod}{PyMethodDef[] table,
5623 PyObject *ob, char *name}
5624Return a bound method object for an extension type implemented in C.
5625This function also handles the special attribute \member{__methods__},
5626returning a list of all the method names defined in \var{table}.
5627\end{cfuncdesc}
5628
Fred Drake659ebfa2000-04-03 15:42:13 +00005629
5630\section{Mapping Object Structures \label{mapping-structs}}
5631
5632\begin{ctypedesc}{PyMappingMethods}
5633Structure used to hold pointers to the functions used to implement the
5634mapping protocol for an extension type.
5635\end{ctypedesc}
5636
5637
5638\section{Number Object Structures \label{number-structs}}
5639
5640\begin{ctypedesc}{PyNumberMethods}
5641Structure used to hold pointers to the functions an extension type
5642uses to implement the number protocol.
5643\end{ctypedesc}
5644
5645
5646\section{Sequence Object Structures \label{sequence-structs}}
5647
5648\begin{ctypedesc}{PySequenceMethods}
5649Structure used to hold pointers to the functions which an object uses
5650to implement the sequence protocol.
5651\end{ctypedesc}
5652
5653
5654\section{Buffer Object Structures \label{buffer-structs}}
5655\sectionauthor{Greg J. Stein}{greg@lyra.org}
5656
5657The buffer interface exports a model where an object can expose its
5658internal data as a set of chunks of data, where each chunk is
5659specified as a pointer/length pair. These chunks are called
5660\dfn{segments} and are presumed to be non-contiguous in memory.
5661
5662If an object does not export the buffer interface, then its
5663\member{tp_as_buffer} member in the \ctype{PyTypeObject} structure
5664should be \NULL{}. Otherwise, the \member{tp_as_buffer} will point to
5665a \ctype{PyBufferProcs} structure.
5666
5667\strong{Note:} It is very important that your
Fred Drakec392b572001-03-21 22:15:01 +00005668\ctype{PyTypeObject} structure uses \constant{Py_TPFLAGS_DEFAULT} for
5669the value of the \member{tp_flags} member rather than \code{0}. This
Fred Drake659ebfa2000-04-03 15:42:13 +00005670tells the Python runtime that your \ctype{PyBufferProcs} structure
5671contains the \member{bf_getcharbuffer} slot. Older versions of Python
5672did not have this member, so a new Python interpreter using an old
5673extension needs to be able to test for its presence before using it.
5674
5675\begin{ctypedesc}{PyBufferProcs}
5676Structure used to hold the function pointers which define an
5677implementation of the buffer protocol.
5678
5679The first slot is \member{bf_getreadbuffer}, of type
5680\ctype{getreadbufferproc}. If this slot is \NULL{}, then the object
5681does not support reading from the internal data. This is
5682non-sensical, so implementors should fill this in, but callers should
5683test that the slot contains a non-\NULL{} value.
5684
5685The next slot is \member{bf_getwritebuffer} having type
5686\ctype{getwritebufferproc}. This slot may be \NULL{} if the object
5687does not allow writing into its returned buffers.
5688
5689The third slot is \member{bf_getsegcount}, with type
5690\ctype{getsegcountproc}. This slot must not be \NULL{} and is used to
5691inform the caller how many segments the object contains. Simple
5692objects such as \ctype{PyString_Type} and
5693\ctype{PyBuffer_Type} objects contain a single segment.
5694
5695The last slot is \member{bf_getcharbuffer}, of type
5696\ctype{getcharbufferproc}. This slot will only be present if the
Fred Drakec392b572001-03-21 22:15:01 +00005697\constant{Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the
Fred Drake659ebfa2000-04-03 15:42:13 +00005698\member{tp_flags} field of the object's \ctype{PyTypeObject}. Before using
5699this slot, the caller should test whether it is present by using the
5700\cfunction{PyType_HasFeature()}\ttindex{PyType_HasFeature()} function.
5701If present, it may be \NULL, indicating that the object's contents
5702cannot be used as \emph{8-bit characters}.
5703The slot function may also raise an error if the object's contents
5704cannot be interpreted as 8-bit characters. For example, if the object
5705is an array which is configured to hold floating point values, an
5706exception may be raised if a caller attempts to use
5707\member{bf_getcharbuffer} to fetch a sequence of 8-bit characters.
5708This notion of exporting the internal buffers as ``text'' is used to
5709distinguish between objects that are binary in nature, and those which
5710have character-based content.
5711
5712\strong{Note:} The current policy seems to state that these characters
5713may be multi-byte characters. This implies that a buffer size of
5714\var{N} does not mean there are \var{N} characters present.
5715\end{ctypedesc}
5716
5717\begin{datadesc}{Py_TPFLAGS_HAVE_GETCHARBUFFER}
5718Flag bit set in the type structure to indicate that the
5719\member{bf_getcharbuffer} slot is known. This being set does not
5720indicate that the object supports the buffer interface or that the
5721\member{bf_getcharbuffer} slot is non-\NULL.
5722\end{datadesc}
5723
5724\begin{ctypedesc}[getreadbufferproc]{int (*getreadbufferproc)
5725 (PyObject *self, int segment, void **ptrptr)}
5726Return a pointer to a readable segment of the buffer. This function
5727is allowed to raise an exception, in which case it must return
5728\code{-1}. The \var{segment} which is passed must be zero or
5729positive, and strictly less than the number of segments returned by
Greg Stein4d4d0032001-04-07 16:14:49 +00005730the \member{bf_getsegcount} slot function. On success, it returns the
5731length of the buffer memory, and sets \code{*\var{ptrptr}} to a
5732pointer to that memory.
Fred Drake659ebfa2000-04-03 15:42:13 +00005733\end{ctypedesc}
5734
5735\begin{ctypedesc}[getwritebufferproc]{int (*getwritebufferproc)
5736 (PyObject *self, int segment, void **ptrptr)}
Greg Stein4d4d0032001-04-07 16:14:49 +00005737Return a pointer to a writable memory buffer in \code{*\var{ptrptr}},
5738and the length of that segment as the function return value.
5739The memory buffer must correspond to buffer segment \var{segment}.
Fred Drake58c5a2a1999-08-04 13:13:24 +00005740Must return \code{-1} and set an exception on error.
5741\exception{TypeError} should be raised if the object only supports
5742read-only buffers, and \exception{SystemError} should be raised when
5743\var{segment} specifies a segment that doesn't exist.
5744% Why doesn't it raise ValueError for this one?
Fred Drake659ebfa2000-04-03 15:42:13 +00005745% GJS: because you shouldn't be calling it with an invalid
5746% segment. That indicates a blatant programming error in the C
5747% code.
Fred Drake58c5a2a1999-08-04 13:13:24 +00005748\end{ctypedesc}
5749
Fred Drake659ebfa2000-04-03 15:42:13 +00005750\begin{ctypedesc}[getsegcountproc]{int (*getsegcountproc)
5751 (PyObject *self, int *lenp)}
5752Return the number of memory segments which comprise the buffer. If
5753\var{lenp} is not \NULL, the implementation must report the sum of the
5754sizes (in bytes) of all segments in \code{*\var{lenp}}.
5755The function cannot fail.
5756\end{ctypedesc}
Guido van Rossumae110af1997-05-22 20:11:52 +00005757
Fred Drake659ebfa2000-04-03 15:42:13 +00005758\begin{ctypedesc}[getcharbufferproc]{int (*getcharbufferproc)
5759 (PyObject *self, int segment, const char **ptrptr)}
5760\end{ctypedesc}
Guido van Rossumae110af1997-05-22 20:11:52 +00005761
Guido van Rossumae110af1997-05-22 20:11:52 +00005762
Fred Drakef90490e2001-08-02 18:00:28 +00005763\section{Supporting the Iterator Protocol
5764 \label{supporting-iteration}}
5765
5766
Fred Drakec392b572001-03-21 22:15:01 +00005767\section{Supporting Cyclic Garbarge Collection
5768 \label{supporting-cycle-detection}}
5769
5770Python's support for detecting and collecting garbage which involves
5771circular references requires support from object types which are
5772``containers'' for other objects which may also be containers. Types
5773which do not store references to other objects, or which only store
5774references to atomic types (such as numbers or strings), do not need
5775to provide any explicit support for garbage collection.
5776
5777To create a container type, the \member{tp_flags} field of the type
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005778object must include the \constant{Py_TPFLAGS_HAVE_GC} and provide an
5779implementation of the \member{tp_traverse} handler. If instances of the
5780type are mutable, a \member{tp_clear} implementation must also be
5781provided.
Fred Drakec392b572001-03-21 22:15:01 +00005782
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005783\begin{datadesc}{Py_TPFLAGS_HAVE_GC}
Fred Drakec392b572001-03-21 22:15:01 +00005784 Objects with a type with this flag set must conform with the rules
5785 documented here. For convenience these objects will be referred to
5786 as container objects.
5787\end{datadesc}
5788
Fred Drakee28d8ae2001-03-22 16:30:17 +00005789Constructors for container types must conform to two rules:
5790
5791\begin{enumerate}
5792\item The memory for the object must be allocated using
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005793 \cfunction{PyObject_GC_New()} or \cfunction{PyObject_GC_VarNew()}.
Fred Drakee28d8ae2001-03-22 16:30:17 +00005794
5795\item Once all the fields which may contain references to other
5796 containers are initialized, it must call
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005797 \cfunction{PyObject_GC_Track()}.
Fred Drakee28d8ae2001-03-22 16:30:17 +00005798\end{enumerate}
5799
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005800\begin{cfuncdesc}{\var{TYPE}*}{PyObject_GC_New}{TYPE, PyTypeObject *type}
5801 Analogous to \cfunction{PyObject_New()} but for container objects with
5802 the \constant{Py_TPFLAGS_HAVE_GC} flag set.
5803\end{cfuncdesc}
5804
5805\begin{cfuncdesc}{\var{TYPE}*}{PyObject_GC_NewVar}{TYPE, PyTypeObject *type,
5806 int size}
5807 Analogous to \cfunction{PyObject_NewVar()} but for container objects
5808 with the \constant{Py_TPFLAGS_HAVE_GC} flag set.
5809\end{cfuncdesc}
5810
5811\begin{cfuncdesc}{PyVarObject *}{PyObject_GC_Resize}{PyVarObject *op, int}
5812 Resize an object allocated by \cfunction{PyObject_NewVar()}. Returns
5813 the resized object or \NULL{} on failure.
5814\end{cfuncdesc}
5815
5816\begin{cfuncdesc}{void}{PyObject_GC_Track}{PyObject *op}
Fred Drakec392b572001-03-21 22:15:01 +00005817 Adds the object \var{op} to the set of container objects tracked by
5818 the collector. The collector can run at unexpected times so objects
5819 must be valid while being tracked. This should be called once all
5820 the fields followed by the \member{tp_traverse} handler become valid,
5821 usually near the end of the constructor.
5822\end{cfuncdesc}
5823
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005824\begin{cfuncdesc}{void}{_PyObject_GC_TRACK}{PyObject *op}
5825 A macro version of \cfunction{PyObject_GC_Track()}. It should not be
5826 used for extension modules.
5827\end{cfuncdesc}
5828
Fred Drakee28d8ae2001-03-22 16:30:17 +00005829Similarly, the deallocator for the object must conform to a similar
5830pair of rules:
5831
5832\begin{enumerate}
5833\item Before fields which refer to other containers are invalidated,
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005834 \cfunction{PyObject_GC_UnTrack()} must be called.
Fred Drakee28d8ae2001-03-22 16:30:17 +00005835
5836\item The object's memory must be deallocated using
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005837 \cfunction{PyObject_GC_Del()}.
Fred Drakee28d8ae2001-03-22 16:30:17 +00005838\end{enumerate}
5839
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005840\begin{cfuncdesc}{void}{PyObject_GC_Del}{PyObject *op}
5841 Releases memory allocated to an object using
5842 \cfunction{PyObject_GC_New()} or \cfunction{PyObject_GC_NewVar()}.
5843\end{cfuncdesc}
5844
5845\begin{cfuncdesc}{void}{PyObject_GC_UnTrack}{PyObject *op}
Fred Drakec392b572001-03-21 22:15:01 +00005846 Remove the object \var{op} from the set of container objects tracked
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005847 by the collector. Note that \cfunction{PyObject_GC_Track()} can be
Fred Drakec392b572001-03-21 22:15:01 +00005848 called again on this object to add it back to the set of tracked
5849 objects. The deallocator (\member{tp_dealloc} handler) should call
5850 this for the object before any of the fields used by the
5851 \member{tp_traverse} handler become invalid.
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005852\end{cfuncdesc}
Fred Drake8f6df462001-03-23 17:42:09 +00005853
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005854\begin{cfuncdesc}{void}{_PyObject_GC_UNTRACK}{PyObject *op}
5855 A macro version of \cfunction{PyObject_GC_UnTrack()}. It should not be
5856 used for extension modules.
Fred Drakec392b572001-03-21 22:15:01 +00005857\end{cfuncdesc}
5858
5859The \member{tp_traverse} handler accepts a function parameter of this
5860type:
5861
5862\begin{ctypedesc}[visitproc]{int (*visitproc)(PyObject *object, void *arg)}
5863 Type of the visitor function passed to the \member{tp_traverse}
5864 handler. The function should be called with an object to traverse
5865 as \var{object} and the third parameter to the \member{tp_traverse}
5866 handler as \var{arg}.
5867\end{ctypedesc}
5868
5869The \member{tp_traverse} handler must have the following type:
5870
5871\begin{ctypedesc}[traverseproc]{int (*traverseproc)(PyObject *self,
5872 visitproc visit, void *arg)}
5873 Traversal function for a container object. Implementations must
5874 call the \var{visit} function for each object directly contained by
5875 \var{self}, with the parameters to \var{visit} being the contained
5876 object and the \var{arg} value passed to the handler. If
5877 \var{visit} returns a non-zero value then an error has occurred and
5878 that value should be returned immediately.
5879\end{ctypedesc}
5880
5881The \member{tp_clear} handler must be of the \ctype{inquiry} type, or
5882\NULL{} if the object is immutable.
5883
5884\begin{ctypedesc}[inquiry]{int (*inquiry)(PyObject *self)}
5885 Drop references that may have created reference cycles. Immutable
5886 objects do not have to define this method since they can never
5887 directly create reference cycles. Note that the object must still
Fred Drakebab29652001-07-10 16:10:08 +00005888 be valid after calling this method (don't just call
Fred Drakec392b572001-03-21 22:15:01 +00005889 \cfunction{Py_DECREF()} on a reference). The collector will call
5890 this method if it detects that this object is involved in a
5891 reference cycle.
5892\end{ctypedesc}
5893
5894
Fred Drakee28d8ae2001-03-22 16:30:17 +00005895\subsection{Example Cycle Collector Support
5896 \label{example-cycle-support}}
5897
5898This example shows only enough of the implementation of an extension
5899type to show how the garbage collector support needs to be added. It
5900shows the definition of the object structure, the
5901\member{tp_traverse}, \member{tp_clear} and \member{tp_dealloc}
5902implementations, the type structure, and a constructor --- the module
5903initialization needed to export the constructor to Python is not shown
5904as there are no special considerations there for the collector. To
5905make this interesting, assume that the module exposes ways for the
5906\member{container} field of the object to be modified. Note that
5907since no checks are made on the type of the object used to initialize
5908\member{container}, we have to assume that it may be a container.
5909
5910\begin{verbatim}
5911#include "Python.h"
5912
5913typedef struct {
5914 PyObject_HEAD
5915 PyObject *container;
5916} MyObject;
5917
5918static int
5919my_traverse(MyObject *self, visitproc visit, void *arg)
5920{
5921 if (self->container != NULL)
5922 return visit(self->container, arg);
5923 else
5924 return 0;
5925}
5926
5927static int
5928my_clear(MyObject *self)
5929{
5930 Py_XDECREF(self->container);
5931 self->container = NULL;
5932
5933 return 0;
5934}
5935
5936static void
5937my_dealloc(MyObject *self)
5938{
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005939 PyObject_GC_UnTrack((PyObject *) self);
Fred Drakee28d8ae2001-03-22 16:30:17 +00005940 Py_XDECREF(self->container);
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005941 PyObject_GC_Del(self);
Fred Drakee28d8ae2001-03-22 16:30:17 +00005942}
5943\end{verbatim}
5944
5945\begin{verbatim}
5946statichere PyTypeObject
5947MyObject_Type = {
5948 PyObject_HEAD_INIT(NULL)
5949 0,
5950 "MyObject",
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005951 sizeof(MyObject),
Fred Drakee28d8ae2001-03-22 16:30:17 +00005952 0,
5953 (destructor)my_dealloc, /* tp_dealloc */
5954 0, /* tp_print */
5955 0, /* tp_getattr */
5956 0, /* tp_setattr */
5957 0, /* tp_compare */
5958 0, /* tp_repr */
5959 0, /* tp_as_number */
5960 0, /* tp_as_sequence */
5961 0, /* tp_as_mapping */
5962 0, /* tp_hash */
5963 0, /* tp_call */
5964 0, /* tp_str */
5965 0, /* tp_getattro */
5966 0, /* tp_setattro */
5967 0, /* tp_as_buffer */
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Fred Drakee28d8ae2001-03-22 16:30:17 +00005969 0, /* tp_doc */
5970 (traverseproc)my_traverse, /* tp_traverse */
5971 (inquiry)my_clear, /* tp_clear */
5972 0, /* tp_richcompare */
5973 0, /* tp_weaklistoffset */
5974};
5975
5976/* This constructor should be made accessible from Python. */
5977static PyObject *
5978new_object(PyObject *unused, PyObject *args)
5979{
5980 PyObject *container = NULL;
5981 MyObject *result = NULL;
5982
5983 if (PyArg_ParseTuple(args, "|O:new_object", &container)) {
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005984 result = PyObject_GC_New(MyObject, &MyObject_Type);
Fred Drakee28d8ae2001-03-22 16:30:17 +00005985 if (result != NULL) {
5986 result->container = container;
Neil Schemenauer55cdc882001-08-30 15:24:17 +00005987 PyObject_GC_Track(result);
Fred Drakee28d8ae2001-03-22 16:30:17 +00005988 }
5989 }
5990 return (PyObject *) result;
5991}
5992\end{verbatim}
5993
5994
Fred Drake659ebfa2000-04-03 15:42:13 +00005995% \chapter{Debugging \label{debugging}}
5996%
5997% XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
Guido van Rossum5b8a5231997-12-30 04:38:44 +00005998
5999
Fred Drakeed773ef2000-09-21 21:35:22 +00006000\appendix
6001\chapter{Reporting Bugs}
6002\input{reportingbugs}
6003
Fred Drake490d34d2001-06-20 21:39:12 +00006004\chapter{History and License}
6005\input{license}
6006
Marc-André Lemburga544ea22001-01-17 18:04:31 +00006007\input{api.ind} % Index -- must be last
Guido van Rossum9231c8f1997-05-15 21:43:21 +00006008
6009\end{document}