Fred Drake | 6659c30 | 1998-03-03 22:02:19 +0000 | [diff] [blame] | 1 | \documentclass{manual} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3 | \title{Python/C API Reference Manual} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 4 | |
| 5 | \input{boilerplate} |
| 6 | |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 7 | \makeindex % tell \index to actually write the .idx file |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | \begin{document} |
| 11 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 12 | \maketitle |
| 13 | |
Fred Drake | 9f86b66 | 1998-07-28 21:55:19 +0000 | [diff] [blame] | 14 | \ifhtml |
| 15 | \chapter*{Front Matter\label{front}} |
| 16 | \fi |
| 17 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 18 | \input{copyright} |
| 19 | |
| 20 | \begin{abstract} |
| 21 | |
| 22 | \noindent |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 23 | This manual documents the API used by C and \Cpp{} programmers who |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 24 | want to write extension modules or embed Python. It is a companion to |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 25 | \citetitle[../ext/ext.html]{Extending and Embedding the Python |
| 26 | Interpreter}, which describes the general principles of extension |
| 27 | writing but does not document the API functions in detail. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 29 | \strong{Warning:} The current version of this document is incomplete. |
| 30 | I hope that it is nevertheless useful. I will continue to work on it, |
| 31 | and release new versions from time to time, independent from Python |
| 32 | source code releases. |
| 33 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 34 | \end{abstract} |
| 35 | |
Fred Drake | 4d4f9e7 | 1998-01-13 22:25:02 +0000 | [diff] [blame] | 36 | \tableofcontents |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 38 | % XXX Consider moving all this back to ext.tex and giving api.tex |
| 39 | % XXX a *really* short intro only. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 40 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 41 | \chapter{Introduction \label{intro}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 42 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 43 | The Application Programmer's Interface to Python gives C and |
| 44 | \Cpp{} programmers access to the Python interpreter at a variety of |
| 45 | levels. The API is equally usable from \Cpp{}, but for brevity it is |
| 46 | generally referred to as the Python/C API. There are two |
| 47 | fundamentally different reasons for using the Python/C API. The first |
| 48 | reason is to write \emph{extension modules} for specific purposes; |
| 49 | these are C modules that extend the Python interpreter. This is |
| 50 | probably the most common use. The second reason is to use Python as a |
| 51 | component in a larger application; this technique is generally |
| 52 | referred to as \dfn{embedding} Python in an application. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 54 | Writing an extension module is a relatively well-understood process, |
| 55 | where a ``cookbook'' approach works well. There are several tools |
| 56 | that automate the process to some extent. While people have embedded |
| 57 | Python in other applications since its early existence, the process of |
| 58 | embedding Python is less straightforward that writing an extension. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 60 | Many API functions are useful independent of whether you're embedding |
| 61 | or extending Python; moreover, most applications that embed Python |
| 62 | will need to provide a custom extension as well, so it's probably a |
| 63 | good idea to become familiar with writing an extension before |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 64 | attempting to embed Python in a real application. |
| 65 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 66 | |
| 67 | \section{Include Files \label{includes}} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 68 | |
| 69 | All function, type and macro definitions needed to use the Python/C |
| 70 | API are included in your code by the following line: |
| 71 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 72 | \begin{verbatim} |
| 73 | #include "Python.h" |
| 74 | \end{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 75 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 76 | This implies inclusion of the following standard headers: |
Fred Drake | 0b71cea | 2000-09-26 05:51:50 +0000 | [diff] [blame] | 77 | \code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, |
| 78 | \code{<limits.h>}, and \code{<stdlib.h>} (if available). |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 79 | |
| 80 | All user visible names defined by Python.h (except those defined by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 81 | the included standard headers) have one of the prefixes \samp{Py} or |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 82 | \samp{_Py}. Names beginning with \samp{_Py} are for internal use by |
| 83 | the Python implementation and should not be used by extension writers. |
| 84 | Structure member names do not have a reserved prefix. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 85 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 86 | \strong{Important:} user code should never define names that begin |
| 87 | with \samp{Py} or \samp{_Py}. This confuses the reader, and |
| 88 | jeopardizes the portability of the user code to future Python |
| 89 | versions, which may define additional names beginning with one of |
| 90 | these prefixes. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 91 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 92 | The header files are typically installed with Python. On \UNIX, these |
| 93 | are located in the directories |
| 94 | \file{\envvar{prefix}/include/python\var{version}/} and |
| 95 | \file{\envvar{exec_prefix}/include/python\var{version}/}, where |
| 96 | \envvar{prefix} and \envvar{exec_prefix} are defined by the |
| 97 | corresponding parameters to Python's \program{configure} script and |
| 98 | \var{version} is \code{sys.version[:3]}. On Windows, the headers are |
| 99 | installed in \file{\envvar{prefix}/include}, where \envvar{prefix} is |
| 100 | the installation directory specified to the installer. |
| 101 | |
| 102 | To include the headers, place both directories (if different) on your |
| 103 | compiler's search path for includes. Do \emph{not} place the parent |
| 104 | directories on the search path and then use |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 105 | \samp{\#include <python\shortversion/Python.h>}; this will break on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 106 | multi-platform builds since the platform independent headers under |
| 107 | \envvar{prefix} include the platform specific headers from |
| 108 | \envvar{exec_prefix}. |
| 109 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 110 | |
| 111 | \section{Objects, Types and Reference Counts \label{objects}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 113 | Most Python/C API functions have one or more arguments as well as a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 114 | return value of type \ctype{PyObject*}. This type is a pointer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 115 | to an opaque data type representing an arbitrary Python |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 116 | object. Since all Python object types are treated the same way by the |
| 117 | Python language in most situations (e.g., assignments, scope rules, |
| 118 | and argument passing), it is only fitting that they should be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 119 | represented by a single C type. Almost all Python objects live on the |
| 120 | heap: you never declare an automatic or static variable of type |
| 121 | \ctype{PyObject}, only pointer variables of type \ctype{PyObject*} can |
| 122 | be declared. The sole exception are the type objects\obindex{type}; |
| 123 | since these must never be deallocated, they are typically static |
| 124 | \ctype{PyTypeObject} objects. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 125 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 126 | All Python objects (even Python integers) have a \dfn{type} and a |
| 127 | \dfn{reference count}. An object's type determines what kind of object |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 128 | it is (e.g., an integer, a list, or a user-defined function; there are |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 129 | many more as explained in the \citetitle[../ref/ref.html]{Python |
| 130 | Reference Manual}). For each of the well-known types there is a macro |
| 131 | to check whether an object is of that type; for instance, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 132 | \samp{PyList_Check(\var{a})} is true if (and only if) the object |
| 133 | pointed to by \var{a} is a Python list. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 134 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 135 | |
| 136 | \subsection{Reference Counts \label{refcounts}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 138 | The reference count is important because today's computers have a |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 139 | finite (and often severely limited) memory size; it counts how many |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 140 | different places there are that have a reference to an object. Such a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 141 | place could be another object, or a global (or static) C variable, or |
| 142 | a local variable in some C function. When an object's reference count |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 143 | becomes zero, the object is deallocated. If it contains references to |
| 144 | other objects, their reference count is decremented. Those other |
| 145 | objects may be deallocated in turn, if this decrement makes their |
| 146 | reference count become zero, and so on. (There's an obvious problem |
| 147 | with objects that reference each other here; for now, the solution is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 148 | ``don't do that.'') |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 150 | Reference counts are always manipulated explicitly. The normal way is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 151 | to use the macro \cfunction{Py_INCREF()}\ttindex{Py_INCREF()} to |
| 152 | increment an object's reference count by one, and |
| 153 | \cfunction{Py_DECREF()}\ttindex{Py_DECREF()} to decrement it by |
| 154 | one. The \cfunction{Py_DECREF()} macro is considerably more complex |
| 155 | than the incref one, since it must check whether the reference count |
| 156 | becomes zero and then cause the object's deallocator to be called. |
| 157 | The deallocator is a function pointer contained in the object's type |
| 158 | structure. The type-specific deallocator takes care of decrementing |
| 159 | the reference counts for other objects contained in the object if this |
| 160 | is a compound object type, such as a list, as well as performing any |
| 161 | additional finalization that's needed. There's no chance that the |
| 162 | reference count can overflow; at least as many bits are used to hold |
| 163 | the reference count as there are distinct memory locations in virtual |
| 164 | memory (assuming \code{sizeof(long) >= sizeof(char*)}). Thus, the |
| 165 | reference count increment is a simple operation. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 167 | It is not necessary to increment an object's reference count for every |
| 168 | local variable that contains a pointer to an object. In theory, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 169 | object's reference count goes up by one when the variable is made to |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 170 | point to it and it goes down by one when the variable goes out of |
| 171 | scope. However, these two cancel each other out, so at the end the |
| 172 | reference count hasn't changed. The only real reason to use the |
| 173 | reference count is to prevent the object from being deallocated as |
| 174 | long as our variable is pointing to it. If we know that there is at |
| 175 | least one other reference to the object that lives at least as long as |
| 176 | our variable, there is no need to increment the reference count |
| 177 | temporarily. An important situation where this arises is in objects |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 178 | that are passed as arguments to C functions in an extension module |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 179 | that are called from Python; the call mechanism guarantees to hold a |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 180 | reference to every argument for the duration of the call. |
| 181 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 182 | However, a common pitfall is to extract an object from a list and |
| 183 | hold on to it for a while without incrementing its reference count. |
| 184 | Some other operation might conceivably remove the object from the |
| 185 | list, decrementing its reference count and possible deallocating it. |
| 186 | The real danger is that innocent-looking operations may invoke |
| 187 | arbitrary Python code which could do this; there is a code path which |
| 188 | allows control to flow back to the user from a \cfunction{Py_DECREF()}, |
| 189 | so almost any operation is potentially dangerous. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 191 | A safe approach is to always use the generic operations (functions |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 192 | whose name begins with \samp{PyObject_}, \samp{PyNumber_}, |
| 193 | \samp{PySequence_} or \samp{PyMapping_}). These operations always |
| 194 | increment the reference count of the object they return. This leaves |
| 195 | the caller with the responsibility to call |
| 196 | \cfunction{Py_DECREF()} when they are done with the result; this soon |
| 197 | becomes second nature. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 198 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 199 | |
| 200 | \subsubsection{Reference Count Details \label{refcountDetails}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 201 | |
| 202 | The reference count behavior of functions in the Python/C API is best |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 203 | explained in terms of \emph{ownership of references}. Note that we |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 204 | talk of owning references, never of owning objects; objects are always |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 205 | shared! When a function owns a reference, it has to dispose of it |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 206 | properly --- either by passing ownership on (usually to its caller) or |
| 207 | by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When |
| 208 | a function passes ownership of a reference on to its caller, the |
| 209 | caller is said to receive a \emph{new} reference. When no ownership |
| 210 | is transferred, the caller is said to \emph{borrow} the reference. |
| 211 | Nothing needs to be done for a borrowed reference. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 212 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 213 | Conversely, when a calling function passes it a reference to an |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 214 | object, there are two possibilities: the function \emph{steals} a |
| 215 | reference to the object, or it does not. Few functions steal |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 216 | references; the two notable exceptions are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 217 | \cfunction{PyList_SetItem()}\ttindex{PyList_SetItem()} and |
| 218 | \cfunction{PyTuple_SetItem()}\ttindex{PyTuple_SetItem()}, which |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 219 | steal a reference to the item (but not to the tuple or list into which |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 220 | the item is put!). These functions were designed to steal a reference |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 221 | because of a common idiom for populating a tuple or list with newly |
| 222 | created objects; for example, the code to create the tuple \code{(1, |
| 223 | 2, "three")} could look like this (forgetting about error handling for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 224 | the moment; a better way to code this is shown below): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 225 | |
| 226 | \begin{verbatim} |
| 227 | PyObject *t; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 229 | t = PyTuple_New(3); |
| 230 | PyTuple_SetItem(t, 0, PyInt_FromLong(1L)); |
| 231 | PyTuple_SetItem(t, 1, PyInt_FromLong(2L)); |
| 232 | PyTuple_SetItem(t, 2, PyString_FromString("three")); |
| 233 | \end{verbatim} |
| 234 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 235 | Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to |
| 236 | set tuple items; \cfunction{PySequence_SetItem()} and |
| 237 | \cfunction{PyObject_SetItem()} refuse to do this since tuples are an |
| 238 | immutable data type. You should only use |
| 239 | \cfunction{PyTuple_SetItem()} for tuples that you are creating |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 240 | yourself. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 241 | |
| 242 | Equivalent code for populating a list can be written using |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 243 | \cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code |
| 244 | can also use \cfunction{PySequence_SetItem()}; this illustrates the |
| 245 | difference between the two (the extra \cfunction{Py_DECREF()} calls): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 246 | |
| 247 | \begin{verbatim} |
| 248 | PyObject *l, *x; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 250 | l = PyList_New(3); |
| 251 | x = PyInt_FromLong(1L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 252 | PySequence_SetItem(l, 0, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 253 | x = PyInt_FromLong(2L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 254 | PySequence_SetItem(l, 1, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 255 | x = PyString_FromString("three"); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 256 | PySequence_SetItem(l, 2, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 257 | \end{verbatim} |
| 258 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 259 | You might find it strange that the ``recommended'' approach takes more |
| 260 | code. However, in practice, you will rarely use these ways of |
| 261 | creating and populating a tuple or list. There's a generic function, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 262 | \cfunction{Py_BuildValue()}, that can create most common objects from |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 263 | C values, directed by a \dfn{format string}. For example, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 264 | above two blocks of code could be replaced by the following (which |
| 265 | also takes care of the error checking): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 266 | |
| 267 | \begin{verbatim} |
| 268 | PyObject *t, *l; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 270 | t = Py_BuildValue("(iis)", 1, 2, "three"); |
| 271 | l = Py_BuildValue("[iis]", 1, 2, "three"); |
| 272 | \end{verbatim} |
| 273 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 274 | It is much more common to use \cfunction{PyObject_SetItem()} and |
| 275 | friends with items whose references you are only borrowing, like |
| 276 | arguments that were passed in to the function you are writing. In |
| 277 | that case, their behaviour regarding reference counts is much saner, |
| 278 | since you don't have to increment a reference count so you can give a |
| 279 | reference away (``have it be stolen''). For example, this function |
| 280 | sets all items of a list (actually, any mutable sequence) to a given |
| 281 | item: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 282 | |
| 283 | \begin{verbatim} |
| 284 | int set_all(PyObject *target, PyObject *item) |
| 285 | { |
| 286 | int i, n; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 288 | n = PyObject_Length(target); |
| 289 | if (n < 0) |
| 290 | return -1; |
| 291 | for (i = 0; i < n; i++) { |
| 292 | if (PyObject_SetItem(target, i, item) < 0) |
| 293 | return -1; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 298 | \ttindex{set_all()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 299 | |
| 300 | The situation is slightly different for function return values. |
| 301 | While passing a reference to most functions does not change your |
| 302 | ownership responsibilities for that reference, many functions that |
| 303 | return a referece to an object give you ownership of the reference. |
| 304 | The reason is simple: in many cases, the returned object is created |
| 305 | on the fly, and the reference you get is the only reference to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 306 | object. Therefore, the generic functions that return object |
| 307 | references, like \cfunction{PyObject_GetItem()} and |
| 308 | \cfunction{PySequence_GetItem()}, always return a new reference (i.e., |
| 309 | the caller becomes the owner of the reference). |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 310 | |
| 311 | It is important to realize that whether you own a reference returned |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 312 | by a function depends on which function you call only --- \emph{the |
| 313 | plumage} (i.e., the type of the type of the object passed as an |
| 314 | argument to the function) \emph{doesn't enter into it!} Thus, if you |
| 315 | extract an item from a list using \cfunction{PyList_GetItem()}, you |
| 316 | don't own the reference --- but if you obtain the same item from the |
| 317 | same list using \cfunction{PySequence_GetItem()} (which happens to |
| 318 | take exactly the same arguments), you do own a reference to the |
| 319 | returned object. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 320 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 321 | Here is an example of how you could write a function that computes the |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 322 | sum of the items in a list of integers; once using |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 323 | \cfunction{PyList_GetItem()}\ttindex{PyList_GetItem()}, and once using |
| 324 | \cfunction{PySequence_GetItem()}\ttindex{PySequence_GetItem()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 325 | |
| 326 | \begin{verbatim} |
| 327 | long sum_list(PyObject *list) |
| 328 | { |
| 329 | int i, n; |
| 330 | long total = 0; |
| 331 | PyObject *item; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 333 | n = PyList_Size(list); |
| 334 | if (n < 0) |
| 335 | return -1; /* Not a list */ |
| 336 | for (i = 0; i < n; i++) { |
| 337 | item = PyList_GetItem(list, i); /* Can't fail */ |
| 338 | if (!PyInt_Check(item)) continue; /* Skip non-integers */ |
| 339 | total += PyInt_AsLong(item); |
| 340 | } |
| 341 | return total; |
| 342 | } |
| 343 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 344 | \ttindex{sum_list()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 345 | |
| 346 | \begin{verbatim} |
| 347 | long sum_sequence(PyObject *sequence) |
| 348 | { |
| 349 | int i, n; |
| 350 | long total = 0; |
| 351 | PyObject *item; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 352 | n = PySequence_Length(sequence); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 353 | if (n < 0) |
| 354 | return -1; /* Has no length */ |
| 355 | for (i = 0; i < n; i++) { |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 356 | item = PySequence_GetItem(sequence, i); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 357 | if (item == NULL) |
| 358 | return -1; /* Not a sequence, or other failure */ |
| 359 | if (PyInt_Check(item)) |
| 360 | total += PyInt_AsLong(item); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 361 | Py_DECREF(item); /* Discard reference ownership */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 362 | } |
| 363 | return total; |
| 364 | } |
| 365 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 366 | \ttindex{sum_sequence()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 367 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 368 | |
| 369 | \subsection{Types \label{types}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 370 | |
| 371 | There are few other data types that play a significant role in |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 372 | the Python/C API; most are simple C types such as \ctype{int}, |
| 373 | \ctype{long}, \ctype{double} and \ctype{char*}. A few structure types |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 374 | are used to describe static tables used to list the functions exported |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 375 | by a module or the data attributes of a new object type, and another |
| 376 | is used to describe the value of a complex number. These will |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 377 | be discussed together with the functions that use them. |
| 378 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 379 | |
| 380 | \section{Exceptions \label{exceptions}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 382 | The Python programmer only needs to deal with exceptions if specific |
| 383 | error handling is required; unhandled exceptions are automatically |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 384 | propagated to the caller, then to the caller's caller, and so on, until |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 385 | they reach the top-level interpreter, where they are reported to the |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 386 | user accompanied by a stack traceback. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 387 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 388 | For C programmers, however, error checking always has to be explicit. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 389 | All functions in the Python/C API can raise exceptions, unless an |
| 390 | explicit claim is made otherwise in a function's documentation. In |
| 391 | general, when a function encounters an error, it sets an exception, |
| 392 | discards any object references that it owns, and returns an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 393 | error indicator --- usually \NULL{} or \code{-1}. A few functions |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 394 | return a Boolean true/false result, with false indicating an error. |
| 395 | Very few functions return no explicit error indicator or have an |
| 396 | ambiguous return value, and require explicit testing for errors with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 397 | \cfunction{PyErr_Occurred()}\ttindex{PyErr_Occurred()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 398 | |
| 399 | Exception state is maintained in per-thread storage (this is |
| 400 | equivalent to using global storage in an unthreaded application). A |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 401 | thread can be in one of two states: an exception has occurred, or not. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 402 | The function \cfunction{PyErr_Occurred()} can be used to check for |
| 403 | this: it returns a borrowed reference to the exception type object |
| 404 | when an exception has occurred, and \NULL{} otherwise. There are a |
| 405 | number of functions to set the exception state: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 406 | \cfunction{PyErr_SetString()}\ttindex{PyErr_SetString()} is the most |
| 407 | common (though not the most general) function to set the exception |
| 408 | state, and \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} clears the |
| 409 | exception state. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 410 | |
| 411 | The full exception state consists of three objects (all of which can |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 412 | be \NULL{}): the exception type, the corresponding exception |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 413 | value, and the traceback. These have the same meanings as the Python |
| 414 | \withsubitem{(in module sys)}{ |
| 415 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
| 416 | objects \code{sys.exc_type}, \code{sys.exc_value}, and |
| 417 | \code{sys.exc_traceback}; however, they are not the same: the Python |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 418 | objects represent the last exception being handled by a Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 419 | \keyword{try} \ldots\ \keyword{except} statement, while the C level |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 420 | exception state only exists while an exception is being passed on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 421 | between C functions until it reaches the Python bytecode interpreter's |
| 422 | main loop, which takes care of transferring it to \code{sys.exc_type} |
| 423 | and friends. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 424 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 425 | Note that starting with Python 1.5, the preferred, thread-safe way to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 426 | access the exception state from Python code is to call the function |
| 427 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 428 | \function{sys.exc_info()}, which returns the per-thread exception state |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 429 | for Python code. Also, the semantics of both ways to access the |
| 430 | exception state have changed so that a function which catches an |
| 431 | exception will save and restore its thread's exception state so as to |
| 432 | preserve the exception state of its caller. This prevents common bugs |
| 433 | in exception handling code caused by an innocent-looking function |
| 434 | overwriting the exception being handled; it also reduces the often |
| 435 | unwanted lifetime extension for objects that are referenced by the |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 436 | stack frames in the traceback. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 437 | |
| 438 | As a general principle, a function that calls another function to |
| 439 | perform some task should check whether the called function raised an |
| 440 | exception, and if so, pass the exception state on to its caller. It |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 441 | should discard any object references that it owns, and return an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 442 | error indicator, but it should \emph{not} set another exception --- |
| 443 | that would overwrite the exception that was just raised, and lose |
| 444 | important information about the exact cause of the error. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 445 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 446 | A simple example of detecting exceptions and passing them on is shown |
| 447 | in the \cfunction{sum_sequence()}\ttindex{sum_sequence()} example |
| 448 | above. It so happens that that example doesn't need to clean up any |
| 449 | owned references when it detects an error. The following example |
| 450 | function shows some error cleanup. First, to remind you why you like |
| 451 | Python, we show the equivalent Python code: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 452 | |
| 453 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 454 | def incr_item(dict, key): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 455 | try: |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 456 | item = dict[key] |
| 457 | except KeyError: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 458 | item = 0 |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 459 | dict[key] = item + 1 |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 460 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 461 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 462 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 463 | Here is the corresponding C code, in all its glory: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 464 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 465 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 466 | int incr_item(PyObject *dict, PyObject *key) |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 467 | { |
| 468 | /* Objects all initialized to NULL for Py_XDECREF */ |
| 469 | PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL; |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 470 | int rv = -1; /* Return value initialized to -1 (failure) */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 472 | item = PyObject_GetItem(dict, key); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 473 | if (item == NULL) { |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 474 | /* Handle KeyError only: */ |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 475 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 476 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 477 | |
| 478 | /* Clear the error and use zero: */ |
| 479 | PyErr_Clear(); |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 480 | item = PyInt_FromLong(0L); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 481 | if (item == NULL) |
| 482 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 484 | const_one = PyInt_FromLong(1L); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 485 | if (const_one == NULL) |
| 486 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 487 | |
| 488 | incremented_item = PyNumber_Add(item, const_one); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 489 | if (incremented_item == NULL) |
| 490 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 491 | |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 492 | if (PyObject_SetItem(dict, key, incremented_item) < 0) |
| 493 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 494 | rv = 0; /* Success */ |
| 495 | /* Continue with cleanup code */ |
| 496 | |
| 497 | error: |
| 498 | /* Cleanup code, shared by success and failure path */ |
| 499 | |
| 500 | /* Use Py_XDECREF() to ignore NULL references */ |
| 501 | Py_XDECREF(item); |
| 502 | Py_XDECREF(const_one); |
| 503 | Py_XDECREF(incremented_item); |
| 504 | |
| 505 | return rv; /* -1 for error, 0 for success */ |
| 506 | } |
| 507 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 508 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 509 | |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 510 | This example represents an endorsed use of the \keyword{goto} statement |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 511 | in C! It illustrates the use of |
| 512 | \cfunction{PyErr_ExceptionMatches()}\ttindex{PyErr_ExceptionMatches()} and |
| 513 | \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} to |
| 514 | handle specific exceptions, and the use of |
| 515 | \cfunction{Py_XDECREF()}\ttindex{Py_XDECREF()} to |
| 516 | dispose of owned references that may be \NULL{} (note the |
| 517 | \character{X} in the name; \cfunction{Py_DECREF()} would crash when |
| 518 | confronted with a \NULL{} reference). It is important that the |
| 519 | variables used to hold owned references are initialized to \NULL{} for |
| 520 | this to work; likewise, the proposed return value is initialized to |
| 521 | \code{-1} (failure) and only set to success after the final call made |
| 522 | is successful. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 523 | |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 524 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 525 | \section{Embedding Python \label{embedding}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 527 | The one important task that only embedders (as opposed to extension |
| 528 | writers) of the Python interpreter have to worry about is the |
| 529 | initialization, and possibly the finalization, of the Python |
| 530 | interpreter. Most functionality of the interpreter can only be used |
| 531 | after the interpreter has been initialized. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 532 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 533 | The basic initialization function is |
| 534 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 535 | This initializes the table of loaded modules, and creates the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 536 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
| 537 | \module{__main__}\refbimodindex{__main__} and |
| 538 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 539 | search path (\code{sys.path}).% |
| 540 | \indexiii{module}{search}{path} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 541 | \withsubitem{(in module sys)}{\ttindex{path}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 542 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 543 | \cfunction{Py_Initialize()} does not set the ``script argument list'' |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 544 | (\code{sys.argv}). If this variable is needed by Python code that |
| 545 | will be executed later, it must be set explicitly with a call to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 546 | \code{PySys_SetArgv(\var{argc}, |
| 547 | \var{argv})}\ttindex{PySys_SetArgv()} subsequent to the call to |
| 548 | \cfunction{Py_Initialize()}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 549 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 550 | On most systems (in particular, on \UNIX{} and Windows, although the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 551 | details are slightly different), |
| 552 | \cfunction{Py_Initialize()} calculates the module search path based |
| 553 | upon its best guess for the location of the standard Python |
| 554 | interpreter executable, assuming that the Python library is found in a |
| 555 | fixed location relative to the Python interpreter executable. In |
| 556 | particular, it looks for a directory named |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 557 | \file{lib/python\shortversion} relative to the parent directory where |
| 558 | the executable named \file{python} is found on the shell command |
| 559 | search path (the environment variable \envvar{PATH}). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 560 | |
| 561 | For instance, if the Python executable is found in |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 562 | \file{/usr/local/bin/python}, it will assume that the libraries are in |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 563 | \file{/usr/local/lib/python\shortversion}. (In fact, this particular path |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 564 | is also the ``fallback'' location, used when no executable file named |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 565 | \file{python} is found along \envvar{PATH}.) The user can override |
| 566 | this behavior by setting the environment variable \envvar{PYTHONHOME}, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 567 | or insert additional directories in front of the standard path by |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 568 | setting \envvar{PYTHONPATH}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 570 | The embedding application can steer the search by calling |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 571 | \code{Py_SetProgramName(\var{file})}\ttindex{Py_SetProgramName()} \emph{before} calling |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 572 | \cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still |
| 573 | overrides this and \envvar{PYTHONPATH} is still inserted in front of |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 574 | the standard path. An application that requires total control has to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 575 | provide its own implementation of |
| 576 | \cfunction{Py_GetPath()}\ttindex{Py_GetPath()}, |
| 577 | \cfunction{Py_GetPrefix()}\ttindex{Py_GetPrefix()}, |
| 578 | \cfunction{Py_GetExecPrefix()}\ttindex{Py_GetExecPrefix()}, and |
| 579 | \cfunction{Py_GetProgramFullPath()}\ttindex{Py_GetProgramFullPath()} (all |
| 580 | defined in \file{Modules/getpath.c}). |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 582 | Sometimes, it is desirable to ``uninitialize'' Python. For instance, |
| 583 | the application may want to start over (make another call to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 584 | \cfunction{Py_Initialize()}) or the application is simply done with its |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 585 | use of Python and wants to free all memory allocated by Python. This |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 586 | can be accomplished by calling \cfunction{Py_Finalize()}. The function |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 587 | \cfunction{Py_IsInitialized()}\ttindex{Py_IsInitialized()} returns |
| 588 | true if Python is currently in the initialized state. More |
| 589 | information about these functions is given in a later chapter. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 591 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 592 | \chapter{The Very High Level Layer \label{veryhigh}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 593 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 594 | The functions in this chapter will let you execute Python source code |
| 595 | given in a file or a buffer, but they will not let you interact in a |
| 596 | more detailed way with the interpreter. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 597 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 598 | Several of these functions accept a start symbol from the grammar as a |
| 599 | parameter. The available start symbols are \constant{Py_eval_input}, |
| 600 | \constant{Py_file_input}, and \constant{Py_single_input}. These are |
| 601 | described following the functions which accept them as parameters. |
| 602 | |
Fred Drake | 510d08b | 2000-08-14 02:50:21 +0000 | [diff] [blame] | 603 | Note also that several of these functions take \ctype{FILE*} |
| 604 | parameters. On particular issue which needs to be handled carefully |
| 605 | is that the \ctype{FILE} structure for different C libraries can be |
| 606 | different and incompatible. Under Windows (at least), it is possible |
| 607 | for dynamically linked extensions to actually use different libraries, |
| 608 | so care should be taken that \ctype{FILE*} parameters are only passed |
| 609 | to these functions if it is certain that they were created by the same |
| 610 | library that the Python runtime is using. |
| 611 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 612 | \begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 613 | If \var{fp} refers to a file associated with an interactive device |
| 614 | (console or terminal input or \UNIX{} pseudo-terminal), return the |
| 615 | value of \cfunction{PyRun_InteractiveLoop()}, otherwise return the |
| 616 | result of \cfunction{PyRun_SimpleFile()}. If \var{filename} is |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 617 | \NULL{}, this function uses \code{"???"} as the filename. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 618 | \end{cfuncdesc} |
| 619 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 620 | \begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 621 | Executes the Python source code from \var{command} in the |
| 622 | \module{__main__} module. If \module{__main__} does not already |
| 623 | exist, it is created. Returns \code{0} on success or \code{-1} if |
| 624 | an exception was raised. If there was an error, there is no way to |
| 625 | get the exception information. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 626 | \end{cfuncdesc} |
| 627 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 628 | \begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 629 | Similar to \cfunction{PyRun_SimpleString()}, but the Python source |
| 630 | code is read from \var{fp} instead of an in-memory string. |
| 631 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 632 | \end{cfuncdesc} |
| 633 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 634 | \begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename} |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 635 | Read and execute a single statement from a file associated with an |
| 636 | interactive device. If \var{filename} is \NULL, \code{"???"} is |
| 637 | used instead. The user will be prompted using \code{sys.ps1} and |
| 638 | \code{sys.ps2}. Returns \code{0} when the input was executed |
| 639 | successfully, \code{-1} if there was an exception, or an error code |
| 640 | from the \file{errcode.h} include file distributed as part of Python |
| 641 | in case of a parse error. (Note that \file{errcode.h} is not |
| 642 | included by \file{Python.h}, so must be included specifically if |
| 643 | needed.) |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 644 | \end{cfuncdesc} |
| 645 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 646 | \begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename} |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 647 | Read and execute statements from a file associated with an |
| 648 | interactive device until \EOF{} is reached. If \var{filename} is |
| 649 | \NULL, \code{"???"} is used instead. The user will be prompted |
| 650 | using \code{sys.ps1} and \code{sys.ps2}. Returns \code{0} at \EOF. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 651 | \end{cfuncdesc} |
| 652 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 653 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str, |
| 654 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 655 | Parse Python source code from \var{str} using the start token |
| 656 | \var{start}. The result can be used to create a code object which |
| 657 | can be evaluated efficiently. This is useful if a code fragment |
| 658 | must be evaluated many times. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 659 | \end{cfuncdesc} |
| 660 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 661 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp, |
| 662 | char *filename, int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 663 | Similar to \cfunction{PyParser_SimpleParseString()}, but the Python |
| 664 | source code is read from \var{fp} instead of an in-memory string. |
| 665 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 666 | \end{cfuncdesc} |
| 667 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 668 | \begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start, |
| 669 | PyObject *globals, |
| 670 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 671 | Execute Python source code from \var{str} in the context specified |
| 672 | by the dictionaries \var{globals} and \var{locals}. The parameter |
| 673 | \var{start} specifies the start token that should be used to parse |
| 674 | the source code. |
| 675 | |
| 676 | Returns the result of executing the code as a Python object, or |
| 677 | \NULL{} if an exception was raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 678 | \end{cfuncdesc} |
| 679 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 680 | \begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename, |
| 681 | int start, PyObject *globals, |
| 682 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 683 | Similar to \cfunction{PyRun_String()}, but the Python source code is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 684 | read from \var{fp} instead of an in-memory string. |
| 685 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 686 | \end{cfuncdesc} |
| 687 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 688 | \begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename, |
| 689 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 690 | Parse and compile the Python source code in \var{str}, returning the |
| 691 | resulting code object. The start token is given by \var{start}; |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 692 | this can be used to constrain the code which can be compiled and should |
| 693 | be \constant{Py_eval_input}, \constant{Py_file_input}, or |
| 694 | \constant{Py_single_input}. The filename specified by |
| 695 | \var{filename} is used to construct the code object and may appear |
| 696 | in tracebacks or \exception{SyntaxError} exception messages. This |
| 697 | returns \NULL{} if the code cannot be parsed or compiled. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 698 | \end{cfuncdesc} |
| 699 | |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 700 | \begin{cvardesc}{int}{Py_eval_input} |
| 701 | The start symbol from the Python grammar for isolated expressions; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 702 | for use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 703 | \end{cvardesc} |
| 704 | |
| 705 | \begin{cvardesc}{int}{Py_file_input} |
| 706 | The start symbol from the Python grammar for sequences of statements |
| 707 | as read from a file or other source; for use with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 708 | \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. This is |
| 709 | the symbol to use when compiling arbitrarily long Python source code. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 710 | \end{cvardesc} |
| 711 | |
| 712 | \begin{cvardesc}{int}{Py_single_input} |
| 713 | The start symbol from the Python grammar for a single statement; for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 714 | use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
| 715 | This is the symbol used for the interactive interpreter loop. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 716 | \end{cvardesc} |
| 717 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 718 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 719 | \chapter{Reference Counting \label{countingRefs}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 720 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 721 | The macros in this section are used for managing reference counts |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 722 | of Python objects. |
| 723 | |
| 724 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 725 | Increment the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 726 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 727 | \cfunction{Py_XINCREF()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 728 | \end{cfuncdesc} |
| 729 | |
| 730 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 731 | Increment the reference count for object \var{o}. The object may be |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 732 | \NULL{}, in which case the macro has no effect. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 733 | \end{cfuncdesc} |
| 734 | |
| 735 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 736 | Decrement the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 737 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 738 | \cfunction{Py_XDECREF()}. If the reference count reaches zero, the |
| 739 | object's type's deallocation function (which must not be \NULL{}) is |
| 740 | invoked. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 741 | |
| 742 | \strong{Warning:} The deallocation function can cause arbitrary Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 743 | code to be invoked (e.g. when a class instance with a |
| 744 | \method{__del__()} method is deallocated). While exceptions in such |
| 745 | code are not propagated, the executed code has free access to all |
| 746 | Python global variables. This means that any object that is reachable |
| 747 | from a global variable should be in a consistent state before |
| 748 | \cfunction{Py_DECREF()} is invoked. For example, code to delete an |
| 749 | object from a list should copy a reference to the deleted object in a |
| 750 | temporary variable, update the list data structure, and then call |
| 751 | \cfunction{Py_DECREF()} for the temporary variable. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 752 | \end{cfuncdesc} |
| 753 | |
| 754 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 755 | Decrement the reference count for object \var{o}. The object may be |
| 756 | \NULL{}, in which case the macro has no effect; otherwise the effect |
| 757 | is the same as for \cfunction{Py_DECREF()}, and the same warning |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 758 | applies. |
| 759 | \end{cfuncdesc} |
| 760 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 761 | The following functions or macros are only for use within the |
| 762 | interpreter core: \cfunction{_Py_Dealloc()}, |
| 763 | \cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as |
| 764 | well as the global variable \cdata{_Py_RefTotal}. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 765 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 766 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 767 | \chapter{Exception Handling \label{exceptionHandling}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 768 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 769 | The functions described in this chapter will let you handle and raise Python |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 770 | exceptions. It is important to understand some of the basics of |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 771 | Python exception handling. It works somewhat like the |
| 772 | \UNIX{} \cdata{errno} variable: there is a global indicator (per |
| 773 | thread) of the last error that occurred. Most functions don't clear |
| 774 | this on success, but will set it to indicate the cause of the error on |
| 775 | failure. Most functions also return an error indicator, usually |
| 776 | \NULL{} if they are supposed to return a pointer, or \code{-1} if they |
| 777 | return an integer (exception: the \cfunction{PyArg_Parse*()} functions |
| 778 | return \code{1} for success and \code{0} for failure). When a |
| 779 | function must fail because some function it called failed, it |
| 780 | generally doesn't set the error indicator; the function it called |
| 781 | already set it. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 782 | |
| 783 | The error indicator consists of three Python objects corresponding to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 784 | \withsubitem{(in module sys)}{ |
| 785 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 786 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and |
| 787 | \code{sys.exc_traceback}. API functions exist to interact with the |
| 788 | error indicator in various ways. There is a separate error indicator |
| 789 | for each thread. |
| 790 | |
| 791 | % XXX Order of these should be more thoughtful. |
| 792 | % Either alphabetical or some kind of structure. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 793 | |
| 794 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 795 | Print a standard traceback to \code{sys.stderr} and clear the error |
| 796 | indicator. Call this function only when the error indicator is set. |
| 797 | (Otherwise it will cause a fatal error!) |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 798 | \end{cfuncdesc} |
| 799 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 800 | \begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 801 | Test whether the error indicator is set. If set, return the exception |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 802 | \emph{type} (the first argument to the last call to one of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 803 | \cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 804 | not set, return \NULL{}. You do not own a reference to the return |
| 805 | value, so you do not need to \cfunction{Py_DECREF()} it. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 806 | \strong{Note:} Do not compare the return value to a specific |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 807 | exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 808 | below. (The comparison could easily fail since the exception may be |
| 809 | an instance instead of a class, in the case of a class exception, or |
| 810 | it may the a subclass of the expected exception.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 811 | \end{cfuncdesc} |
| 812 | |
| 813 | \begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 814 | Equivalent to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 815 | \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 816 | This should only be called when an exception is actually set; a memory |
| 817 | access violation will occur if no exception has been raised. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 818 | \end{cfuncdesc} |
| 819 | |
| 820 | \begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 821 | Return true if the \var{given} exception matches the exception in |
| 822 | \var{exc}. If \var{exc} is a class object, this also returns true |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 823 | when \var{given} is an instance of a subclass. If \var{exc} is a tuple, all |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 824 | exceptions in the tuple (and recursively in subtuples) are searched |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 825 | for a match. If \var{given} is \NULL, a memory access violation will |
| 826 | occur. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 827 | \end{cfuncdesc} |
| 828 | |
| 829 | \begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 830 | Under certain circumstances, the values returned by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 831 | \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that |
| 832 | \code{*\var{exc}} is a class object but \code{*\var{val}} is not an |
| 833 | instance of the same class. This function can be used to instantiate |
| 834 | the class in that case. If the values are already normalized, nothing |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 835 | happens. The delayed normalization is implemented to improve |
| 836 | performance. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 837 | \end{cfuncdesc} |
| 838 | |
| 839 | \begin{cfuncdesc}{void}{PyErr_Clear}{} |
| 840 | Clear the error indicator. If the error indicator is not set, there |
| 841 | is no effect. |
| 842 | \end{cfuncdesc} |
| 843 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 844 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, |
| 845 | PyObject **ptraceback} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 846 | Retrieve the error indicator into three variables whose addresses are |
| 847 | passed. If the error indicator is not set, set all three variables to |
| 848 | \NULL{}. If it is set, it will be cleared and you own a reference to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 849 | each object retrieved. The value and traceback object may be |
| 850 | \NULL{} even when the type object is not. \strong{Note:} This |
| 851 | function is normally only used by code that needs to handle exceptions |
| 852 | or by code that needs to save and restore the error indicator |
| 853 | temporarily. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 854 | \end{cfuncdesc} |
| 855 | |
Fred Drake | 17e6343 | 2000-08-31 05:50:40 +0000 | [diff] [blame] | 856 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, |
| 857 | PyObject *traceback} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 858 | Set the error indicator from the three objects. If the error |
| 859 | indicator is already set, it is cleared first. If the objects are |
| 860 | \NULL{}, the error indicator is cleared. Do not pass a \NULL{} type |
| 861 | and non-\NULL{} value or traceback. The exception type should be a |
| 862 | string or class; if it is a class, the value should be an instance of |
| 863 | that class. Do not pass an invalid exception type or value. |
| 864 | (Violating these rules will cause subtle problems later.) This call |
Fred Drake | 17e6343 | 2000-08-31 05:50:40 +0000 | [diff] [blame] | 865 | takes away a reference to each object, i.e.\ you must own a reference |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 866 | to each object before the call and after the call you no longer own |
| 867 | these references. (If you don't understand this, don't use this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 868 | function. I warned you.) \strong{Note:} This function is normally |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 869 | only used by code that needs to save and restore the error indicator |
| 870 | temporarily. |
| 871 | \end{cfuncdesc} |
| 872 | |
| 873 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message} |
| 874 | This is the most common way to set the error indicator. The first |
| 875 | argument specifies the exception type; it is normally one of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 876 | standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 877 | increment its reference count. The second argument is an error |
| 878 | message; it is converted to a string object. |
| 879 | \end{cfuncdesc} |
| 880 | |
| 881 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 882 | This function is similar to \cfunction{PyErr_SetString()} but lets you |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 883 | specify an arbitrary Python object for the ``value'' of the exception. |
| 884 | You need not increment its reference count. |
| 885 | \end{cfuncdesc} |
| 886 | |
Fred Drake | 7357770 | 2000-04-10 18:50:14 +0000 | [diff] [blame] | 887 | \begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception, |
Moshe Zadka | 57a5932 | 2000-09-01 09:47:20 +0000 | [diff] [blame] | 888 | const char *format, \moreargs} |
Fred Drake | 89fb035 | 2000-10-14 05:49:30 +0000 | [diff] [blame] | 889 | This function sets the error indicator. \var{exception} should be a |
| 890 | Python exception (string or class, not an instance). |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 891 | \var{format} should be a string, containing format codes, similar to |
Moshe Zadka | 57a5932 | 2000-09-01 09:47:20 +0000 | [diff] [blame] | 892 | \cfunction{printf}. The \code{width.precision} before a format code |
| 893 | is parsed, but the width part is ignored. |
| 894 | |
| 895 | \begin{tableii}{c|l}{character}{Character}{Meaning} |
| 896 | \lineii{c}{Character, as an \ctype{int} parameter} |
| 897 | \lineii{d}{Number in decimal, as an \ctype{int} parameter} |
| 898 | \lineii{x}{Number in hexadecimal, as an \ctype{int} parameter} |
| 899 | \lineii{x}{A string, as a \ctype{char *} parameter} |
| 900 | \end{tableii} |
| 901 | |
| 902 | An unrecognized format character causes all the rest of |
| 903 | the format string to be copied as-is to the result string, |
| 904 | and any extra arguments discarded. |
| 905 | |
| 906 | A new reference is returned, which is owned by the caller. |
Jeremy Hylton | 98605b5 | 2000-04-10 18:40:57 +0000 | [diff] [blame] | 907 | \end{cfuncdesc} |
| 908 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 909 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 910 | This is a shorthand for \samp{PyErr_SetObject(\var{type}, Py_None)}. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 911 | \end{cfuncdesc} |
| 912 | |
| 913 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 914 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 915 | \var{message})}, where \var{message} indicates that a built-in operation |
| 916 | was invoked with an illegal argument. It is mostly for internal use. |
| 917 | \end{cfuncdesc} |
| 918 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 919 | \begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 920 | This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 921 | returns \NULL{} so an object allocation function can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 922 | \samp{return PyErr_NoMemory();} when it runs out of memory. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 923 | \end{cfuncdesc} |
| 924 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 925 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 926 | This is a convenience function to raise an exception when a C library |
| 927 | function has returned an error and set the C variable \cdata{errno}. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 928 | It constructs a tuple object whose first item is the integer |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 929 | \cdata{errno} value and whose second item is the corresponding error |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 930 | message (gotten from \cfunction{strerror()}\ttindex{strerror()}), and |
| 931 | then calls |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 932 | \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 933 | the \cdata{errno} value is \constant{EINTR}, indicating an interrupted |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 934 | system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 935 | the error indicator, leaves it set to that. The function always |
| 936 | returns \NULL{}, so a wrapper function around a system call can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 937 | \samp{return PyErr_SetFromErrno();} when the system call returns an |
| 938 | error. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 939 | \end{cfuncdesc} |
| 940 | |
| 941 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 942 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 943 | \var{message})}, where \var{message} indicates that an internal |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 944 | operation (e.g. a Python/C API function) was invoked with an illegal |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 945 | argument. It is mostly for internal use. |
| 946 | \end{cfuncdesc} |
| 947 | |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 948 | \begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message} |
| 949 | Issue a warning message. The \var{category} argument is a warning |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 950 | category (see below) or \NULL; the \var{message} argument is a message |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 951 | string. |
| 952 | |
| 953 | This function normally prints a warning message to \var{sys.stderr}; |
| 954 | however, it is also possible that the user has specified that warnings |
| 955 | are to be turned into errors, and in that case this will raise an |
| 956 | exception. It is also possible that the function raises an exception |
| 957 | because of a problem with the warning machinery (the implementation |
| 958 | imports the \module{warnings} module to do the heavy lifting). The |
| 959 | return value is \code{0} if no exception is raised, or \code{-1} if |
| 960 | an exception is raised. (It is not possible to determine whether a |
| 961 | warning message is actually printed, nor what the reason is for the |
| 962 | exception; this is intentional.) If an exception is raised, the |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 963 | caller should do its normal exception handling |
| 964 | (e.g. \cfunction{Py_DECREF()} owned references and return an error |
| 965 | value). |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 966 | |
| 967 | Warning categories must be subclasses of \cdata{Warning}; the default |
| 968 | warning category is \cdata{RuntimeWarning}. The standard Python |
| 969 | warning categories are available as global variables whose names are |
| 970 | \samp{PyExc_} followed by the Python exception name. These have the |
| 971 | type \ctype{PyObject*}; they are all class objects. Their names are |
| 972 | \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning}, |
| 973 | \cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning}, and |
| 974 | \cdata{PyExc_RuntimeWarning}. \cdata{PyExc_Warning} is a subclass of |
| 975 | \cdata{PyExc_Exception}; the other warning categories are subclasses |
| 976 | of \cdata{PyExc_Warning}. |
| 977 | |
| 978 | For information about warning control, see the documentation for the |
Fred Drake | 316ef7c | 2001-01-04 05:56:34 +0000 | [diff] [blame] | 979 | \module{warnings} module and the \programopt{-W} option in the command |
| 980 | line documentation. There is no C API for warning control. |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 981 | \end{cfuncdesc} |
| 982 | |
Guido van Rossum | 1874c8f | 2001-02-28 23:46:44 +0000 | [diff] [blame] | 983 | \begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category, char *message, |
| 984 | char *filename, int lineno, char *module, PyObject *registry} |
| 985 | Issue a warning message with explicit control over all warning |
| 986 | attributes. This is a straightforward wrapper around the Python |
| 987 | function \function{warnings.warn_explicit()}, see there for more |
| 988 | information. The \var{module} and \var{registry} arguments may be |
| 989 | set to \code{NULL} to get the default effect described there. |
| 990 | \end{cfuncdesc} |
| 991 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 992 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{} |
| 993 | This function interacts with Python's signal handling. It checks |
| 994 | whether a signal has been sent to the processes and if so, invokes the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 995 | corresponding signal handler. If the |
| 996 | \module{signal}\refbimodindex{signal} module is supported, this can |
| 997 | invoke a signal handler written in Python. In all cases, the default |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 998 | effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the |
| 999 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 1000 | \exception{KeyboardInterrupt} exception. If an exception is raised the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1001 | error indicator is set and the function returns \code{1}; otherwise |
| 1002 | the function returns \code{0}. The error indicator may or may not be |
| 1003 | cleared if it was previously set. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1004 | \end{cfuncdesc} |
| 1005 | |
| 1006 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1007 | This function is obsolete. It simulates the effect of a |
| 1008 | \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1009 | \cfunction{PyErr_CheckSignals()} is called, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1010 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 1011 | \exception{KeyboardInterrupt} will be raised. |
| 1012 | It may be called without holding the interpreter lock. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1013 | \end{cfuncdesc} |
| 1014 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1015 | \begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name, |
| 1016 | PyObject *base, |
| 1017 | PyObject *dict} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1018 | This utility function creates and returns a new exception object. The |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1019 | \var{name} argument must be the name of the new exception, a C string |
| 1020 | of the form \code{module.class}. The \var{base} and |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1021 | \var{dict} arguments are normally \NULL{}. This creates a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1022 | class object derived from the root for all exceptions, the built-in |
| 1023 | name \exception{Exception} (accessible in C as |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1024 | \cdata{PyExc_Exception}). The \member{__module__} attribute of the |
| 1025 | new class is set to the first part (up to the last dot) of the |
| 1026 | \var{name} argument, and the class name is set to the last part (after |
| 1027 | the last dot). The \var{base} argument can be used to specify an |
| 1028 | alternate base class. The \var{dict} argument can be used to specify |
| 1029 | a dictionary of class variables and methods. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1030 | \end{cfuncdesc} |
| 1031 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1032 | \begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj} |
| 1033 | This utility function prints a warning message to \var{sys.stderr} |
| 1034 | when an exception has been set but it is impossible for the |
| 1035 | interpreter to actually raise the exception. It is used, for example, |
| 1036 | when an exception occurs in an \member{__del__} method. |
| 1037 | |
| 1038 | The function is called with a single argument \var{obj} that |
| 1039 | identifies where the context in which the unraisable exception |
| 1040 | occurred. The repr of \var{obj} will be printed in the warning |
| 1041 | message. |
| 1042 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1043 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1044 | \section{Standard Exceptions \label{standardExceptions}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1045 | |
| 1046 | All standard Python exceptions are available as global variables whose |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1047 | names are \samp{PyExc_} followed by the Python exception name. These |
| 1048 | have the type \ctype{PyObject*}; they are all class objects. For |
| 1049 | completeness, here are all the variables: |
| 1050 | |
| 1051 | \begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes} |
| 1052 | \lineiii{PyExc_Exception}{\exception{Exception}}{(1)} |
| 1053 | \lineiii{PyExc_StandardError}{\exception{StandardError}}{(1)} |
| 1054 | \lineiii{PyExc_ArithmeticError}{\exception{ArithmeticError}}{(1)} |
| 1055 | \lineiii{PyExc_LookupError}{\exception{LookupError}}{(1)} |
| 1056 | \lineiii{PyExc_AssertionError}{\exception{AssertionError}}{} |
| 1057 | \lineiii{PyExc_AttributeError}{\exception{AttributeError}}{} |
| 1058 | \lineiii{PyExc_EOFError}{\exception{EOFError}}{} |
| 1059 | \lineiii{PyExc_EnvironmentError}{\exception{EnvironmentError}}{(1)} |
| 1060 | \lineiii{PyExc_FloatingPointError}{\exception{FloatingPointError}}{} |
| 1061 | \lineiii{PyExc_IOError}{\exception{IOError}}{} |
| 1062 | \lineiii{PyExc_ImportError}{\exception{ImportError}}{} |
| 1063 | \lineiii{PyExc_IndexError}{\exception{IndexError}}{} |
| 1064 | \lineiii{PyExc_KeyError}{\exception{KeyError}}{} |
| 1065 | \lineiii{PyExc_KeyboardInterrupt}{\exception{KeyboardInterrupt}}{} |
| 1066 | \lineiii{PyExc_MemoryError}{\exception{MemoryError}}{} |
| 1067 | \lineiii{PyExc_NameError}{\exception{NameError}}{} |
| 1068 | \lineiii{PyExc_NotImplementedError}{\exception{NotImplementedError}}{} |
| 1069 | \lineiii{PyExc_OSError}{\exception{OSError}}{} |
| 1070 | \lineiii{PyExc_OverflowError}{\exception{OverflowError}}{} |
| 1071 | \lineiii{PyExc_RuntimeError}{\exception{RuntimeError}}{} |
| 1072 | \lineiii{PyExc_SyntaxError}{\exception{SyntaxError}}{} |
| 1073 | \lineiii{PyExc_SystemError}{\exception{SystemError}}{} |
| 1074 | \lineiii{PyExc_SystemExit}{\exception{SystemExit}}{} |
| 1075 | \lineiii{PyExc_TypeError}{\exception{TypeError}}{} |
| 1076 | \lineiii{PyExc_ValueError}{\exception{ValueError}}{} |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1077 | \lineiii{PyExc_WindowsError}{\exception{WindowsError}}{(2)} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1078 | \lineiii{PyExc_ZeroDivisionError}{\exception{ZeroDivisionError}}{} |
| 1079 | \end{tableiii} |
| 1080 | |
| 1081 | \noindent |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1082 | Notes: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1083 | \begin{description} |
| 1084 | \item[(1)] |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1085 | This is a base class for other standard exceptions. |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1086 | |
| 1087 | \item[(2)] |
| 1088 | Only defined on Windows; protect code that uses this by testing that |
| 1089 | the preprocessor macro \code{MS_WINDOWS} is defined. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1090 | \end{description} |
| 1091 | |
| 1092 | |
| 1093 | \section{Deprecation of String Exceptions} |
| 1094 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1095 | All exceptions built into Python or provided in the standard library |
| 1096 | are derived from \exception{Exception}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1097 | \withsubitem{(built-in exception)}{\ttindex{Exception}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1098 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1099 | String exceptions are still supported in the interpreter to allow |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1100 | existing code to run unmodified, but this will also change in a future |
| 1101 | release. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1103 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1104 | \chapter{Utilities \label{utilities}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1105 | |
| 1106 | The functions in this chapter perform various utility tasks, such as |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1107 | parsing function arguments and constructing Python values from C |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1108 | values. |
| 1109 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1110 | \section{OS Utilities \label{os}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1112 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1113 | Return true (nonzero) if the standard I/O file \var{fp} with name |
| 1114 | \var{filename} is deemed interactive. This is the case for files for |
| 1115 | which \samp{isatty(fileno(\var{fp}))} is true. If the global flag |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1116 | \cdata{Py_InteractiveFlag} is true, this function also returns true if |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1117 | the \var{filename} pointer is \NULL{} or if the name is equal to one of |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 1118 | the strings \code{'<stdin>'} or \code{'???'}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1119 | \end{cfuncdesc} |
| 1120 | |
| 1121 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1122 | Return the time of last modification of the file \var{filename}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1123 | The result is encoded in the same way as the timestamp returned by |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1124 | the standard C library function \cfunction{time()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1125 | \end{cfuncdesc} |
| 1126 | |
Fred Drake | cabbc3b | 2000-06-28 15:53:13 +0000 | [diff] [blame] | 1127 | \begin{cfuncdesc}{void}{PyOS_AfterFork}{} |
| 1128 | Function to update some internal state after a process fork; this |
| 1129 | should be called in the new process if the Python interpreter will |
| 1130 | continue to be used. If a new executable is loaded into the new |
| 1131 | process, this function does not need to be called. |
| 1132 | \end{cfuncdesc} |
| 1133 | |
Fred Drake | 17e6343 | 2000-08-31 05:50:40 +0000 | [diff] [blame] | 1134 | \begin{cfuncdesc}{int}{PyOS_CheckStack}{} |
| 1135 | Return true when the interpreter runs out of stack space. This is a |
| 1136 | reliable check, but is only available when \code{USE_STACKCHECK} is |
| 1137 | defined (currently on Windows using the Microsoft Visual C++ compiler |
| 1138 | and on the Macintosh). \code{USE_CHECKSTACK} will be defined |
| 1139 | automatically; you should never change the definition in your own |
| 1140 | code. |
| 1141 | \end{cfuncdesc} |
| 1142 | |
Guido van Rossum | c96ec6e | 2000-09-16 16:30:48 +0000 | [diff] [blame] | 1143 | \begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_getsig}{int i} |
| 1144 | Return the current signal handler for signal \var{i}. |
| 1145 | This is a thin wrapper around either \cfunction{sigaction} or |
| 1146 | \cfunction{signal}. Do not call those functions directly! |
| 1147 | \ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}. |
| 1148 | \end{cfuncdesc} |
| 1149 | |
| 1150 | \begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_setsig}{int i, PyOS_sighandler_t h} |
| 1151 | Set the signal handler for signal \var{i} to be \var{h}; |
| 1152 | return the old signal handler. |
| 1153 | This is a thin wrapper around either \cfunction{sigaction} or |
| 1154 | \cfunction{signal}. Do not call those functions directly! |
| 1155 | \ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}. |
| 1156 | \end{cfuncdesc} |
| 1157 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1158 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1159 | \section{Process Control \label{processControl}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1160 | |
| 1161 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 1162 | Print a fatal error message and kill the process. No cleanup is |
| 1163 | performed. This function should only be invoked when a condition is |
| 1164 | detected that would make it dangerous to continue using the Python |
| 1165 | interpreter; e.g., when the object administration appears to be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1166 | corrupted. On \UNIX{}, the standard C library function |
| 1167 | \cfunction{abort()}\ttindex{abort()} is called which will attempt to |
| 1168 | produce a \file{core} file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1169 | \end{cfuncdesc} |
| 1170 | |
| 1171 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1172 | Exit the current process. This calls |
| 1173 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 1174 | then calls the standard C library function |
| 1175 | \code{exit(\var{status})}\ttindex{exit()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1176 | \end{cfuncdesc} |
| 1177 | |
| 1178 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1179 | Register a cleanup function to be called by |
| 1180 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1181 | The cleanup function will be called with no arguments and should |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1182 | return no value. At most 32 \index{cleanup functions}cleanup |
| 1183 | functions can be registered. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1184 | When the registration is successful, \cfunction{Py_AtExit()} returns |
| 1185 | \code{0}; on failure, it returns \code{-1}. The cleanup function |
| 1186 | registered last is called first. Each cleanup function will be called |
| 1187 | at most once. Since Python's internal finallization will have |
| 1188 | completed before the cleanup function, no Python APIs should be called |
| 1189 | by \var{func}. |
| 1190 | \end{cfuncdesc} |
| 1191 | |
| 1192 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1193 | \section{Importing Modules \label{importing}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1194 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1195 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1196 | This is a simplified interface to |
| 1197 | \cfunction{PyImport_ImportModuleEx()} below, leaving the |
| 1198 | \var{globals} and \var{locals} arguments set to \NULL{}. When the |
| 1199 | \var{name} argument contains a dot (i.e., when it specifies a |
| 1200 | submodule of a package), the \var{fromlist} argument is set to the |
| 1201 | list \code{['*']} so that the return value is the named module rather |
| 1202 | than the top-level package containing it as would otherwise be the |
| 1203 | case. (Unfortunately, this has an additional side effect when |
| 1204 | \var{name} in fact specifies a subpackage instead of a submodule: the |
| 1205 | submodules specified in the package's \code{__all__} variable are |
| 1206 | \index{package variable!\code{__all__}} |
| 1207 | \withsubitem{(package variable)}{\ttindex{__all__}}loaded.) Return a |
| 1208 | new reference to the imported module, or |
| 1209 | \NULL{} with an exception set on failure (the module may still be |
| 1210 | created in this case --- examine \code{sys.modules} to find out). |
| 1211 | \withsubitem{(in module sys)}{\ttindex{modules}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1212 | \end{cfuncdesc} |
| 1213 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1214 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1215 | Import a module. This is best described by referring to the built-in |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1216 | Python function \function{__import__()}\bifuncindex{__import__}, as |
| 1217 | the standard \function{__import__()} function calls this function |
| 1218 | directly. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1219 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1220 | The return value is a new reference to the imported module or |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 1221 | top-level package, or \NULL{} with an exception set on failure |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 1222 | (the module may still be created in this case). Like for |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1223 | \function{__import__()}, the return value when a submodule of a |
| 1224 | package was requested is normally the top-level package, unless a |
| 1225 | non-empty \var{fromlist} was given. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1226 | \end{cfuncdesc} |
| 1227 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1228 | \begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1229 | This is a higher-level interface that calls the current ``import hook |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1230 | function''. It invokes the \function{__import__()} function from the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1231 | \code{__builtins__} of the current globals. This means that the |
| 1232 | import is done using whatever import hooks are installed in the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 1233 | current environment, e.g. by \module{rexec}\refstmodindex{rexec} or |
| 1234 | \module{ihooks}\refstmodindex{ihooks}. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1235 | \end{cfuncdesc} |
| 1236 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1237 | \begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1238 | Reload a module. This is best described by referring to the built-in |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1239 | Python function \function{reload()}\bifuncindex{reload}, as the standard |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1240 | \function{reload()} function calls this function directly. Return a |
| 1241 | new reference to the reloaded module, or \NULL{} with an exception set |
| 1242 | on failure (the module still exists in this case). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1243 | \end{cfuncdesc} |
| 1244 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1245 | \begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1246 | Return the module object corresponding to a module name. The |
| 1247 | \var{name} argument may be of the form \code{package.module}). First |
| 1248 | check the modules dictionary if there's one there, and if not, create |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1249 | a new one and insert in in the modules dictionary. |
Guido van Rossum | a096a2e | 1998-11-02 17:02:42 +0000 | [diff] [blame] | 1250 | Warning: this function does not load or import the module; if the |
| 1251 | module wasn't already loaded, you will get an empty module object. |
| 1252 | Use \cfunction{PyImport_ImportModule()} or one of its variants to |
| 1253 | import a module. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1254 | Return \NULL{} with an exception set on failure. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1255 | \end{cfuncdesc} |
| 1256 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1257 | \begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1258 | Given a module name (possibly of the form \code{package.module}) and a |
| 1259 | code object read from a Python bytecode file or obtained from the |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1260 | built-in function \function{compile()}\bifuncindex{compile}, load the |
| 1261 | module. Return a new reference to the module object, or \NULL{} with |
| 1262 | an exception set if an error occurred (the module may still be created |
| 1263 | in this case). (This function would reload the module if it was |
| 1264 | already imported.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1265 | \end{cfuncdesc} |
| 1266 | |
| 1267 | \begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1268 | Return the magic number for Python bytecode files (a.k.a. |
| 1269 | \file{.pyc} and \file{.pyo} files). The magic number should be |
| 1270 | present in the first four bytes of the bytecode file, in little-endian |
| 1271 | byte order. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1272 | \end{cfuncdesc} |
| 1273 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1274 | \begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1275 | Return the dictionary used for the module administration |
| 1276 | (a.k.a. \code{sys.modules}). Note that this is a per-interpreter |
| 1277 | variable. |
| 1278 | \end{cfuncdesc} |
| 1279 | |
| 1280 | \begin{cfuncdesc}{void}{_PyImport_Init}{} |
| 1281 | Initialize the import mechanism. For internal use only. |
| 1282 | \end{cfuncdesc} |
| 1283 | |
| 1284 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 1285 | Empty the module table. For internal use only. |
| 1286 | \end{cfuncdesc} |
| 1287 | |
| 1288 | \begin{cfuncdesc}{void}{_PyImport_Fini}{} |
| 1289 | Finalize the import mechanism. For internal use only. |
| 1290 | \end{cfuncdesc} |
| 1291 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1292 | \begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1293 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1294 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1295 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1296 | \begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1297 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1298 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1299 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 1300 | \begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name} |
| 1301 | Load a frozen module named \var{name}. Return \code{1} for success, |
| 1302 | \code{0} if the module is not found, and \code{-1} with an exception |
| 1303 | set if the initialization failed. To access the imported module on a |
| 1304 | successful load, use \cfunction{PyImport_ImportModule()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1305 | (Note the misnomer --- this function would reload the module if it was |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1306 | already imported.) |
| 1307 | \end{cfuncdesc} |
| 1308 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1309 | \begin{ctypedesc}[_frozen]{struct _frozen} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1310 | This is the structure type definition for frozen module descriptors, |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1311 | as generated by the \program{freeze}\index{freeze utility} utility |
| 1312 | (see \file{Tools/freeze/} in the Python source distribution). Its |
Fred Drake | e0d9a83 | 2000-09-01 05:30:00 +0000 | [diff] [blame] | 1313 | definition, found in \file{Include/import.h}, is: |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1314 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1315 | \begin{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1316 | struct _frozen { |
Fred Drake | 36fbe76 | 1997-10-13 18:18:33 +0000 | [diff] [blame] | 1317 | char *name; |
| 1318 | unsigned char *code; |
| 1319 | int size; |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1320 | }; |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1321 | \end{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1322 | \end{ctypedesc} |
| 1323 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1324 | \begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1325 | This pointer is initialized to point to an array of \ctype{struct |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1326 | _frozen} records, terminated by one whose members are all |
| 1327 | \NULL{} or zero. When a frozen module is imported, it is searched in |
| 1328 | this table. Third-party code could play tricks with this to provide a |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1329 | dynamically created collection of frozen modules. |
| 1330 | \end{cvardesc} |
| 1331 | |
Fred Drake | e0d9a83 | 2000-09-01 05:30:00 +0000 | [diff] [blame] | 1332 | \begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name, |
| 1333 | void (*initfunc)(void)} |
| 1334 | Add a single module to the existing table of built-in modules. This |
| 1335 | is a convenience wrapper around \cfunction{PyImport_ExtendInittab()}, |
| 1336 | returning \code{-1} if the table could not be extended. The new |
| 1337 | module can be imported by the name \var{name}, and uses the function |
| 1338 | \var{initfunc} as the initialization function called on the first |
| 1339 | attempted import. This should be called before |
| 1340 | \cfunction{Py_Initialize()}. |
| 1341 | \end{cfuncdesc} |
| 1342 | |
| 1343 | \begin{ctypedesc}[_inittab]{struct _inittab} |
| 1344 | Structure describing a single entry in the list of built-in modules. |
| 1345 | Each of these structures gives the name and initialization function |
| 1346 | for a module built into the interpreter. Programs which embed Python |
| 1347 | may use an array of these structures in conjunction with |
| 1348 | \cfunction{PyImport_ExtendInittab()} to provide additional built-in |
| 1349 | modules. The structure is defined in \file{Include/import.h} as: |
| 1350 | |
| 1351 | \begin{verbatim} |
| 1352 | struct _inittab { |
| 1353 | char *name; |
| 1354 | void (*initfunc)(void); |
| 1355 | }; |
| 1356 | \end{verbatim} |
| 1357 | \end{ctypedesc} |
| 1358 | |
| 1359 | \begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab} |
| 1360 | Add a collection of modules to the table of built-in modules. The |
| 1361 | \var{newtab} array must end with a sentinel entry which contains |
| 1362 | \NULL{} for the \member{name} field; failure to provide the sentinel |
| 1363 | value can result in a memory fault. Returns \code{0} on success or |
| 1364 | \code{-1} if insufficient memory could be allocated to extend the |
| 1365 | internal table. In the event of failure, no modules are added to the |
| 1366 | internal table. This should be called before |
| 1367 | \cfunction{Py_Initialize()}. |
| 1368 | \end{cfuncdesc} |
| 1369 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1370 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1371 | \chapter{Abstract Objects Layer \label{abstract}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1372 | |
| 1373 | The functions in this chapter interact with Python objects regardless |
| 1374 | of their type, or with wide classes of object types (e.g. all |
| 1375 | numerical types, or all sequence types). When used on object types |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1376 | for which they do not apply, they will raise a Python exception. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1377 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1378 | \section{Object Protocol \label{object}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1379 | |
| 1380 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1381 | Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error. |
| 1382 | The flags argument is used to enable certain printing options. The |
| 1383 | only option currently supported is \constant{Py_PRINT_RAW}; if given, |
| 1384 | the \function{str()} of the object is written instead of the |
| 1385 | \function{repr()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1386 | \end{cfuncdesc} |
| 1387 | |
| 1388 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1389 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1390 | \code{0} otherwise. This is equivalent to the Python expression |
| 1391 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1392 | This function always succeeds. |
| 1393 | \end{cfuncdesc} |
| 1394 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1395 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, |
| 1396 | char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1397 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1398 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1399 | This is the equivalent of the Python expression |
| 1400 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1401 | \end{cfuncdesc} |
| 1402 | |
| 1403 | |
| 1404 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1405 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1406 | \code{0} otherwise. This is equivalent to the Python expression |
| 1407 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1408 | This function always succeeds. |
| 1409 | \end{cfuncdesc} |
| 1410 | |
| 1411 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1412 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, |
| 1413 | PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1414 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1415 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1416 | This is the equivalent of the Python expression |
| 1417 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1418 | \end{cfuncdesc} |
| 1419 | |
| 1420 | |
| 1421 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1422 | Set the value of the attribute named \var{attr_name}, for object |
| 1423 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This is |
| 1424 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1425 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1426 | \end{cfuncdesc} |
| 1427 | |
| 1428 | |
| 1429 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1430 | Set the value of the attribute named \var{attr_name}, for |
| 1431 | object \var{o}, |
| 1432 | to the value \var{v}. Returns \code{-1} on failure. This is |
| 1433 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1434 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1435 | \end{cfuncdesc} |
| 1436 | |
| 1437 | |
| 1438 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1439 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1440 | \code{-1} on failure. This is the equivalent of the Python |
| 1441 | statement: \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1442 | \end{cfuncdesc} |
| 1443 | |
| 1444 | |
| 1445 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1446 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1447 | \code{-1} on failure. This is the equivalent of the Python |
| 1448 | statement \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1449 | \end{cfuncdesc} |
| 1450 | |
| 1451 | |
| 1452 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1453 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1454 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1455 | \var{o2}. The result of the comparison is returned in \var{result}. |
| 1456 | Returns \code{-1} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1457 | statement\bifuncindex{cmp} \samp{\var{result} = cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1458 | \end{cfuncdesc} |
| 1459 | |
| 1460 | |
| 1461 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1462 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1463 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1464 | \var{o2}. Returns the result of the comparison on success. On error, |
| 1465 | the value returned is undefined; use \cfunction{PyErr_Occurred()} to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1466 | detect an error. This is equivalent to the Python |
| 1467 | expression\bifuncindex{cmp} \samp{cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1468 | \end{cfuncdesc} |
| 1469 | |
| 1470 | |
| 1471 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1472 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1473 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1474 | the equivalent of the Python expression \samp{repr(\var{o})}. |
| 1475 | Called by the \function{repr()}\bifuncindex{repr} built-in function |
| 1476 | and by reverse quotes. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1477 | \end{cfuncdesc} |
| 1478 | |
| 1479 | |
| 1480 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1481 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1482 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1483 | the equivalent of the Python expression \samp{str(\var{o})}. |
| 1484 | Called by the \function{str()}\bifuncindex{str} built-in function and |
| 1485 | by the \keyword{print} statement. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1486 | \end{cfuncdesc} |
| 1487 | |
| 1488 | |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1489 | \begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o} |
| 1490 | Compute a Unicode string representation of object \var{o}. Returns the |
| 1491 | Unicode string representation on success, \NULL{} on failure. This is |
| 1492 | the equivalent of the Python expression \samp{unistr(\var{o})}. |
| 1493 | Called by the \function{unistr()}\bifuncindex{unistr} built-in function. |
| 1494 | \end{cfuncdesc} |
| 1495 | |
Fred Drake | 58c8f9f | 2001-03-28 21:14:32 +0000 | [diff] [blame] | 1496 | \begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls} |
| 1497 | Return \code{1} if \var{inst} is an instance of the class \var{cls} or |
| 1498 | a subclass of \var{cls}. If \var{cls} is a type object rather than a |
| 1499 | class object, \cfunction{PyObject_IsInstance()} returns \code{1} if |
| 1500 | \var{inst} is of type \var{cls}. If \var{inst} is not a class |
| 1501 | instance and \var{cls} is neither a type object or class object, |
| 1502 | \var{inst} must have a \member{__class__} attribute --- the class |
| 1503 | relationship of the value of that attribute with \var{cls} will be |
| 1504 | used to determine the result of this function. |
| 1505 | \versionadded{2.1} |
| 1506 | \end{cfuncdesc} |
| 1507 | |
| 1508 | Subclass determination is done in a fairly straightforward way, but |
| 1509 | includes a wrinkle that implementors of extensions to the class system |
| 1510 | may want to be aware of. If \class{A} and \class{B} are class |
| 1511 | objects, \class{B} is a subclass of \class{A} if it inherits from |
| 1512 | \class{A} either directly or indirectly. If either is not a class |
| 1513 | object, a more general mechanism is used to determine the class |
| 1514 | relationship of the two objects. When testing if \var{B} is a |
| 1515 | subclass of \var{A}, if \var{A} is \var{B}, |
| 1516 | \cfunction{PyObject_IsSubclass()} returns true. If \var{A} and |
| 1517 | \var{B} are different objects, \var{B}'s \member{__bases__} attribute |
| 1518 | is searched in a depth-first fashion for \var{A} --- the presence of |
| 1519 | the \member{__bases__} attribute is considered sufficient for this |
| 1520 | determination. |
| 1521 | |
| 1522 | \begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived, |
| 1523 | PyObject *cls} |
| 1524 | Returns \code{1} if the class \var{derived} is identical to or derived |
| 1525 | from the class \var{cls}, otherwise returns \code{0}. In case of an |
| 1526 | error, returns \code{-1}. If either \var{derived} or \var{cls} is not |
| 1527 | an actual class object, this function uses the generic algorithm |
| 1528 | described above. |
| 1529 | \versionadded{2.1} |
| 1530 | \end{cfuncdesc} |
| 1531 | |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1532 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1533 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1534 | Determine if the object \var{o} is callable. Return \code{1} if the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1535 | object is callable and \code{0} otherwise. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1536 | This function always succeeds. |
| 1537 | \end{cfuncdesc} |
| 1538 | |
| 1539 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1540 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, |
| 1541 | PyObject *args} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1542 | Call a callable Python object \var{callable_object}, with |
| 1543 | arguments given by the tuple \var{args}. If no arguments are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1544 | needed, then \var{args} may be \NULL{}. Returns the result of the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1545 | call on success, or \NULL{} on failure. This is the equivalent |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1546 | of the Python expression \samp{apply(\var{callable_object}, \var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1547 | \bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1548 | \end{cfuncdesc} |
| 1549 | |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1550 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, |
| 1551 | char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1552 | Call a callable Python object \var{callable_object}, with a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1553 | variable number of C arguments. The C arguments are described |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1554 | using a \cfunction{Py_BuildValue()} style format string. The format may |
| 1555 | be \NULL{}, indicating that no arguments are provided. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1556 | result of the call on success, or \NULL{} on failure. This is |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1557 | the equivalent of the Python expression \samp{apply(\var{callable_object}, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1558 | \var{args})}.\bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1559 | \end{cfuncdesc} |
| 1560 | |
| 1561 | |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1562 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, |
| 1563 | char *method, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1564 | Call the method named \var{m} of object \var{o} with a variable number |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1565 | of C arguments. The C arguments are described by a |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1566 | \cfunction{Py_BuildValue()} format string. The format may be \NULL{}, |
| 1567 | indicating that no arguments are provided. Returns the result of the |
| 1568 | call on success, or \NULL{} on failure. This is the equivalent of the |
| 1569 | Python expression \samp{\var{o}.\var{method}(\var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1570 | Note that special method names, such as \method{__add__()}, |
| 1571 | \method{__getitem__()}, and so on are not supported. The specific |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1572 | abstract-object routines for these must be used. |
| 1573 | \end{cfuncdesc} |
| 1574 | |
| 1575 | |
| 1576 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1577 | Compute and return the hash value of an object \var{o}. On |
| 1578 | failure, return \code{-1}. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1579 | expression \samp{hash(\var{o})}.\bifuncindex{hash} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1580 | \end{cfuncdesc} |
| 1581 | |
| 1582 | |
| 1583 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1584 | Returns \code{1} if the object \var{o} is considered to be true, and |
| 1585 | \code{0} otherwise. This is equivalent to the Python expression |
| 1586 | \samp{not not \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1587 | This function always succeeds. |
| 1588 | \end{cfuncdesc} |
| 1589 | |
| 1590 | |
| 1591 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 1592 | On success, returns a type object corresponding to the object |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1593 | type of object \var{o}. On failure, returns \NULL{}. This is |
| 1594 | equivalent to the Python expression \samp{type(\var{o})}. |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1595 | \bifuncindex{type} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1596 | \end{cfuncdesc} |
| 1597 | |
| 1598 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1599 | Return the length of object \var{o}. If the object \var{o} provides |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1600 | both sequence and mapping protocols, the sequence length is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1601 | returned. On error, \code{-1} is returned. This is the equivalent |
| 1602 | to the Python expression \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1603 | \end{cfuncdesc} |
| 1604 | |
| 1605 | |
| 1606 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1607 | Return element of \var{o} corresponding to the object \var{key} or |
| 1608 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1609 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1610 | \end{cfuncdesc} |
| 1611 | |
| 1612 | |
| 1613 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1614 | Map the object \var{key} to the value \var{v}. |
| 1615 | Returns \code{-1} on failure. This is the equivalent |
| 1616 | of the Python statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1617 | \end{cfuncdesc} |
| 1618 | |
| 1619 | |
Guido van Rossum | d1dbf63 | 1999-01-22 20:10:49 +0000 | [diff] [blame] | 1620 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1621 | Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on |
| 1622 | failure. This is the equivalent of the Python statement \samp{del |
| 1623 | \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1624 | \end{cfuncdesc} |
| 1625 | |
Andrew M. Kuchling | 8c46b30 | 2000-07-13 23:58:16 +0000 | [diff] [blame] | 1626 | \begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o} |
| 1627 | Derives a file-descriptor from a Python object. If the object |
| 1628 | is an integer or long integer, its value is returned. If not, the |
| 1629 | object's \method{fileno()} method is called if it exists; the method |
| 1630 | must return an integer or long integer, which is returned as the file |
| 1631 | descriptor value. Returns \code{-1} on failure. |
| 1632 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1633 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1634 | \section{Number Protocol \label{number}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1635 | |
| 1636 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1637 | Returns \code{1} if the object \var{o} provides numeric protocols, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1638 | false otherwise. |
| 1639 | This function always succeeds. |
| 1640 | \end{cfuncdesc} |
| 1641 | |
| 1642 | |
| 1643 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1644 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 1645 | failure. This is the equivalent of the Python expression |
| 1646 | \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1647 | \end{cfuncdesc} |
| 1648 | |
| 1649 | |
| 1650 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1651 | Returns the result of subtracting \var{o2} from \var{o1}, or |
| 1652 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1653 | \samp{\var{o1} - \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1654 | \end{cfuncdesc} |
| 1655 | |
| 1656 | |
| 1657 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1658 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1659 | failure. This is the equivalent of the Python expression |
| 1660 | \samp{\var{o1} * \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1661 | \end{cfuncdesc} |
| 1662 | |
| 1663 | |
| 1664 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1665 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1666 | failure. |
| 1667 | This is the equivalent of the Python expression \samp{\var{o1} / |
| 1668 | \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1669 | \end{cfuncdesc} |
| 1670 | |
| 1671 | |
| 1672 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1673 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1674 | failure. This is the equivalent of the Python expression |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1675 | \samp{\var{o1} \%\ \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1676 | \end{cfuncdesc} |
| 1677 | |
| 1678 | |
| 1679 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1680 | See the built-in function \function{divmod()}\bifuncindex{divmod}. |
| 1681 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 1682 | expression \samp{divmod(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1683 | \end{cfuncdesc} |
| 1684 | |
| 1685 | |
| 1686 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1687 | See the built-in function \function{pow()}\bifuncindex{pow}. Returns |
| 1688 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1689 | \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1690 | If \var{o3} is to be ignored, pass \cdata{Py_None} in its place |
| 1691 | (passing \NULL{} for \var{o3} would cause an illegal memory access). |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1692 | \end{cfuncdesc} |
| 1693 | |
| 1694 | |
| 1695 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1696 | Returns the negation of \var{o} on success, or \NULL{} on failure. |
| 1697 | This is the equivalent of the Python expression \samp{-\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1698 | \end{cfuncdesc} |
| 1699 | |
| 1700 | |
| 1701 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1702 | Returns \var{o} on success, or \NULL{} on failure. |
| 1703 | This is the equivalent of the Python expression \samp{+\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1704 | \end{cfuncdesc} |
| 1705 | |
| 1706 | |
| 1707 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1708 | Returns the absolute value of \var{o}, or \NULL{} on failure. This is |
| 1709 | the equivalent of the Python expression \samp{abs(\var{o})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1710 | \bifuncindex{abs} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1711 | \end{cfuncdesc} |
| 1712 | |
| 1713 | |
| 1714 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1715 | Returns the bitwise negation of \var{o} on success, or \NULL{} on |
| 1716 | failure. This is the equivalent of the Python expression |
| 1717 | \samp{\~\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1718 | \end{cfuncdesc} |
| 1719 | |
| 1720 | |
| 1721 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1722 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 1723 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1724 | expression \samp{\var{o1} <\code{<} \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1725 | \end{cfuncdesc} |
| 1726 | |
| 1727 | |
| 1728 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1729 | Returns the result of right shifting \var{o1} by \var{o2} on success, |
| 1730 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1731 | expression \samp{\var{o1} >\code{>} \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1732 | \end{cfuncdesc} |
| 1733 | |
| 1734 | |
| 1735 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1736 | Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and |
| 1737 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1738 | \samp{\var{o1} \&\ \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1739 | \end{cfuncdesc} |
| 1740 | |
| 1741 | |
| 1742 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1743 | Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on success, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1744 | or \NULL{} on failure. This is the equivalent of the Python |
| 1745 | expression \samp{\var{o1} \^{ }\var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1746 | \end{cfuncdesc} |
| 1747 | |
| 1748 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1749 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or |
| 1750 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1751 | \samp{\var{o1} | \var{o2}}. |
| 1752 | \end{cfuncdesc} |
| 1753 | |
| 1754 | |
| 1755 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2} |
| 1756 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on failure. |
| 1757 | The operation is done \emph{in-place} when \var{o1} supports it. This is the |
| 1758 | equivalent of the Python expression \samp{\var{o1} += \var{o2}}. |
| 1759 | \end{cfuncdesc} |
| 1760 | |
| 1761 | |
| 1762 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1, PyObject *o2} |
| 1763 | Returns the result of subtracting \var{o2} from \var{o1}, or |
| 1764 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o1} |
| 1765 | supports it. This is the equivalent of the Python expression \samp{\var{o1} |
| 1766 | -= \var{o2}}. |
| 1767 | \end{cfuncdesc} |
| 1768 | |
| 1769 | |
| 1770 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1, PyObject *o2} |
| 1771 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1772 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
| 1773 | This is the equivalent of the Python expression \samp{\var{o1} *= \var{o2}}. |
| 1774 | \end{cfuncdesc} |
| 1775 | |
| 1776 | |
| 1777 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1, PyObject *o2} |
| 1778 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on failure. |
| 1779 | The operation is done \emph{in-place} when \var{o1} supports it. This is the |
| 1780 | equivalent of the Python expression \samp{\var{o1} /= \var{o2}}. |
| 1781 | \end{cfuncdesc} |
| 1782 | |
| 1783 | |
| 1784 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1, PyObject *o2} |
| 1785 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1786 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
| 1787 | This is the equivalent of the Python expression \samp{\var{o1} \%= \var{o2}}. |
| 1788 | \end{cfuncdesc} |
| 1789 | |
| 1790 | |
| 1791 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1, PyObject *o2, PyObject *o3} |
| 1792 | See the built-in function \function{pow()}\bifuncindex{pow}. Returns |
| 1793 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o1} |
| 1794 | supports it. This is the equivalent of the Python expression \samp{\var{o1} |
| 1795 | **= \var{o2}} when o3 is \cdata{Py_None}, or an in-place variant of |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1796 | \samp{pow(\var{o1}, \var{o2}, \var{o3})} otherwise. If \var{o3} is to be |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1797 | ignored, pass \cdata{Py_None} in its place (passing \NULL{} for \var{o3} |
| 1798 | would cause an illegal memory access). |
| 1799 | \end{cfuncdesc} |
| 1800 | |
| 1801 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1, PyObject *o2} |
| 1802 | Returns the result of left shifting \var{o1} by \var{o2} on success, or |
| 1803 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o1} |
| 1804 | supports it. This is the equivalent of the Python expression \samp{\var{o1} |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1805 | <\code{<=} \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1806 | \end{cfuncdesc} |
| 1807 | |
| 1808 | |
| 1809 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1, PyObject *o2} |
| 1810 | Returns the result of right shifting \var{o1} by \var{o2} on success, or |
| 1811 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o1} |
| 1812 | supports it. This is the equivalent of the Python expression \samp{\var{o1} |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1813 | >\code{>=} \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1814 | \end{cfuncdesc} |
| 1815 | |
| 1816 | |
| 1817 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2} |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1818 | Returns the ``bitwise and'' of \var{o1} and \var{o2} on success |
| 1819 | and \NULL{} on failure. The operation is done \emph{in-place} when |
| 1820 | \var{o1} supports it. This is the equivalent of the Python expression |
| 1821 | \samp{\var{o1} \&= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1822 | \end{cfuncdesc} |
| 1823 | |
| 1824 | |
| 1825 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2} |
| 1826 | Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on success, or |
| 1827 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o1} |
| 1828 | supports it. This is the equivalent of the Python expression \samp{\var{o1} |
| 1829 | \^= \var{o2}}. |
| 1830 | \end{cfuncdesc} |
| 1831 | |
| 1832 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2} |
| 1833 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or \NULL{} |
| 1834 | on failure. The operation is done \emph{in-place} when \var{o1} supports |
| 1835 | it. This is the equivalent of the Python expression \samp{\var{o1} |= |
| 1836 | \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1837 | \end{cfuncdesc} |
| 1838 | |
Fred Drake | c0e6c5b | 2000-09-22 18:17:49 +0000 | [diff] [blame] | 1839 | \begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1840 | This function takes the addresses of two variables of type |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1841 | \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}} and |
| 1842 | \code{*\var{p2}} have the same type, increment their reference count |
| 1843 | and return \code{0} (success). If the objects can be converted to a |
| 1844 | common numeric type, replace \code{*p1} and \code{*p2} by their |
| 1845 | converted value (with 'new' reference counts), and return \code{0}. |
| 1846 | If no conversion is possible, or if some other error occurs, return |
| 1847 | \code{-1} (failure) and don't increment the reference counts. The |
| 1848 | call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 1849 | statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}. |
| 1850 | \bifuncindex{coerce} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1851 | \end{cfuncdesc} |
| 1852 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1853 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1854 | Returns the \var{o} converted to an integer object on success, or |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1855 | \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1856 | expression \samp{int(\var{o})}.\bifuncindex{int} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1857 | \end{cfuncdesc} |
| 1858 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1859 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1860 | Returns the \var{o} converted to a long integer object on success, |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1861 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1862 | expression \samp{long(\var{o})}.\bifuncindex{long} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1863 | \end{cfuncdesc} |
| 1864 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1865 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1866 | Returns the \var{o} converted to a float object on success, or |
| 1867 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1868 | \samp{float(\var{o})}.\bifuncindex{float} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1869 | \end{cfuncdesc} |
| 1870 | |
| 1871 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1872 | \section{Sequence Protocol \label{sequence}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1873 | |
| 1874 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1875 | Return \code{1} if the object provides sequence protocol, and |
| 1876 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1877 | \end{cfuncdesc} |
| 1878 | |
Fred Drake | c6a3cb4 | 2001-04-04 01:25:17 +0000 | [diff] [blame] | 1879 | \begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1880 | Returns the number of objects in sequence \var{o} on success, and |
| 1881 | \code{-1} on failure. For objects that do not provide sequence |
| 1882 | protocol, this is equivalent to the Python expression |
| 1883 | \samp{len(\var{o})}.\bifuncindex{len} |
| 1884 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1885 | |
Fred Drake | c6a3cb4 | 2001-04-04 01:25:17 +0000 | [diff] [blame] | 1886 | \begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o} |
| 1887 | Alternate name for \cfunction{PySequence_Size()}. |
| 1888 | \end{cfuncdesc} |
| 1889 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1890 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1891 | Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1892 | failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1893 | expression \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1894 | \end{cfuncdesc} |
| 1895 | |
| 1896 | |
| 1897 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1898 | Return the result of repeating sequence object |
| 1899 | \var{o} \var{count} times, or \NULL{} on failure. This is the |
| 1900 | equivalent of the Python expression \samp{\var{o} * \var{count}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1901 | \end{cfuncdesc} |
| 1902 | |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1903 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1, PyObject *o2} |
| 1904 | Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on |
| 1905 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
| 1906 | This is the equivalent of the Python expression \samp{\var{o1} += \var{o2}}. |
| 1907 | \end{cfuncdesc} |
| 1908 | |
| 1909 | |
| 1910 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count} |
| 1911 | Return the result of repeating sequence object \var{o} \var{count} times, or |
| 1912 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o} |
| 1913 | supports it. This is the equivalent of the Python expression \samp{\var{o} |
| 1914 | *= \var{count}}. |
| 1915 | \end{cfuncdesc} |
| 1916 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1917 | |
| 1918 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1919 | Return the \var{i}th element of \var{o}, or \NULL{} on failure. This |
| 1920 | is the equivalent of the Python expression \samp{\var{o}[\var{i}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1921 | \end{cfuncdesc} |
| 1922 | |
| 1923 | |
| 1924 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1925 | Return the slice of sequence object \var{o} between \var{i1} and |
| 1926 | \var{i2}, or \NULL{} on failure. This is the equivalent of the Python |
| 1927 | expression \samp{\var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1928 | \end{cfuncdesc} |
| 1929 | |
| 1930 | |
| 1931 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1932 | Assign object \var{v} to the \var{i}th element of \var{o}. |
| 1933 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1934 | statement \samp{\var{o}[\var{i}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1935 | \end{cfuncdesc} |
| 1936 | |
| 1937 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1938 | Delete the \var{i}th element of object \var{o}. Returns |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1939 | \code{-1} on failure. This is the equivalent of the Python |
| 1940 | statement \samp{del \var{o}[\var{i}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1941 | \end{cfuncdesc} |
| 1942 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1943 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, |
| 1944 | int i2, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1945 | Assign the sequence object \var{v} to the slice in sequence |
| 1946 | object \var{o} from \var{i1} to \var{i2}. This is the equivalent of |
| 1947 | the Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1948 | \end{cfuncdesc} |
| 1949 | |
| 1950 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1951 | Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}. |
| 1952 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1953 | statement \samp{del \var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1954 | \end{cfuncdesc} |
| 1955 | |
| 1956 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1957 | Returns the \var{o} as a tuple on success, and \NULL{} on failure. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1958 | This is equivalent to the Python expression \samp{tuple(\var{o})}. |
| 1959 | \bifuncindex{tuple} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1960 | \end{cfuncdesc} |
| 1961 | |
| 1962 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1963 | Return the number of occurrences of \var{value} in \var{o}, that is, |
| 1964 | return the number of keys for which \code{\var{o}[\var{key}] == |
| 1965 | \var{value}}. On failure, return \code{-1}. This is equivalent to |
| 1966 | the Python expression \samp{\var{o}.count(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1967 | \end{cfuncdesc} |
| 1968 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1969 | \begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1970 | Determine if \var{o} contains \var{value}. If an item in \var{o} is |
| 1971 | equal to \var{value}, return \code{1}, otherwise return \code{0}. On |
| 1972 | error, return \code{-1}. This is equivalent to the Python expression |
| 1973 | \samp{\var{value} in \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1974 | \end{cfuncdesc} |
| 1975 | |
| 1976 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1977 | Return the first index \var{i} for which \code{\var{o}[\var{i}] == |
| 1978 | \var{value}}. On error, return \code{-1}. This is equivalent to |
| 1979 | the Python expression \samp{\var{o}.index(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1980 | \end{cfuncdesc} |
| 1981 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 1982 | \begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o} |
| 1983 | Return a list object with the same contents as the arbitrary sequence |
| 1984 | \var{o}. The returned list is guaranteed to be new. |
| 1985 | \end{cfuncdesc} |
| 1986 | |
| 1987 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 1988 | Return a tuple object with the same contents as the arbitrary sequence |
| 1989 | \var{o}. If \var{o} is a tuple, a new reference will be returned, |
| 1990 | otherwise a tuple will be constructed with the appropriate contents. |
| 1991 | \end{cfuncdesc} |
| 1992 | |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1993 | |
Fred Drake | 81cccb7 | 2000-09-12 15:22:05 +0000 | [diff] [blame] | 1994 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m} |
| 1995 | Returns the sequence \var{o} as a tuple, unless it is already a |
| 1996 | tuple or list, in which case \var{o} is returned. Use |
| 1997 | \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the |
| 1998 | result. Returns \NULL{} on failure. If the object is not a sequence, |
| 1999 | raises \exception{TypeError} with \var{m} as the message text. |
| 2000 | \end{cfuncdesc} |
| 2001 | |
| 2002 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i} |
| 2003 | Return the \var{i}th element of \var{o}, assuming that \var{o} was |
| 2004 | returned by \cfunction{PySequence_Fast()}, and that \var{i} is within |
| 2005 | bounds. The caller is expected to get the length of the sequence by |
| 2006 | calling \cfunction{PyObject_Size()} on \var{o}, since lists and tuples |
| 2007 | are guaranteed to always return their true length. |
| 2008 | \end{cfuncdesc} |
| 2009 | |
| 2010 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2011 | \section{Mapping Protocol \label{mapping}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2012 | |
| 2013 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2014 | Return \code{1} if the object provides mapping protocol, and |
| 2015 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2016 | \end{cfuncdesc} |
| 2017 | |
| 2018 | |
| 2019 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2020 | Returns the number of keys in object \var{o} on success, and |
| 2021 | \code{-1} on failure. For objects that do not provide mapping |
| 2022 | protocol, this is equivalent to the Python expression |
| 2023 | \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2024 | \end{cfuncdesc} |
| 2025 | |
| 2026 | |
| 2027 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2028 | Remove the mapping for object \var{key} from the object \var{o}. |
| 2029 | Return \code{-1} on failure. This is equivalent to |
| 2030 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2031 | \end{cfuncdesc} |
| 2032 | |
| 2033 | |
| 2034 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2035 | Remove the mapping for object \var{key} from the object \var{o}. |
| 2036 | Return \code{-1} on failure. This is equivalent to |
| 2037 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2038 | \end{cfuncdesc} |
| 2039 | |
| 2040 | |
| 2041 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2042 | On success, return \code{1} if the mapping object has the key |
| 2043 | \var{key} and \code{0} otherwise. This is equivalent to the Python |
| 2044 | expression \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2045 | This function always succeeds. |
| 2046 | \end{cfuncdesc} |
| 2047 | |
| 2048 | |
| 2049 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2050 | Return \code{1} if the mapping object has the key \var{key} and |
| 2051 | \code{0} otherwise. This is equivalent to the Python expression |
| 2052 | \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2053 | This function always succeeds. |
| 2054 | \end{cfuncdesc} |
| 2055 | |
| 2056 | |
| 2057 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2058 | On success, return a list of the keys in object \var{o}. On |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2059 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2060 | expression \samp{\var{o}.keys()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2061 | \end{cfuncdesc} |
| 2062 | |
| 2063 | |
| 2064 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2065 | On success, return a list of the values in object \var{o}. On |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2066 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2067 | expression \samp{\var{o}.values()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2068 | \end{cfuncdesc} |
| 2069 | |
| 2070 | |
| 2071 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2072 | On success, return a list of the items in object \var{o}, where |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2073 | each item is a tuple containing a key-value pair. On |
| 2074 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2075 | expression \samp{\var{o}.items()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2076 | \end{cfuncdesc} |
| 2077 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2078 | |
| 2079 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2080 | Return element of \var{o} corresponding to the object \var{key} or |
| 2081 | \NULL{} on failure. This is the equivalent of the Python expression |
| 2082 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2083 | \end{cfuncdesc} |
| 2084 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2085 | \begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, |
| 2086 | PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2087 | Map the object \var{key} to the value \var{v} in object \var{o}. |
| 2088 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 2089 | statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2090 | \end{cfuncdesc} |
| 2091 | |
| 2092 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2093 | \section{Iterator Protocol \label{iterator}} |
| 2094 | |
Fred Drake | a8e0827 | 2001-05-07 17:47:07 +0000 | [diff] [blame] | 2095 | \versionadded{2.2} |
| 2096 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2097 | There are only a couple of functions specifically for working with |
| 2098 | iterators. |
| 2099 | |
| 2100 | \begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o} |
| 2101 | Return true if the object \var{o} supports the iterator protocol. |
| 2102 | \end{cfuncdesc} |
| 2103 | |
| 2104 | \begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o} |
| 2105 | Return the next value from the iteration \var{o}. If the object is |
| 2106 | an iterator, this retrieves the next value from the iteration, and |
| 2107 | returns \NULL{} with no exception set if there are no remaining |
| 2108 | items. If the object is not an iterator, \exception{TypeError} is |
| 2109 | raised, or if there is an error in retrieving the item, returns |
| 2110 | \NULL{} and passes along the exception. |
| 2111 | \end{cfuncdesc} |
| 2112 | |
| 2113 | To write a loop which iterates over an iterator, the C code should |
| 2114 | look something like this: |
| 2115 | |
| 2116 | \begin{verbatim} |
| 2117 | PyObject *iterator = ...; |
| 2118 | PyObject *item; |
| 2119 | |
| 2120 | while (item = PyIter_Next(iter)) { |
| 2121 | /* do something with item */ |
| 2122 | } |
| 2123 | if (PyErr_Occurred()) { |
| 2124 | /* propogate error */ |
| 2125 | } |
| 2126 | else { |
| 2127 | /* continue doing useful work */ |
| 2128 | } |
| 2129 | \end{verbatim} |
| 2130 | |
| 2131 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2132 | \chapter{Concrete Objects Layer \label{concrete}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2133 | |
| 2134 | The functions in this chapter are specific to certain Python object |
| 2135 | types. Passing them an object of the wrong type is not a good idea; |
| 2136 | if you receive an object from a Python program and you are not sure |
| 2137 | that it has the right type, you must perform a type check first; |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 2138 | for example, to check that an object is a dictionary, use |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2139 | \cfunction{PyDict_Check()}. The chapter is structured like the |
| 2140 | ``family tree'' of Python object types. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2141 | |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2142 | \strong{Warning:} |
| 2143 | While the functions described in this chapter carefully check the type |
| 2144 | of the objects which are passed in, many of them do not check for |
| 2145 | \NULL{} being passed instead of a valid object. Allowing \NULL{} to |
| 2146 | be passed in can cause memory access violations and immediate |
| 2147 | termination of the interpreter. |
| 2148 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2149 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2150 | \section{Fundamental Objects \label{fundamental}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2151 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2152 | This section describes Python type objects and the singleton object |
| 2153 | \code{None}. |
| 2154 | |
| 2155 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2156 | \subsection{Type Objects \label{typeObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2157 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2158 | \obindex{type} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2159 | \begin{ctypedesc}{PyTypeObject} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2160 | The C structure of the objects used to describe built-in types. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2161 | \end{ctypedesc} |
| 2162 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2163 | \begin{cvardesc}{PyObject*}{PyType_Type} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2164 | This is the type object for type objects; it is the same object as |
| 2165 | \code{types.TypeType} in the Python layer. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2166 | \withsubitem{(in module types)}{\ttindex{TypeType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2167 | \end{cvardesc} |
| 2168 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2169 | \begin{cfuncdesc}{int}{PyType_Check}{PyObject *o} |
| 2170 | Returns true is the object \var{o} is a type object. |
| 2171 | \end{cfuncdesc} |
| 2172 | |
| 2173 | \begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature} |
| 2174 | Returns true if the type object \var{o} sets the feature |
Fred Drake | f0e08ef | 2001-02-03 01:11:26 +0000 | [diff] [blame] | 2175 | \var{feature}. Type features are denoted by single bit flags. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2176 | \end{cfuncdesc} |
| 2177 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2178 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2179 | \subsection{The None Object \label{noneObject}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2180 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2181 | \obindex{None@\texttt{None}} |
| 2182 | Note that the \ctype{PyTypeObject} for \code{None} is not directly |
| 2183 | exposed in the Python/C API. Since \code{None} is a singleton, |
| 2184 | testing for object identity (using \samp{==} in C) is sufficient. |
| 2185 | There is no \cfunction{PyNone_Check()} function for the same reason. |
| 2186 | |
| 2187 | \begin{cvardesc}{PyObject*}{Py_None} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2188 | The Python \code{None} object, denoting lack of value. This object has |
| 2189 | no methods. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2190 | \end{cvardesc} |
| 2191 | |
| 2192 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2193 | \section{Sequence Objects \label{sequenceObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2194 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2195 | \obindex{sequence} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2196 | Generic operations on sequence objects were discussed in the previous |
| 2197 | chapter; this section deals with the specific kinds of sequence |
| 2198 | objects that are intrinsic to the Python language. |
| 2199 | |
| 2200 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2201 | \subsection{String Objects \label{stringObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2202 | |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2203 | These functions raise \exception{TypeError} when expecting a string |
| 2204 | parameter and are called with a non-string parameter. |
| 2205 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2206 | \obindex{string} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2207 | \begin{ctypedesc}{PyStringObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2208 | This subtype of \ctype{PyObject} represents a Python string object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2209 | \end{ctypedesc} |
| 2210 | |
| 2211 | \begin{cvardesc}{PyTypeObject}{PyString_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2212 | This instance of \ctype{PyTypeObject} represents the Python string |
| 2213 | type; it is the same object as \code{types.TypeType} in the Python |
| 2214 | layer.\withsubitem{(in module types)}{\ttindex{StringType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2215 | \end{cvardesc} |
| 2216 | |
| 2217 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2218 | Returns true if the object \var{o} is a string object. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2219 | \end{cfuncdesc} |
| 2220 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2221 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2222 | Returns a new string object with the value \var{v} on success, and |
| 2223 | \NULL{} on failure. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2224 | \end{cfuncdesc} |
| 2225 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2226 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v, |
| 2227 | int len} |
| 2228 | Returns a new string object with the value \var{v} and length |
| 2229 | \var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{}, |
| 2230 | the contents of the string are uninitialized. |
| 2231 | \end{cfuncdesc} |
| 2232 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2233 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2234 | Returns the length of the string in string object \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2235 | \end{cfuncdesc} |
| 2236 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2237 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string} |
Fred Drake | 5d64421 | 2000-10-07 12:31:50 +0000 | [diff] [blame] | 2238 | Macro form of \cfunction{PyString_Size()} but without error |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2239 | checking. |
| 2240 | \end{cfuncdesc} |
| 2241 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2242 | \begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2243 | Returns a null-terminated representation of the contents of |
| 2244 | \var{string}. The pointer refers to the internal buffer of |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2245 | \var{string}, not a copy. The data must not be modified in any way, |
| 2246 | unless the string was just created using |
| 2247 | \code{PyString_FromStringAndSize(NULL, \var{size})}. |
| 2248 | It must not be deallocated. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2249 | \end{cfuncdesc} |
| 2250 | |
| 2251 | \begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string} |
| 2252 | Macro form of \cfunction{PyString_AsString()} but without error |
| 2253 | checking. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2254 | \end{cfuncdesc} |
| 2255 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 2256 | \begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj, |
| 2257 | char **buffer, |
| 2258 | int *length} |
| 2259 | Returns a null-terminated representation of the contents of the object |
| 2260 | \var{obj} through the output variables \var{buffer} and \var{length}. |
| 2261 | |
| 2262 | The function accepts both string and Unicode objects as input. For |
| 2263 | Unicode objects it returns the default encoded version of the object. |
| 2264 | If \var{length} is set to \NULL{}, the resulting buffer may not contain |
| 2265 | null characters; if it does, the function returns -1 and a |
| 2266 | TypeError is raised. |
| 2267 | |
| 2268 | The buffer refers to an internal string buffer of \var{obj}, not a |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2269 | copy. The data must not be modified in any way, unless the string was |
| 2270 | just created using \code{PyString_FromStringAndSize(NULL, |
| 2271 | \var{size})}. It must not be deallocated. |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 2272 | \end{cfuncdesc} |
| 2273 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2274 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string, |
| 2275 | PyObject *newpart} |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 2276 | Creates a new string object in \var{*string} containing the |
Fred Drake | ddc6c27 | 2000-03-31 18:22:38 +0000 | [diff] [blame] | 2277 | contents of \var{newpart} appended to \var{string}; the caller will |
| 2278 | own the new reference. The reference to the old value of \var{string} |
| 2279 | will be stolen. If the new string |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 2280 | cannot be created, the old reference to \var{string} will still be |
| 2281 | discarded and the value of \var{*string} will be set to |
| 2282 | \NULL{}; the appropriate exception will be set. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2283 | \end{cfuncdesc} |
| 2284 | |
| 2285 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string, |
| 2286 | PyObject *newpart} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2287 | Creates a new string object in \var{*string} containing the contents |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2288 | of \var{newpart} appended to \var{string}. This version decrements |
| 2289 | the reference count of \var{newpart}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2290 | \end{cfuncdesc} |
| 2291 | |
| 2292 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2293 | A way to resize a string object even though it is ``immutable''. |
| 2294 | Only use this to build up a brand new string object; don't use this if |
| 2295 | the string may already be known in other parts of the code. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2296 | \end{cfuncdesc} |
| 2297 | |
| 2298 | \begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format, |
| 2299 | PyObject *args} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2300 | Returns a new string object from \var{format} and \var{args}. Analogous |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2301 | to \code{\var{format} \%\ \var{args}}. The \var{args} argument must be |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2302 | a tuple. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2303 | \end{cfuncdesc} |
| 2304 | |
| 2305 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2306 | Intern the argument \var{*string} in place. The argument must be the |
| 2307 | address of a pointer variable pointing to a Python string object. |
| 2308 | If there is an existing interned string that is the same as |
| 2309 | \var{*string}, it sets \var{*string} to it (decrementing the reference |
| 2310 | count of the old string object and incrementing the reference count of |
| 2311 | the interned string object), otherwise it leaves \var{*string} alone |
| 2312 | and interns it (incrementing its reference count). (Clarification: |
| 2313 | even though there is a lot of talk about reference counts, think of |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2314 | this function as reference-count-neutral; you own the object after |
| 2315 | the call if and only if you owned it before the call.) |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2316 | \end{cfuncdesc} |
| 2317 | |
| 2318 | \begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2319 | A combination of \cfunction{PyString_FromString()} and |
| 2320 | \cfunction{PyString_InternInPlace()}, returning either a new string object |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2321 | that has been interned, or a new (``owned'') reference to an earlier |
| 2322 | interned string object with the same value. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2323 | \end{cfuncdesc} |
| 2324 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2325 | \begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s, |
| 2326 | int size, |
| 2327 | const char *encoding, |
| 2328 | const char *errors} |
| 2329 | Create a string object by decoding \var{size} bytes of the encoded |
| 2330 | buffer \var{s}. \var{encoding} and \var{errors} have the same meaning |
| 2331 | as the parameters of the same name in the unicode() builtin |
| 2332 | function. The codec to be used is looked up using the Python codec |
| 2333 | registry. Returns \NULL{} in case an exception was raised by the |
| 2334 | codec. |
| 2335 | \end{cfuncdesc} |
| 2336 | |
| 2337 | \begin{cfuncdesc}{PyObject*}{PyString_Encode}{const Py_UNICODE *s, |
| 2338 | int size, |
| 2339 | const char *encoding, |
| 2340 | const char *errors} |
| 2341 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a |
| 2342 | Python string object. \var{encoding} and \var{errors} have the same |
| 2343 | meaning as the parameters of the same name in the string .encode() |
| 2344 | method. The codec to be used is looked up using the Python codec |
| 2345 | registry. Returns \NULL{} in case an exception was raised by the |
| 2346 | codec. |
| 2347 | \end{cfuncdesc} |
| 2348 | |
| 2349 | \begin{cfuncdesc}{PyObject*}{PyString_AsEncodedString}{PyObject *unicode, |
| 2350 | const char *encoding, |
| 2351 | const char *errors} |
| 2352 | Encodes a string object and returns the result as Python string |
| 2353 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 2354 | parameters of the same name in the string .encode() method. The codec |
| 2355 | to be used is looked up using the Python codec registry. Returns |
| 2356 | \NULL{} in case an exception was raised by the codec. |
| 2357 | \end{cfuncdesc} |
| 2358 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2359 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2360 | \subsection{Unicode Objects \label{unicodeObjects}} |
| 2361 | \sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com} |
| 2362 | |
| 2363 | %--- Unicode Type ------------------------------------------------------- |
| 2364 | |
| 2365 | These are the basic Unicode object types used for the Unicode |
| 2366 | implementation in Python: |
| 2367 | |
| 2368 | \begin{ctypedesc}{Py_UNICODE} |
| 2369 | This type represents a 16-bit unsigned storage type which is used by |
| 2370 | Python internally as basis for holding Unicode ordinals. On platforms |
| 2371 | where \ctype{wchar_t} is available and also has 16-bits, |
| 2372 | \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance |
| 2373 | native platform compatibility. On all other platforms, |
| 2374 | \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}. |
| 2375 | \end{ctypedesc} |
| 2376 | |
| 2377 | \begin{ctypedesc}{PyUnicodeObject} |
| 2378 | This subtype of \ctype{PyObject} represents a Python Unicode object. |
| 2379 | \end{ctypedesc} |
| 2380 | |
| 2381 | \begin{cvardesc}{PyTypeObject}{PyUnicode_Type} |
| 2382 | This instance of \ctype{PyTypeObject} represents the Python Unicode type. |
| 2383 | \end{cvardesc} |
| 2384 | |
| 2385 | %--- These are really C macros... is there a macrodesc TeX macro ? |
| 2386 | |
| 2387 | The following APIs are really C macros and can be used to do fast |
| 2388 | checks and to access internal read-only data of Unicode objects: |
| 2389 | |
| 2390 | \begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o} |
| 2391 | Returns true if the object \var{o} is a Unicode object. |
| 2392 | \end{cfuncdesc} |
| 2393 | |
| 2394 | \begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o} |
| 2395 | Returns the size of the object. o has to be a |
| 2396 | PyUnicodeObject (not checked). |
| 2397 | \end{cfuncdesc} |
| 2398 | |
| 2399 | \begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o} |
| 2400 | Returns the size of the object's internal buffer in bytes. o has to be |
| 2401 | a PyUnicodeObject (not checked). |
| 2402 | \end{cfuncdesc} |
| 2403 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2404 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2405 | Returns a pointer to the internal Py_UNICODE buffer of the object. o |
| 2406 | has to be a PyUnicodeObject (not checked). |
| 2407 | \end{cfuncdesc} |
| 2408 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2409 | \begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2410 | Returns a (const char *) pointer to the internal buffer of the object. |
| 2411 | o has to be a PyUnicodeObject (not checked). |
| 2412 | \end{cfuncdesc} |
| 2413 | |
| 2414 | % --- Unicode character properties --------------------------------------- |
| 2415 | |
| 2416 | Unicode provides many different character properties. The most often |
| 2417 | needed ones are available through these macros which are mapped to C |
| 2418 | functions depending on the Python configuration. |
| 2419 | |
| 2420 | \begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch} |
| 2421 | Returns 1/0 depending on whether \var{ch} is a whitespace character. |
| 2422 | \end{cfuncdesc} |
| 2423 | |
| 2424 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch} |
| 2425 | Returns 1/0 depending on whether \var{ch} is a lowercase character. |
| 2426 | \end{cfuncdesc} |
| 2427 | |
| 2428 | \begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch} |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 2429 | Returns 1/0 depending on whether \var{ch} is an uppercase character. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2430 | \end{cfuncdesc} |
| 2431 | |
| 2432 | \begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch} |
| 2433 | Returns 1/0 depending on whether \var{ch} is a titlecase character. |
| 2434 | \end{cfuncdesc} |
| 2435 | |
| 2436 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch} |
| 2437 | Returns 1/0 depending on whether \var{ch} is a linebreak character. |
| 2438 | \end{cfuncdesc} |
| 2439 | |
| 2440 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch} |
| 2441 | Returns 1/0 depending on whether \var{ch} is a decimal character. |
| 2442 | \end{cfuncdesc} |
| 2443 | |
| 2444 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch} |
| 2445 | Returns 1/0 depending on whether \var{ch} is a digit character. |
| 2446 | \end{cfuncdesc} |
| 2447 | |
| 2448 | \begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch} |
| 2449 | Returns 1/0 depending on whether \var{ch} is a numeric character. |
| 2450 | \end{cfuncdesc} |
| 2451 | |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 2452 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch} |
| 2453 | Returns 1/0 depending on whether \var{ch} is an alphabetic character. |
| 2454 | \end{cfuncdesc} |
| 2455 | |
| 2456 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch} |
| 2457 | Returns 1/0 depending on whether \var{ch} is an alphanumeric character. |
| 2458 | \end{cfuncdesc} |
| 2459 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2460 | These APIs can be used for fast direct character conversions: |
| 2461 | |
| 2462 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch} |
| 2463 | Returns the character \var{ch} converted to lower case. |
| 2464 | \end{cfuncdesc} |
| 2465 | |
| 2466 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch} |
| 2467 | Returns the character \var{ch} converted to upper case. |
| 2468 | \end{cfuncdesc} |
| 2469 | |
| 2470 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch} |
| 2471 | Returns the character \var{ch} converted to title case. |
| 2472 | \end{cfuncdesc} |
| 2473 | |
| 2474 | \begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch} |
| 2475 | Returns the character \var{ch} converted to a decimal positive integer. |
| 2476 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 2477 | \end{cfuncdesc} |
| 2478 | |
| 2479 | \begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch} |
| 2480 | Returns the character \var{ch} converted to a single digit integer. |
| 2481 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 2482 | \end{cfuncdesc} |
| 2483 | |
| 2484 | \begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch} |
| 2485 | Returns the character \var{ch} converted to a (positive) double. |
| 2486 | Returns -1.0 in case this is not possible. Does not raise exceptions. |
| 2487 | \end{cfuncdesc} |
| 2488 | |
| 2489 | % --- Plain Py_UNICODE --------------------------------------------------- |
| 2490 | |
| 2491 | To create Unicode objects and access their basic sequence properties, |
| 2492 | use these APIs: |
| 2493 | |
| 2494 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u, |
| 2495 | int size} |
| 2496 | |
| 2497 | Create a Unicode Object from the Py_UNICODE buffer \var{u} of the |
| 2498 | given size. \var{u} may be \NULL{} which causes the contents to be |
| 2499 | undefined. It is the user's responsibility to fill in the needed data. |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2500 | The buffer is copied into the new object. If the buffer is not \NULL{}, |
| 2501 | the return value might be a shared object. Therefore, modification of |
| 2502 | the resulting Unicode Object is only allowed when \var{u} is \NULL{}. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2503 | \end{cfuncdesc} |
| 2504 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2505 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2506 | Return a read-only pointer to the Unicode object's internal |
| 2507 | \ctype{Py_UNICODE} buffer. |
| 2508 | \end{cfuncdesc} |
| 2509 | |
| 2510 | \begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode} |
| 2511 | Return the length of the Unicode object. |
| 2512 | \end{cfuncdesc} |
| 2513 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2514 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj, |
| 2515 | const char *encoding, |
| 2516 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2517 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2518 | Coerce an encoded object obj to an Unicode object and return a |
| 2519 | reference with incremented refcount. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2520 | |
| 2521 | Coercion is done in the following way: |
| 2522 | \begin{enumerate} |
| 2523 | \item Unicode objects are passed back as-is with incremented |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2524 | refcount. Note: these cannot be decoded; passing a non-NULL |
| 2525 | value for encoding will result in a TypeError. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2526 | |
| 2527 | \item String and other char buffer compatible objects are decoded |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2528 | according to the given encoding and using the error handling |
| 2529 | defined by errors. Both can be NULL to have the interface use |
| 2530 | the default values (see the next section for details). |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2531 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2532 | \item All other objects cause an exception. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2533 | \end{enumerate} |
| 2534 | The API returns NULL in case of an error. The caller is responsible |
| 2535 | for decref'ing the returned objects. |
| 2536 | \end{cfuncdesc} |
| 2537 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2538 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj} |
| 2539 | |
| 2540 | Shortcut for PyUnicode_FromEncodedObject(obj, NULL, ``strict'') |
| 2541 | which is used throughout the interpreter whenever coercion to |
| 2542 | Unicode is needed. |
| 2543 | \end{cfuncdesc} |
| 2544 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2545 | % --- wchar_t support for platforms which support it --------------------- |
| 2546 | |
| 2547 | If the platform supports \ctype{wchar_t} and provides a header file |
| 2548 | wchar.h, Python can interface directly to this type using the |
| 2549 | following functions. Support is optimized if Python's own |
| 2550 | \ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}. |
| 2551 | |
| 2552 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w, |
| 2553 | int size} |
| 2554 | Create a Unicode Object from the \ctype{whcar_t} buffer \var{w} of the |
| 2555 | given size. Returns \NULL{} on failure. |
| 2556 | \end{cfuncdesc} |
| 2557 | |
| 2558 | \begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode, |
| 2559 | wchar_t *w, |
| 2560 | int size} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2561 | Copies the Unicode Object contents into the \ctype{whcar_t} buffer |
| 2562 | \var{w}. At most \var{size} \ctype{whcar_t} characters are copied. |
| 2563 | Returns the number of \ctype{whcar_t} characters copied or -1 in case |
| 2564 | of an error. |
| 2565 | \end{cfuncdesc} |
| 2566 | |
| 2567 | |
| 2568 | \subsubsection{Builtin Codecs \label{builtinCodecs}} |
| 2569 | |
| 2570 | Python provides a set of builtin codecs which are written in C |
| 2571 | for speed. All of these codecs are directly usable via the |
| 2572 | following functions. |
| 2573 | |
| 2574 | Many of the following APIs take two arguments encoding and |
| 2575 | errors. These parameters encoding and errors have the same semantics |
| 2576 | as the ones of the builtin unicode() Unicode object constructor. |
| 2577 | |
| 2578 | Setting encoding to NULL causes the default encoding to be used which |
| 2579 | is UTF-8. |
| 2580 | |
| 2581 | Error handling is set by errors which may also be set to NULL meaning |
| 2582 | to use the default handling defined for the codec. Default error |
| 2583 | handling for all builtin codecs is ``strict'' (ValueErrors are raised). |
| 2584 | |
| 2585 | The codecs all use a similar interface. Only deviation from the |
| 2586 | following generic ones are documented for simplicity. |
| 2587 | |
| 2588 | % --- Generic Codecs ----------------------------------------------------- |
| 2589 | |
| 2590 | These are the generic codec APIs: |
| 2591 | |
| 2592 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s, |
| 2593 | int size, |
| 2594 | const char *encoding, |
| 2595 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2596 | Create a Unicode object by decoding \var{size} bytes of the encoded |
| 2597 | string \var{s}. \var{encoding} and \var{errors} have the same meaning |
| 2598 | as the parameters of the same name in the unicode() builtin |
| 2599 | function. The codec to be used is looked up using the Python codec |
| 2600 | registry. Returns \NULL{} in case an exception was raised by the |
| 2601 | codec. |
| 2602 | \end{cfuncdesc} |
| 2603 | |
| 2604 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s, |
| 2605 | int size, |
| 2606 | const char *encoding, |
| 2607 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2608 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a |
| 2609 | Python string object. \var{encoding} and \var{errors} have the same |
| 2610 | meaning as the parameters of the same name in the Unicode .encode() |
| 2611 | method. The codec to be used is looked up using the Python codec |
| 2612 | registry. Returns \NULL{} in case an exception was raised by the |
| 2613 | codec. |
| 2614 | \end{cfuncdesc} |
| 2615 | |
| 2616 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode, |
| 2617 | const char *encoding, |
| 2618 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2619 | Encodes a Unicode object and returns the result as Python string |
| 2620 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 2621 | parameters of the same name in the Unicode .encode() method. The codec |
| 2622 | to be used is looked up using the Python codec registry. Returns |
| 2623 | \NULL{} in case an exception was raised by the codec. |
| 2624 | \end{cfuncdesc} |
| 2625 | |
| 2626 | % --- UTF-8 Codecs ------------------------------------------------------- |
| 2627 | |
| 2628 | These are the UTF-8 codec APIs: |
| 2629 | |
| 2630 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s, |
| 2631 | int size, |
| 2632 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2633 | Creates a Unicode object by decoding \var{size} bytes of the UTF-8 |
| 2634 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2635 | raised by the codec. |
| 2636 | \end{cfuncdesc} |
| 2637 | |
| 2638 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s, |
| 2639 | int size, |
| 2640 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2641 | Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8 |
| 2642 | and returns a Python string object. Returns \NULL{} in case an |
| 2643 | exception was raised by the codec. |
| 2644 | \end{cfuncdesc} |
| 2645 | |
| 2646 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2647 | Encodes a Unicode objects using UTF-8 and returns the result as Python |
| 2648 | string object. Error handling is ``strict''. Returns |
| 2649 | \NULL{} in case an exception was raised by the codec. |
| 2650 | \end{cfuncdesc} |
| 2651 | |
| 2652 | % --- UTF-16 Codecs ------------------------------------------------------ */ |
| 2653 | |
| 2654 | These are the UTF-16 codec APIs: |
| 2655 | |
| 2656 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s, |
| 2657 | int size, |
| 2658 | const char *errors, |
| 2659 | int *byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2660 | Decodes \var{length} bytes from a UTF-16 encoded buffer string and |
| 2661 | returns the corresponding Unicode object. |
| 2662 | |
| 2663 | \var{errors} (if non-NULL) defines the error handling. It defaults |
| 2664 | to ``strict''. |
| 2665 | |
| 2666 | If \var{byteorder} is non-\NULL{}, the decoder starts decoding using |
| 2667 | the given byte order: |
| 2668 | |
| 2669 | \begin{verbatim} |
| 2670 | *byteorder == -1: little endian |
| 2671 | *byteorder == 0: native order |
| 2672 | *byteorder == 1: big endian |
| 2673 | \end{verbatim} |
| 2674 | |
| 2675 | and then switches according to all byte order marks (BOM) it finds in |
| 2676 | the input data. BOM marks are not copied into the resulting Unicode |
| 2677 | string. After completion, \var{*byteorder} is set to the current byte |
| 2678 | order at the end of input data. |
| 2679 | |
| 2680 | If \var{byteorder} is \NULL{}, the codec starts in native order mode. |
| 2681 | |
| 2682 | Returns \NULL{} in case an exception was raised by the codec. |
| 2683 | \end{cfuncdesc} |
| 2684 | |
| 2685 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s, |
| 2686 | int size, |
| 2687 | const char *errors, |
| 2688 | int byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2689 | Returns a Python string object holding the UTF-16 encoded value of the |
| 2690 | Unicode data in \var{s}. |
| 2691 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 2692 | If \var{byteorder} is not \code{0}, output is written according to the |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2693 | following byte order: |
| 2694 | |
| 2695 | \begin{verbatim} |
| 2696 | byteorder == -1: little endian |
| 2697 | byteorder == 0: native byte order (writes a BOM mark) |
| 2698 | byteorder == 1: big endian |
| 2699 | \end{verbatim} |
| 2700 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 2701 | If byteorder is \code{0}, the output string will always start with the |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2702 | Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is |
| 2703 | prepended. |
| 2704 | |
| 2705 | Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16 |
| 2706 | reduced to UCS-2. This trick makes it possible to add full UTF-16 |
| 2707 | capabilities at a later point without comprimising the APIs. |
| 2708 | |
| 2709 | Returns \NULL{} in case an exception was raised by the codec. |
| 2710 | \end{cfuncdesc} |
| 2711 | |
| 2712 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2713 | Returns a Python string using the UTF-16 encoding in native byte |
| 2714 | order. The string always starts with a BOM mark. Error handling is |
| 2715 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 2716 | codec. |
| 2717 | \end{cfuncdesc} |
| 2718 | |
| 2719 | % --- Unicode-Escape Codecs ---------------------------------------------- |
| 2720 | |
| 2721 | These are the ``Unicode Esacpe'' codec APIs: |
| 2722 | |
| 2723 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s, |
| 2724 | int size, |
| 2725 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2726 | Creates a Unicode object by decoding \var{size} bytes of the Unicode-Esacpe |
| 2727 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2728 | raised by the codec. |
| 2729 | \end{cfuncdesc} |
| 2730 | |
| 2731 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s, |
| 2732 | int size, |
| 2733 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2734 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Unicode-Escape |
| 2735 | and returns a Python string object. Returns \NULL{} in case an |
| 2736 | exception was raised by the codec. |
| 2737 | \end{cfuncdesc} |
| 2738 | |
| 2739 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2740 | Encodes a Unicode objects using Unicode-Escape and returns the result |
| 2741 | as Python string object. Error handling is ``strict''. Returns |
| 2742 | \NULL{} in case an exception was raised by the codec. |
| 2743 | \end{cfuncdesc} |
| 2744 | |
| 2745 | % --- Raw-Unicode-Escape Codecs ------------------------------------------ |
| 2746 | |
| 2747 | These are the ``Raw Unicode Esacpe'' codec APIs: |
| 2748 | |
| 2749 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s, |
| 2750 | int size, |
| 2751 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2752 | Creates a Unicode object by decoding \var{size} bytes of the Raw-Unicode-Esacpe |
| 2753 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2754 | raised by the codec. |
| 2755 | \end{cfuncdesc} |
| 2756 | |
| 2757 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s, |
| 2758 | int size, |
| 2759 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2760 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Raw-Unicode-Escape |
| 2761 | and returns a Python string object. Returns \NULL{} in case an |
| 2762 | exception was raised by the codec. |
| 2763 | \end{cfuncdesc} |
| 2764 | |
| 2765 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2766 | Encodes a Unicode objects using Raw-Unicode-Escape and returns the result |
| 2767 | as Python string object. Error handling is ``strict''. Returns |
| 2768 | \NULL{} in case an exception was raised by the codec. |
| 2769 | \end{cfuncdesc} |
| 2770 | |
| 2771 | % --- Latin-1 Codecs ----------------------------------------------------- |
| 2772 | |
| 2773 | These are the Latin-1 codec APIs: |
| 2774 | |
| 2775 | Latin-1 corresponds to the first 256 Unicode ordinals and only these |
| 2776 | are accepted by the codecs during encoding. |
| 2777 | |
| 2778 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2779 | int size, |
| 2780 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2781 | Creates a Unicode object by decoding \var{size} bytes of the Latin-1 |
| 2782 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2783 | raised by the codec. |
| 2784 | \end{cfuncdesc} |
| 2785 | |
| 2786 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2787 | int size, |
| 2788 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2789 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Latin-1 |
| 2790 | and returns a Python string object. Returns \NULL{} in case an |
| 2791 | exception was raised by the codec. |
| 2792 | \end{cfuncdesc} |
| 2793 | |
| 2794 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2795 | Encodes a Unicode objects using Latin-1 and returns the result as |
| 2796 | Python string object. Error handling is ``strict''. Returns |
| 2797 | \NULL{} in case an exception was raised by the codec. |
| 2798 | \end{cfuncdesc} |
| 2799 | |
| 2800 | % --- ASCII Codecs ------------------------------------------------------- |
| 2801 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2802 | These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is |
| 2803 | accepted. All other codes generate errors. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2804 | |
| 2805 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2806 | int size, |
| 2807 | const char *errors} |
| 2808 | Creates a Unicode object by decoding \var{size} bytes of the |
| 2809 | \ASCII{} encoded string \var{s}. Returns \NULL{} in case an exception |
| 2810 | was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2811 | \end{cfuncdesc} |
| 2812 | |
| 2813 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2814 | int size, |
| 2815 | const char *errors} |
| 2816 | Encodes the \ctype{Py_UNICODE} buffer of the given size using |
| 2817 | \ASCII{} and returns a Python string object. Returns \NULL{} in case |
| 2818 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2819 | \end{cfuncdesc} |
| 2820 | |
| 2821 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2822 | Encodes a Unicode objects using \ASCII{} and returns the result as Python |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2823 | string object. Error handling is ``strict''. Returns |
| 2824 | \NULL{} in case an exception was raised by the codec. |
| 2825 | \end{cfuncdesc} |
| 2826 | |
| 2827 | % --- Character Map Codecs ----------------------------------------------- |
| 2828 | |
| 2829 | These are the mapping codec APIs: |
| 2830 | |
| 2831 | This codec is special in that it can be used to implement many |
| 2832 | different codecs (and this is in fact what was done to obtain most of |
| 2833 | the standard codecs included in the \module{encodings} package). The |
| 2834 | codec uses mapping to encode and decode characters. |
| 2835 | |
| 2836 | Decoding mappings must map single string characters to single Unicode |
| 2837 | characters, integers (which are then interpreted as Unicode ordinals) |
| 2838 | or None (meaning "undefined mapping" and causing an error). |
| 2839 | |
| 2840 | Encoding mappings must map single Unicode characters to single string |
| 2841 | characters, integers (which are then interpreted as Latin-1 ordinals) |
| 2842 | or None (meaning "undefined mapping" and causing an error). |
| 2843 | |
| 2844 | The mapping objects provided must only support the __getitem__ mapping |
| 2845 | interface. |
| 2846 | |
| 2847 | If a character lookup fails with a LookupError, the character is |
| 2848 | copied as-is meaning that its ordinal value will be interpreted as |
| 2849 | Unicode or Latin-1 ordinal resp. Because of this, mappings only need |
| 2850 | to contain those mappings which map characters to different code |
| 2851 | points. |
| 2852 | |
| 2853 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s, |
| 2854 | int size, |
| 2855 | PyObject *mapping, |
| 2856 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2857 | Creates a Unicode object by decoding \var{size} bytes of the encoded |
| 2858 | string \var{s} using the given \var{mapping} object. Returns \NULL{} |
| 2859 | in case an exception was raised by the codec. |
| 2860 | \end{cfuncdesc} |
| 2861 | |
| 2862 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s, |
| 2863 | int size, |
| 2864 | PyObject *mapping, |
| 2865 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2866 | Encodes the \ctype{Py_UNICODE} buffer of the given size using the |
| 2867 | given \var{mapping} object and returns a Python string object. |
| 2868 | Returns \NULL{} in case an exception was raised by the codec. |
| 2869 | \end{cfuncdesc} |
| 2870 | |
| 2871 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode, |
| 2872 | PyObject *mapping} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2873 | Encodes a Unicode objects using the given \var{mapping} object and |
| 2874 | returns the result as Python string object. Error handling is |
| 2875 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 2876 | codec. |
| 2877 | \end{cfuncdesc} |
| 2878 | |
| 2879 | The following codec API is special in that maps Unicode to Unicode. |
| 2880 | |
| 2881 | \begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s, |
| 2882 | int size, |
| 2883 | PyObject *table, |
| 2884 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2885 | Translates a \ctype{Py_UNICODE} buffer of the given length by applying |
| 2886 | a character mapping \var{table} to it and returns the resulting |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2887 | Unicode object. Returns \NULL{} when an exception was raised by the |
| 2888 | codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2889 | |
| 2890 | The \var{mapping} table must map Unicode ordinal integers to Unicode |
| 2891 | ordinal integers or None (causing deletion of the character). |
| 2892 | |
| 2893 | Mapping tables must only provide the __getitem__ interface, |
| 2894 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 2895 | which cause a LookupError) are left untouched and are copied as-is. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2896 | \end{cfuncdesc} |
| 2897 | |
| 2898 | % --- MBCS codecs for Windows -------------------------------------------- |
| 2899 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2900 | These are the MBCS codec APIs. They are currently only available on |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2901 | Windows and use the Win32 MBCS converters to implement the |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2902 | conversions. Note that MBCS (or DBCS) is a class of encodings, not |
| 2903 | just one. The target encoding is defined by the user settings on the |
| 2904 | machine running the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2905 | |
| 2906 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s, |
| 2907 | int size, |
| 2908 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2909 | Creates a Unicode object by decoding \var{size} bytes of the MBCS |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2910 | encoded string \var{s}. Returns \NULL{} in case an exception was |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2911 | raised by the codec. |
| 2912 | \end{cfuncdesc} |
| 2913 | |
| 2914 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s, |
| 2915 | int size, |
| 2916 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2917 | Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS |
| 2918 | and returns a Python string object. Returns \NULL{} in case an |
| 2919 | exception was raised by the codec. |
| 2920 | \end{cfuncdesc} |
| 2921 | |
| 2922 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2923 | Encodes a Unicode objects using MBCS and returns the result as Python |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2924 | string object. Error handling is ``strict''. Returns \NULL{} in case |
| 2925 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2926 | \end{cfuncdesc} |
| 2927 | |
| 2928 | % --- Methods & Slots ---------------------------------------------------- |
| 2929 | |
| 2930 | \subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}} |
| 2931 | |
| 2932 | The following APIs are capable of handling Unicode objects and strings |
| 2933 | on input (we refer to them as strings in the descriptions) and return |
| 2934 | Unicode objects or integers as apporpriate. |
| 2935 | |
| 2936 | They all return \NULL{} or -1 in case an exception occurrs. |
| 2937 | |
| 2938 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left, |
| 2939 | PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2940 | Concat two strings giving a new Unicode string. |
| 2941 | \end{cfuncdesc} |
| 2942 | |
| 2943 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s, |
| 2944 | PyObject *sep, |
| 2945 | int maxsplit} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2946 | Split a string giving a list of Unicode strings. |
| 2947 | |
| 2948 | If sep is NULL, splitting will be done at all whitespace |
| 2949 | substrings. Otherwise, splits occur at the given separator. |
| 2950 | |
| 2951 | At most maxsplit splits will be done. If negative, no limit is set. |
| 2952 | |
| 2953 | Separators are not included in the resulting list. |
| 2954 | \end{cfuncdesc} |
| 2955 | |
| 2956 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s, |
| 2957 | int maxsplit} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2958 | Split a Unicode string at line breaks, returning a list of Unicode |
| 2959 | strings. CRLF is considered to be one line break. The Line break |
| 2960 | characters are not included in the resulting strings. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2961 | \end{cfuncdesc} |
| 2962 | |
| 2963 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str, |
| 2964 | PyObject *table, |
| 2965 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2966 | Translate a string by applying a character mapping table to it and |
| 2967 | return the resulting Unicode object. |
| 2968 | |
| 2969 | The mapping table must map Unicode ordinal integers to Unicode ordinal |
| 2970 | integers or None (causing deletion of the character). |
| 2971 | |
| 2972 | Mapping tables must only provide the __getitem__ interface, |
| 2973 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 2974 | which cause a LookupError) are left untouched and are copied as-is. |
| 2975 | |
| 2976 | \var{errors} has the usual meaning for codecs. It may be \NULL{} |
| 2977 | which indicates to use the default error handling. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2978 | \end{cfuncdesc} |
| 2979 | |
| 2980 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator, |
| 2981 | PyObject *seq} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2982 | Join a sequence of strings using the given separator and return |
| 2983 | the resulting Unicode string. |
| 2984 | \end{cfuncdesc} |
| 2985 | |
| 2986 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str, |
| 2987 | PyObject *substr, |
| 2988 | int start, |
| 2989 | int end, |
| 2990 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2991 | Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at |
| 2992 | the given tail end (\var{direction} == -1 means to do a prefix match, |
| 2993 | \var{direction} == 1 a suffix match), 0 otherwise. |
| 2994 | \end{cfuncdesc} |
| 2995 | |
| 2996 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Find}{PyObject *str, |
| 2997 | PyObject *substr, |
| 2998 | int start, |
| 2999 | int end, |
| 3000 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3001 | Return the first position of \var{substr} in |
| 3002 | \var{str}[\var{start}:\var{end}] using the given \var{direction} |
| 3003 | (\var{direction} == 1 means to do a forward search, |
| 3004 | \var{direction} == -1 a backward search), 0 otherwise. |
| 3005 | \end{cfuncdesc} |
| 3006 | |
| 3007 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Count}{PyObject *str, |
| 3008 | PyObject *substr, |
| 3009 | int start, |
| 3010 | int end} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3011 | Count the number of occurrences of \var{substr} in |
| 3012 | \var{str}[\var{start}:\var{end}] |
| 3013 | \end{cfuncdesc} |
| 3014 | |
| 3015 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str, |
| 3016 | PyObject *substr, |
| 3017 | PyObject *replstr, |
| 3018 | int maxcount} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3019 | Replace at most \var{maxcount} occurrences of \var{substr} in |
| 3020 | \var{str} with \var{replstr} and return the resulting Unicode object. |
| 3021 | \var{maxcount} == -1 means: replace all occurrences. |
| 3022 | \end{cfuncdesc} |
| 3023 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3024 | \begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3025 | Compare two strings and return -1, 0, 1 for less than, equal, |
| 3026 | greater than resp. |
| 3027 | \end{cfuncdesc} |
| 3028 | |
| 3029 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, |
| 3030 | PyObject *args} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3031 | Returns a new string object from \var{format} and \var{args}; this is |
| 3032 | analogous to \code{\var{format} \%\ \var{args}}. The |
| 3033 | \var{args} argument must be a tuple. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3034 | \end{cfuncdesc} |
| 3035 | |
| 3036 | \begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container, |
| 3037 | PyObject *element} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3038 | Checks whether \var{element} is contained in \var{container} and |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3039 | returns true or false accordingly. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3040 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3041 | \var{element} has to coerce to a one element Unicode string. \code{-1} is |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3042 | returned in case of an error. |
| 3043 | \end{cfuncdesc} |
| 3044 | |
| 3045 | |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3046 | \subsection{Buffer Objects \label{bufferObjects}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3047 | \sectionauthor{Greg Stein}{gstein@lyra.org} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3048 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3049 | \obindex{buffer} |
| 3050 | Python objects implemented in C can export a group of functions called |
| 3051 | the ``buffer\index{buffer interface} interface.'' These functions can |
| 3052 | be used by an object to expose its data in a raw, byte-oriented |
| 3053 | format. Clients of the object can use the buffer interface to access |
| 3054 | the object data directly, without needing to copy it first. |
| 3055 | |
| 3056 | Two examples of objects that support |
| 3057 | the buffer interface are strings and arrays. The string object exposes |
| 3058 | the character contents in the buffer interface's byte-oriented |
| 3059 | form. An array can also expose its contents, but it should be noted |
| 3060 | that array elements may be multi-byte values. |
| 3061 | |
| 3062 | An example user of the buffer interface is the file object's |
| 3063 | \method{write()} method. Any object that can export a series of bytes |
| 3064 | through the buffer interface can be written to a file. There are a |
| 3065 | number of format codes to \cfunction{PyArgs_ParseTuple()} that operate |
| 3066 | against an object's buffer interface, returning data from the target |
| 3067 | object. |
| 3068 | |
| 3069 | More information on the buffer interface is provided in the section |
| 3070 | ``Buffer Object Structures'' (section \ref{buffer-structs}), under |
| 3071 | the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}. |
| 3072 | |
| 3073 | A ``buffer object'' is defined in the \file{bufferobject.h} header |
| 3074 | (included by \file{Python.h}). These objects look very similar to |
| 3075 | string objects at the Python programming level: they support slicing, |
| 3076 | indexing, concatenation, and some other standard string |
| 3077 | operations. However, their data can come from one of two sources: from |
| 3078 | a block of memory, or from another object which exports the buffer |
| 3079 | interface. |
| 3080 | |
| 3081 | Buffer objects are useful as a way to expose the data from another |
| 3082 | object's buffer interface to the Python programmer. They can also be |
| 3083 | used as a zero-copy slicing mechanism. Using their ability to |
| 3084 | reference a block of memory, it is possible to expose any data to the |
| 3085 | Python programmer quite easily. The memory could be a large, constant |
| 3086 | array in a C extension, it could be a raw block of memory for |
| 3087 | manipulation before passing to an operating system library, or it |
| 3088 | could be used to pass around structured data in its native, in-memory |
| 3089 | format. |
| 3090 | |
| 3091 | \begin{ctypedesc}{PyBufferObject} |
| 3092 | This subtype of \ctype{PyObject} represents a buffer object. |
| 3093 | \end{ctypedesc} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3094 | |
| 3095 | \begin{cvardesc}{PyTypeObject}{PyBuffer_Type} |
| 3096 | The instance of \ctype{PyTypeObject} which represents the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3097 | buffer type; it is the same object as \code{types.BufferType} in the |
| 3098 | Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3099 | \end{cvardesc} |
| 3100 | |
| 3101 | \begin{cvardesc}{int}{Py_END_OF_BUFFER} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3102 | This constant may be passed as the \var{size} parameter to |
| 3103 | \cfunction{PyBuffer_FromObject()} or |
| 3104 | \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the new |
| 3105 | \ctype{PyBufferObject} should refer to \var{base} object from the |
| 3106 | specified \var{offset} to the end of its exported buffer. Using this |
| 3107 | enables the caller to avoid querying the \var{base} object for its |
| 3108 | length. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3109 | \end{cvardesc} |
| 3110 | |
| 3111 | \begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p} |
| 3112 | Return true if the argument has type \cdata{PyBuffer_Type}. |
| 3113 | \end{cfuncdesc} |
| 3114 | |
| 3115 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base, |
| 3116 | int offset, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3117 | Return a new read-only buffer object. This raises |
| 3118 | \exception{TypeError} if \var{base} doesn't support the read-only |
| 3119 | buffer protocol or doesn't provide exactly one buffer segment, or it |
| 3120 | raises \exception{ValueError} if \var{offset} is less than zero. The |
| 3121 | buffer will hold a reference to the \var{base} object, and the |
| 3122 | buffer's contents will refer to the \var{base} object's buffer |
| 3123 | interface, starting as position \var{offset} and extending for |
| 3124 | \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then |
| 3125 | the new buffer's contents extend to the length of the |
| 3126 | \var{base} object's exported buffer data. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3127 | \end{cfuncdesc} |
| 3128 | |
| 3129 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base, |
| 3130 | int offset, |
| 3131 | int size} |
| 3132 | Return a new writable buffer object. Parameters and exceptions are |
| 3133 | similar to those for \cfunction{PyBuffer_FromObject()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3134 | If the \var{base} object does not export the writeable buffer |
| 3135 | protocol, then \exception{TypeError} is raised. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3136 | \end{cfuncdesc} |
| 3137 | |
| 3138 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3139 | Return a new read-only buffer object that reads from a specified |
| 3140 | location in memory, with a specified size. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3141 | The caller is responsible for ensuring that the memory buffer, passed |
| 3142 | in as \var{ptr}, is not deallocated while the returned buffer object |
| 3143 | exists. Raises \exception{ValueError} if \var{size} is less than |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3144 | zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be passed |
| 3145 | for the \var{size} parameter; \exception{ValueError} will be raised in |
| 3146 | that case. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3147 | \end{cfuncdesc} |
| 3148 | |
| 3149 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3150 | Similar to \cfunction{PyBuffer_FromMemory()}, but the returned buffer |
| 3151 | is writable. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3152 | \end{cfuncdesc} |
| 3153 | |
| 3154 | \begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size} |
| 3155 | Returns a new writable buffer object that maintains its own memory |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3156 | buffer of \var{size} bytes. \exception{ValueError} is returned if |
| 3157 | \var{size} is not zero or positive. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3158 | \end{cfuncdesc} |
| 3159 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3160 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3161 | \subsection{Tuple Objects \label{tupleObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3162 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3163 | \obindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3164 | \begin{ctypedesc}{PyTupleObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3165 | This subtype of \ctype{PyObject} represents a Python tuple object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3166 | \end{ctypedesc} |
| 3167 | |
| 3168 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3169 | This instance of \ctype{PyTypeObject} represents the Python tuple |
| 3170 | type; it is the same object as \code{types.TupleType} in the Python |
| 3171 | layer.\withsubitem{(in module types)}{\ttindex{TupleType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3172 | \end{cvardesc} |
| 3173 | |
| 3174 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} |
| 3175 | Return true if the argument is a tuple object. |
| 3176 | \end{cfuncdesc} |
| 3177 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3178 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len} |
| 3179 | Return a new tuple object of size \var{len}, or \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3180 | \end{cfuncdesc} |
| 3181 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3182 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3183 | Takes a pointer to a tuple object, and returns the size |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3184 | of that tuple. |
| 3185 | \end{cfuncdesc} |
| 3186 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3187 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3188 | Returns the object at position \var{pos} in the tuple pointed |
| 3189 | to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3190 | sets an \exception{IndexError} exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3191 | \end{cfuncdesc} |
| 3192 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3193 | \begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3194 | Does the same, but does no checking of its arguments. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3195 | \end{cfuncdesc} |
| 3196 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3197 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p, |
| 3198 | int low, int high} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3199 | Takes a slice of the tuple pointed to by \var{p} from |
| 3200 | \var{low} to \var{high} and returns it as a new tuple. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3201 | \end{cfuncdesc} |
| 3202 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3203 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p, |
| 3204 | int pos, PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3205 | Inserts a reference to object \var{o} at position \var{pos} of |
| 3206 | the tuple pointed to by \var{p}. It returns \code{0} on success. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3207 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3208 | \end{cfuncdesc} |
| 3209 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3210 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p, |
| 3211 | int pos, PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3212 | Does the same, but does no error checking, and |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3213 | should \emph{only} be used to fill in brand new tuples. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3214 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3215 | \end{cfuncdesc} |
| 3216 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3217 | \begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3218 | int newsize, int last_is_sticky} |
| 3219 | Can be used to resize a tuple. \var{newsize} will be the new length |
| 3220 | of the tuple. Because tuples are \emph{supposed} to be immutable, |
| 3221 | this should only be used if there is only one reference to the object. |
| 3222 | Do \emph{not} use this if the tuple may already be known to some other |
Neil Schemenauer | 410cb6b | 2000-10-05 19:38:24 +0000 | [diff] [blame] | 3223 | part of the code. The tuple will always grow or shrink at the end. The |
| 3224 | \var{last_is_sticky} flag is not used and should always be false. Think |
| 3225 | of this as destroying the old tuple and creating a new one, only more |
| 3226 | efficiently. Returns \code{0} on success and \code{-1} on failure (in |
| 3227 | which case a \exception{MemoryError} or \exception{SystemError} will be |
| 3228 | raised). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3229 | \end{cfuncdesc} |
| 3230 | |
| 3231 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3232 | \subsection{List Objects \label{listObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3233 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3234 | \obindex{list} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3235 | \begin{ctypedesc}{PyListObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3236 | This subtype of \ctype{PyObject} represents a Python list object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3237 | \end{ctypedesc} |
| 3238 | |
| 3239 | \begin{cvardesc}{PyTypeObject}{PyList_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3240 | This instance of \ctype{PyTypeObject} represents the Python list |
| 3241 | type. This is the same object as \code{types.ListType}. |
| 3242 | \withsubitem{(in module types)}{\ttindex{ListType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3243 | \end{cvardesc} |
| 3244 | |
| 3245 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3246 | Returns true if its argument is a \ctype{PyListObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3247 | \end{cfuncdesc} |
| 3248 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3249 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int len} |
| 3250 | Returns a new list of length \var{len} on success, or \NULL{} on |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 3251 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3252 | \end{cfuncdesc} |
| 3253 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3254 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3255 | Returns the length of the list object in \var{list}; this is |
| 3256 | equivalent to \samp{len(\var{list})} on a list object. |
| 3257 | \bifuncindex{len} |
| 3258 | \end{cfuncdesc} |
| 3259 | |
| 3260 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} |
Fred Drake | 5d64421 | 2000-10-07 12:31:50 +0000 | [diff] [blame] | 3261 | Macro form of \cfunction{PyList_Size()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3262 | \end{cfuncdesc} |
| 3263 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3264 | \begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3265 | Returns the object at position \var{pos} in the list pointed |
| 3266 | to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3267 | sets an \exception{IndexError} exception. |
| 3268 | \end{cfuncdesc} |
| 3269 | |
| 3270 | \begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i} |
| 3271 | Macro form of \cfunction{PyList_GetItem()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3272 | \end{cfuncdesc} |
| 3273 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3274 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index, |
| 3275 | PyObject *item} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 3276 | Sets the item at index \var{index} in list to \var{item}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3277 | \strong{Note:} This function ``steals'' a reference to \var{item}. |
| 3278 | \end{cfuncdesc} |
| 3279 | |
| 3280 | \begin{cfuncdesc}{PyObject*}{PyList_SET_ITEM}{PyObject *list, int i, |
| 3281 | PyObject *o} |
| 3282 | Macro form of \cfunction{PyList_SetItem()} without error checking. |
| 3283 | \strong{Note:} This function ``steals'' a reference to \var{item}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3284 | \end{cfuncdesc} |
| 3285 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3286 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3287 | PyObject *item} |
| 3288 | Inserts the item \var{item} into list \var{list} in front of index |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3289 | \var{index}. Returns \code{0} if successful; returns \code{-1} and |
| 3290 | raises an exception if unsuccessful. Analogous to |
| 3291 | \code{\var{list}.insert(\var{index}, \var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3292 | \end{cfuncdesc} |
| 3293 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3294 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3295 | Appends the object \var{item} at the end of list \var{list}. Returns |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3296 | \code{0} if successful; returns \code{-1} and sets an exception if |
| 3297 | unsuccessful. Analogous to \code{\var{list}.append(\var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3298 | \end{cfuncdesc} |
| 3299 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3300 | \begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list, |
| 3301 | int low, int high} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 3302 | Returns a list of the objects in \var{list} containing the objects |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3303 | \emph{between} \var{low} and \var{high}. Returns NULL and sets an |
| 3304 | exception if unsuccessful. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3305 | Analogous to \code{\var{list}[\var{low}:\var{high}]}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3306 | \end{cfuncdesc} |
| 3307 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3308 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list, |
| 3309 | int low, int high, |
| 3310 | PyObject *itemlist} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3311 | Sets the slice of \var{list} between \var{low} and \var{high} to the |
| 3312 | contents of \var{itemlist}. Analogous to |
| 3313 | \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns |
| 3314 | \code{0} on success, \code{-1} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3315 | \end{cfuncdesc} |
| 3316 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3317 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3318 | Sorts the items of \var{list} in place. Returns \code{0} on success, |
| 3319 | \code{-1} on failure. This is equivalent to |
| 3320 | \samp{\var{list}.sort()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3321 | \end{cfuncdesc} |
| 3322 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3323 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3324 | Reverses the items of \var{list} in place. Returns \code{0} on |
| 3325 | success, \code{-1} on failure. This is the equivalent of |
| 3326 | \samp{\var{list}.reverse()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3327 | \end{cfuncdesc} |
| 3328 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3329 | \begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3330 | Returns a new tuple object containing the contents of \var{list}; |
| 3331 | equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3332 | \end{cfuncdesc} |
| 3333 | |
| 3334 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3335 | \section{Mapping Objects \label{mapObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3336 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3337 | \obindex{mapping} |
| 3338 | |
| 3339 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3340 | \subsection{Dictionary Objects \label{dictObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3341 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3342 | \obindex{dictionary} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3343 | \begin{ctypedesc}{PyDictObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3344 | This subtype of \ctype{PyObject} represents a Python dictionary object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3345 | \end{ctypedesc} |
| 3346 | |
| 3347 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3348 | This instance of \ctype{PyTypeObject} represents the Python dictionary |
| 3349 | type. This is exposed to Python programs as \code{types.DictType} and |
| 3350 | \code{types.DictionaryType}. |
| 3351 | \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3352 | \end{cvardesc} |
| 3353 | |
| 3354 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3355 | Returns true if its argument is a \ctype{PyDictObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3356 | \end{cfuncdesc} |
| 3357 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3358 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3359 | Returns a new empty dictionary, or \NULL{} on failure. |
| 3360 | \end{cfuncdesc} |
| 3361 | |
| 3362 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p} |
| 3363 | Empties an existing dictionary of all key-value pairs. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3364 | \end{cfuncdesc} |
| 3365 | |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 3366 | \begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3367 | Returns a new dictionary that contains the same key-value pairs as p. |
| 3368 | Empties an existing dictionary of all key-value pairs. |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 3369 | \end{cfuncdesc} |
| 3370 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3371 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key, |
| 3372 | PyObject *val} |
| 3373 | Inserts \var{value} into the dictionary with a key of \var{key}. |
| 3374 | \var{key} must be hashable; if it isn't, \exception{TypeError} will be |
| 3375 | raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3376 | \end{cfuncdesc} |
| 3377 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3378 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p, |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3379 | char *key, |
| 3380 | PyObject *val} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3381 | Inserts \var{value} into the dictionary using \var{key} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3382 | as a key. \var{key} should be a \ctype{char*}. The key object is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3383 | created using \code{PyString_FromString(\var{key})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3384 | \ttindex{PyString_FromString()} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3385 | \end{cfuncdesc} |
| 3386 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3387 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3388 | Removes the entry in dictionary \var{p} with key \var{key}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3389 | \var{key} must be hashable; if it isn't, \exception{TypeError} is |
| 3390 | raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3391 | \end{cfuncdesc} |
| 3392 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3393 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3394 | Removes the entry in dictionary \var{p} which has a key |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3395 | specified by the string \var{key}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3396 | \end{cfuncdesc} |
| 3397 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3398 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3399 | Returns the object from dictionary \var{p} which has a key |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3400 | \var{key}. Returns \NULL{} if the key \var{key} is not present, but |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3401 | \emph{without} setting an exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3402 | \end{cfuncdesc} |
| 3403 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3404 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3405 | This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3406 | specified as a \ctype{char*}, rather than a \ctype{PyObject*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3407 | \end{cfuncdesc} |
| 3408 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3409 | \begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3410 | Returns a \ctype{PyListObject} containing all the items |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3411 | from the dictionary, as in the dictinoary method \method{items()} (see |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3412 | the \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3413 | \end{cfuncdesc} |
| 3414 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3415 | \begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3416 | Returns a \ctype{PyListObject} containing all the keys |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3417 | from the dictionary, as in the dictionary method \method{keys()} (see the |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3418 | \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3419 | \end{cfuncdesc} |
| 3420 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3421 | \begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3422 | Returns a \ctype{PyListObject} containing all the values |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3423 | from the dictionary \var{p}, as in the dictionary method |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3424 | \method{values()} (see the \citetitle[../lib/lib.html]{Python Library |
| 3425 | Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3426 | \end{cfuncdesc} |
| 3427 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3428 | \begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p} |
| 3429 | Returns the number of items in the dictionary. This is equivalent to |
| 3430 | \samp{len(\var{p})} on a dictionary.\bifuncindex{len} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3431 | \end{cfuncdesc} |
| 3432 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3433 | \begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos, |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 3434 | PyObject **pkey, PyObject **pvalue} |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3435 | Iterate over all key-value pairs in the dictionary \var{p}. The |
| 3436 | \ctype{int} referred to by \var{ppos} must be initialized to \code{0} |
| 3437 | prior to the first call to this function to start the iteration; the |
| 3438 | function returns true for each pair in the dictionary, and false once |
| 3439 | all pairs have been reported. The parameters \var{pkey} and |
| 3440 | \var{pvalue} should either point to \ctype{PyObject*} variables that |
| 3441 | will be filled in with each key and value, respectively, or may be |
Fred Drake | 8d00a0f | 2001-04-13 17:55:02 +0000 | [diff] [blame] | 3442 | \NULL. |
| 3443 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3444 | For example: |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3445 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3446 | \begin{verbatim} |
| 3447 | PyObject *key, *value; |
| 3448 | int pos = 0; |
| 3449 | |
| 3450 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 3451 | /* do something interesting with the values... */ |
| 3452 | ... |
| 3453 | } |
| 3454 | \end{verbatim} |
Fred Drake | 8d00a0f | 2001-04-13 17:55:02 +0000 | [diff] [blame] | 3455 | |
| 3456 | The dictionary \var{p} should not be mutated during iteration. It is |
| 3457 | safe (since Python 2.1) to modify the values of the keys as you |
| 3458 | iterate over the dictionary, for example: |
| 3459 | |
| 3460 | \begin{verbatim} |
| 3461 | PyObject *key, *value; |
| 3462 | int pos = 0; |
| 3463 | |
| 3464 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 3465 | int i = PyInt_AS_LONG(value) + 1; |
| 3466 | PyObject *o = PyInt_FromLong(i); |
| 3467 | if (o == NULL) |
| 3468 | return -1; |
| 3469 | if (PyDict_SetItem(self->dict, key, o) < 0) { |
| 3470 | Py_DECREF(o); |
| 3471 | return -1; |
| 3472 | } |
| 3473 | Py_DECREF(o); |
| 3474 | } |
| 3475 | \end{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3476 | \end{cfuncdesc} |
| 3477 | |
| 3478 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3479 | \section{Numeric Objects \label{numericObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3480 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3481 | \obindex{numeric} |
| 3482 | |
| 3483 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3484 | \subsection{Plain Integer Objects \label{intObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3485 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3486 | \obindex{integer} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3487 | \begin{ctypedesc}{PyIntObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3488 | This subtype of \ctype{PyObject} represents a Python integer object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3489 | \end{ctypedesc} |
| 3490 | |
| 3491 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3492 | This instance of \ctype{PyTypeObject} represents the Python plain |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3493 | integer type. This is the same object as \code{types.IntType}. |
| 3494 | \withsubitem{(in modules types)}{\ttindex{IntType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3495 | \end{cvardesc} |
| 3496 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3497 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o} |
| 3498 | Returns true if \var{o} is of type \cdata{PyInt_Type}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3499 | \end{cfuncdesc} |
| 3500 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3501 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3502 | Creates a new integer object with a value of \var{ival}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3503 | |
| 3504 | The current implementation keeps an array of integer objects for all |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3505 | integers between \code{-1} and \code{100}, when you create an int in |
| 3506 | that range you actually just get back a reference to the existing |
| 3507 | object. So it should be possible to change the value of \code{1}. I |
Fred Drake | 7e9d314 | 1998-04-03 05:02:28 +0000 | [diff] [blame] | 3508 | suspect the behaviour of Python in this case is undefined. :-) |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3509 | \end{cfuncdesc} |
| 3510 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3511 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3512 | Will first attempt to cast the object to a \ctype{PyIntObject}, if |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3513 | it is not already one, and then return its value. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3514 | \end{cfuncdesc} |
| 3515 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3516 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io} |
| 3517 | Returns the value of the object \var{io}. No error checking is |
| 3518 | performed. |
| 3519 | \end{cfuncdesc} |
| 3520 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3521 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3522 | Returns the system's idea of the largest integer it can handle |
| 3523 | (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system |
| 3524 | header files). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3525 | \end{cfuncdesc} |
| 3526 | |
| 3527 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3528 | \subsection{Long Integer Objects \label{longObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3529 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3530 | \obindex{long integer} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3531 | \begin{ctypedesc}{PyLongObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3532 | This subtype of \ctype{PyObject} represents a Python long integer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3533 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3534 | \end{ctypedesc} |
| 3535 | |
| 3536 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3537 | This instance of \ctype{PyTypeObject} represents the Python long |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3538 | integer type. This is the same object as \code{types.LongType}. |
| 3539 | \withsubitem{(in modules types)}{\ttindex{LongType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3540 | \end{cvardesc} |
| 3541 | |
| 3542 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3543 | Returns true if its argument is a \ctype{PyLongObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3544 | \end{cfuncdesc} |
| 3545 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3546 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3547 | Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{} on |
| 3548 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3549 | \end{cfuncdesc} |
| 3550 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3551 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3552 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned |
| 3553 | long}, or \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3554 | \end{cfuncdesc} |
| 3555 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3556 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3557 | Returns a new \ctype{PyLongObject} object from the integer part of |
| 3558 | \var{v}, or \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3559 | \end{cfuncdesc} |
| 3560 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3561 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3562 | Returns a C \ctype{long} representation of the contents of |
| 3563 | \var{pylong}. If \var{pylong} is greater than |
| 3564 | \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError} is |
| 3565 | raised.\withsubitem{(built-in exception)}{OverflowError} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3566 | \end{cfuncdesc} |
| 3567 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3568 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3569 | Returns a C \ctype{unsigned long} representation of the contents of |
| 3570 | \var{pylong}. If \var{pylong} is greater than |
| 3571 | \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an \exception{OverflowError} |
| 3572 | is raised.\withsubitem{(built-in exception)}{OverflowError} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3573 | \end{cfuncdesc} |
| 3574 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3575 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3576 | Returns a C \ctype{double} representation of the contents of \var{pylong}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3577 | \end{cfuncdesc} |
| 3578 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3579 | \begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend, |
| 3580 | int base} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3581 | Return a new \ctype{PyLongObject} based on the string value in |
| 3582 | \var{str}, which is interpreted according to the radix in \var{base}. |
| 3583 | If \var{pend} is non-\NULL, \code{*\var{pend}} will point to the first |
| 3584 | character in \var{str} which follows the representation of the |
| 3585 | number. If \var{base} is \code{0}, the radix will be determined base |
| 3586 | on the leading characters of \var{str}: if \var{str} starts with |
| 3587 | \code{'0x'} or \code{'0X'}, radix 16 will be used; if \var{str} starts |
| 3588 | with \code{'0'}, radix 8 will be used; otherwise radix 10 will be |
| 3589 | used. If \var{base} is not \code{0}, it must be between \code{2} and |
| 3590 | \code{36}, inclusive. Leading spaces are ignored. If there are no |
| 3591 | digits, \exception{ValueError} will be raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3592 | \end{cfuncdesc} |
| 3593 | |
| 3594 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3595 | \subsection{Floating Point Objects \label{floatObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3596 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3597 | \obindex{floating point} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3598 | \begin{ctypedesc}{PyFloatObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3599 | This subtype of \ctype{PyObject} represents a Python floating point |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3600 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3601 | \end{ctypedesc} |
| 3602 | |
| 3603 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3604 | This instance of \ctype{PyTypeObject} represents the Python floating |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3605 | point type. This is the same object as \code{types.FloatType}. |
| 3606 | \withsubitem{(in modules types)}{\ttindex{FloatType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3607 | \end{cvardesc} |
| 3608 | |
| 3609 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3610 | Returns true if its argument is a \ctype{PyFloatObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3611 | \end{cfuncdesc} |
| 3612 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3613 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3614 | Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on |
| 3615 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3616 | \end{cfuncdesc} |
| 3617 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3618 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3619 | Returns a C \ctype{double} representation of the contents of \var{pyfloat}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3620 | \end{cfuncdesc} |
| 3621 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3622 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3623 | Returns a C \ctype{double} representation of the contents of |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3624 | \var{pyfloat}, but without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3625 | \end{cfuncdesc} |
| 3626 | |
| 3627 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3628 | \subsection{Complex Number Objects \label{complexObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3629 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3630 | \obindex{complex number} |
| 3631 | Python's complex number objects are implemented as two distinct types |
| 3632 | when viewed from the C API: one is the Python object exposed to |
| 3633 | Python programs, and the other is a C structure which represents the |
| 3634 | actual complex number value. The API provides functions for working |
| 3635 | with both. |
| 3636 | |
| 3637 | \subsubsection{Complex Numbers as C Structures} |
| 3638 | |
| 3639 | Note that the functions which accept these structures as parameters |
| 3640 | and return them as results do so \emph{by value} rather than |
| 3641 | dereferencing them through pointers. This is consistent throughout |
| 3642 | the API. |
| 3643 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3644 | \begin{ctypedesc}{Py_complex} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3645 | The C structure which corresponds to the value portion of a Python |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3646 | complex number object. Most of the functions for dealing with complex |
| 3647 | number objects use structures of this type as input or output values, |
| 3648 | as appropriate. It is defined as: |
| 3649 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3650 | \begin{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3651 | typedef struct { |
| 3652 | double real; |
| 3653 | double imag; |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3654 | } Py_complex; |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3655 | \end{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3656 | \end{ctypedesc} |
| 3657 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3658 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right} |
| 3659 | Return the sum of two complex numbers, using the C |
| 3660 | \ctype{Py_complex} representation. |
| 3661 | \end{cfuncdesc} |
| 3662 | |
| 3663 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right} |
| 3664 | Return the difference between two complex numbers, using the C |
| 3665 | \ctype{Py_complex} representation. |
| 3666 | \end{cfuncdesc} |
| 3667 | |
| 3668 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex} |
| 3669 | Return the negation of the complex number \var{complex}, using the C |
| 3670 | \ctype{Py_complex} representation. |
| 3671 | \end{cfuncdesc} |
| 3672 | |
| 3673 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right} |
| 3674 | Return the product of two complex numbers, using the C |
| 3675 | \ctype{Py_complex} representation. |
| 3676 | \end{cfuncdesc} |
| 3677 | |
| 3678 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend, |
| 3679 | Py_complex divisor} |
| 3680 | Return the quotient of two complex numbers, using the C |
| 3681 | \ctype{Py_complex} representation. |
| 3682 | \end{cfuncdesc} |
| 3683 | |
| 3684 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp} |
| 3685 | Return the exponentiation of \var{num} by \var{exp}, using the C |
| 3686 | \ctype{Py_complex} representation. |
| 3687 | \end{cfuncdesc} |
| 3688 | |
| 3689 | |
| 3690 | \subsubsection{Complex Numbers as Python Objects} |
| 3691 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3692 | \begin{ctypedesc}{PyComplexObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3693 | This subtype of \ctype{PyObject} represents a Python complex number object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3694 | \end{ctypedesc} |
| 3695 | |
| 3696 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3697 | This instance of \ctype{PyTypeObject} represents the Python complex |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3698 | number type. |
| 3699 | \end{cvardesc} |
| 3700 | |
| 3701 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3702 | Returns true if its argument is a \ctype{PyComplexObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3703 | \end{cfuncdesc} |
| 3704 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3705 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3706 | Create a new Python complex number object from a C |
| 3707 | \ctype{Py_complex} value. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3708 | \end{cfuncdesc} |
| 3709 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3710 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3711 | Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3712 | \end{cfuncdesc} |
| 3713 | |
| 3714 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3715 | Returns the real part of \var{op} as a C \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3716 | \end{cfuncdesc} |
| 3717 | |
| 3718 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3719 | Returns the imaginary part of \var{op} as a C \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3720 | \end{cfuncdesc} |
| 3721 | |
| 3722 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3723 | Returns the \ctype{Py_complex} value of the complex number \var{op}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3724 | \end{cfuncdesc} |
| 3725 | |
| 3726 | |
| 3727 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3728 | \section{Other Objects \label{otherObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3729 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3730 | \subsection{File Objects \label{fileObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3731 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3732 | \obindex{file} |
| 3733 | Python's built-in file objects are implemented entirely on the |
| 3734 | \ctype{FILE*} support from the C standard library. This is an |
| 3735 | implementation detail and may change in future releases of Python. |
| 3736 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3737 | \begin{ctypedesc}{PyFileObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3738 | This subtype of \ctype{PyObject} represents a Python file object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3739 | \end{ctypedesc} |
| 3740 | |
| 3741 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3742 | This instance of \ctype{PyTypeObject} represents the Python file |
| 3743 | type. This is exposed to Python programs as \code{types.FileType}. |
| 3744 | \withsubitem{(in module types)}{\ttindex{FileType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3745 | \end{cvardesc} |
| 3746 | |
| 3747 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3748 | Returns true if its argument is a \ctype{PyFileObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3749 | \end{cfuncdesc} |
| 3750 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3751 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode} |
| 3752 | On success, returns a new file object that is opened on the |
| 3753 | file given by \var{filename}, with a file mode given by \var{mode}, |
| 3754 | where \var{mode} has the same semantics as the standard C routine |
| 3755 | \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3756 | \end{cfuncdesc} |
| 3757 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3758 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3759 | char *name, char *mode, |
| 3760 | int (*close)(FILE*)} |
| 3761 | Creates a new \ctype{PyFileObject} from the already-open standard C |
| 3762 | file pointer, \var{fp}. The function \var{close} will be called when |
| 3763 | the file should be closed. Returns \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3764 | \end{cfuncdesc} |
| 3765 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3766 | \begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p} |
| 3767 | Returns the file object associated with \var{p} as a \ctype{FILE*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3768 | \end{cfuncdesc} |
| 3769 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3770 | \begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3771 | Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this |
| 3772 | function reads one line from the object \var{p}. \var{p} may be a |
| 3773 | file object or any object with a \method{readline()} method. If |
| 3774 | \var{n} is \code{0}, exactly one line is read, regardless of the |
| 3775 | length of the line. If \var{n} is greater than \code{0}, no more than |
| 3776 | \var{n} bytes will be read from the file; a partial line can be |
| 3777 | returned. In both cases, an empty string is returned if the end of |
| 3778 | the file is reached immediately. If \var{n} is less than \code{0}, |
| 3779 | however, one line is read regardless of length, but |
| 3780 | \exception{EOFError} is raised if the end of the file is reached |
| 3781 | immediately. |
| 3782 | \withsubitem{(built-in exception)}{\ttindex{EOFError}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3783 | \end{cfuncdesc} |
| 3784 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3785 | \begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3786 | Returns the name of the file specified by \var{p} as a string object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3787 | \end{cfuncdesc} |
| 3788 | |
| 3789 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3790 | Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()} |
| 3791 | only. This should only be called immediately after file object |
| 3792 | creation. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3793 | \end{cfuncdesc} |
| 3794 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3795 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag} |
| 3796 | This function exists for internal use by the interpreter. |
| 3797 | Sets the \member{softspace} attribute of \var{p} to \var{newflag} and |
| 3798 | \withsubitem{(file attribute)}{\ttindex{softspace}}returns the |
| 3799 | previous value. \var{p} does not have to be a file object |
| 3800 | for this function to work properly; any object is supported (thought |
| 3801 | its only interesting if the \member{softspace} attribute can be set). |
| 3802 | This function clears any errors, and will return \code{0} as the |
| 3803 | previous value if the attribute either does not exist or if there were |
| 3804 | errors in retrieving it. There is no way to detect errors from this |
| 3805 | function, but doing so should not be needed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3806 | \end{cfuncdesc} |
| 3807 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3808 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p, |
| 3809 | int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3810 | Writes object \var{obj} to file object \var{p}. The only supported |
| 3811 | flag for \var{flags} is \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; |
| 3812 | if given, the \function{str()} of the object is written instead of the |
| 3813 | \function{repr()}. Returns \code{0} on success or \code{-1} on |
| 3814 | failure; the appropriate exception will be set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3815 | \end{cfuncdesc} |
| 3816 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3817 | \begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p, |
| 3818 | int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3819 | Writes string \var{s} to file object \var{p}. Returns \code{0} on |
| 3820 | success or \code{-1} on failure; the appropriate exception will be |
| 3821 | set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3822 | \end{cfuncdesc} |
| 3823 | |
| 3824 | |
Fred Drake | 5838d0f | 2001-01-28 06:39:35 +0000 | [diff] [blame] | 3825 | \subsection{Instance Objects \label{instanceObjects}} |
| 3826 | |
| 3827 | \obindex{instance} |
| 3828 | There are very few functions specific to instance objects. |
| 3829 | |
| 3830 | \begin{cvardesc}{PyTypeObject}{PyInstance_Type} |
| 3831 | Type object for class instances. |
| 3832 | \end{cvardesc} |
| 3833 | |
| 3834 | \begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj} |
| 3835 | Returns true if \var{obj} is an instance. |
| 3836 | \end{cfuncdesc} |
| 3837 | |
| 3838 | \begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class, |
| 3839 | PyObject *arg, |
| 3840 | PyObject *kw} |
| 3841 | Create a new instance of a specific class. The parameters \var{arg} |
| 3842 | and \var{kw} are used as the positional and keyword parameters to |
| 3843 | the object's constructor. |
| 3844 | \end{cfuncdesc} |
| 3845 | |
| 3846 | \begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class, |
| 3847 | PyObject *dict} |
| 3848 | Create a new instance of a specific class without calling it's |
| 3849 | constructor. \var{class} is the class of new object. The |
| 3850 | \var{dict} parameter will be used as the object's \member{__dict__}; |
| 3851 | if \NULL, a new dictionary will be created for the instance. |
| 3852 | \end{cfuncdesc} |
| 3853 | |
| 3854 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3855 | \subsection{Module Objects \label{moduleObjects}} |
| 3856 | |
| 3857 | \obindex{module} |
| 3858 | There are only a few functions special to module objects. |
| 3859 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3860 | \begin{cvardesc}{PyTypeObject}{PyModule_Type} |
| 3861 | This instance of \ctype{PyTypeObject} represents the Python module |
| 3862 | type. This is exposed to Python programs as \code{types.ModuleType}. |
| 3863 | \withsubitem{(in module types)}{\ttindex{ModuleType}} |
| 3864 | \end{cvardesc} |
| 3865 | |
| 3866 | \begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p} |
| 3867 | Returns true if its argument is a module object. |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3868 | \end{cfuncdesc} |
| 3869 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3870 | \begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name} |
| 3871 | Return a new module object with the \member{__name__} attribute set to |
| 3872 | \var{name}. Only the module's \member{__doc__} and |
| 3873 | \member{__name__} attributes are filled in; the caller is responsible |
| 3874 | for providing a \member{__file__} attribute. |
| 3875 | \withsubitem{(module attribute)}{ |
| 3876 | \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}} |
| 3877 | \end{cfuncdesc} |
| 3878 | |
| 3879 | \begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3880 | Return the dictionary object that implements \var{module}'s namespace; |
| 3881 | this object is the same as the \member{__dict__} attribute of the |
| 3882 | module object. This function never fails. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3883 | \withsubitem{(module attribute)}{\ttindex{__dict__}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3884 | \end{cfuncdesc} |
| 3885 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3886 | \begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3887 | Return \var{module}'s \member{__name__} value. If the module does not |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3888 | provide one, or if it is not a string, \exception{SystemError} is |
| 3889 | raised and \NULL{} is returned. |
| 3890 | \withsubitem{(module attribute)}{\ttindex{__name__}} |
| 3891 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3892 | \end{cfuncdesc} |
| 3893 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3894 | \begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3895 | Return the name of the file from which \var{module} was loaded using |
| 3896 | \var{module}'s \member{__file__} attribute. If this is not defined, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3897 | or if it is not a string, raise \exception{SystemError} and return |
| 3898 | \NULL. |
| 3899 | \withsubitem{(module attribute)}{\ttindex{__file__}} |
| 3900 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3901 | \end{cfuncdesc} |
| 3902 | |
Fred Drake | 891150b | 2000-09-23 03:25:42 +0000 | [diff] [blame] | 3903 | \begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module, |
| 3904 | char *name, PyObject *value} |
| 3905 | Add an object to \var{module} as \var{name}. This is a convenience |
| 3906 | function which can be used from the module's initialization function. |
| 3907 | This steals a reference to \var{value}. Returns \code{-1} on error, |
| 3908 | \code{0} on success. |
| 3909 | \versionadded{2.0} |
| 3910 | \end{cfuncdesc} |
| 3911 | |
| 3912 | \begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module, |
| 3913 | char *name, int value} |
| 3914 | Add an integer constant to \var{module} as \var{name}. This convenience |
| 3915 | function can be used from the module's initialization function. |
| 3916 | Returns \code{-1} on error, \code{0} on success. |
| 3917 | \versionadded{2.0} |
| 3918 | \end{cfuncdesc} |
| 3919 | |
| 3920 | \begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module, |
| 3921 | char *name, char *value} |
| 3922 | Add a string constant to \var{module} as \var{name}. This convenience |
| 3923 | function can be used from the module's initialization function. The |
| 3924 | string \var{value} must be null-terminated. Returns \code{-1} on |
| 3925 | error, \code{0} on success. |
| 3926 | \versionadded{2.0} |
| 3927 | \end{cfuncdesc} |
| 3928 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3929 | |
| 3930 | \subsection{CObjects \label{cObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3931 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3932 | \obindex{CObject} |
| 3933 | Refer to \emph{Extending and Embedding the Python Interpreter}, |
| 3934 | section 1.12 (``Providing a C API for an Extension Module''), for more |
| 3935 | information on using these objects. |
| 3936 | |
| 3937 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3938 | \begin{ctypedesc}{PyCObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3939 | This subtype of \ctype{PyObject} represents an opaque value, useful for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3940 | C extension modules who need to pass an opaque value (as a |
| 3941 | \ctype{void*} pointer) through Python code to other C code. It is |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3942 | often used to make a C function pointer defined in one module |
| 3943 | available to other modules, so the regular import mechanism can be |
| 3944 | used to access C APIs defined in dynamically loaded modules. |
| 3945 | \end{ctypedesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3946 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3947 | \begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p} |
| 3948 | Returns true if its argument is a \ctype{PyCObject}. |
| 3949 | \end{cfuncdesc} |
| 3950 | |
| 3951 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 3952 | void (*destr)(void *)} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3953 | Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The |
Fred Drake | dab4468 | 1999-05-13 18:41:14 +0000 | [diff] [blame] | 3954 | \var{destr} function will be called when the object is reclaimed, unless |
| 3955 | it is \NULL. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3956 | \end{cfuncdesc} |
| 3957 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3958 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 3959 | void* desc, void (*destr)(void *, void *) } |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3960 | Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The |
| 3961 | \var{destr} function will be called when the object is reclaimed. The |
| 3962 | \var{desc} argument can be used to pass extra callback data for the |
| 3963 | destructor function. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3964 | \end{cfuncdesc} |
| 3965 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3966 | \begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self} |
| 3967 | Returns the object \ctype{void *} that the |
| 3968 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3969 | \end{cfuncdesc} |
| 3970 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3971 | \begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self} |
| 3972 | Returns the description \ctype{void *} that the |
| 3973 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3974 | \end{cfuncdesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3975 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3976 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3977 | \chapter{Initialization, Finalization, and Threads |
| 3978 | \label{initialization}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3979 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3980 | \begin{cfuncdesc}{void}{Py_Initialize}{} |
| 3981 | Initialize the Python interpreter. In an application embedding |
| 3982 | Python, this should be called before using any other Python/C API |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3983 | functions; with the exception of |
| 3984 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, |
| 3985 | \cfunction{PyEval_InitThreads()}\ttindex{PyEval_InitThreads()}, |
| 3986 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()}, |
| 3987 | and \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()}. |
| 3988 | This initializes the table of loaded modules (\code{sys.modules}), and |
| 3989 | \withsubitem{(in module sys)}{\ttindex{modules}\ttindex{path}}creates the |
| 3990 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3991 | \module{__main__}\refbimodindex{__main__} and |
| 3992 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3993 | search\indexiii{module}{search}{path} path (\code{sys.path}). |
| 3994 | It does not set \code{sys.argv}; use |
| 3995 | \cfunction{PySys_SetArgv()}\ttindex{PySys_SetArgv()} for that. This |
| 3996 | is a no-op when called for a second time (without calling |
| 3997 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} first). There is no |
| 3998 | return value; it is a fatal error if the initialization fails. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 3999 | \end{cfuncdesc} |
| 4000 | |
| 4001 | \begin{cfuncdesc}{int}{Py_IsInitialized}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4002 | Return true (nonzero) when the Python interpreter has been |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4003 | initialized, false (zero) if not. After \cfunction{Py_Finalize()} is |
| 4004 | called, this returns false until \cfunction{Py_Initialize()} is called |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4005 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4006 | \end{cfuncdesc} |
| 4007 | |
| 4008 | \begin{cfuncdesc}{void}{Py_Finalize}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4009 | Undo all initializations made by \cfunction{Py_Initialize()} and |
| 4010 | subsequent use of Python/C API functions, and destroy all |
| 4011 | sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were |
| 4012 | created and not yet destroyed since the last call to |
| 4013 | \cfunction{Py_Initialize()}. Ideally, this frees all memory allocated |
| 4014 | by the Python interpreter. This is a no-op when called for a second |
| 4015 | time (without calling \cfunction{Py_Initialize()} again first). There |
| 4016 | is no return value; errors during finalization are ignored. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4017 | |
| 4018 | This function is provided for a number of reasons. An embedding |
| 4019 | application might want to restart Python without having to restart the |
| 4020 | application itself. An application that has loaded the Python |
| 4021 | interpreter from a dynamically loadable library (or DLL) might want to |
| 4022 | free all memory allocated by Python before unloading the DLL. During a |
| 4023 | hunt for memory leaks in an application a developer might want to free |
| 4024 | all memory allocated by Python before exiting from the application. |
| 4025 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4026 | \strong{Bugs and caveats:} The destruction of modules and objects in |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4027 | modules is done in random order; this may cause destructors |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4028 | (\method{__del__()} methods) to fail when they depend on other objects |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4029 | (even functions) or modules. Dynamically loaded extension modules |
| 4030 | loaded by Python are not unloaded. Small amounts of memory allocated |
| 4031 | by the Python interpreter may not be freed (if you find a leak, please |
| 4032 | report it). Memory tied up in circular references between objects is |
| 4033 | not freed. Some memory allocated by extension modules may not be |
| 4034 | freed. Some extension may not work properly if their initialization |
| 4035 | routine is called more than once; this can happen if an applcation |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4036 | calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more |
| 4037 | than once. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4038 | \end{cfuncdesc} |
| 4039 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4040 | \begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4041 | Create a new sub-interpreter. This is an (almost) totally separate |
| 4042 | environment for the execution of Python code. In particular, the new |
| 4043 | interpreter has separate, independent versions of all imported |
| 4044 | modules, including the fundamental modules |
| 4045 | \module{__builtin__}\refbimodindex{__builtin__}, |
| 4046 | \module{__main__}\refbimodindex{__main__} and |
| 4047 | \module{sys}\refbimodindex{sys}. The table of loaded modules |
| 4048 | (\code{sys.modules}) and the module search path (\code{sys.path}) are |
| 4049 | also separate. The new environment has no \code{sys.argv} variable. |
| 4050 | It has new standard I/O stream file objects \code{sys.stdin}, |
| 4051 | \code{sys.stdout} and \code{sys.stderr} (however these refer to the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4052 | same underlying \ctype{FILE} structures in the C library). |
| 4053 | \withsubitem{(in module sys)}{ |
| 4054 | \ttindex{stdout}\ttindex{stderr}\ttindex{stdin}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4055 | |
| 4056 | The return value points to the first thread state created in the new |
| 4057 | sub-interpreter. This thread state is made the current thread state. |
| 4058 | Note that no actual thread is created; see the discussion of thread |
| 4059 | states below. If creation of the new interpreter is unsuccessful, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4060 | \NULL{} is returned; no exception is set since the exception state |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4061 | is stored in the current thread state and there may not be a current |
| 4062 | thread state. (Like all other Python/C API functions, the global |
| 4063 | interpreter lock must be held before calling this function and is |
| 4064 | still held when it returns; however, unlike most other Python/C API |
| 4065 | functions, there needn't be a current thread state on entry.) |
| 4066 | |
| 4067 | Extension modules are shared between (sub-)interpreters as follows: |
| 4068 | the first time a particular extension is imported, it is initialized |
| 4069 | normally, and a (shallow) copy of its module's dictionary is |
| 4070 | squirreled away. When the same extension is imported by another |
| 4071 | (sub-)interpreter, a new module is initialized and filled with the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4072 | contents of this copy; the extension's \code{init} function is not |
| 4073 | called. Note that this is different from what happens when an |
| 4074 | extension is imported after the interpreter has been completely |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4075 | re-initialized by calling |
| 4076 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 4077 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}; in that case, |
| 4078 | the extension's \code{init\var{module}} function \emph{is} called |
| 4079 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4080 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4081 | \strong{Bugs and caveats:} Because sub-interpreters (and the main |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4082 | interpreter) are part of the same process, the insulation between them |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4083 | isn't perfect --- for example, using low-level file operations like |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4084 | \withsubitem{(in module os)}{\ttindex{close()}} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4085 | \function{os.close()} they can (accidentally or maliciously) affect each |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4086 | other's open files. Because of the way extensions are shared between |
| 4087 | (sub-)interpreters, some extensions may not work properly; this is |
| 4088 | especially likely when the extension makes use of (static) global |
| 4089 | variables, or when the extension manipulates its module's dictionary |
| 4090 | after its initialization. It is possible to insert objects created in |
| 4091 | one sub-interpreter into a namespace of another sub-interpreter; this |
| 4092 | should be done with great care to avoid sharing user-defined |
| 4093 | functions, methods, instances or classes between sub-interpreters, |
| 4094 | since import operations executed by such objects may affect the |
| 4095 | wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is |
| 4096 | a hard-to-fix bug that will be addressed in a future release.) |
| 4097 | \end{cfuncdesc} |
| 4098 | |
| 4099 | \begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate} |
| 4100 | Destroy the (sub-)interpreter represented by the given thread state. |
| 4101 | The given thread state must be the current thread state. See the |
| 4102 | discussion of thread states below. When the call returns, the current |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4103 | thread state is \NULL{}. All thread states associated with this |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4104 | interpreted are destroyed. (The global interpreter lock must be held |
| 4105 | before calling this function and is still held when it returns.) |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4106 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} will destroy all |
| 4107 | sub-interpreters that haven't been explicitly destroyed at that point. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4108 | \end{cfuncdesc} |
| 4109 | |
| 4110 | \begin{cfuncdesc}{void}{Py_SetProgramName}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4111 | This function should be called before |
| 4112 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()} is called |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4113 | for the first time, if it is called at all. It tells the interpreter |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4114 | the value of the \code{argv[0]} argument to the |
| 4115 | \cfunction{main()}\ttindex{main()} function of the program. This is |
| 4116 | used by \cfunction{Py_GetPath()}\ttindex{Py_GetPath()} and some other |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4117 | functions below to find the Python run-time libraries relative to the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4118 | interpreter executable. The default value is \code{'python'}. The |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4119 | argument should point to a zero-terminated character string in static |
| 4120 | storage whose contents will not change for the duration of the |
| 4121 | program's execution. No code in the Python interpreter will change |
| 4122 | the contents of this storage. |
| 4123 | \end{cfuncdesc} |
| 4124 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4125 | \begin{cfuncdesc}{char*}{Py_GetProgramName}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4126 | Return the program name set with |
| 4127 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, or the |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4128 | default. The returned string points into static storage; the caller |
| 4129 | should not modify its value. |
| 4130 | \end{cfuncdesc} |
| 4131 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4132 | \begin{cfuncdesc}{char*}{Py_GetPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4133 | Return the \emph{prefix} for installed platform-independent files. This |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4134 | is derived through a number of complicated rules from the program name |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4135 | set with \cfunction{Py_SetProgramName()} and some environment variables; |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4136 | for example, if the program name is \code{'/usr/local/bin/python'}, |
| 4137 | the prefix is \code{'/usr/local'}. The returned string points into |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4138 | static storage; the caller should not modify its value. This |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 4139 | corresponds to the \makevar{prefix} variable in the top-level |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4140 | \file{Makefile} and the \longprogramopt{prefix} argument to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4141 | \program{configure} script at build time. The value is available to |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4142 | Python code as \code{sys.prefix}. It is only useful on \UNIX{}. See |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4143 | also the next function. |
| 4144 | \end{cfuncdesc} |
| 4145 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4146 | \begin{cfuncdesc}{char*}{Py_GetExecPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4147 | Return the \emph{exec-prefix} for installed platform-\emph{de}pendent |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4148 | files. This is derived through a number of complicated rules from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4149 | program name set with \cfunction{Py_SetProgramName()} and some environment |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4150 | variables; for example, if the program name is |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4151 | \code{'/usr/local/bin/python'}, the exec-prefix is |
| 4152 | \code{'/usr/local'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4153 | the caller should not modify its value. This corresponds to the |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 4154 | \makevar{exec_prefix} variable in the top-level \file{Makefile} and the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4155 | \longprogramopt{exec-prefix} argument to the |
Fred Drake | 310ee61 | 1999-11-09 17:31:42 +0000 | [diff] [blame] | 4156 | \program{configure} script at build time. The value is available to |
| 4157 | Python code as \code{sys.exec_prefix}. It is only useful on \UNIX{}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4158 | |
| 4159 | Background: The exec-prefix differs from the prefix when platform |
| 4160 | dependent files (such as executables and shared libraries) are |
| 4161 | installed in a different directory tree. In a typical installation, |
| 4162 | platform dependent files may be installed in the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4163 | \file{/usr/local/plat} subtree while platform independent may be |
| 4164 | installed in \file{/usr/local}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4165 | |
| 4166 | Generally speaking, a platform is a combination of hardware and |
| 4167 | software families, e.g. Sparc machines running the Solaris 2.x |
| 4168 | operating system are considered the same platform, but Intel machines |
| 4169 | running Solaris 2.x are another platform, and Intel machines running |
| 4170 | Linux are yet another platform. Different major revisions of the same |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4171 | operating system generally also form different platforms. Non-\UNIX{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4172 | operating systems are a different story; the installation strategies |
| 4173 | on those systems are so different that the prefix and exec-prefix are |
| 4174 | meaningless, and set to the empty string. Note that compiled Python |
| 4175 | bytecode files are platform independent (but not independent from the |
| 4176 | Python version by which they were compiled!). |
| 4177 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4178 | System administrators will know how to configure the \program{mount} or |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4179 | \program{automount} programs to share \file{/usr/local} between platforms |
| 4180 | while having \file{/usr/local/plat} be a different filesystem for each |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4181 | platform. |
| 4182 | \end{cfuncdesc} |
| 4183 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4184 | \begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4185 | Return the full program name of the Python executable; this is |
| 4186 | computed as a side-effect of deriving the default module search path |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4187 | from the program name (set by |
| 4188 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()} above). |
| 4189 | The returned string points into static storage; the caller should not |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4190 | modify its value. The value is available to Python code as |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4191 | \code{sys.executable}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4192 | \withsubitem{(in module sys)}{\ttindex{executable}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4193 | \end{cfuncdesc} |
| 4194 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4195 | \begin{cfuncdesc}{char*}{Py_GetPath}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4196 | \indexiii{module}{search}{path} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4197 | Return the default module search path; this is computed from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4198 | program name (set by \cfunction{Py_SetProgramName()} above) and some |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4199 | environment variables. The returned string consists of a series of |
| 4200 | directory names separated by a platform dependent delimiter character. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4201 | The delimiter character is \character{:} on \UNIX{}, \character{;} on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4202 | DOS/Windows, and \character{\e n} (the \ASCII{} newline character) on |
Fred Drake | e5bc497 | 1998-02-12 23:36:49 +0000 | [diff] [blame] | 4203 | Macintosh. The returned string points into static storage; the caller |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4204 | should not modify its value. The value is available to Python code |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4205 | as the list \code{sys.path}\withsubitem{(in module sys)}{\ttindex{path}}, |
| 4206 | which may be modified to change the future search path for loaded |
| 4207 | modules. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4208 | |
| 4209 | % XXX should give the exact rules |
| 4210 | \end{cfuncdesc} |
| 4211 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4212 | \begin{cfuncdesc}{const char*}{Py_GetVersion}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4213 | Return the version of this Python interpreter. This is a string that |
| 4214 | looks something like |
| 4215 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 4216 | \begin{verbatim} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4217 | "1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]" |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 4218 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4219 | |
| 4220 | The first word (up to the first space character) is the current Python |
| 4221 | version; the first three characters are the major and minor version |
| 4222 | separated by a period. The returned string points into static storage; |
| 4223 | the caller should not modify its value. The value is available to |
| 4224 | Python code as the list \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4225 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4226 | \end{cfuncdesc} |
| 4227 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4228 | \begin{cfuncdesc}{const char*}{Py_GetPlatform}{} |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4229 | Return the platform identifier for the current platform. On \UNIX{}, |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4230 | this is formed from the ``official'' name of the operating system, |
| 4231 | converted to lower case, followed by the major revision number; e.g., |
| 4232 | for Solaris 2.x, which is also known as SunOS 5.x, the value is |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4233 | \code{'sunos5'}. On Macintosh, it is \code{'mac'}. On Windows, it |
| 4234 | is \code{'win'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4235 | the caller should not modify its value. The value is available to |
| 4236 | Python code as \code{sys.platform}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4237 | \withsubitem{(in module sys)}{\ttindex{platform}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4238 | \end{cfuncdesc} |
| 4239 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4240 | \begin{cfuncdesc}{const char*}{Py_GetCopyright}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4241 | Return the official copyright string for the current Python version, |
| 4242 | for example |
| 4243 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4244 | \code{'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4245 | |
| 4246 | The returned string points into static storage; the caller should not |
| 4247 | modify its value. The value is available to Python code as the list |
| 4248 | \code{sys.copyright}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4249 | \withsubitem{(in module sys)}{\ttindex{copyright}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4250 | \end{cfuncdesc} |
| 4251 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4252 | \begin{cfuncdesc}{const char*}{Py_GetCompiler}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4253 | Return an indication of the compiler used to build the current Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4254 | version, in square brackets, for example: |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4255 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4256 | \begin{verbatim} |
| 4257 | "[GCC 2.7.2.2]" |
| 4258 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4259 | |
| 4260 | The returned string points into static storage; the caller should not |
| 4261 | modify its value. The value is available to Python code as part of |
| 4262 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4263 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4264 | \end{cfuncdesc} |
| 4265 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4266 | \begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4267 | Return information about the sequence number and build date and time |
| 4268 | of the current Python interpreter instance, for example |
| 4269 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 4270 | \begin{verbatim} |
| 4271 | "#67, Aug 1 1997, 22:34:28" |
| 4272 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4273 | |
| 4274 | The returned string points into static storage; the caller should not |
| 4275 | modify its value. The value is available to Python code as part of |
| 4276 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4277 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4278 | \end{cfuncdesc} |
| 4279 | |
| 4280 | \begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4281 | Set \code{sys.argv} based on \var{argc} and \var{argv}. These |
| 4282 | parameters are similar to those passed to the program's |
| 4283 | \cfunction{main()}\ttindex{main()} function with the difference that |
| 4284 | the first entry should refer to the script file to be executed rather |
| 4285 | than the executable hosting the Python interpreter. If there isn't a |
| 4286 | script that will be run, the first entry in \var{argv} can be an empty |
| 4287 | string. If this function fails to initialize \code{sys.argv}, a fatal |
| 4288 | condition is signalled using |
| 4289 | \cfunction{Py_FatalError()}\ttindex{Py_FatalError()}. |
| 4290 | \withsubitem{(in module sys)}{\ttindex{argv}} |
| 4291 | % XXX impl. doesn't seem consistent in allowing 0/NULL for the params; |
| 4292 | % check w/ Guido. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4293 | \end{cfuncdesc} |
| 4294 | |
| 4295 | % XXX Other PySys thingies (doesn't really belong in this chapter) |
| 4296 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4297 | \section{Thread State and the Global Interpreter Lock |
| 4298 | \label{threads}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4299 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4300 | \index{global interpreter lock} |
| 4301 | \index{interpreter lock} |
| 4302 | \index{lock, interpreter} |
| 4303 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4304 | The Python interpreter is not fully thread safe. In order to support |
| 4305 | multi-threaded Python programs, there's a global lock that must be |
| 4306 | held by the current thread before it can safely access Python objects. |
| 4307 | Without the lock, even the simplest operations could cause problems in |
Fred Drake | 7baf3d4 | 1998-02-20 00:45:52 +0000 | [diff] [blame] | 4308 | a multi-threaded program: for example, when two threads simultaneously |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4309 | increment the reference count of the same object, the reference count |
| 4310 | could end up being incremented only once instead of twice. |
| 4311 | |
| 4312 | Therefore, the rule exists that only the thread that has acquired the |
| 4313 | global interpreter lock may operate on Python objects or call Python/C |
| 4314 | API functions. In order to support multi-threaded Python programs, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4315 | the interpreter regularly releases and reacquires the lock --- by |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4316 | default, every ten bytecode instructions (this can be changed with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4317 | \withsubitem{(in module sys)}{\ttindex{setcheckinterval()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4318 | \function{sys.setcheckinterval()}). The lock is also released and |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4319 | reacquired around potentially blocking I/O operations like reading or |
| 4320 | writing a file, so that other threads can run while the thread that |
| 4321 | requests the I/O is waiting for the I/O operation to complete. |
| 4322 | |
| 4323 | The Python interpreter needs to keep some bookkeeping information |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4324 | separate per thread --- for this it uses a data structure called |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4325 | \ctype{PyThreadState}\ttindex{PyThreadState}. This is new in Python |
| 4326 | 1.5; in earlier versions, such state was stored in global variables, |
| 4327 | and switching threads could cause problems. In particular, exception |
| 4328 | handling is now thread safe, when the application uses |
| 4329 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
| 4330 | \function{sys.exc_info()} to access the exception last raised in the |
| 4331 | current thread. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4332 | |
| 4333 | There's one global variable left, however: the pointer to the current |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4334 | \ctype{PyThreadState}\ttindex{PyThreadState} structure. While most |
| 4335 | thread packages have a way to store ``per-thread global data,'' |
| 4336 | Python's internal platform independent thread abstraction doesn't |
| 4337 | support this yet. Therefore, the current thread state must be |
| 4338 | manipulated explicitly. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4339 | |
| 4340 | This is easy enough in most cases. Most code manipulating the global |
| 4341 | interpreter lock has the following simple structure: |
| 4342 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4343 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4344 | Save the thread state in a local variable. |
| 4345 | Release the interpreter lock. |
| 4346 | ...Do some blocking I/O operation... |
| 4347 | Reacquire the interpreter lock. |
| 4348 | Restore the thread state from the local variable. |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4349 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4350 | |
| 4351 | This is so common that a pair of macros exists to simplify it: |
| 4352 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4353 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4354 | Py_BEGIN_ALLOW_THREADS |
| 4355 | ...Do some blocking I/O operation... |
| 4356 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4357 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4358 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4359 | The \code{Py_BEGIN_ALLOW_THREADS}\ttindex{Py_BEGIN_ALLOW_THREADS} macro |
| 4360 | opens a new block and declares a hidden local variable; the |
| 4361 | \code{Py_END_ALLOW_THREADS}\ttindex{Py_END_ALLOW_THREADS} macro closes |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4362 | the block. Another advantage of using these two macros is that when |
| 4363 | Python is compiled without thread support, they are defined empty, |
| 4364 | thus saving the thread state and lock manipulations. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4365 | |
| 4366 | When thread support is enabled, the block above expands to the |
| 4367 | following code: |
| 4368 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4369 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4370 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4371 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4372 | _save = PyEval_SaveThread(); |
| 4373 | ...Do some blocking I/O operation... |
| 4374 | PyEval_RestoreThread(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4375 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4376 | |
| 4377 | Using even lower level primitives, we can get roughly the same effect |
| 4378 | as follows: |
| 4379 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4380 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4381 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4383 | _save = PyThreadState_Swap(NULL); |
| 4384 | PyEval_ReleaseLock(); |
| 4385 | ...Do some blocking I/O operation... |
| 4386 | PyEval_AcquireLock(); |
| 4387 | PyThreadState_Swap(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4388 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4389 | |
| 4390 | There are some subtle differences; in particular, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4391 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()} saves |
| 4392 | and restores the value of the global variable |
| 4393 | \cdata{errno}\ttindex{errno}, since the lock manipulation does not |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4394 | guarantee that \cdata{errno} is left alone. Also, when thread support |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4395 | is disabled, |
| 4396 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} and |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4397 | \cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4398 | case, \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} and |
| 4399 | \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()} are not |
| 4400 | available. This is done so that dynamically loaded extensions |
| 4401 | compiled with thread support enabled can be loaded by an interpreter |
| 4402 | that was compiled with disabled thread support. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4403 | |
| 4404 | The global interpreter lock is used to protect the pointer to the |
| 4405 | current thread state. When releasing the lock and saving the thread |
| 4406 | state, the current thread state pointer must be retrieved before the |
| 4407 | lock is released (since another thread could immediately acquire the |
| 4408 | lock and store its own thread state in the global variable). |
Fred Drake | ffe58ca | 2000-09-29 17:31:54 +0000 | [diff] [blame] | 4409 | Conversely, when acquiring the lock and restoring the thread state, |
| 4410 | the lock must be acquired before storing the thread state pointer. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4411 | |
| 4412 | Why am I going on with so much detail about this? Because when |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4413 | threads are created from C, they don't have the global interpreter |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4414 | lock, nor is there a thread state data structure for them. Such |
| 4415 | threads must bootstrap themselves into existence, by first creating a |
| 4416 | thread state data structure, then acquiring the lock, and finally |
| 4417 | storing their thread state pointer, before they can start using the |
| 4418 | Python/C API. When they are done, they should reset the thread state |
| 4419 | pointer, release the lock, and finally free their thread state data |
| 4420 | structure. |
| 4421 | |
| 4422 | When creating a thread data structure, you need to provide an |
| 4423 | interpreter state data structure. The interpreter state data |
| 4424 | structure hold global data that is shared by all threads in an |
| 4425 | interpreter, for example the module administration |
| 4426 | (\code{sys.modules}). Depending on your needs, you can either create |
| 4427 | a new interpreter state data structure, or share the interpreter state |
| 4428 | data structure used by the Python main thread (to access the latter, |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4429 | you must obtain the thread state and access its \member{interp} member; |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4430 | this must be done by a thread that is created by Python or by the main |
| 4431 | thread after Python is initialized). |
| 4432 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4433 | |
| 4434 | \begin{ctypedesc}{PyInterpreterState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4435 | This data structure represents the state shared by a number of |
| 4436 | cooperating threads. Threads belonging to the same interpreter |
| 4437 | share their module administration and a few other internal items. |
| 4438 | There are no public members in this structure. |
| 4439 | |
| 4440 | Threads belonging to different interpreters initially share nothing, |
| 4441 | except process state like available memory, open file descriptors and |
| 4442 | such. The global interpreter lock is also shared by all threads, |
| 4443 | regardless of to which interpreter they belong. |
| 4444 | \end{ctypedesc} |
| 4445 | |
| 4446 | \begin{ctypedesc}{PyThreadState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4447 | This data structure represents the state of a single thread. The only |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4448 | public data member is \ctype{PyInterpreterState *}\member{interp}, |
| 4449 | which points to this thread's interpreter state. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4450 | \end{ctypedesc} |
| 4451 | |
| 4452 | \begin{cfuncdesc}{void}{PyEval_InitThreads}{} |
| 4453 | Initialize and acquire the global interpreter lock. It should be |
| 4454 | called in the main thread before creating a second thread or engaging |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4455 | in any other thread operations such as |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4456 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} or |
| 4457 | \code{PyEval_ReleaseThread(\var{tstate})}\ttindex{PyEval_ReleaseThread()}. |
| 4458 | It is not needed before calling |
| 4459 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} or |
| 4460 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4461 | |
| 4462 | This is a no-op when called for a second time. It is safe to call |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4463 | this function before calling |
| 4464 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4465 | |
| 4466 | When only the main thread exists, no lock operations are needed. This |
| 4467 | is a common situation (most Python programs do not use threads), and |
| 4468 | the lock operations slow the interpreter down a bit. Therefore, the |
| 4469 | lock is not created initially. This situation is equivalent to having |
| 4470 | acquired the lock: when there is only a single thread, all object |
| 4471 | accesses are safe. Therefore, when this function initializes the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4472 | lock, it also acquires it. Before the Python |
| 4473 | \module{thread}\refbimodindex{thread} module creates a new thread, |
| 4474 | knowing that either it has the lock or the lock hasn't been created |
| 4475 | yet, it calls \cfunction{PyEval_InitThreads()}. When this call |
| 4476 | returns, it is guaranteed that the lock has been created and that it |
| 4477 | has acquired it. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4478 | |
| 4479 | It is \strong{not} safe to call this function when it is unknown which |
| 4480 | thread (if any) currently has the global interpreter lock. |
| 4481 | |
| 4482 | This function is not available when thread support is disabled at |
| 4483 | compile time. |
| 4484 | \end{cfuncdesc} |
| 4485 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4486 | \begin{cfuncdesc}{void}{PyEval_AcquireLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4487 | Acquire the global interpreter lock. The lock must have been created |
| 4488 | earlier. If this thread already has the lock, a deadlock ensues. |
| 4489 | This function is not available when thread support is disabled at |
| 4490 | compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4491 | \end{cfuncdesc} |
| 4492 | |
| 4493 | \begin{cfuncdesc}{void}{PyEval_ReleaseLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4494 | Release the global interpreter lock. The lock must have been created |
| 4495 | earlier. This function is not available when thread support is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4496 | disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4497 | \end{cfuncdesc} |
| 4498 | |
| 4499 | \begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4500 | Acquire the global interpreter lock and then set the current thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4501 | state to \var{tstate}, which should not be \NULL{}. The lock must |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4502 | have been created earlier. If this thread already has the lock, |
| 4503 | deadlock ensues. This function is not available when thread support |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4504 | is disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4505 | \end{cfuncdesc} |
| 4506 | |
| 4507 | \begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4508 | Reset the current thread state to \NULL{} and release the global |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4509 | interpreter lock. The lock must have been created earlier and must be |
| 4510 | held by the current thread. The \var{tstate} argument, which must not |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4511 | be \NULL{}, is only used to check that it represents the current |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4512 | thread state --- if it isn't, a fatal error is reported. This |
| 4513 | function is not available when thread support is disabled at compile |
| 4514 | time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4515 | \end{cfuncdesc} |
| 4516 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4517 | \begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4518 | Release the interpreter lock (if it has been created and thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4519 | support is enabled) and reset the thread state to \NULL{}, |
| 4520 | returning the previous thread state (which is not \NULL{}). If |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4521 | the lock has been created, the current thread must have acquired it. |
| 4522 | (This function is available even when thread support is disabled at |
| 4523 | compile time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4524 | \end{cfuncdesc} |
| 4525 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4526 | \begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4527 | Acquire the interpreter lock (if it has been created and thread |
| 4528 | support is enabled) and set the thread state to \var{tstate}, which |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4529 | must not be \NULL{}. If the lock has been created, the current |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4530 | thread must not have acquired it, otherwise deadlock ensues. (This |
| 4531 | function is available even when thread support is disabled at compile |
| 4532 | time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4533 | \end{cfuncdesc} |
| 4534 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4535 | The following macros are normally used without a trailing semicolon; |
| 4536 | look for example usage in the Python source distribution. |
| 4537 | |
| 4538 | \begin{csimplemacrodesc}{Py_BEGIN_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4539 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4540 | \samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4541 | Note that it contains an opening brace; it must be matched with a |
| 4542 | following \code{Py_END_ALLOW_THREADS} macro. See above for further |
| 4543 | discussion of this macro. It is a no-op when thread support is |
| 4544 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4545 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4546 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4547 | \begin{csimplemacrodesc}{Py_END_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4548 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4549 | \samp{PyEval_RestoreThread(_save); \}}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4550 | Note that it contains a closing brace; it must be matched with an |
| 4551 | earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further |
| 4552 | discussion of this macro. It is a no-op when thread support is |
| 4553 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4554 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4555 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4556 | \begin{csimplemacrodesc}{Py_BEGIN_BLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4557 | This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4558 | is equivalent to \code{Py_END_ALLOW_THREADS} without the closing |
| 4559 | brace. It is a no-op when thread support is disabled at compile |
| 4560 | time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4561 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4562 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4563 | \begin{csimplemacrodesc}{Py_BEGIN_UNBLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4564 | This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4565 | equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace |
| 4566 | and variable declaration. It is a no-op when thread support is |
| 4567 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4568 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4569 | |
| 4570 | All of the following functions are only available when thread support |
| 4571 | is enabled at compile time, and must be called only when the |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 4572 | interpreter lock has been created. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4573 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4574 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{} |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 4575 | Create a new interpreter state object. The interpreter lock need not |
| 4576 | be held, but may be held if it is necessary to serialize calls to this |
| 4577 | function. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4578 | \end{cfuncdesc} |
| 4579 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4580 | \begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp} |
| 4581 | Reset all information in an interpreter state object. The interpreter |
| 4582 | lock must be held. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4583 | \end{cfuncdesc} |
| 4584 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4585 | \begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp} |
| 4586 | Destroy an interpreter state object. The interpreter lock need not be |
| 4587 | held. The interpreter state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4588 | call to \cfunction{PyInterpreterState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4589 | \end{cfuncdesc} |
| 4590 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4591 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4592 | Create a new thread state object belonging to the given interpreter |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 4593 | object. The interpreter lock need not be held, but may be held if it |
| 4594 | is necessary to serialize calls to this function. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4595 | \end{cfuncdesc} |
| 4596 | |
| 4597 | \begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate} |
| 4598 | Reset all information in a thread state object. The interpreter lock |
| 4599 | must be held. |
| 4600 | \end{cfuncdesc} |
| 4601 | |
| 4602 | \begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate} |
| 4603 | Destroy a thread state object. The interpreter lock need not be |
| 4604 | held. The thread state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4605 | call to \cfunction{PyThreadState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4606 | \end{cfuncdesc} |
| 4607 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4608 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4609 | Return the current thread state. The interpreter lock must be held. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4610 | When the current thread state is \NULL{}, this issues a fatal |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 4611 | error (so that the caller needn't check for \NULL{}). |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4612 | \end{cfuncdesc} |
| 4613 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4614 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4615 | Swap the current thread state with the thread state given by the |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4616 | argument \var{tstate}, which may be \NULL{}. The interpreter lock |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4617 | must be held. |
| 4618 | \end{cfuncdesc} |
| 4619 | |
| 4620 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4621 | \chapter{Memory Management \label{memory}} |
| 4622 | \sectionauthor{Vladimir Marangozov}{Vladimir.Marangozov@inrialpes.fr} |
| 4623 | |
| 4624 | |
| 4625 | \section{Overview \label{memoryOverview}} |
| 4626 | |
| 4627 | Memory management in Python involves a private heap containing all |
| 4628 | Python objects and data structures. The management of this private |
| 4629 | heap is ensured internally by the \emph{Python memory manager}. The |
| 4630 | Python memory manager has different components which deal with various |
| 4631 | dynamic storage management aspects, like sharing, segmentation, |
| 4632 | preallocation or caching. |
| 4633 | |
| 4634 | At the lowest level, a raw memory allocator ensures that there is |
| 4635 | enough room in the private heap for storing all Python-related data |
| 4636 | by interacting with the memory manager of the operating system. On top |
| 4637 | of the raw memory allocator, several object-specific allocators |
| 4638 | operate on the same heap and implement distinct memory management |
| 4639 | policies adapted to the peculiarities of every object type. For |
| 4640 | example, integer objects are managed differently within the heap than |
| 4641 | strings, tuples or dictionaries because integers imply different |
| 4642 | storage requirements and speed/space tradeoffs. The Python memory |
| 4643 | manager thus delegates some of the work to the object-specific |
| 4644 | allocators, but ensures that the latter operate within the bounds of |
| 4645 | the private heap. |
| 4646 | |
| 4647 | It is important to understand that the management of the Python heap |
| 4648 | is performed by the interpreter itself and that the user has no |
| 4649 | control on it, even if she regularly manipulates object pointers to |
| 4650 | memory blocks inside that heap. The allocation of heap space for |
| 4651 | Python objects and other internal buffers is performed on demand by |
| 4652 | the Python memory manager through the Python/C API functions listed in |
| 4653 | this document. |
| 4654 | |
| 4655 | To avoid memory corruption, extension writers should never try to |
| 4656 | operate on Python objects with the functions exported by the C |
| 4657 | library: \cfunction{malloc()}\ttindex{malloc()}, |
| 4658 | \cfunction{calloc()}\ttindex{calloc()}, |
| 4659 | \cfunction{realloc()}\ttindex{realloc()} and |
| 4660 | \cfunction{free()}\ttindex{free()}. This will result in |
| 4661 | mixed calls between the C allocator and the Python memory manager |
| 4662 | with fatal consequences, because they implement different algorithms |
| 4663 | and operate on different heaps. However, one may safely allocate and |
| 4664 | release memory blocks with the C library allocator for individual |
| 4665 | purposes, as shown in the following example: |
| 4666 | |
| 4667 | \begin{verbatim} |
| 4668 | PyObject *res; |
| 4669 | char *buf = (char *) malloc(BUFSIZ); /* for I/O */ |
| 4670 | |
| 4671 | if (buf == NULL) |
| 4672 | return PyErr_NoMemory(); |
| 4673 | ...Do some I/O operation involving buf... |
| 4674 | res = PyString_FromString(buf); |
| 4675 | free(buf); /* malloc'ed */ |
| 4676 | return res; |
| 4677 | \end{verbatim} |
| 4678 | |
| 4679 | In this example, the memory request for the I/O buffer is handled by |
| 4680 | the C library allocator. The Python memory manager is involved only |
| 4681 | in the allocation of the string object returned as a result. |
| 4682 | |
| 4683 | In most situations, however, it is recommended to allocate memory from |
| 4684 | the Python heap specifically because the latter is under control of |
| 4685 | the Python memory manager. For example, this is required when the |
| 4686 | interpreter is extended with new object types written in C. Another |
| 4687 | reason for using the Python heap is the desire to \emph{inform} the |
| 4688 | Python memory manager about the memory needs of the extension module. |
| 4689 | Even when the requested memory is used exclusively for internal, |
| 4690 | highly-specific purposes, delegating all memory requests to the Python |
| 4691 | memory manager causes the interpreter to have a more accurate image of |
| 4692 | its memory footprint as a whole. Consequently, under certain |
| 4693 | circumstances, the Python memory manager may or may not trigger |
| 4694 | appropriate actions, like garbage collection, memory compaction or |
| 4695 | other preventive procedures. Note that by using the C library |
| 4696 | allocator as shown in the previous example, the allocated memory for |
| 4697 | the I/O buffer escapes completely the Python memory manager. |
| 4698 | |
| 4699 | |
| 4700 | \section{Memory Interface \label{memoryInterface}} |
| 4701 | |
| 4702 | The following function sets, modeled after the ANSI C standard, are |
| 4703 | available for allocating and releasing memory from the Python heap: |
| 4704 | |
| 4705 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 4706 | \begin{cfuncdesc}{void*}{PyMem_Malloc}{size_t n} |
| 4707 | Allocates \var{n} bytes and returns a pointer of type \ctype{void*} to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4708 | the allocated memory, or \NULL{} if the request fails. Requesting zero |
| 4709 | bytes returns a non-\NULL{} pointer. |
| 4710 | \end{cfuncdesc} |
| 4711 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 4712 | \begin{cfuncdesc}{void*}{PyMem_Realloc}{void *p, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4713 | Resizes the memory block pointed to by \var{p} to \var{n} bytes. The |
| 4714 | contents will be unchanged to the minimum of the old and the new |
| 4715 | sizes. If \var{p} is \NULL{}, the call is equivalent to |
| 4716 | \cfunction{PyMem_Malloc(\var{n})}; if \var{n} is equal to zero, the memory block |
| 4717 | is resized but is not freed, and the returned pointer is non-\NULL{}. |
| 4718 | Unless \var{p} is \NULL{}, it must have been returned by a previous |
| 4719 | call to \cfunction{PyMem_Malloc()} or \cfunction{PyMem_Realloc()}. |
| 4720 | \end{cfuncdesc} |
| 4721 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 4722 | \begin{cfuncdesc}{void}{PyMem_Free}{void *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4723 | Frees the memory block pointed to by \var{p}, which must have been |
| 4724 | returned by a previous call to \cfunction{PyMem_Malloc()} or |
| 4725 | \cfunction{PyMem_Realloc()}. Otherwise, or if |
| 4726 | \cfunction{PyMem_Free(p)} has been called before, undefined behaviour |
| 4727 | occurs. If \var{p} is \NULL{}, no operation is performed. |
| 4728 | \end{cfuncdesc} |
| 4729 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4730 | The following type-oriented macros are provided for convenience. Note |
| 4731 | that \var{TYPE} refers to any C type. |
| 4732 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4733 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_New}{TYPE, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4734 | Same as \cfunction{PyMem_Malloc()}, but allocates \code{(\var{n} * |
| 4735 | sizeof(\var{TYPE}))} bytes of memory. Returns a pointer cast to |
| 4736 | \ctype{\var{TYPE}*}. |
| 4737 | \end{cfuncdesc} |
| 4738 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4739 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_Resize}{void *p, TYPE, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4740 | Same as \cfunction{PyMem_Realloc()}, but the memory block is resized |
| 4741 | to \code{(\var{n} * sizeof(\var{TYPE}))} bytes. Returns a pointer |
| 4742 | cast to \ctype{\var{TYPE}*}. |
| 4743 | \end{cfuncdesc} |
| 4744 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4745 | \begin{cfuncdesc}{void}{PyMem_Del}{void *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4746 | Same as \cfunction{PyMem_Free()}. |
| 4747 | \end{cfuncdesc} |
| 4748 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4749 | In addition, the following macro sets are provided for calling the |
| 4750 | Python memory allocator directly, without involving the C API functions |
| 4751 | listed above. However, note that their use does not preserve binary |
| 4752 | compatibility accross Python versions and is therefore deprecated in |
| 4753 | extension modules. |
| 4754 | |
| 4755 | \cfunction{PyMem_MALLOC()}, \cfunction{PyMem_REALLOC()}, \cfunction{PyMem_FREE()}. |
| 4756 | |
| 4757 | \cfunction{PyMem_NEW()}, \cfunction{PyMem_RESIZE()}, \cfunction{PyMem_DEL()}. |
| 4758 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4759 | |
| 4760 | \section{Examples \label{memoryExamples}} |
| 4761 | |
| 4762 | Here is the example from section \ref{memoryOverview}, rewritten so |
| 4763 | that the I/O buffer is allocated from the Python heap by using the |
| 4764 | first function set: |
| 4765 | |
| 4766 | \begin{verbatim} |
| 4767 | PyObject *res; |
| 4768 | char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */ |
| 4769 | |
| 4770 | if (buf == NULL) |
| 4771 | return PyErr_NoMemory(); |
| 4772 | /* ...Do some I/O operation involving buf... */ |
| 4773 | res = PyString_FromString(buf); |
| 4774 | PyMem_Free(buf); /* allocated with PyMem_Malloc */ |
| 4775 | return res; |
| 4776 | \end{verbatim} |
| 4777 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4778 | The same code using the type-oriented function set: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4779 | |
| 4780 | \begin{verbatim} |
| 4781 | PyObject *res; |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4782 | char *buf = PyMem_New(char, BUFSIZ); /* for I/O */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4783 | |
| 4784 | if (buf == NULL) |
| 4785 | return PyErr_NoMemory(); |
| 4786 | /* ...Do some I/O operation involving buf... */ |
| 4787 | res = PyString_FromString(buf); |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4788 | PyMem_Del(buf); /* allocated with PyMem_New */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4789 | return res; |
| 4790 | \end{verbatim} |
| 4791 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4792 | Note that in the two examples above, the buffer is always |
| 4793 | manipulated via functions belonging to the same set. Indeed, it |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4794 | is required to use the same memory API family for a given |
| 4795 | memory block, so that the risk of mixing different allocators is |
| 4796 | reduced to a minimum. The following code sequence contains two errors, |
| 4797 | one of which is labeled as \emph{fatal} because it mixes two different |
| 4798 | allocators operating on different heaps. |
| 4799 | |
| 4800 | \begin{verbatim} |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4801 | char *buf1 = PyMem_New(char, BUFSIZ); |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4802 | char *buf2 = (char *) malloc(BUFSIZ); |
| 4803 | char *buf3 = (char *) PyMem_Malloc(BUFSIZ); |
| 4804 | ... |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4805 | PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4806 | free(buf2); /* Right -- allocated via malloc() */ |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4807 | free(buf1); /* Fatal -- should be PyMem_Del() */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4808 | \end{verbatim} |
| 4809 | |
| 4810 | In addition to the functions aimed at handling raw memory blocks from |
| 4811 | the Python heap, objects in Python are allocated and released with |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4812 | \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()} and |
| 4813 | \cfunction{PyObject_Del()}, or with their corresponding macros |
| 4814 | \cfunction{PyObject_NEW()}, \cfunction{PyObject_NEW_VAR()} and |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 4815 | \cfunction{PyObject_DEL()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4816 | |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 4817 | These will be explained in the next chapter on defining and |
| 4818 | implementing new object types in C. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4819 | |
| 4820 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4821 | \chapter{Defining New Object Types \label{newTypes}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4822 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4823 | \begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4824 | \end{cfuncdesc} |
| 4825 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4826 | \begin{cfuncdesc}{PyVarObject*}{_PyObject_NewVar}{PyTypeObject *type, int size} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4827 | \end{cfuncdesc} |
| 4828 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4829 | \begin{cfuncdesc}{void}{_PyObject_Del}{PyObject *op} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4830 | \end{cfuncdesc} |
| 4831 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4832 | \begin{cfuncdesc}{PyObject*}{PyObject_Init}{PyObject *op, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 4833 | PyTypeObject *type} |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4834 | \end{cfuncdesc} |
| 4835 | |
| 4836 | \begin{cfuncdesc}{PyVarObject*}{PyObject_InitVar}{PyVarObject *op, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 4837 | PyTypeObject *type, int size} |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 4838 | \end{cfuncdesc} |
| 4839 | |
| 4840 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_New}{TYPE, PyTypeObject *type} |
| 4841 | \end{cfuncdesc} |
| 4842 | |
| 4843 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NewVar}{TYPE, PyTypeObject *type, |
| 4844 | int size} |
| 4845 | \end{cfuncdesc} |
| 4846 | |
| 4847 | \begin{cfuncdesc}{void}{PyObject_Del}{PyObject *op} |
| 4848 | \end{cfuncdesc} |
| 4849 | |
| 4850 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW}{TYPE, PyTypeObject *type} |
| 4851 | \end{cfuncdesc} |
| 4852 | |
| 4853 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW_VAR}{TYPE, PyTypeObject *type, |
| 4854 | int size} |
| 4855 | \end{cfuncdesc} |
| 4856 | |
| 4857 | \begin{cfuncdesc}{void}{PyObject_DEL}{PyObject *op} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4858 | \end{cfuncdesc} |
| 4859 | |
Fred Drake | ee814bf | 2000-11-28 22:34:32 +0000 | [diff] [blame] | 4860 | \begin{cfuncdesc}{PyObject*}{Py_InitModule}{char *name, |
| 4861 | PyMethodDef *methods} |
| 4862 | Create a new module object based on a name and table of functions, |
| 4863 | returning the new module object. |
| 4864 | \end{cfuncdesc} |
| 4865 | |
| 4866 | \begin{cfuncdesc}{PyObject*}{Py_InitModule3}{char *name, |
| 4867 | PyMethodDef *methods, |
| 4868 | char *doc} |
| 4869 | Create a new module object based on a name and table of functions, |
| 4870 | returning the new module object. If \var{doc} is non-\NULL, it will |
| 4871 | be used to define the docstring for the module. |
| 4872 | \end{cfuncdesc} |
| 4873 | |
| 4874 | \begin{cfuncdesc}{PyObject*}{Py_InitModule4}{char *name, |
| 4875 | PyMethodDef *methods, |
| 4876 | char *doc, PyObject *self, |
| 4877 | int apiver} |
| 4878 | Create a new module object based on a name and table of functions, |
| 4879 | returning the new module object. If \var{doc} is non-\NULL, it will |
| 4880 | be used to define the docstring for the module. If \var{self} is |
| 4881 | non-\NULL, it will passed to the functions of the module as their |
| 4882 | (otherwise \NULL) first parameter. (This was added as an |
| 4883 | experimental feature, and there are no known uses in the current |
| 4884 | version of Python.) For \var{apiver}, the only value which should |
| 4885 | be passed is defined by the constant \constant{PYTHON_API_VERSION}. |
| 4886 | |
| 4887 | \strong{Note:} Most uses of this function should probably be using |
| 4888 | the \cfunction{Py_InitModule3()} instead; only use this if you are |
| 4889 | sure you need it. |
| 4890 | \end{cfuncdesc} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 4891 | |
| 4892 | PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse |
| 4893 | |
| 4894 | Py_BuildValue |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4895 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4896 | DL_IMPORT |
| 4897 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4898 | _Py_NoneStruct |
| 4899 | |
| 4900 | |
| 4901 | \section{Common Object Structures \label{common-structs}} |
| 4902 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4903 | PyObject, PyVarObject |
| 4904 | |
| 4905 | PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD |
| 4906 | |
| 4907 | Typedefs: |
| 4908 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 4909 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4910 | destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 4911 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 4912 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4913 | \begin{ctypedesc}{PyCFunction} |
| 4914 | Type of the functions used to implement most Python callables in C. |
| 4915 | \end{ctypedesc} |
| 4916 | |
| 4917 | \begin{ctypedesc}{PyMethodDef} |
| 4918 | Structure used to describe a method of an extension type. This |
| 4919 | structure has four fields: |
| 4920 | |
| 4921 | \begin{tableiii}{l|l|l}{member}{Field}{C Type}{Meaning} |
| 4922 | \lineiii{ml_name}{char *}{name of the method} |
| 4923 | \lineiii{ml_meth}{PyCFunction}{pointer to the C implementation} |
| 4924 | \lineiii{ml_flags}{int}{flag bits indicating how the call should be |
| 4925 | constructed} |
| 4926 | \lineiii{ml_doc}{char *}{points to the contents of the docstring} |
| 4927 | \end{tableiii} |
| 4928 | \end{ctypedesc} |
| 4929 | |
| 4930 | \begin{cfuncdesc}{PyObject*}{Py_FindMethod}{PyMethodDef[] table, |
| 4931 | PyObject *ob, char *name} |
| 4932 | Return a bound method object for an extension type implemented in C. |
| 4933 | This function also handles the special attribute \member{__methods__}, |
| 4934 | returning a list of all the method names defined in \var{table}. |
| 4935 | \end{cfuncdesc} |
| 4936 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4937 | |
| 4938 | \section{Mapping Object Structures \label{mapping-structs}} |
| 4939 | |
| 4940 | \begin{ctypedesc}{PyMappingMethods} |
| 4941 | Structure used to hold pointers to the functions used to implement the |
| 4942 | mapping protocol for an extension type. |
| 4943 | \end{ctypedesc} |
| 4944 | |
| 4945 | |
| 4946 | \section{Number Object Structures \label{number-structs}} |
| 4947 | |
| 4948 | \begin{ctypedesc}{PyNumberMethods} |
| 4949 | Structure used to hold pointers to the functions an extension type |
| 4950 | uses to implement the number protocol. |
| 4951 | \end{ctypedesc} |
| 4952 | |
| 4953 | |
| 4954 | \section{Sequence Object Structures \label{sequence-structs}} |
| 4955 | |
| 4956 | \begin{ctypedesc}{PySequenceMethods} |
| 4957 | Structure used to hold pointers to the functions which an object uses |
| 4958 | to implement the sequence protocol. |
| 4959 | \end{ctypedesc} |
| 4960 | |
| 4961 | |
| 4962 | \section{Buffer Object Structures \label{buffer-structs}} |
| 4963 | \sectionauthor{Greg J. Stein}{greg@lyra.org} |
| 4964 | |
| 4965 | The buffer interface exports a model where an object can expose its |
| 4966 | internal data as a set of chunks of data, where each chunk is |
| 4967 | specified as a pointer/length pair. These chunks are called |
| 4968 | \dfn{segments} and are presumed to be non-contiguous in memory. |
| 4969 | |
| 4970 | If an object does not export the buffer interface, then its |
| 4971 | \member{tp_as_buffer} member in the \ctype{PyTypeObject} structure |
| 4972 | should be \NULL{}. Otherwise, the \member{tp_as_buffer} will point to |
| 4973 | a \ctype{PyBufferProcs} structure. |
| 4974 | |
| 4975 | \strong{Note:} It is very important that your |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 4976 | \ctype{PyTypeObject} structure uses \constant{Py_TPFLAGS_DEFAULT} for |
| 4977 | the value of the \member{tp_flags} member rather than \code{0}. This |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4978 | tells the Python runtime that your \ctype{PyBufferProcs} structure |
| 4979 | contains the \member{bf_getcharbuffer} slot. Older versions of Python |
| 4980 | did not have this member, so a new Python interpreter using an old |
| 4981 | extension needs to be able to test for its presence before using it. |
| 4982 | |
| 4983 | \begin{ctypedesc}{PyBufferProcs} |
| 4984 | Structure used to hold the function pointers which define an |
| 4985 | implementation of the buffer protocol. |
| 4986 | |
| 4987 | The first slot is \member{bf_getreadbuffer}, of type |
| 4988 | \ctype{getreadbufferproc}. If this slot is \NULL{}, then the object |
| 4989 | does not support reading from the internal data. This is |
| 4990 | non-sensical, so implementors should fill this in, but callers should |
| 4991 | test that the slot contains a non-\NULL{} value. |
| 4992 | |
| 4993 | The next slot is \member{bf_getwritebuffer} having type |
| 4994 | \ctype{getwritebufferproc}. This slot may be \NULL{} if the object |
| 4995 | does not allow writing into its returned buffers. |
| 4996 | |
| 4997 | The third slot is \member{bf_getsegcount}, with type |
| 4998 | \ctype{getsegcountproc}. This slot must not be \NULL{} and is used to |
| 4999 | inform the caller how many segments the object contains. Simple |
| 5000 | objects such as \ctype{PyString_Type} and |
| 5001 | \ctype{PyBuffer_Type} objects contain a single segment. |
| 5002 | |
| 5003 | The last slot is \member{bf_getcharbuffer}, of type |
| 5004 | \ctype{getcharbufferproc}. This slot will only be present if the |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5005 | \constant{Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5006 | \member{tp_flags} field of the object's \ctype{PyTypeObject}. Before using |
| 5007 | this slot, the caller should test whether it is present by using the |
| 5008 | \cfunction{PyType_HasFeature()}\ttindex{PyType_HasFeature()} function. |
| 5009 | If present, it may be \NULL, indicating that the object's contents |
| 5010 | cannot be used as \emph{8-bit characters}. |
| 5011 | The slot function may also raise an error if the object's contents |
| 5012 | cannot be interpreted as 8-bit characters. For example, if the object |
| 5013 | is an array which is configured to hold floating point values, an |
| 5014 | exception may be raised if a caller attempts to use |
| 5015 | \member{bf_getcharbuffer} to fetch a sequence of 8-bit characters. |
| 5016 | This notion of exporting the internal buffers as ``text'' is used to |
| 5017 | distinguish between objects that are binary in nature, and those which |
| 5018 | have character-based content. |
| 5019 | |
| 5020 | \strong{Note:} The current policy seems to state that these characters |
| 5021 | may be multi-byte characters. This implies that a buffer size of |
| 5022 | \var{N} does not mean there are \var{N} characters present. |
| 5023 | \end{ctypedesc} |
| 5024 | |
| 5025 | \begin{datadesc}{Py_TPFLAGS_HAVE_GETCHARBUFFER} |
| 5026 | Flag bit set in the type structure to indicate that the |
| 5027 | \member{bf_getcharbuffer} slot is known. This being set does not |
| 5028 | indicate that the object supports the buffer interface or that the |
| 5029 | \member{bf_getcharbuffer} slot is non-\NULL. |
| 5030 | \end{datadesc} |
| 5031 | |
| 5032 | \begin{ctypedesc}[getreadbufferproc]{int (*getreadbufferproc) |
| 5033 | (PyObject *self, int segment, void **ptrptr)} |
| 5034 | Return a pointer to a readable segment of the buffer. This function |
| 5035 | is allowed to raise an exception, in which case it must return |
| 5036 | \code{-1}. The \var{segment} which is passed must be zero or |
| 5037 | positive, and strictly less than the number of segments returned by |
Greg Stein | 4d4d003 | 2001-04-07 16:14:49 +0000 | [diff] [blame] | 5038 | the \member{bf_getsegcount} slot function. On success, it returns the |
| 5039 | length of the buffer memory, and sets \code{*\var{ptrptr}} to a |
| 5040 | pointer to that memory. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5041 | \end{ctypedesc} |
| 5042 | |
| 5043 | \begin{ctypedesc}[getwritebufferproc]{int (*getwritebufferproc) |
| 5044 | (PyObject *self, int segment, void **ptrptr)} |
Greg Stein | 4d4d003 | 2001-04-07 16:14:49 +0000 | [diff] [blame] | 5045 | Return a pointer to a writable memory buffer in \code{*\var{ptrptr}}, |
| 5046 | and the length of that segment as the function return value. |
| 5047 | The memory buffer must correspond to buffer segment \var{segment}. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 5048 | Must return \code{-1} and set an exception on error. |
| 5049 | \exception{TypeError} should be raised if the object only supports |
| 5050 | read-only buffers, and \exception{SystemError} should be raised when |
| 5051 | \var{segment} specifies a segment that doesn't exist. |
| 5052 | % Why doesn't it raise ValueError for this one? |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5053 | % GJS: because you shouldn't be calling it with an invalid |
| 5054 | % segment. That indicates a blatant programming error in the C |
| 5055 | % code. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 5056 | \end{ctypedesc} |
| 5057 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5058 | \begin{ctypedesc}[getsegcountproc]{int (*getsegcountproc) |
| 5059 | (PyObject *self, int *lenp)} |
| 5060 | Return the number of memory segments which comprise the buffer. If |
| 5061 | \var{lenp} is not \NULL, the implementation must report the sum of the |
| 5062 | sizes (in bytes) of all segments in \code{*\var{lenp}}. |
| 5063 | The function cannot fail. |
| 5064 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5065 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5066 | \begin{ctypedesc}[getcharbufferproc]{int (*getcharbufferproc) |
| 5067 | (PyObject *self, int segment, const char **ptrptr)} |
| 5068 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5069 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5070 | |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5071 | \section{Supporting Cyclic Garbarge Collection |
| 5072 | \label{supporting-cycle-detection}} |
| 5073 | |
| 5074 | Python's support for detecting and collecting garbage which involves |
| 5075 | circular references requires support from object types which are |
| 5076 | ``containers'' for other objects which may also be containers. Types |
| 5077 | which do not store references to other objects, or which only store |
| 5078 | references to atomic types (such as numbers or strings), do not need |
| 5079 | to provide any explicit support for garbage collection. |
| 5080 | |
| 5081 | To create a container type, the \member{tp_flags} field of the type |
| 5082 | object must include the \constant{Py_TPFLAGS_GC} and provide an |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5083 | implementation of the \member{tp_traverse} handler. The computed |
| 5084 | value of the \member{tp_basicsize} field must include |
| 5085 | \constant{PyGC_HEAD_SIZE} as well. If instances of the type are |
| 5086 | mutable, a \member{tp_clear} implementation must also be provided. |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5087 | |
| 5088 | \begin{datadesc}{Py_TPFLAGS_GC} |
| 5089 | Objects with a type with this flag set must conform with the rules |
| 5090 | documented here. For convenience these objects will be referred to |
| 5091 | as container objects. |
| 5092 | \end{datadesc} |
| 5093 | |
| 5094 | \begin{datadesc}{PyGC_HEAD_SIZE} |
| 5095 | Extra memory needed for the garbage collector. Container objects |
| 5096 | must include this in the calculation of their tp_basicsize. If the |
| 5097 | collector is disabled at compile time then this is \code{0}. |
| 5098 | \end{datadesc} |
| 5099 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5100 | Constructors for container types must conform to two rules: |
| 5101 | |
| 5102 | \begin{enumerate} |
| 5103 | \item The memory for the object must be allocated using |
| 5104 | \cfunction{PyObject_New()} or \cfunction{PyObject_VarNew()}. |
| 5105 | |
| 5106 | \item Once all the fields which may contain references to other |
| 5107 | containers are initialized, it must call |
| 5108 | \cfunction{PyObject_GC_Init()}. |
| 5109 | \end{enumerate} |
| 5110 | |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5111 | \begin{cfuncdesc}{void}{PyObject_GC_Init}{PyObject *op} |
| 5112 | Adds the object \var{op} to the set of container objects tracked by |
| 5113 | the collector. The collector can run at unexpected times so objects |
| 5114 | must be valid while being tracked. This should be called once all |
| 5115 | the fields followed by the \member{tp_traverse} handler become valid, |
| 5116 | usually near the end of the constructor. |
| 5117 | \end{cfuncdesc} |
| 5118 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5119 | Similarly, the deallocator for the object must conform to a similar |
| 5120 | pair of rules: |
| 5121 | |
| 5122 | \begin{enumerate} |
| 5123 | \item Before fields which refer to other containers are invalidated, |
| 5124 | \cfunction{PyObject_GC_Fini()} must be called. |
| 5125 | |
| 5126 | \item The object's memory must be deallocated using |
| 5127 | \cfunction{PyObject_Del()}. |
| 5128 | \end{enumerate} |
| 5129 | |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5130 | \begin{cfuncdesc}{void}{PyObject_GC_Fini}{PyObject *op} |
| 5131 | Remove the object \var{op} from the set of container objects tracked |
| 5132 | by the collector. Note that \cfunction{PyObject_GC_Init()} can be |
| 5133 | called again on this object to add it back to the set of tracked |
| 5134 | objects. The deallocator (\member{tp_dealloc} handler) should call |
| 5135 | this for the object before any of the fields used by the |
| 5136 | \member{tp_traverse} handler become invalid. |
Fred Drake | 8f6df46 | 2001-03-23 17:42:09 +0000 | [diff] [blame] | 5137 | |
| 5138 | \strong{Note:} Any container which may be referenced from another |
| 5139 | object reachable by the collector must itself be tracked by the |
| 5140 | collector, so it is generally not safe to call this function |
| 5141 | anywhere but in the object's deallocator. |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5142 | \end{cfuncdesc} |
| 5143 | |
| 5144 | The \member{tp_traverse} handler accepts a function parameter of this |
| 5145 | type: |
| 5146 | |
| 5147 | \begin{ctypedesc}[visitproc]{int (*visitproc)(PyObject *object, void *arg)} |
| 5148 | Type of the visitor function passed to the \member{tp_traverse} |
| 5149 | handler. The function should be called with an object to traverse |
| 5150 | as \var{object} and the third parameter to the \member{tp_traverse} |
| 5151 | handler as \var{arg}. |
| 5152 | \end{ctypedesc} |
| 5153 | |
| 5154 | The \member{tp_traverse} handler must have the following type: |
| 5155 | |
| 5156 | \begin{ctypedesc}[traverseproc]{int (*traverseproc)(PyObject *self, |
| 5157 | visitproc visit, void *arg)} |
| 5158 | Traversal function for a container object. Implementations must |
| 5159 | call the \var{visit} function for each object directly contained by |
| 5160 | \var{self}, with the parameters to \var{visit} being the contained |
| 5161 | object and the \var{arg} value passed to the handler. If |
| 5162 | \var{visit} returns a non-zero value then an error has occurred and |
| 5163 | that value should be returned immediately. |
| 5164 | \end{ctypedesc} |
| 5165 | |
| 5166 | The \member{tp_clear} handler must be of the \ctype{inquiry} type, or |
| 5167 | \NULL{} if the object is immutable. |
| 5168 | |
| 5169 | \begin{ctypedesc}[inquiry]{int (*inquiry)(PyObject *self)} |
| 5170 | Drop references that may have created reference cycles. Immutable |
| 5171 | objects do not have to define this method since they can never |
| 5172 | directly create reference cycles. Note that the object must still |
| 5173 | be valid after calling this method (i.e., don't just call |
| 5174 | \cfunction{Py_DECREF()} on a reference). The collector will call |
| 5175 | this method if it detects that this object is involved in a |
| 5176 | reference cycle. |
| 5177 | \end{ctypedesc} |
| 5178 | |
| 5179 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5180 | \subsection{Example Cycle Collector Support |
| 5181 | \label{example-cycle-support}} |
| 5182 | |
| 5183 | This example shows only enough of the implementation of an extension |
| 5184 | type to show how the garbage collector support needs to be added. It |
| 5185 | shows the definition of the object structure, the |
| 5186 | \member{tp_traverse}, \member{tp_clear} and \member{tp_dealloc} |
| 5187 | implementations, the type structure, and a constructor --- the module |
| 5188 | initialization needed to export the constructor to Python is not shown |
| 5189 | as there are no special considerations there for the collector. To |
| 5190 | make this interesting, assume that the module exposes ways for the |
| 5191 | \member{container} field of the object to be modified. Note that |
| 5192 | since no checks are made on the type of the object used to initialize |
| 5193 | \member{container}, we have to assume that it may be a container. |
| 5194 | |
| 5195 | \begin{verbatim} |
| 5196 | #include "Python.h" |
| 5197 | |
| 5198 | typedef struct { |
| 5199 | PyObject_HEAD |
| 5200 | PyObject *container; |
| 5201 | } MyObject; |
| 5202 | |
| 5203 | static int |
| 5204 | my_traverse(MyObject *self, visitproc visit, void *arg) |
| 5205 | { |
| 5206 | if (self->container != NULL) |
| 5207 | return visit(self->container, arg); |
| 5208 | else |
| 5209 | return 0; |
| 5210 | } |
| 5211 | |
| 5212 | static int |
| 5213 | my_clear(MyObject *self) |
| 5214 | { |
| 5215 | Py_XDECREF(self->container); |
| 5216 | self->container = NULL; |
| 5217 | |
| 5218 | return 0; |
| 5219 | } |
| 5220 | |
| 5221 | static void |
| 5222 | my_dealloc(MyObject *self) |
| 5223 | { |
| 5224 | PyObject_GC_Fini((PyObject *) self); |
| 5225 | Py_XDECREF(self->container); |
| 5226 | PyObject_Del(self); |
| 5227 | } |
| 5228 | \end{verbatim} |
| 5229 | |
| 5230 | \begin{verbatim} |
| 5231 | statichere PyTypeObject |
| 5232 | MyObject_Type = { |
| 5233 | PyObject_HEAD_INIT(NULL) |
| 5234 | 0, |
| 5235 | "MyObject", |
| 5236 | sizeof(MyObject) + PyGC_HEAD_SIZE, |
| 5237 | 0, |
| 5238 | (destructor)my_dealloc, /* tp_dealloc */ |
| 5239 | 0, /* tp_print */ |
| 5240 | 0, /* tp_getattr */ |
| 5241 | 0, /* tp_setattr */ |
| 5242 | 0, /* tp_compare */ |
| 5243 | 0, /* tp_repr */ |
| 5244 | 0, /* tp_as_number */ |
| 5245 | 0, /* tp_as_sequence */ |
| 5246 | 0, /* tp_as_mapping */ |
| 5247 | 0, /* tp_hash */ |
| 5248 | 0, /* tp_call */ |
| 5249 | 0, /* tp_str */ |
| 5250 | 0, /* tp_getattro */ |
| 5251 | 0, /* tp_setattro */ |
| 5252 | 0, /* tp_as_buffer */ |
| 5253 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, |
| 5254 | 0, /* tp_doc */ |
| 5255 | (traverseproc)my_traverse, /* tp_traverse */ |
| 5256 | (inquiry)my_clear, /* tp_clear */ |
| 5257 | 0, /* tp_richcompare */ |
| 5258 | 0, /* tp_weaklistoffset */ |
| 5259 | }; |
| 5260 | |
| 5261 | /* This constructor should be made accessible from Python. */ |
| 5262 | static PyObject * |
| 5263 | new_object(PyObject *unused, PyObject *args) |
| 5264 | { |
| 5265 | PyObject *container = NULL; |
| 5266 | MyObject *result = NULL; |
| 5267 | |
| 5268 | if (PyArg_ParseTuple(args, "|O:new_object", &container)) { |
| 5269 | result = PyObject_New(MyObject, &MyObject_Type); |
| 5270 | if (result != NULL) { |
| 5271 | result->container = container; |
| 5272 | PyObject_GC_Init(); |
| 5273 | } |
| 5274 | } |
| 5275 | return (PyObject *) result; |
| 5276 | } |
| 5277 | \end{verbatim} |
| 5278 | |
| 5279 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5280 | % \chapter{Debugging \label{debugging}} |
| 5281 | % |
| 5282 | % XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 5283 | |
| 5284 | |
Fred Drake | ed773ef | 2000-09-21 21:35:22 +0000 | [diff] [blame] | 5285 | \appendix |
| 5286 | \chapter{Reporting Bugs} |
| 5287 | \input{reportingbugs} |
| 5288 | |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 5289 | \input{api.ind} % Index -- must be last |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 5290 | |
| 5291 | \end{document} |