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 |
Fred Drake | fc43d00 | 2001-05-21 15:03:35 +0000 | [diff] [blame] | 58 | embedding Python is less straightforward than 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). |
Fred Drake | 396ca57 | 2001-09-06 16:30:30 +0000 | [diff] [blame] | 79 | Since Python may define some pre-processor definitions which affect |
| 80 | the standard headers on some systems, you must include \file{Python.h} |
| 81 | before any standard headers are included. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 82 | |
| 83 | All user visible names defined by Python.h (except those defined by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 84 | the included standard headers) have one of the prefixes \samp{Py} or |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 85 | \samp{_Py}. Names beginning with \samp{_Py} are for internal use by |
| 86 | the Python implementation and should not be used by extension writers. |
| 87 | Structure member names do not have a reserved prefix. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 88 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 89 | \strong{Important:} user code should never define names that begin |
| 90 | with \samp{Py} or \samp{_Py}. This confuses the reader, and |
| 91 | jeopardizes the portability of the user code to future Python |
| 92 | versions, which may define additional names beginning with one of |
| 93 | these prefixes. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 94 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 95 | The header files are typically installed with Python. On \UNIX, these |
| 96 | are located in the directories |
| 97 | \file{\envvar{prefix}/include/python\var{version}/} and |
| 98 | \file{\envvar{exec_prefix}/include/python\var{version}/}, where |
| 99 | \envvar{prefix} and \envvar{exec_prefix} are defined by the |
| 100 | corresponding parameters to Python's \program{configure} script and |
| 101 | \var{version} is \code{sys.version[:3]}. On Windows, the headers are |
| 102 | installed in \file{\envvar{prefix}/include}, where \envvar{prefix} is |
| 103 | the installation directory specified to the installer. |
| 104 | |
| 105 | To include the headers, place both directories (if different) on your |
| 106 | compiler's search path for includes. Do \emph{not} place the parent |
| 107 | directories on the search path and then use |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 108 | \samp{\#include <python\shortversion/Python.h>}; this will break on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 109 | multi-platform builds since the platform independent headers under |
| 110 | \envvar{prefix} include the platform specific headers from |
| 111 | \envvar{exec_prefix}. |
| 112 | |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 113 | \Cpp{} users should note that though the API is defined entirely using |
| 114 | C, the header files do properly declare the entry points to be |
| 115 | \code{extern "C"}, so there is no need to do anything special to use |
| 116 | the API from \Cpp. |
| 117 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 118 | |
| 119 | \section{Objects, Types and Reference Counts \label{objects}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 121 | 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] | 122 | return value of type \ctype{PyObject*}. This type is a pointer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 123 | to an opaque data type representing an arbitrary Python |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 124 | object. Since all Python object types are treated the same way by the |
| 125 | Python language in most situations (e.g., assignments, scope rules, |
| 126 | and argument passing), it is only fitting that they should be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 127 | represented by a single C type. Almost all Python objects live on the |
| 128 | heap: you never declare an automatic or static variable of type |
| 129 | \ctype{PyObject}, only pointer variables of type \ctype{PyObject*} can |
| 130 | be declared. The sole exception are the type objects\obindex{type}; |
| 131 | since these must never be deallocated, they are typically static |
| 132 | \ctype{PyTypeObject} objects. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 133 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 134 | All Python objects (even Python integers) have a \dfn{type} and a |
| 135 | \dfn{reference count}. An object's type determines what kind of object |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 136 | 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] | 137 | many more as explained in the \citetitle[../ref/ref.html]{Python |
| 138 | Reference Manual}). For each of the well-known types there is a macro |
| 139 | to check whether an object is of that type; for instance, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 140 | \samp{PyList_Check(\var{a})} is true if (and only if) the object |
| 141 | pointed to by \var{a} is a Python list. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 142 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 143 | |
| 144 | \subsection{Reference Counts \label{refcounts}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 146 | The reference count is important because today's computers have a |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 147 | finite (and often severely limited) memory size; it counts how many |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 148 | 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] | 149 | place could be another object, or a global (or static) C variable, or |
| 150 | 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] | 151 | becomes zero, the object is deallocated. If it contains references to |
| 152 | other objects, their reference count is decremented. Those other |
| 153 | objects may be deallocated in turn, if this decrement makes their |
| 154 | reference count become zero, and so on. (There's an obvious problem |
| 155 | with objects that reference each other here; for now, the solution is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 156 | ``don't do that.'') |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 158 | Reference counts are always manipulated explicitly. The normal way is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 159 | to use the macro \cfunction{Py_INCREF()}\ttindex{Py_INCREF()} to |
| 160 | increment an object's reference count by one, and |
| 161 | \cfunction{Py_DECREF()}\ttindex{Py_DECREF()} to decrement it by |
| 162 | one. The \cfunction{Py_DECREF()} macro is considerably more complex |
| 163 | than the incref one, since it must check whether the reference count |
| 164 | becomes zero and then cause the object's deallocator to be called. |
| 165 | The deallocator is a function pointer contained in the object's type |
| 166 | structure. The type-specific deallocator takes care of decrementing |
| 167 | the reference counts for other objects contained in the object if this |
| 168 | is a compound object type, such as a list, as well as performing any |
| 169 | additional finalization that's needed. There's no chance that the |
| 170 | reference count can overflow; at least as many bits are used to hold |
| 171 | the reference count as there are distinct memory locations in virtual |
| 172 | memory (assuming \code{sizeof(long) >= sizeof(char*)}). Thus, the |
| 173 | reference count increment is a simple operation. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 175 | It is not necessary to increment an object's reference count for every |
| 176 | local variable that contains a pointer to an object. In theory, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 177 | 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] | 178 | point to it and it goes down by one when the variable goes out of |
| 179 | scope. However, these two cancel each other out, so at the end the |
| 180 | reference count hasn't changed. The only real reason to use the |
| 181 | reference count is to prevent the object from being deallocated as |
| 182 | long as our variable is pointing to it. If we know that there is at |
| 183 | least one other reference to the object that lives at least as long as |
| 184 | our variable, there is no need to increment the reference count |
| 185 | temporarily. An important situation where this arises is in objects |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 186 | 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] | 187 | 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] | 188 | reference to every argument for the duration of the call. |
| 189 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 190 | However, a common pitfall is to extract an object from a list and |
| 191 | hold on to it for a while without incrementing its reference count. |
| 192 | Some other operation might conceivably remove the object from the |
| 193 | list, decrementing its reference count and possible deallocating it. |
| 194 | The real danger is that innocent-looking operations may invoke |
| 195 | arbitrary Python code which could do this; there is a code path which |
| 196 | allows control to flow back to the user from a \cfunction{Py_DECREF()}, |
| 197 | so almost any operation is potentially dangerous. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 199 | A safe approach is to always use the generic operations (functions |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 200 | whose name begins with \samp{PyObject_}, \samp{PyNumber_}, |
| 201 | \samp{PySequence_} or \samp{PyMapping_}). These operations always |
| 202 | increment the reference count of the object they return. This leaves |
| 203 | the caller with the responsibility to call |
| 204 | \cfunction{Py_DECREF()} when they are done with the result; this soon |
| 205 | becomes second nature. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 206 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 207 | |
| 208 | \subsubsection{Reference Count Details \label{refcountDetails}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 209 | |
| 210 | 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] | 211 | explained in terms of \emph{ownership of references}. Note that we |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 212 | talk of owning references, never of owning objects; objects are always |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 213 | 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] | 214 | properly --- either by passing ownership on (usually to its caller) or |
| 215 | by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When |
| 216 | a function passes ownership of a reference on to its caller, the |
| 217 | caller is said to receive a \emph{new} reference. When no ownership |
| 218 | is transferred, the caller is said to \emph{borrow} the reference. |
| 219 | Nothing needs to be done for a borrowed reference. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 220 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 221 | Conversely, when a calling function passes it a reference to an |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 222 | object, there are two possibilities: the function \emph{steals} a |
| 223 | reference to the object, or it does not. Few functions steal |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 224 | references; the two notable exceptions are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 225 | \cfunction{PyList_SetItem()}\ttindex{PyList_SetItem()} and |
| 226 | \cfunction{PyTuple_SetItem()}\ttindex{PyTuple_SetItem()}, which |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 227 | 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] | 228 | the item is put!). These functions were designed to steal a reference |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 229 | because of a common idiom for populating a tuple or list with newly |
| 230 | created objects; for example, the code to create the tuple \code{(1, |
| 231 | 2, "three")} could look like this (forgetting about error handling for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 232 | the moment; a better way to code this is shown below): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 233 | |
| 234 | \begin{verbatim} |
| 235 | PyObject *t; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 237 | t = PyTuple_New(3); |
| 238 | PyTuple_SetItem(t, 0, PyInt_FromLong(1L)); |
| 239 | PyTuple_SetItem(t, 1, PyInt_FromLong(2L)); |
| 240 | PyTuple_SetItem(t, 2, PyString_FromString("three")); |
| 241 | \end{verbatim} |
| 242 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 243 | Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to |
| 244 | set tuple items; \cfunction{PySequence_SetItem()} and |
| 245 | \cfunction{PyObject_SetItem()} refuse to do this since tuples are an |
| 246 | immutable data type. You should only use |
| 247 | \cfunction{PyTuple_SetItem()} for tuples that you are creating |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 248 | yourself. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 249 | |
| 250 | Equivalent code for populating a list can be written using |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 251 | \cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code |
| 252 | can also use \cfunction{PySequence_SetItem()}; this illustrates the |
| 253 | difference between the two (the extra \cfunction{Py_DECREF()} calls): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 254 | |
| 255 | \begin{verbatim} |
| 256 | PyObject *l, *x; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 258 | l = PyList_New(3); |
| 259 | x = PyInt_FromLong(1L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 260 | PySequence_SetItem(l, 0, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 261 | x = PyInt_FromLong(2L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 262 | PySequence_SetItem(l, 1, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 263 | x = PyString_FromString("three"); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 264 | PySequence_SetItem(l, 2, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 265 | \end{verbatim} |
| 266 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 267 | You might find it strange that the ``recommended'' approach takes more |
| 268 | code. However, in practice, you will rarely use these ways of |
| 269 | creating and populating a tuple or list. There's a generic function, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 270 | \cfunction{Py_BuildValue()}, that can create most common objects from |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 271 | C values, directed by a \dfn{format string}. For example, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 272 | above two blocks of code could be replaced by the following (which |
| 273 | also takes care of the error checking): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 274 | |
| 275 | \begin{verbatim} |
| 276 | PyObject *t, *l; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 278 | t = Py_BuildValue("(iis)", 1, 2, "three"); |
| 279 | l = Py_BuildValue("[iis]", 1, 2, "three"); |
| 280 | \end{verbatim} |
| 281 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 282 | It is much more common to use \cfunction{PyObject_SetItem()} and |
| 283 | friends with items whose references you are only borrowing, like |
| 284 | arguments that were passed in to the function you are writing. In |
| 285 | that case, their behaviour regarding reference counts is much saner, |
| 286 | since you don't have to increment a reference count so you can give a |
| 287 | reference away (``have it be stolen''). For example, this function |
| 288 | sets all items of a list (actually, any mutable sequence) to a given |
| 289 | item: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 290 | |
| 291 | \begin{verbatim} |
| 292 | int set_all(PyObject *target, PyObject *item) |
| 293 | { |
| 294 | int i, n; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 296 | n = PyObject_Length(target); |
| 297 | if (n < 0) |
| 298 | return -1; |
| 299 | for (i = 0; i < n; i++) { |
| 300 | if (PyObject_SetItem(target, i, item) < 0) |
| 301 | return -1; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 306 | \ttindex{set_all()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 307 | |
| 308 | The situation is slightly different for function return values. |
| 309 | While passing a reference to most functions does not change your |
| 310 | ownership responsibilities for that reference, many functions that |
| 311 | return a referece to an object give you ownership of the reference. |
| 312 | The reason is simple: in many cases, the returned object is created |
| 313 | 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] | 314 | object. Therefore, the generic functions that return object |
| 315 | references, like \cfunction{PyObject_GetItem()} and |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 316 | \cfunction{PySequence_GetItem()}, always return a new reference (the |
| 317 | caller becomes the owner of the reference). |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 318 | |
| 319 | It is important to realize that whether you own a reference returned |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 320 | by a function depends on which function you call only --- \emph{the |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 321 | plumage} (the type of the type of the object passed as an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 322 | argument to the function) \emph{doesn't enter into it!} Thus, if you |
| 323 | extract an item from a list using \cfunction{PyList_GetItem()}, you |
| 324 | don't own the reference --- but if you obtain the same item from the |
| 325 | same list using \cfunction{PySequence_GetItem()} (which happens to |
| 326 | take exactly the same arguments), you do own a reference to the |
| 327 | returned object. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 328 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 329 | 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] | 330 | sum of the items in a list of integers; once using |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 331 | \cfunction{PyList_GetItem()}\ttindex{PyList_GetItem()}, and once using |
| 332 | \cfunction{PySequence_GetItem()}\ttindex{PySequence_GetItem()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 333 | |
| 334 | \begin{verbatim} |
| 335 | long sum_list(PyObject *list) |
| 336 | { |
| 337 | int i, n; |
| 338 | long total = 0; |
| 339 | PyObject *item; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 341 | n = PyList_Size(list); |
| 342 | if (n < 0) |
| 343 | return -1; /* Not a list */ |
| 344 | for (i = 0; i < n; i++) { |
| 345 | item = PyList_GetItem(list, i); /* Can't fail */ |
| 346 | if (!PyInt_Check(item)) continue; /* Skip non-integers */ |
| 347 | total += PyInt_AsLong(item); |
| 348 | } |
| 349 | return total; |
| 350 | } |
| 351 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 352 | \ttindex{sum_list()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 353 | |
| 354 | \begin{verbatim} |
| 355 | long sum_sequence(PyObject *sequence) |
| 356 | { |
| 357 | int i, n; |
| 358 | long total = 0; |
| 359 | PyObject *item; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 360 | n = PySequence_Length(sequence); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 361 | if (n < 0) |
| 362 | return -1; /* Has no length */ |
| 363 | for (i = 0; i < n; i++) { |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 364 | item = PySequence_GetItem(sequence, i); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 365 | if (item == NULL) |
| 366 | return -1; /* Not a sequence, or other failure */ |
| 367 | if (PyInt_Check(item)) |
| 368 | total += PyInt_AsLong(item); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 369 | Py_DECREF(item); /* Discard reference ownership */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 370 | } |
| 371 | return total; |
| 372 | } |
| 373 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 374 | \ttindex{sum_sequence()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 375 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 376 | |
| 377 | \subsection{Types \label{types}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 378 | |
| 379 | There are few other data types that play a significant role in |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 380 | the Python/C API; most are simple C types such as \ctype{int}, |
| 381 | \ctype{long}, \ctype{double} and \ctype{char*}. A few structure types |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 382 | are used to describe static tables used to list the functions exported |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 383 | by a module or the data attributes of a new object type, and another |
| 384 | 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] | 385 | be discussed together with the functions that use them. |
| 386 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 387 | |
| 388 | \section{Exceptions \label{exceptions}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 390 | The Python programmer only needs to deal with exceptions if specific |
| 391 | error handling is required; unhandled exceptions are automatically |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 392 | 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] | 393 | 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] | 394 | user accompanied by a stack traceback. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 395 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 396 | For C programmers, however, error checking always has to be explicit. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 397 | All functions in the Python/C API can raise exceptions, unless an |
| 398 | explicit claim is made otherwise in a function's documentation. In |
| 399 | general, when a function encounters an error, it sets an exception, |
| 400 | discards any object references that it owns, and returns an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 401 | error indicator --- usually \NULL{} or \code{-1}. A few functions |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 402 | return a Boolean true/false result, with false indicating an error. |
| 403 | Very few functions return no explicit error indicator or have an |
| 404 | ambiguous return value, and require explicit testing for errors with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 405 | \cfunction{PyErr_Occurred()}\ttindex{PyErr_Occurred()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 406 | |
| 407 | Exception state is maintained in per-thread storage (this is |
| 408 | equivalent to using global storage in an unthreaded application). A |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 409 | 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] | 410 | The function \cfunction{PyErr_Occurred()} can be used to check for |
| 411 | this: it returns a borrowed reference to the exception type object |
| 412 | when an exception has occurred, and \NULL{} otherwise. There are a |
| 413 | number of functions to set the exception state: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 414 | \cfunction{PyErr_SetString()}\ttindex{PyErr_SetString()} is the most |
| 415 | common (though not the most general) function to set the exception |
| 416 | state, and \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} clears the |
| 417 | exception state. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 418 | |
| 419 | The full exception state consists of three objects (all of which can |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 420 | be \NULL{}): the exception type, the corresponding exception |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 421 | value, and the traceback. These have the same meanings as the Python |
| 422 | \withsubitem{(in module sys)}{ |
| 423 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
| 424 | objects \code{sys.exc_type}, \code{sys.exc_value}, and |
| 425 | \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] | 426 | objects represent the last exception being handled by a Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 427 | \keyword{try} \ldots\ \keyword{except} statement, while the C level |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 428 | exception state only exists while an exception is being passed on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 429 | between C functions until it reaches the Python bytecode interpreter's |
| 430 | main loop, which takes care of transferring it to \code{sys.exc_type} |
| 431 | and friends. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 432 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 433 | 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] | 434 | access the exception state from Python code is to call the function |
| 435 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 436 | \function{sys.exc_info()}, which returns the per-thread exception state |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 437 | for Python code. Also, the semantics of both ways to access the |
| 438 | exception state have changed so that a function which catches an |
| 439 | exception will save and restore its thread's exception state so as to |
| 440 | preserve the exception state of its caller. This prevents common bugs |
| 441 | in exception handling code caused by an innocent-looking function |
| 442 | overwriting the exception being handled; it also reduces the often |
| 443 | unwanted lifetime extension for objects that are referenced by the |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 444 | stack frames in the traceback. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 445 | |
| 446 | As a general principle, a function that calls another function to |
| 447 | perform some task should check whether the called function raised an |
| 448 | 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] | 449 | should discard any object references that it owns, and return an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 450 | error indicator, but it should \emph{not} set another exception --- |
| 451 | that would overwrite the exception that was just raised, and lose |
| 452 | important information about the exact cause of the error. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 453 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 454 | A simple example of detecting exceptions and passing them on is shown |
| 455 | in the \cfunction{sum_sequence()}\ttindex{sum_sequence()} example |
| 456 | above. It so happens that that example doesn't need to clean up any |
| 457 | owned references when it detects an error. The following example |
| 458 | function shows some error cleanup. First, to remind you why you like |
| 459 | Python, we show the equivalent Python code: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 460 | |
| 461 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 462 | def incr_item(dict, key): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 463 | try: |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 464 | item = dict[key] |
| 465 | except KeyError: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 466 | item = 0 |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 467 | dict[key] = item + 1 |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 468 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 469 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 470 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 471 | Here is the corresponding C code, in all its glory: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 473 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 474 | int incr_item(PyObject *dict, PyObject *key) |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 475 | { |
| 476 | /* Objects all initialized to NULL for Py_XDECREF */ |
| 477 | PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL; |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 478 | int rv = -1; /* Return value initialized to -1 (failure) */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 480 | item = PyObject_GetItem(dict, key); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 481 | if (item == NULL) { |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 482 | /* Handle KeyError only: */ |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 483 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 484 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 485 | |
| 486 | /* Clear the error and use zero: */ |
| 487 | PyErr_Clear(); |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 488 | item = PyInt_FromLong(0L); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 489 | if (item == NULL) |
| 490 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 491 | } |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 492 | const_one = PyInt_FromLong(1L); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 493 | if (const_one == NULL) |
| 494 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 495 | |
| 496 | incremented_item = PyNumber_Add(item, const_one); |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 497 | if (incremented_item == NULL) |
| 498 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 499 | |
Fred Drake | 6b3f3f2 | 2000-11-29 15:48:22 +0000 | [diff] [blame] | 500 | if (PyObject_SetItem(dict, key, incremented_item) < 0) |
| 501 | goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 502 | rv = 0; /* Success */ |
| 503 | /* Continue with cleanup code */ |
| 504 | |
| 505 | error: |
| 506 | /* Cleanup code, shared by success and failure path */ |
| 507 | |
| 508 | /* Use Py_XDECREF() to ignore NULL references */ |
| 509 | Py_XDECREF(item); |
| 510 | Py_XDECREF(const_one); |
| 511 | Py_XDECREF(incremented_item); |
| 512 | |
| 513 | return rv; /* -1 for error, 0 for success */ |
| 514 | } |
| 515 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 516 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 517 | |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 518 | This example represents an endorsed use of the \keyword{goto} statement |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 519 | in C! It illustrates the use of |
| 520 | \cfunction{PyErr_ExceptionMatches()}\ttindex{PyErr_ExceptionMatches()} and |
| 521 | \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} to |
| 522 | handle specific exceptions, and the use of |
| 523 | \cfunction{Py_XDECREF()}\ttindex{Py_XDECREF()} to |
| 524 | dispose of owned references that may be \NULL{} (note the |
| 525 | \character{X} in the name; \cfunction{Py_DECREF()} would crash when |
| 526 | confronted with a \NULL{} reference). It is important that the |
| 527 | variables used to hold owned references are initialized to \NULL{} for |
| 528 | this to work; likewise, the proposed return value is initialized to |
| 529 | \code{-1} (failure) and only set to success after the final call made |
| 530 | is successful. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 532 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 533 | \section{Embedding Python \label{embedding}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 534 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 535 | The one important task that only embedders (as opposed to extension |
| 536 | writers) of the Python interpreter have to worry about is the |
| 537 | initialization, and possibly the finalization, of the Python |
| 538 | interpreter. Most functionality of the interpreter can only be used |
| 539 | after the interpreter has been initialized. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 540 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 541 | The basic initialization function is |
| 542 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 543 | This initializes the table of loaded modules, and creates the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 544 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
Fred Drake | 680cabb | 2001-08-14 15:32:16 +0000 | [diff] [blame] | 545 | \module{__main__}\refbimodindex{__main__}, \module{sys}\refbimodindex{sys}, |
| 546 | and \module{exceptions}.\refbimodindex{exceptions} It also initializes |
| 547 | the module search path (\code{sys.path}).% |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 548 | \indexiii{module}{search}{path} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 549 | \withsubitem{(in module sys)}{\ttindex{path}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 550 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 551 | \cfunction{Py_Initialize()} does not set the ``script argument list'' |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 552 | (\code{sys.argv}). If this variable is needed by Python code that |
| 553 | 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] | 554 | \code{PySys_SetArgv(\var{argc}, |
| 555 | \var{argv})}\ttindex{PySys_SetArgv()} subsequent to the call to |
| 556 | \cfunction{Py_Initialize()}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 557 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 558 | On most systems (in particular, on \UNIX{} and Windows, although the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 559 | details are slightly different), |
| 560 | \cfunction{Py_Initialize()} calculates the module search path based |
| 561 | upon its best guess for the location of the standard Python |
| 562 | interpreter executable, assuming that the Python library is found in a |
| 563 | fixed location relative to the Python interpreter executable. In |
| 564 | particular, it looks for a directory named |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 565 | \file{lib/python\shortversion} relative to the parent directory where |
| 566 | the executable named \file{python} is found on the shell command |
| 567 | search path (the environment variable \envvar{PATH}). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 568 | |
| 569 | For instance, if the Python executable is found in |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 570 | \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] | 571 | \file{/usr/local/lib/python\shortversion}. (In fact, this particular path |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 572 | is also the ``fallback'' location, used when no executable file named |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 573 | \file{python} is found along \envvar{PATH}.) The user can override |
| 574 | this behavior by setting the environment variable \envvar{PYTHONHOME}, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 575 | or insert additional directories in front of the standard path by |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 576 | setting \envvar{PYTHONPATH}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 578 | The embedding application can steer the search by calling |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 579 | \code{Py_SetProgramName(\var{file})}\ttindex{Py_SetProgramName()} \emph{before} calling |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 580 | \cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still |
| 581 | overrides this and \envvar{PYTHONPATH} is still inserted in front of |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 582 | the standard path. An application that requires total control has to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 583 | provide its own implementation of |
| 584 | \cfunction{Py_GetPath()}\ttindex{Py_GetPath()}, |
| 585 | \cfunction{Py_GetPrefix()}\ttindex{Py_GetPrefix()}, |
| 586 | \cfunction{Py_GetExecPrefix()}\ttindex{Py_GetExecPrefix()}, and |
| 587 | \cfunction{Py_GetProgramFullPath()}\ttindex{Py_GetProgramFullPath()} (all |
| 588 | defined in \file{Modules/getpath.c}). |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 590 | Sometimes, it is desirable to ``uninitialize'' Python. For instance, |
| 591 | the application may want to start over (make another call to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 592 | \cfunction{Py_Initialize()}) or the application is simply done with its |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 593 | 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] | 594 | can be accomplished by calling \cfunction{Py_Finalize()}. The function |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 595 | \cfunction{Py_IsInitialized()}\ttindex{Py_IsInitialized()} returns |
| 596 | true if Python is currently in the initialized state. More |
| 597 | information about these functions is given in a later chapter. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 599 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 600 | \chapter{The Very High Level Layer \label{veryhigh}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 601 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 602 | The functions in this chapter will let you execute Python source code |
| 603 | given in a file or a buffer, but they will not let you interact in a |
| 604 | more detailed way with the interpreter. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 605 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 606 | Several of these functions accept a start symbol from the grammar as a |
| 607 | parameter. The available start symbols are \constant{Py_eval_input}, |
| 608 | \constant{Py_file_input}, and \constant{Py_single_input}. These are |
| 609 | described following the functions which accept them as parameters. |
| 610 | |
Fred Drake | 510d08b | 2000-08-14 02:50:21 +0000 | [diff] [blame] | 611 | Note also that several of these functions take \ctype{FILE*} |
| 612 | parameters. On particular issue which needs to be handled carefully |
| 613 | is that the \ctype{FILE} structure for different C libraries can be |
| 614 | different and incompatible. Under Windows (at least), it is possible |
| 615 | for dynamically linked extensions to actually use different libraries, |
| 616 | so care should be taken that \ctype{FILE*} parameters are only passed |
| 617 | to these functions if it is certain that they were created by the same |
| 618 | library that the Python runtime is using. |
| 619 | |
Fred Drake | 24e6219 | 2001-05-21 15:56:55 +0000 | [diff] [blame] | 620 | \begin{cfuncdesc}{int}{Py_Main}{int argc, char **argv} |
| 621 | The main program for the standard interpreter. This is made |
| 622 | available for programs which embed Python. The \var{argc} and |
| 623 | \var{argv} parameters should be prepared exactly as those which are |
| 624 | passed to a C program's \cfunction{main()} function. It is |
| 625 | important to note that the argument list may be modified (but the |
| 626 | contents of the strings pointed to by the argument list are not). |
| 627 | The return value will be the integer passed to the |
| 628 | \function{sys.exit()} function, \code{1} if the interpreter exits |
| 629 | due to an exception, or \code{2} if the parameter list does not |
| 630 | represent a valid Python command line. |
| 631 | \end{cfuncdesc} |
| 632 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 633 | \begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 634 | If \var{fp} refers to a file associated with an interactive device |
| 635 | (console or terminal input or \UNIX{} pseudo-terminal), return the |
| 636 | value of \cfunction{PyRun_InteractiveLoop()}, otherwise return the |
| 637 | result of \cfunction{PyRun_SimpleFile()}. If \var{filename} is |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 638 | \NULL{}, this function uses \code{"???"} as the filename. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 639 | \end{cfuncdesc} |
| 640 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 641 | \begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 642 | Executes the Python source code from \var{command} in the |
| 643 | \module{__main__} module. If \module{__main__} does not already |
| 644 | exist, it is created. Returns \code{0} on success or \code{-1} if |
| 645 | an exception was raised. If there was an error, there is no way to |
| 646 | get the exception information. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 647 | \end{cfuncdesc} |
| 648 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 649 | \begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 650 | Similar to \cfunction{PyRun_SimpleString()}, but the Python source |
| 651 | code is read from \var{fp} instead of an in-memory string. |
| 652 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 653 | \end{cfuncdesc} |
| 654 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 655 | \begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename} |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 656 | Read and execute a single statement from a file associated with an |
| 657 | interactive device. If \var{filename} is \NULL, \code{"???"} is |
| 658 | used instead. The user will be prompted using \code{sys.ps1} and |
| 659 | \code{sys.ps2}. Returns \code{0} when the input was executed |
| 660 | successfully, \code{-1} if there was an exception, or an error code |
| 661 | from the \file{errcode.h} include file distributed as part of Python |
| 662 | in case of a parse error. (Note that \file{errcode.h} is not |
| 663 | included by \file{Python.h}, so must be included specifically if |
| 664 | needed.) |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 665 | \end{cfuncdesc} |
| 666 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 667 | \begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename} |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 668 | Read and execute statements from a file associated with an |
| 669 | interactive device until \EOF{} is reached. If \var{filename} is |
| 670 | \NULL, \code{"???"} is used instead. The user will be prompted |
| 671 | using \code{sys.ps1} and \code{sys.ps2}. Returns \code{0} at \EOF. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 672 | \end{cfuncdesc} |
| 673 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 674 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str, |
| 675 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 676 | Parse Python source code from \var{str} using the start token |
| 677 | \var{start}. The result can be used to create a code object which |
| 678 | can be evaluated efficiently. This is useful if a code fragment |
| 679 | must be evaluated many times. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 680 | \end{cfuncdesc} |
| 681 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 682 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp, |
| 683 | char *filename, int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 684 | Similar to \cfunction{PyParser_SimpleParseString()}, but the Python |
| 685 | source code is read from \var{fp} instead of an in-memory string. |
| 686 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 687 | \end{cfuncdesc} |
| 688 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 689 | \begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start, |
| 690 | PyObject *globals, |
| 691 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 692 | Execute Python source code from \var{str} in the context specified |
| 693 | by the dictionaries \var{globals} and \var{locals}. The parameter |
| 694 | \var{start} specifies the start token that should be used to parse |
| 695 | the source code. |
| 696 | |
| 697 | Returns the result of executing the code as a Python object, or |
| 698 | \NULL{} if an exception was raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 699 | \end{cfuncdesc} |
| 700 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 701 | \begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename, |
| 702 | int start, PyObject *globals, |
| 703 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 704 | Similar to \cfunction{PyRun_String()}, but the Python source code is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 705 | read from \var{fp} instead of an in-memory string. |
| 706 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 707 | \end{cfuncdesc} |
| 708 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 709 | \begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename, |
| 710 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 711 | Parse and compile the Python source code in \var{str}, returning the |
| 712 | resulting code object. The start token is given by \var{start}; |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 713 | this can be used to constrain the code which can be compiled and should |
| 714 | be \constant{Py_eval_input}, \constant{Py_file_input}, or |
| 715 | \constant{Py_single_input}. The filename specified by |
| 716 | \var{filename} is used to construct the code object and may appear |
| 717 | in tracebacks or \exception{SyntaxError} exception messages. This |
| 718 | returns \NULL{} if the code cannot be parsed or compiled. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 719 | \end{cfuncdesc} |
| 720 | |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 721 | \begin{cvardesc}{int}{Py_eval_input} |
| 722 | The start symbol from the Python grammar for isolated expressions; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 723 | for use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 724 | \end{cvardesc} |
| 725 | |
| 726 | \begin{cvardesc}{int}{Py_file_input} |
| 727 | The start symbol from the Python grammar for sequences of statements |
| 728 | as read from a file or other source; for use with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 729 | \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. This is |
| 730 | the symbol to use when compiling arbitrarily long Python source code. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 731 | \end{cvardesc} |
| 732 | |
| 733 | \begin{cvardesc}{int}{Py_single_input} |
| 734 | The start symbol from the Python grammar for a single statement; for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 735 | use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
| 736 | This is the symbol used for the interactive interpreter loop. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 737 | \end{cvardesc} |
| 738 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 739 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 740 | \chapter{Reference Counting \label{countingRefs}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 742 | The macros in this section are used for managing reference counts |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 743 | of Python objects. |
| 744 | |
| 745 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 746 | Increment the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 747 | 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] | 748 | \cfunction{Py_XINCREF()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 749 | \end{cfuncdesc} |
| 750 | |
| 751 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 752 | 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] | 753 | \NULL{}, in which case the macro has no effect. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 754 | \end{cfuncdesc} |
| 755 | |
| 756 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 757 | Decrement the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 758 | 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] | 759 | \cfunction{Py_XDECREF()}. If the reference count reaches zero, the |
| 760 | object's type's deallocation function (which must not be \NULL{}) is |
| 761 | invoked. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 762 | |
| 763 | \strong{Warning:} The deallocation function can cause arbitrary Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 764 | code to be invoked (e.g. when a class instance with a |
| 765 | \method{__del__()} method is deallocated). While exceptions in such |
| 766 | code are not propagated, the executed code has free access to all |
| 767 | Python global variables. This means that any object that is reachable |
| 768 | from a global variable should be in a consistent state before |
| 769 | \cfunction{Py_DECREF()} is invoked. For example, code to delete an |
| 770 | object from a list should copy a reference to the deleted object in a |
| 771 | temporary variable, update the list data structure, and then call |
| 772 | \cfunction{Py_DECREF()} for the temporary variable. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 773 | \end{cfuncdesc} |
| 774 | |
| 775 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 776 | Decrement the reference count for object \var{o}. The object may be |
| 777 | \NULL{}, in which case the macro has no effect; otherwise the effect |
| 778 | 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] | 779 | applies. |
| 780 | \end{cfuncdesc} |
| 781 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 782 | The following functions or macros are only for use within the |
| 783 | interpreter core: \cfunction{_Py_Dealloc()}, |
| 784 | \cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as |
| 785 | well as the global variable \cdata{_Py_RefTotal}. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 787 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 788 | \chapter{Exception Handling \label{exceptionHandling}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 789 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 790 | 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] | 791 | exceptions. It is important to understand some of the basics of |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 792 | Python exception handling. It works somewhat like the |
| 793 | \UNIX{} \cdata{errno} variable: there is a global indicator (per |
| 794 | thread) of the last error that occurred. Most functions don't clear |
| 795 | this on success, but will set it to indicate the cause of the error on |
| 796 | failure. Most functions also return an error indicator, usually |
| 797 | \NULL{} if they are supposed to return a pointer, or \code{-1} if they |
| 798 | return an integer (exception: the \cfunction{PyArg_Parse*()} functions |
| 799 | return \code{1} for success and \code{0} for failure). When a |
| 800 | function must fail because some function it called failed, it |
| 801 | generally doesn't set the error indicator; the function it called |
| 802 | already set it. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 803 | |
| 804 | The error indicator consists of three Python objects corresponding to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 805 | \withsubitem{(in module sys)}{ |
| 806 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 807 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and |
| 808 | \code{sys.exc_traceback}. API functions exist to interact with the |
| 809 | error indicator in various ways. There is a separate error indicator |
| 810 | for each thread. |
| 811 | |
| 812 | % XXX Order of these should be more thoughtful. |
| 813 | % Either alphabetical or some kind of structure. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 814 | |
| 815 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 816 | Print a standard traceback to \code{sys.stderr} and clear the error |
| 817 | indicator. Call this function only when the error indicator is set. |
| 818 | (Otherwise it will cause a fatal error!) |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 819 | \end{cfuncdesc} |
| 820 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 821 | \begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 822 | Test whether the error indicator is set. If set, return the exception |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 823 | \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] | 824 | \cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 825 | not set, return \NULL{}. You do not own a reference to the return |
| 826 | value, so you do not need to \cfunction{Py_DECREF()} it. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 827 | \strong{Note:} Do not compare the return value to a specific |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 828 | exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 829 | below. (The comparison could easily fail since the exception may be |
| 830 | an instance instead of a class, in the case of a class exception, or |
| 831 | it may the a subclass of the expected exception.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 832 | \end{cfuncdesc} |
| 833 | |
| 834 | \begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 835 | Equivalent to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 836 | \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 837 | This should only be called when an exception is actually set; a memory |
| 838 | access violation will occur if no exception has been raised. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 839 | \end{cfuncdesc} |
| 840 | |
| 841 | \begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 842 | Return true if the \var{given} exception matches the exception in |
| 843 | \var{exc}. If \var{exc} is a class object, this also returns true |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 844 | 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] | 845 | exceptions in the tuple (and recursively in subtuples) are searched |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 846 | for a match. If \var{given} is \NULL, a memory access violation will |
| 847 | occur. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 848 | \end{cfuncdesc} |
| 849 | |
| 850 | \begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 851 | Under certain circumstances, the values returned by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 852 | \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that |
| 853 | \code{*\var{exc}} is a class object but \code{*\var{val}} is not an |
| 854 | instance of the same class. This function can be used to instantiate |
| 855 | the class in that case. If the values are already normalized, nothing |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 856 | happens. The delayed normalization is implemented to improve |
| 857 | performance. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 858 | \end{cfuncdesc} |
| 859 | |
| 860 | \begin{cfuncdesc}{void}{PyErr_Clear}{} |
| 861 | Clear the error indicator. If the error indicator is not set, there |
| 862 | is no effect. |
| 863 | \end{cfuncdesc} |
| 864 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 865 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, |
| 866 | PyObject **ptraceback} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 867 | Retrieve the error indicator into three variables whose addresses are |
| 868 | passed. If the error indicator is not set, set all three variables to |
| 869 | \NULL{}. If it is set, it will be cleared and you own a reference to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 870 | each object retrieved. The value and traceback object may be |
| 871 | \NULL{} even when the type object is not. \strong{Note:} This |
| 872 | function is normally only used by code that needs to handle exceptions |
| 873 | or by code that needs to save and restore the error indicator |
| 874 | temporarily. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 875 | \end{cfuncdesc} |
| 876 | |
Fred Drake | 17e6343 | 2000-08-31 05:50:40 +0000 | [diff] [blame] | 877 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, |
| 878 | PyObject *traceback} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 879 | Set the error indicator from the three objects. If the error |
| 880 | indicator is already set, it is cleared first. If the objects are |
| 881 | \NULL{}, the error indicator is cleared. Do not pass a \NULL{} type |
| 882 | and non-\NULL{} value or traceback. The exception type should be a |
| 883 | string or class; if it is a class, the value should be an instance of |
| 884 | that class. Do not pass an invalid exception type or value. |
| 885 | (Violating these rules will cause subtle problems later.) This call |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 886 | takes away a reference to each object: you must own a reference |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 887 | to each object before the call and after the call you no longer own |
| 888 | these references. (If you don't understand this, don't use this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 889 | function. I warned you.) \strong{Note:} This function is normally |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 890 | only used by code that needs to save and restore the error indicator |
| 891 | temporarily. |
| 892 | \end{cfuncdesc} |
| 893 | |
| 894 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message} |
| 895 | This is the most common way to set the error indicator. The first |
| 896 | argument specifies the exception type; it is normally one of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 897 | standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 898 | increment its reference count. The second argument is an error |
| 899 | message; it is converted to a string object. |
| 900 | \end{cfuncdesc} |
| 901 | |
| 902 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 903 | This function is similar to \cfunction{PyErr_SetString()} but lets you |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 904 | specify an arbitrary Python object for the ``value'' of the exception. |
| 905 | You need not increment its reference count. |
| 906 | \end{cfuncdesc} |
| 907 | |
Fred Drake | 7357770 | 2000-04-10 18:50:14 +0000 | [diff] [blame] | 908 | \begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception, |
Moshe Zadka | 57a5932 | 2000-09-01 09:47:20 +0000 | [diff] [blame] | 909 | const char *format, \moreargs} |
Fred Drake | 89fb035 | 2000-10-14 05:49:30 +0000 | [diff] [blame] | 910 | This function sets the error indicator. \var{exception} should be a |
| 911 | Python exception (string or class, not an instance). |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 912 | \var{format} should be a string, containing format codes, similar to |
Moshe Zadka | 57a5932 | 2000-09-01 09:47:20 +0000 | [diff] [blame] | 913 | \cfunction{printf}. The \code{width.precision} before a format code |
| 914 | is parsed, but the width part is ignored. |
| 915 | |
| 916 | \begin{tableii}{c|l}{character}{Character}{Meaning} |
| 917 | \lineii{c}{Character, as an \ctype{int} parameter} |
| 918 | \lineii{d}{Number in decimal, as an \ctype{int} parameter} |
| 919 | \lineii{x}{Number in hexadecimal, as an \ctype{int} parameter} |
| 920 | \lineii{x}{A string, as a \ctype{char *} parameter} |
| 921 | \end{tableii} |
| 922 | |
| 923 | An unrecognized format character causes all the rest of |
| 924 | the format string to be copied as-is to the result string, |
| 925 | and any extra arguments discarded. |
| 926 | |
| 927 | A new reference is returned, which is owned by the caller. |
Jeremy Hylton | 98605b5 | 2000-04-10 18:40:57 +0000 | [diff] [blame] | 928 | \end{cfuncdesc} |
| 929 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 930 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 931 | 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] | 932 | \end{cfuncdesc} |
| 933 | |
| 934 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 935 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 936 | \var{message})}, where \var{message} indicates that a built-in operation |
| 937 | was invoked with an illegal argument. It is mostly for internal use. |
| 938 | \end{cfuncdesc} |
| 939 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 940 | \begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 941 | This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 942 | returns \NULL{} so an object allocation function can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 943 | \samp{return PyErr_NoMemory();} when it runs out of memory. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 944 | \end{cfuncdesc} |
| 945 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 946 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 947 | This is a convenience function to raise an exception when a C library |
| 948 | 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] | 949 | It constructs a tuple object whose first item is the integer |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 950 | \cdata{errno} value and whose second item is the corresponding error |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 951 | message (gotten from \cfunction{strerror()}\ttindex{strerror()}), and |
| 952 | then calls |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 953 | \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 954 | the \cdata{errno} value is \constant{EINTR}, indicating an interrupted |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 955 | system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 956 | the error indicator, leaves it set to that. The function always |
| 957 | returns \NULL{}, so a wrapper function around a system call can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 958 | \samp{return PyErr_SetFromErrno();} when the system call returns an |
| 959 | error. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 960 | \end{cfuncdesc} |
| 961 | |
Fred Drake | 490d34d | 2001-06-20 21:39:12 +0000 | [diff] [blame] | 962 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrnoWithFilename}{PyObject *type, |
| 963 | char *filename} |
| 964 | Similar to \cfunction{PyErr_SetFromErrno()}, with the additional |
| 965 | behavior that if \var{filename} is not \NULL, it is passed to the |
| 966 | constructor of \var{type} as a third parameter. In the case of |
| 967 | exceptions such as \exception{IOError} and \exception{OSError}, this |
| 968 | is used to define the \member{filename} attribute of the exception |
| 969 | instance. |
| 970 | \end{cfuncdesc} |
| 971 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 972 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 973 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 974 | \var{message})}, where \var{message} indicates that an internal |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 975 | 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] | 976 | argument. It is mostly for internal use. |
| 977 | \end{cfuncdesc} |
| 978 | |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 979 | \begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message} |
| 980 | Issue a warning message. The \var{category} argument is a warning |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 981 | 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] | 982 | string. |
| 983 | |
| 984 | This function normally prints a warning message to \var{sys.stderr}; |
| 985 | however, it is also possible that the user has specified that warnings |
| 986 | are to be turned into errors, and in that case this will raise an |
| 987 | exception. It is also possible that the function raises an exception |
| 988 | because of a problem with the warning machinery (the implementation |
| 989 | imports the \module{warnings} module to do the heavy lifting). The |
| 990 | return value is \code{0} if no exception is raised, or \code{-1} if |
| 991 | an exception is raised. (It is not possible to determine whether a |
| 992 | warning message is actually printed, nor what the reason is for the |
| 993 | exception; this is intentional.) If an exception is raised, the |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 994 | caller should do its normal exception handling |
| 995 | (e.g. \cfunction{Py_DECREF()} owned references and return an error |
| 996 | value). |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 997 | |
| 998 | Warning categories must be subclasses of \cdata{Warning}; the default |
| 999 | warning category is \cdata{RuntimeWarning}. The standard Python |
| 1000 | warning categories are available as global variables whose names are |
| 1001 | \samp{PyExc_} followed by the Python exception name. These have the |
| 1002 | type \ctype{PyObject*}; they are all class objects. Their names are |
| 1003 | \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning}, |
| 1004 | \cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning}, and |
| 1005 | \cdata{PyExc_RuntimeWarning}. \cdata{PyExc_Warning} is a subclass of |
| 1006 | \cdata{PyExc_Exception}; the other warning categories are subclasses |
| 1007 | of \cdata{PyExc_Warning}. |
| 1008 | |
| 1009 | For information about warning control, see the documentation for the |
Fred Drake | 316ef7c | 2001-01-04 05:56:34 +0000 | [diff] [blame] | 1010 | \module{warnings} module and the \programopt{-W} option in the command |
| 1011 | line documentation. There is no C API for warning control. |
Guido van Rossum | 3dbb406 | 2000-12-19 03:53:01 +0000 | [diff] [blame] | 1012 | \end{cfuncdesc} |
| 1013 | |
Guido van Rossum | 1874c8f | 2001-02-28 23:46:44 +0000 | [diff] [blame] | 1014 | \begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category, char *message, |
| 1015 | char *filename, int lineno, char *module, PyObject *registry} |
| 1016 | Issue a warning message with explicit control over all warning |
| 1017 | attributes. This is a straightforward wrapper around the Python |
| 1018 | function \function{warnings.warn_explicit()}, see there for more |
| 1019 | information. The \var{module} and \var{registry} arguments may be |
| 1020 | set to \code{NULL} to get the default effect described there. |
| 1021 | \end{cfuncdesc} |
| 1022 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1023 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{} |
| 1024 | This function interacts with Python's signal handling. It checks |
| 1025 | 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] | 1026 | corresponding signal handler. If the |
| 1027 | \module{signal}\refbimodindex{signal} module is supported, this can |
| 1028 | invoke a signal handler written in Python. In all cases, the default |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1029 | effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the |
| 1030 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 1031 | \exception{KeyboardInterrupt} exception. If an exception is raised the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1032 | error indicator is set and the function returns \code{1}; otherwise |
| 1033 | the function returns \code{0}. The error indicator may or may not be |
| 1034 | cleared if it was previously set. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1035 | \end{cfuncdesc} |
| 1036 | |
| 1037 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1038 | This function is obsolete. It simulates the effect of a |
| 1039 | \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1040 | \cfunction{PyErr_CheckSignals()} is called, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1041 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 1042 | \exception{KeyboardInterrupt} will be raised. |
| 1043 | It may be called without holding the interpreter lock. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1044 | \end{cfuncdesc} |
| 1045 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1046 | \begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name, |
| 1047 | PyObject *base, |
| 1048 | PyObject *dict} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1049 | This utility function creates and returns a new exception object. The |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1050 | \var{name} argument must be the name of the new exception, a C string |
| 1051 | of the form \code{module.class}. The \var{base} and |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1052 | \var{dict} arguments are normally \NULL{}. This creates a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1053 | class object derived from the root for all exceptions, the built-in |
| 1054 | name \exception{Exception} (accessible in C as |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1055 | \cdata{PyExc_Exception}). The \member{__module__} attribute of the |
| 1056 | new class is set to the first part (up to the last dot) of the |
| 1057 | \var{name} argument, and the class name is set to the last part (after |
| 1058 | the last dot). The \var{base} argument can be used to specify an |
| 1059 | alternate base class. The \var{dict} argument can be used to specify |
| 1060 | a dictionary of class variables and methods. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1061 | \end{cfuncdesc} |
| 1062 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1063 | \begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj} |
| 1064 | This utility function prints a warning message to \var{sys.stderr} |
| 1065 | when an exception has been set but it is impossible for the |
| 1066 | interpreter to actually raise the exception. It is used, for example, |
| 1067 | when an exception occurs in an \member{__del__} method. |
| 1068 | |
| 1069 | The function is called with a single argument \var{obj} that |
| 1070 | identifies where the context in which the unraisable exception |
| 1071 | occurred. The repr of \var{obj} will be printed in the warning |
| 1072 | message. |
| 1073 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1074 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1075 | \section{Standard Exceptions \label{standardExceptions}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1076 | |
| 1077 | All standard Python exceptions are available as global variables whose |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1078 | names are \samp{PyExc_} followed by the Python exception name. These |
| 1079 | have the type \ctype{PyObject*}; they are all class objects. For |
| 1080 | completeness, here are all the variables: |
| 1081 | |
| 1082 | \begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes} |
| 1083 | \lineiii{PyExc_Exception}{\exception{Exception}}{(1)} |
| 1084 | \lineiii{PyExc_StandardError}{\exception{StandardError}}{(1)} |
| 1085 | \lineiii{PyExc_ArithmeticError}{\exception{ArithmeticError}}{(1)} |
| 1086 | \lineiii{PyExc_LookupError}{\exception{LookupError}}{(1)} |
| 1087 | \lineiii{PyExc_AssertionError}{\exception{AssertionError}}{} |
| 1088 | \lineiii{PyExc_AttributeError}{\exception{AttributeError}}{} |
| 1089 | \lineiii{PyExc_EOFError}{\exception{EOFError}}{} |
| 1090 | \lineiii{PyExc_EnvironmentError}{\exception{EnvironmentError}}{(1)} |
| 1091 | \lineiii{PyExc_FloatingPointError}{\exception{FloatingPointError}}{} |
| 1092 | \lineiii{PyExc_IOError}{\exception{IOError}}{} |
| 1093 | \lineiii{PyExc_ImportError}{\exception{ImportError}}{} |
| 1094 | \lineiii{PyExc_IndexError}{\exception{IndexError}}{} |
| 1095 | \lineiii{PyExc_KeyError}{\exception{KeyError}}{} |
| 1096 | \lineiii{PyExc_KeyboardInterrupt}{\exception{KeyboardInterrupt}}{} |
| 1097 | \lineiii{PyExc_MemoryError}{\exception{MemoryError}}{} |
| 1098 | \lineiii{PyExc_NameError}{\exception{NameError}}{} |
| 1099 | \lineiii{PyExc_NotImplementedError}{\exception{NotImplementedError}}{} |
| 1100 | \lineiii{PyExc_OSError}{\exception{OSError}}{} |
| 1101 | \lineiii{PyExc_OverflowError}{\exception{OverflowError}}{} |
Fred Drake | bf88b68 | 2001-10-05 22:03:58 +0000 | [diff] [blame^] | 1102 | \lineiii{PyExc_ReferenceError}{\exception{ReferenceError}}{(2)} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1103 | \lineiii{PyExc_RuntimeError}{\exception{RuntimeError}}{} |
| 1104 | \lineiii{PyExc_SyntaxError}{\exception{SyntaxError}}{} |
| 1105 | \lineiii{PyExc_SystemError}{\exception{SystemError}}{} |
| 1106 | \lineiii{PyExc_SystemExit}{\exception{SystemExit}}{} |
| 1107 | \lineiii{PyExc_TypeError}{\exception{TypeError}}{} |
| 1108 | \lineiii{PyExc_ValueError}{\exception{ValueError}}{} |
Fred Drake | bf88b68 | 2001-10-05 22:03:58 +0000 | [diff] [blame^] | 1109 | \lineiii{PyExc_WindowsError}{\exception{WindowsError}}{(3)} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1110 | \lineiii{PyExc_ZeroDivisionError}{\exception{ZeroDivisionError}}{} |
| 1111 | \end{tableiii} |
| 1112 | |
| 1113 | \noindent |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1114 | Notes: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1115 | \begin{description} |
| 1116 | \item[(1)] |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1117 | This is a base class for other standard exceptions. |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1118 | |
| 1119 | \item[(2)] |
Fred Drake | bf88b68 | 2001-10-05 22:03:58 +0000 | [diff] [blame^] | 1120 | This is the same as \exception{weakref.ReferenceError}. |
| 1121 | |
| 1122 | \item[(3)] |
Fred Drake | a8d7341 | 2000-08-11 20:39:29 +0000 | [diff] [blame] | 1123 | Only defined on Windows; protect code that uses this by testing that |
| 1124 | the preprocessor macro \code{MS_WINDOWS} is defined. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1125 | \end{description} |
| 1126 | |
| 1127 | |
| 1128 | \section{Deprecation of String Exceptions} |
| 1129 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1130 | All exceptions built into Python or provided in the standard library |
| 1131 | are derived from \exception{Exception}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1132 | \withsubitem{(built-in exception)}{\ttindex{Exception}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1133 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1134 | String exceptions are still supported in the interpreter to allow |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1135 | existing code to run unmodified, but this will also change in a future |
| 1136 | release. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1137 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1138 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1139 | \chapter{Utilities \label{utilities}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1140 | |
Fred Drake | 88fdaa7 | 2001-07-20 20:56:11 +0000 | [diff] [blame] | 1141 | The functions in this chapter perform various utility tasks, ranging |
| 1142 | from helping C code be more portable across platforms, using Python |
| 1143 | modules from C, and parsing function arguments and constructing Python |
| 1144 | values from C values. |
| 1145 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1146 | |
Fred Drake | 377fb1e | 2001-07-14 03:01:48 +0000 | [diff] [blame] | 1147 | \section{Operating System Utilities \label{os}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1148 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1149 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1150 | Return true (nonzero) if the standard I/O file \var{fp} with name |
| 1151 | \var{filename} is deemed interactive. This is the case for files for |
| 1152 | which \samp{isatty(fileno(\var{fp}))} is true. If the global flag |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1153 | \cdata{Py_InteractiveFlag} is true, this function also returns true if |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1154 | 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] | 1155 | the strings \code{'<stdin>'} or \code{'???'}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1156 | \end{cfuncdesc} |
| 1157 | |
| 1158 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1159 | Return the time of last modification of the file \var{filename}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1160 | 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] | 1161 | the standard C library function \cfunction{time()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1162 | \end{cfuncdesc} |
| 1163 | |
Fred Drake | cabbc3b | 2000-06-28 15:53:13 +0000 | [diff] [blame] | 1164 | \begin{cfuncdesc}{void}{PyOS_AfterFork}{} |
| 1165 | Function to update some internal state after a process fork; this |
| 1166 | should be called in the new process if the Python interpreter will |
| 1167 | continue to be used. If a new executable is loaded into the new |
| 1168 | process, this function does not need to be called. |
| 1169 | \end{cfuncdesc} |
| 1170 | |
Fred Drake | 17e6343 | 2000-08-31 05:50:40 +0000 | [diff] [blame] | 1171 | \begin{cfuncdesc}{int}{PyOS_CheckStack}{} |
| 1172 | Return true when the interpreter runs out of stack space. This is a |
| 1173 | reliable check, but is only available when \code{USE_STACKCHECK} is |
| 1174 | defined (currently on Windows using the Microsoft Visual C++ compiler |
| 1175 | and on the Macintosh). \code{USE_CHECKSTACK} will be defined |
| 1176 | automatically; you should never change the definition in your own |
| 1177 | code. |
| 1178 | \end{cfuncdesc} |
| 1179 | |
Guido van Rossum | c96ec6e | 2000-09-16 16:30:48 +0000 | [diff] [blame] | 1180 | \begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_getsig}{int i} |
| 1181 | Return the current signal handler for signal \var{i}. |
| 1182 | This is a thin wrapper around either \cfunction{sigaction} or |
| 1183 | \cfunction{signal}. Do not call those functions directly! |
| 1184 | \ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}. |
| 1185 | \end{cfuncdesc} |
| 1186 | |
| 1187 | \begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_setsig}{int i, PyOS_sighandler_t h} |
| 1188 | Set the signal handler for signal \var{i} to be \var{h}; |
| 1189 | return the old signal handler. |
| 1190 | This is a thin wrapper around either \cfunction{sigaction} or |
| 1191 | \cfunction{signal}. Do not call those functions directly! |
| 1192 | \ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void (*)(int)}. |
| 1193 | \end{cfuncdesc} |
| 1194 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1195 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1196 | \section{Process Control \label{processControl}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1197 | |
| 1198 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 1199 | Print a fatal error message and kill the process. No cleanup is |
| 1200 | performed. This function should only be invoked when a condition is |
| 1201 | detected that would make it dangerous to continue using the Python |
| 1202 | interpreter; e.g., when the object administration appears to be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1203 | corrupted. On \UNIX{}, the standard C library function |
| 1204 | \cfunction{abort()}\ttindex{abort()} is called which will attempt to |
| 1205 | produce a \file{core} file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1206 | \end{cfuncdesc} |
| 1207 | |
| 1208 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1209 | Exit the current process. This calls |
| 1210 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 1211 | then calls the standard C library function |
| 1212 | \code{exit(\var{status})}\ttindex{exit()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1213 | \end{cfuncdesc} |
| 1214 | |
| 1215 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1216 | Register a cleanup function to be called by |
| 1217 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1218 | The cleanup function will be called with no arguments and should |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1219 | return no value. At most 32 \index{cleanup functions}cleanup |
| 1220 | functions can be registered. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1221 | When the registration is successful, \cfunction{Py_AtExit()} returns |
| 1222 | \code{0}; on failure, it returns \code{-1}. The cleanup function |
| 1223 | registered last is called first. Each cleanup function will be called |
| 1224 | at most once. Since Python's internal finallization will have |
| 1225 | completed before the cleanup function, no Python APIs should be called |
| 1226 | by \var{func}. |
| 1227 | \end{cfuncdesc} |
| 1228 | |
| 1229 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1230 | \section{Importing Modules \label{importing}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1231 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1232 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1233 | This is a simplified interface to |
| 1234 | \cfunction{PyImport_ImportModuleEx()} below, leaving the |
| 1235 | \var{globals} and \var{locals} arguments set to \NULL{}. When the |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 1236 | \var{name} argument contains a dot (when it specifies a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1237 | submodule of a package), the \var{fromlist} argument is set to the |
| 1238 | list \code{['*']} so that the return value is the named module rather |
| 1239 | than the top-level package containing it as would otherwise be the |
| 1240 | case. (Unfortunately, this has an additional side effect when |
| 1241 | \var{name} in fact specifies a subpackage instead of a submodule: the |
| 1242 | submodules specified in the package's \code{__all__} variable are |
| 1243 | \index{package variable!\code{__all__}} |
| 1244 | \withsubitem{(package variable)}{\ttindex{__all__}}loaded.) Return a |
| 1245 | new reference to the imported module, or |
| 1246 | \NULL{} with an exception set on failure (the module may still be |
| 1247 | created in this case --- examine \code{sys.modules} to find out). |
| 1248 | \withsubitem{(in module sys)}{\ttindex{modules}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1249 | \end{cfuncdesc} |
| 1250 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1251 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, |
| 1252 | PyObject *globals, PyObject *locals, PyObject *fromlist} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1253 | 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] | 1254 | Python function \function{__import__()}\bifuncindex{__import__}, as |
| 1255 | the standard \function{__import__()} function calls this function |
| 1256 | directly. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1258 | 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] | 1259 | top-level package, or \NULL{} with an exception set on failure |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 1260 | (the module may still be created in this case). Like for |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1261 | \function{__import__()}, the return value when a submodule of a |
| 1262 | package was requested is normally the top-level package, unless a |
| 1263 | non-empty \var{fromlist} was given. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1264 | \end{cfuncdesc} |
| 1265 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1266 | \begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1267 | This is a higher-level interface that calls the current ``import hook |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1268 | function''. It invokes the \function{__import__()} function from the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1269 | \code{__builtins__} of the current globals. This means that the |
| 1270 | import is done using whatever import hooks are installed in the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 1271 | current environment, e.g. by \module{rexec}\refstmodindex{rexec} or |
| 1272 | \module{ihooks}\refstmodindex{ihooks}. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1273 | \end{cfuncdesc} |
| 1274 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1275 | \begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1276 | 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] | 1277 | Python function \function{reload()}\bifuncindex{reload}, as the standard |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1278 | \function{reload()} function calls this function directly. Return a |
| 1279 | new reference to the reloaded module, or \NULL{} with an exception set |
| 1280 | on failure (the module still exists in this case). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1281 | \end{cfuncdesc} |
| 1282 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1283 | \begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1284 | Return the module object corresponding to a module name. The |
| 1285 | \var{name} argument may be of the form \code{package.module}). First |
| 1286 | 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] | 1287 | a new one and insert in in the modules dictionary. |
Guido van Rossum | a096a2e | 1998-11-02 17:02:42 +0000 | [diff] [blame] | 1288 | Warning: this function does not load or import the module; if the |
| 1289 | module wasn't already loaded, you will get an empty module object. |
| 1290 | Use \cfunction{PyImport_ImportModule()} or one of its variants to |
| 1291 | import a module. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1292 | Return \NULL{} with an exception set on failure. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1293 | \end{cfuncdesc} |
| 1294 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1295 | \begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1296 | Given a module name (possibly of the form \code{package.module}) and a |
| 1297 | code object read from a Python bytecode file or obtained from the |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1298 | built-in function \function{compile()}\bifuncindex{compile}, load the |
| 1299 | module. Return a new reference to the module object, or \NULL{} with |
| 1300 | an exception set if an error occurred (the module may still be created |
| 1301 | in this case). (This function would reload the module if it was |
| 1302 | already imported.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1303 | \end{cfuncdesc} |
| 1304 | |
| 1305 | \begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1306 | Return the magic number for Python bytecode files (a.k.a. |
| 1307 | \file{.pyc} and \file{.pyo} files). The magic number should be |
| 1308 | present in the first four bytes of the bytecode file, in little-endian |
| 1309 | byte order. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1310 | \end{cfuncdesc} |
| 1311 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1312 | \begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1313 | Return the dictionary used for the module administration |
| 1314 | (a.k.a. \code{sys.modules}). Note that this is a per-interpreter |
| 1315 | variable. |
| 1316 | \end{cfuncdesc} |
| 1317 | |
| 1318 | \begin{cfuncdesc}{void}{_PyImport_Init}{} |
| 1319 | Initialize the import mechanism. For internal use only. |
| 1320 | \end{cfuncdesc} |
| 1321 | |
| 1322 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 1323 | Empty the module table. For internal use only. |
| 1324 | \end{cfuncdesc} |
| 1325 | |
| 1326 | \begin{cfuncdesc}{void}{_PyImport_Fini}{} |
| 1327 | Finalize the import mechanism. For internal use only. |
| 1328 | \end{cfuncdesc} |
| 1329 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1330 | \begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1331 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1332 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1333 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1334 | \begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1335 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1336 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1337 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 1338 | \begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name} |
| 1339 | Load a frozen module named \var{name}. Return \code{1} for success, |
| 1340 | \code{0} if the module is not found, and \code{-1} with an exception |
| 1341 | set if the initialization failed. To access the imported module on a |
| 1342 | successful load, use \cfunction{PyImport_ImportModule()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1343 | (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] | 1344 | already imported.) |
| 1345 | \end{cfuncdesc} |
| 1346 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1347 | \begin{ctypedesc}[_frozen]{struct _frozen} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1348 | This is the structure type definition for frozen module descriptors, |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1349 | as generated by the \program{freeze}\index{freeze utility} utility |
| 1350 | (see \file{Tools/freeze/} in the Python source distribution). Its |
Fred Drake | e0d9a83 | 2000-09-01 05:30:00 +0000 | [diff] [blame] | 1351 | definition, found in \file{Include/import.h}, is: |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1352 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1353 | \begin{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1354 | struct _frozen { |
Fred Drake | 36fbe76 | 1997-10-13 18:18:33 +0000 | [diff] [blame] | 1355 | char *name; |
| 1356 | unsigned char *code; |
| 1357 | int size; |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1358 | }; |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1359 | \end{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1360 | \end{ctypedesc} |
| 1361 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1362 | \begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1363 | This pointer is initialized to point to an array of \ctype{struct |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1364 | _frozen} records, terminated by one whose members are all |
| 1365 | \NULL{} or zero. When a frozen module is imported, it is searched in |
| 1366 | 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] | 1367 | dynamically created collection of frozen modules. |
| 1368 | \end{cvardesc} |
| 1369 | |
Fred Drake | e0d9a83 | 2000-09-01 05:30:00 +0000 | [diff] [blame] | 1370 | \begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name, |
| 1371 | void (*initfunc)(void)} |
| 1372 | Add a single module to the existing table of built-in modules. This |
| 1373 | is a convenience wrapper around \cfunction{PyImport_ExtendInittab()}, |
| 1374 | returning \code{-1} if the table could not be extended. The new |
| 1375 | module can be imported by the name \var{name}, and uses the function |
| 1376 | \var{initfunc} as the initialization function called on the first |
| 1377 | attempted import. This should be called before |
| 1378 | \cfunction{Py_Initialize()}. |
| 1379 | \end{cfuncdesc} |
| 1380 | |
| 1381 | \begin{ctypedesc}[_inittab]{struct _inittab} |
| 1382 | Structure describing a single entry in the list of built-in modules. |
| 1383 | Each of these structures gives the name and initialization function |
| 1384 | for a module built into the interpreter. Programs which embed Python |
| 1385 | may use an array of these structures in conjunction with |
| 1386 | \cfunction{PyImport_ExtendInittab()} to provide additional built-in |
| 1387 | modules. The structure is defined in \file{Include/import.h} as: |
| 1388 | |
| 1389 | \begin{verbatim} |
| 1390 | struct _inittab { |
| 1391 | char *name; |
| 1392 | void (*initfunc)(void); |
| 1393 | }; |
| 1394 | \end{verbatim} |
| 1395 | \end{ctypedesc} |
| 1396 | |
| 1397 | \begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab} |
| 1398 | Add a collection of modules to the table of built-in modules. The |
| 1399 | \var{newtab} array must end with a sentinel entry which contains |
| 1400 | \NULL{} for the \member{name} field; failure to provide the sentinel |
| 1401 | value can result in a memory fault. Returns \code{0} on success or |
| 1402 | \code{-1} if insufficient memory could be allocated to extend the |
| 1403 | internal table. In the event of failure, no modules are added to the |
| 1404 | internal table. This should be called before |
| 1405 | \cfunction{Py_Initialize()}. |
| 1406 | \end{cfuncdesc} |
| 1407 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1408 | |
Greg Ward | e22871e | 2001-09-26 18:12:49 +0000 | [diff] [blame] | 1409 | \section{Parsing arguments and building values |
Fred Drake | 88fdaa7 | 2001-07-20 20:56:11 +0000 | [diff] [blame] | 1410 | \label{arg-parsing}} |
| 1411 | |
| 1412 | These functions are useful when creating your own extensions functions |
| 1413 | and methods. Additional information and examples are available in |
| 1414 | \citetitle[../ext/ext.html]{Extending and Embedding the Python |
| 1415 | Interpreter}. |
| 1416 | |
| 1417 | \begin{cfuncdesc}{int}{PyArg_ParseTuple}{PyObject *args, char *format, |
| 1418 | \moreargs} |
| 1419 | Parse the parameters of a function that takes only positional |
| 1420 | parameters into local variables. Returns true on success; on |
| 1421 | failure, it returns false and raises the appropriate exception. See |
| 1422 | \citetitle[../ext/parseTuple.html]{Extending and Embedding the |
| 1423 | Python Interpreter} for more information. |
| 1424 | \end{cfuncdesc} |
| 1425 | |
| 1426 | \begin{cfuncdesc}{int}{PyArg_ParseTupleAndKeywords}{PyObject *args, |
| 1427 | PyObject *kw, char *format, char *keywords[], |
| 1428 | \moreargs} |
| 1429 | Parse the parameters of a function that takes both positional and |
| 1430 | keyword parameters into local variables. Returns true on success; |
| 1431 | on failure, it returns false and raises the appropriate exception. |
| 1432 | See \citetitle[../ext/parseTupleAndKeywords.html]{Extending and |
| 1433 | Embedding the Python Interpreter} for more information. |
| 1434 | \end{cfuncdesc} |
| 1435 | |
| 1436 | \begin{cfuncdesc}{int}{PyArg_Parse}{PyObject *args, char *format, |
| 1437 | \moreargs} |
| 1438 | Function used to deconstruct the argument lists of ``old-style'' |
| 1439 | functions --- these are functions which use the |
| 1440 | \constant{METH_OLDARGS} parameter parsing method. This is not |
| 1441 | recommended for use in parameter parsing in new code, and most code |
| 1442 | in the standard interpreter has been modified to no longer use this |
| 1443 | for that purpose. It does remain a convenient way to decompose |
| 1444 | other tuples, however, and may continue to be used for that |
| 1445 | purpose. |
| 1446 | \end{cfuncdesc} |
| 1447 | |
| 1448 | \begin{cfuncdesc}{PyObject*}{Py_BuildValue}{char *format, |
| 1449 | \moreargs} |
| 1450 | Create a new value based on a format string similar to those |
| 1451 | accepted by the \cfunction{PyArg_Parse*()} family of functions and a |
| 1452 | sequence of values. Returns the value or \NULL{} in the case of an |
| 1453 | error; an exception will be raised if \NULL{} is returned. For more |
| 1454 | information on the format string and additional parameters, see |
| 1455 | \citetitle[../ext/buildValue.html]{Extending and Embedding the |
| 1456 | Python Interpreter}. |
| 1457 | \end{cfuncdesc} |
| 1458 | |
| 1459 | |
| 1460 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1461 | \chapter{Abstract Objects Layer \label{abstract}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1462 | |
| 1463 | The functions in this chapter interact with Python objects regardless |
| 1464 | of their type, or with wide classes of object types (e.g. all |
| 1465 | numerical types, or all sequence types). When used on object types |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1466 | 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] | 1467 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1468 | \section{Object Protocol \label{object}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1469 | |
| 1470 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1471 | Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error. |
| 1472 | The flags argument is used to enable certain printing options. The |
| 1473 | only option currently supported is \constant{Py_PRINT_RAW}; if given, |
| 1474 | the \function{str()} of the object is written instead of the |
| 1475 | \function{repr()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1476 | \end{cfuncdesc} |
| 1477 | |
| 1478 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1479 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1480 | \code{0} otherwise. This is equivalent to the Python expression |
| 1481 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1482 | This function always succeeds. |
| 1483 | \end{cfuncdesc} |
| 1484 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1485 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, |
| 1486 | char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1487 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1488 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1489 | This is the equivalent of the Python expression |
| 1490 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1491 | \end{cfuncdesc} |
| 1492 | |
| 1493 | |
| 1494 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1495 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1496 | \code{0} otherwise. This is equivalent to the Python expression |
| 1497 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1498 | This function always succeeds. |
| 1499 | \end{cfuncdesc} |
| 1500 | |
| 1501 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1502 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, |
| 1503 | PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1504 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1505 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1506 | This is the equivalent of the Python expression |
| 1507 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1508 | \end{cfuncdesc} |
| 1509 | |
| 1510 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1511 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, |
| 1512 | char *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1513 | Set the value of the attribute named \var{attr_name}, for object |
| 1514 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This is |
| 1515 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1516 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1517 | \end{cfuncdesc} |
| 1518 | |
| 1519 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1520 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, |
| 1521 | PyObject *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1522 | Set the value of the attribute named \var{attr_name}, for |
| 1523 | object \var{o}, |
| 1524 | to the value \var{v}. Returns \code{-1} on failure. This is |
| 1525 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1526 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1527 | \end{cfuncdesc} |
| 1528 | |
| 1529 | |
| 1530 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1531 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1532 | \code{-1} on failure. This is the equivalent of the Python |
| 1533 | statement: \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1534 | \end{cfuncdesc} |
| 1535 | |
| 1536 | |
| 1537 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1538 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1539 | \code{-1} on failure. This is the equivalent of the Python |
| 1540 | statement \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1541 | \end{cfuncdesc} |
| 1542 | |
| 1543 | |
| 1544 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1545 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1546 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1547 | \var{o2}. The result of the comparison is returned in \var{result}. |
| 1548 | Returns \code{-1} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1549 | statement\bifuncindex{cmp} \samp{\var{result} = cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1550 | \end{cfuncdesc} |
| 1551 | |
| 1552 | |
| 1553 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1554 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1555 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1556 | \var{o2}. Returns the result of the comparison on success. On error, |
| 1557 | the value returned is undefined; use \cfunction{PyErr_Occurred()} to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1558 | detect an error. This is equivalent to the Python |
| 1559 | expression\bifuncindex{cmp} \samp{cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1560 | \end{cfuncdesc} |
| 1561 | |
| 1562 | |
| 1563 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1564 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1565 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1566 | the equivalent of the Python expression \samp{repr(\var{o})}. |
| 1567 | Called by the \function{repr()}\bifuncindex{repr} built-in function |
| 1568 | and by reverse quotes. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1569 | \end{cfuncdesc} |
| 1570 | |
| 1571 | |
| 1572 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1573 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1574 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1575 | the equivalent of the Python expression \samp{str(\var{o})}. |
| 1576 | Called by the \function{str()}\bifuncindex{str} built-in function and |
| 1577 | by the \keyword{print} statement. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1578 | \end{cfuncdesc} |
| 1579 | |
| 1580 | |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1581 | \begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o} |
| 1582 | Compute a Unicode string representation of object \var{o}. Returns the |
| 1583 | Unicode string representation on success, \NULL{} on failure. This is |
| 1584 | the equivalent of the Python expression \samp{unistr(\var{o})}. |
| 1585 | Called by the \function{unistr()}\bifuncindex{unistr} built-in function. |
| 1586 | \end{cfuncdesc} |
| 1587 | |
Fred Drake | 58c8f9f | 2001-03-28 21:14:32 +0000 | [diff] [blame] | 1588 | \begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls} |
| 1589 | Return \code{1} if \var{inst} is an instance of the class \var{cls} or |
| 1590 | a subclass of \var{cls}. If \var{cls} is a type object rather than a |
| 1591 | class object, \cfunction{PyObject_IsInstance()} returns \code{1} if |
| 1592 | \var{inst} is of type \var{cls}. If \var{inst} is not a class |
| 1593 | instance and \var{cls} is neither a type object or class object, |
| 1594 | \var{inst} must have a \member{__class__} attribute --- the class |
| 1595 | relationship of the value of that attribute with \var{cls} will be |
| 1596 | used to determine the result of this function. |
| 1597 | \versionadded{2.1} |
| 1598 | \end{cfuncdesc} |
| 1599 | |
| 1600 | Subclass determination is done in a fairly straightforward way, but |
| 1601 | includes a wrinkle that implementors of extensions to the class system |
| 1602 | may want to be aware of. If \class{A} and \class{B} are class |
| 1603 | objects, \class{B} is a subclass of \class{A} if it inherits from |
| 1604 | \class{A} either directly or indirectly. If either is not a class |
| 1605 | object, a more general mechanism is used to determine the class |
| 1606 | relationship of the two objects. When testing if \var{B} is a |
| 1607 | subclass of \var{A}, if \var{A} is \var{B}, |
| 1608 | \cfunction{PyObject_IsSubclass()} returns true. If \var{A} and |
| 1609 | \var{B} are different objects, \var{B}'s \member{__bases__} attribute |
| 1610 | is searched in a depth-first fashion for \var{A} --- the presence of |
| 1611 | the \member{__bases__} attribute is considered sufficient for this |
| 1612 | determination. |
| 1613 | |
| 1614 | \begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived, |
| 1615 | PyObject *cls} |
| 1616 | Returns \code{1} if the class \var{derived} is identical to or derived |
| 1617 | from the class \var{cls}, otherwise returns \code{0}. In case of an |
| 1618 | error, returns \code{-1}. If either \var{derived} or \var{cls} is not |
| 1619 | an actual class object, this function uses the generic algorithm |
| 1620 | described above. |
| 1621 | \versionadded{2.1} |
| 1622 | \end{cfuncdesc} |
| 1623 | |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1624 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1625 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1626 | 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] | 1627 | object is callable and \code{0} otherwise. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1628 | This function always succeeds. |
| 1629 | \end{cfuncdesc} |
| 1630 | |
| 1631 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1632 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, |
| 1633 | PyObject *args} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1634 | Call a callable Python object \var{callable_object}, with |
| 1635 | arguments given by the tuple \var{args}. If no arguments are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1636 | 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] | 1637 | call on success, or \NULL{} on failure. This is the equivalent |
Fred Drake | f90490e | 2001-08-02 18:00:28 +0000 | [diff] [blame] | 1638 | of the Python expression \samp{apply(\var{callable_object}, |
| 1639 | \var{args})} or \samp{\var{callable_object}(*\var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1640 | \bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1641 | \end{cfuncdesc} |
| 1642 | |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1643 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, |
| 1644 | char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1645 | Call a callable Python object \var{callable_object}, with a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1646 | variable number of C arguments. The C arguments are described |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1647 | using a \cfunction{Py_BuildValue()} style format string. The format may |
| 1648 | be \NULL{}, indicating that no arguments are provided. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1649 | result of the call on success, or \NULL{} on failure. This is |
Fred Drake | f90490e | 2001-08-02 18:00:28 +0000 | [diff] [blame] | 1650 | the equivalent of the Python expression |
| 1651 | \samp{apply(\var{callable_object}\var{args})} or |
| 1652 | \samp{\var{callable_object}(*\var{args})}. |
| 1653 | \bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1654 | \end{cfuncdesc} |
| 1655 | |
| 1656 | |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1657 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, |
| 1658 | char *method, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1659 | 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] | 1660 | of C arguments. The C arguments are described by a |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1661 | \cfunction{Py_BuildValue()} format string. The format may be \NULL{}, |
| 1662 | indicating that no arguments are provided. Returns the result of the |
| 1663 | call on success, or \NULL{} on failure. This is the equivalent of the |
| 1664 | Python expression \samp{\var{o}.\var{method}(\var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1665 | Note that special method names, such as \method{__add__()}, |
| 1666 | \method{__getitem__()}, and so on are not supported. The specific |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1667 | abstract-object routines for these must be used. |
| 1668 | \end{cfuncdesc} |
| 1669 | |
| 1670 | |
| 1671 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1672 | Compute and return the hash value of an object \var{o}. On |
| 1673 | failure, return \code{-1}. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1674 | expression \samp{hash(\var{o})}.\bifuncindex{hash} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1675 | \end{cfuncdesc} |
| 1676 | |
| 1677 | |
| 1678 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1679 | Returns \code{1} if the object \var{o} is considered to be true, and |
| 1680 | \code{0} otherwise. This is equivalent to the Python expression |
| 1681 | \samp{not not \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1682 | This function always succeeds. |
| 1683 | \end{cfuncdesc} |
| 1684 | |
| 1685 | |
| 1686 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 1687 | When \var{o} is non-\NULL, returns a type object corresponding to the |
| 1688 | object type of object \var{o}. On failure, raises |
| 1689 | \exception{SystemError} and returns \NULL. This is equivalent to the |
| 1690 | Python expression \code{type(\var{o})}. |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1691 | \bifuncindex{type} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1692 | \end{cfuncdesc} |
| 1693 | |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 1694 | \begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type} |
| 1695 | Return true if the object \var{o} is of type \var{type} or a subtype |
| 1696 | of \var{type}. Both parameters must be non-\NULL. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 1697 | \versionadded{2.2} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 1698 | \end{cfuncdesc} |
| 1699 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1700 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1701 | 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] | 1702 | both sequence and mapping protocols, the sequence length is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1703 | returned. On error, \code{-1} is returned. This is the equivalent |
| 1704 | to the Python expression \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1705 | \end{cfuncdesc} |
| 1706 | |
| 1707 | |
| 1708 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1709 | Return element of \var{o} corresponding to the object \var{key} or |
| 1710 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1711 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1712 | \end{cfuncdesc} |
| 1713 | |
| 1714 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1715 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, |
| 1716 | PyObject *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1717 | Map the object \var{key} to the value \var{v}. |
| 1718 | Returns \code{-1} on failure. This is the equivalent |
| 1719 | of the Python statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1720 | \end{cfuncdesc} |
| 1721 | |
| 1722 | |
Guido van Rossum | d1dbf63 | 1999-01-22 20:10:49 +0000 | [diff] [blame] | 1723 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1724 | Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on |
| 1725 | failure. This is the equivalent of the Python statement \samp{del |
| 1726 | \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1727 | \end{cfuncdesc} |
| 1728 | |
Andrew M. Kuchling | 8c46b30 | 2000-07-13 23:58:16 +0000 | [diff] [blame] | 1729 | \begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o} |
| 1730 | Derives a file-descriptor from a Python object. If the object |
| 1731 | is an integer or long integer, its value is returned. If not, the |
| 1732 | object's \method{fileno()} method is called if it exists; the method |
| 1733 | must return an integer or long integer, which is returned as the file |
| 1734 | descriptor value. Returns \code{-1} on failure. |
| 1735 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1736 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1737 | \begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o} |
| 1738 | This is equivalent to the Python expression \samp{dir(\var{o})}, |
| 1739 | returning a (possibly empty) list of strings appropriate for the |
| 1740 | object argument, or \NULL{} in case of error. |
| 1741 | If the argument is \NULL{}, this is like the Python \samp{dir()}, |
| 1742 | returning the names of the current locals; in this case, if no |
| 1743 | execution frame is active then \NULL{} is returned but |
| 1744 | \cfunction{PyErr_Occurred()} will return false. |
| 1745 | \end{cfuncdesc} |
| 1746 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1747 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1748 | \section{Number Protocol \label{number}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1749 | |
| 1750 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1751 | 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] | 1752 | false otherwise. |
| 1753 | This function always succeeds. |
| 1754 | \end{cfuncdesc} |
| 1755 | |
| 1756 | |
| 1757 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1758 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 1759 | failure. This is the equivalent of the Python expression |
| 1760 | \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1761 | \end{cfuncdesc} |
| 1762 | |
| 1763 | |
| 1764 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1765 | Returns the result of subtracting \var{o2} from \var{o1}, or |
| 1766 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1767 | \samp{\var{o1} - \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1768 | \end{cfuncdesc} |
| 1769 | |
| 1770 | |
| 1771 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1772 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1773 | failure. This is the equivalent of the Python expression |
| 1774 | \samp{\var{o1} * \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1775 | \end{cfuncdesc} |
| 1776 | |
| 1777 | |
| 1778 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1779 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1780 | failure. |
| 1781 | This is the equivalent of the Python expression \samp{\var{o1} / |
| 1782 | \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1783 | \end{cfuncdesc} |
| 1784 | |
| 1785 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1786 | \begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2} |
| 1787 | Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on |
| 1788 | failure. This is equivalent to the ``classic'' division of integers. |
| 1789 | \versionadded{2.2} |
| 1790 | \end{cfuncdesc} |
| 1791 | |
| 1792 | |
| 1793 | \begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2} |
| 1794 | Return a reasonable approximation for the mathematical value of |
| 1795 | \var{o1} divided by \var{o2}, or \NULL{} on failure. The return value |
| 1796 | is ``approximate'' because binary floating point numbers are |
| 1797 | approximate; it is not possible to represent all real numbers in base |
| 1798 | two. This function can return a floating point value when passed two |
| 1799 | integers. |
| 1800 | \versionadded{2.2} |
| 1801 | \end{cfuncdesc} |
| 1802 | |
| 1803 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1804 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1805 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1806 | failure. This is the equivalent of the Python expression |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1807 | \samp{\var{o1} \%\ \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1808 | \end{cfuncdesc} |
| 1809 | |
| 1810 | |
| 1811 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1812 | See the built-in function \function{divmod()}\bifuncindex{divmod}. |
| 1813 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 1814 | expression \samp{divmod(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1815 | \end{cfuncdesc} |
| 1816 | |
| 1817 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1818 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, |
| 1819 | PyObject *o2, PyObject *o3} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1820 | See the built-in function \function{pow()}\bifuncindex{pow}. Returns |
| 1821 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1822 | \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] | 1823 | If \var{o3} is to be ignored, pass \cdata{Py_None} in its place |
| 1824 | (passing \NULL{} for \var{o3} would cause an illegal memory access). |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1825 | \end{cfuncdesc} |
| 1826 | |
| 1827 | |
| 1828 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1829 | Returns the negation of \var{o} on success, or \NULL{} on failure. |
| 1830 | This is the equivalent of the Python expression \samp{-\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1831 | \end{cfuncdesc} |
| 1832 | |
| 1833 | |
| 1834 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1835 | Returns \var{o} on success, or \NULL{} on failure. |
| 1836 | This is the equivalent of the Python expression \samp{+\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1837 | \end{cfuncdesc} |
| 1838 | |
| 1839 | |
| 1840 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1841 | Returns the absolute value of \var{o}, or \NULL{} on failure. This is |
| 1842 | the equivalent of the Python expression \samp{abs(\var{o})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1843 | \bifuncindex{abs} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1844 | \end{cfuncdesc} |
| 1845 | |
| 1846 | |
| 1847 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1848 | Returns the bitwise negation of \var{o} on success, or \NULL{} on |
| 1849 | failure. This is the equivalent of the Python expression |
| 1850 | \samp{\~\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1851 | \end{cfuncdesc} |
| 1852 | |
| 1853 | |
| 1854 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1855 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 1856 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1857 | expression \samp{\var{o1} <\code{<} \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1858 | \end{cfuncdesc} |
| 1859 | |
| 1860 | |
| 1861 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1862 | Returns the result of right shifting \var{o1} by \var{o2} on success, |
| 1863 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | d20d8b3 | 2001-04-13 14:52:39 +0000 | [diff] [blame] | 1864 | expression \samp{\var{o1} >\code{>} \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1865 | \end{cfuncdesc} |
| 1866 | |
| 1867 | |
| 1868 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1869 | Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and |
| 1870 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1871 | \samp{\var{o1} \&\ \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1872 | \end{cfuncdesc} |
| 1873 | |
| 1874 | |
| 1875 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1876 | 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] | 1877 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 755c23d | 2001-07-14 03:05:53 +0000 | [diff] [blame] | 1878 | expression \samp{\var{o1} \textasciicircum{} \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1879 | \end{cfuncdesc} |
| 1880 | |
| 1881 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1882 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or |
| 1883 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1884 | \samp{\var{o1} | \var{o2}}. |
| 1885 | \end{cfuncdesc} |
| 1886 | |
| 1887 | |
| 1888 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2} |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1889 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 1890 | failure. The operation is done \emph{in-place} when \var{o1} supports |
| 1891 | it. This is the equivalent of the Python statement \samp{\var{o1} += |
| 1892 | \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1893 | \end{cfuncdesc} |
| 1894 | |
| 1895 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1896 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1, |
| 1897 | PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1898 | Returns the result of subtracting \var{o2} from \var{o1}, or |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1899 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 1900 | \var{o1} supports it. This is the equivalent of the Python statement |
| 1901 | \samp{\var{o1} -= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1902 | \end{cfuncdesc} |
| 1903 | |
| 1904 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1905 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1, |
| 1906 | PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1907 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1908 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1909 | This is the equivalent of the Python statement \samp{\var{o1} *= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1910 | \end{cfuncdesc} |
| 1911 | |
| 1912 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1913 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1, |
| 1914 | PyObject *o2} |
| 1915 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1916 | failure. The operation is done \emph{in-place} when \var{o1} supports |
| 1917 | it. This is the equivalent of the Python statement \samp{\var{o1} /= |
| 1918 | \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1919 | \end{cfuncdesc} |
| 1920 | |
| 1921 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1922 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1, |
| 1923 | PyObject *o2} |
| 1924 | Returns the mathematical of dividing \var{o1} by \var{o2}, or \NULL{} |
| 1925 | on failure. The operation is done \emph{in-place} when \var{o1} |
| 1926 | supports it. This is the equivalent of the Python statement |
| 1927 | \samp{\var{o1} //= \var{o2}}. |
| 1928 | \versionadded{2.2} |
| 1929 | \end{cfuncdesc} |
| 1930 | |
| 1931 | |
| 1932 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1, |
| 1933 | PyObject *o2} |
| 1934 | Return a reasonable approximation for the mathematical value of |
| 1935 | \var{o1} divided by \var{o2}, or \NULL{} on failure. The return value |
| 1936 | is ``approximate'' because binary floating point numbers are |
| 1937 | approximate; it is not possible to represent all real numbers in base |
| 1938 | two. This function can return a floating point value when passed two |
| 1939 | integers. The operation is done \emph{in-place} when \var{o1} |
| 1940 | supports it. |
| 1941 | \versionadded{2.2} |
| 1942 | \end{cfuncdesc} |
| 1943 | |
| 1944 | |
| 1945 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1, |
| 1946 | PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1947 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1948 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1949 | This is the equivalent of the Python statement \samp{\var{o1} \%= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1950 | \end{cfuncdesc} |
| 1951 | |
| 1952 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1953 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1, |
| 1954 | PyObject *o2, PyObject *o3} |
| 1955 | See the built-in function \function{pow()}.\bifuncindex{pow} Returns |
| 1956 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 1957 | \var{o1} supports it. This is the equivalent of the Python statement |
| 1958 | \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None}, or an |
| 1959 | in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})} |
| 1960 | otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its |
| 1961 | place (passing \NULL{} for \var{o3} would cause an illegal memory |
| 1962 | access). |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1963 | \end{cfuncdesc} |
| 1964 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1965 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1, |
| 1966 | PyObject *o2} |
| 1967 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 1968 | or \NULL{} on failure. The operation is done \emph{in-place} when |
| 1969 | \var{o1} supports it. This is the equivalent of the Python statement |
| 1970 | \samp{\var{o1} <\code{<=} \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1971 | \end{cfuncdesc} |
| 1972 | |
| 1973 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1974 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1, |
| 1975 | PyObject *o2} |
| 1976 | Returns the result of right shifting \var{o1} by \var{o2} on success, |
| 1977 | or \NULL{} on failure. The operation is done \emph{in-place} when |
| 1978 | \var{o1} supports it. This is the equivalent of the Python statement |
| 1979 | \samp{\var{o1} >\code{>=} \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1980 | \end{cfuncdesc} |
| 1981 | |
| 1982 | |
| 1983 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2} |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1984 | Returns the ``bitwise and'' of \var{o1} and \var{o2} on success |
| 1985 | and \NULL{} on failure. The operation is done \emph{in-place} when |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1986 | \var{o1} supports it. This is the equivalent of the Python statement |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 1987 | \samp{\var{o1} \&= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1988 | \end{cfuncdesc} |
| 1989 | |
| 1990 | |
| 1991 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2} |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1992 | Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on |
| 1993 | success, or \NULL{} on failure. The operation is done \emph{in-place} |
| 1994 | when \var{o1} supports it. This is the equivalent of the Python |
| 1995 | statement \samp{\var{o1} \textasciicircum= \var{o2}}. |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 1996 | \end{cfuncdesc} |
| 1997 | |
| 1998 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2} |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 1999 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or |
| 2000 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 2001 | \var{o1} supports it. This is the equivalent of the Python statement |
| 2002 | \samp{\var{o1} |= \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2003 | \end{cfuncdesc} |
| 2004 | |
Fred Drake | c0e6c5b | 2000-09-22 18:17:49 +0000 | [diff] [blame] | 2005 | \begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2006 | This function takes the addresses of two variables of type |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2007 | \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}} and |
| 2008 | \code{*\var{p2}} have the same type, increment their reference count |
| 2009 | and return \code{0} (success). If the objects can be converted to a |
| 2010 | common numeric type, replace \code{*p1} and \code{*p2} by their |
| 2011 | converted value (with 'new' reference counts), and return \code{0}. |
| 2012 | If no conversion is possible, or if some other error occurs, return |
| 2013 | \code{-1} (failure) and don't increment the reference counts. The |
| 2014 | call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 2015 | statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}. |
| 2016 | \bifuncindex{coerce} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2017 | \end{cfuncdesc} |
| 2018 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2019 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2020 | 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] | 2021 | \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2022 | expression \samp{int(\var{o})}.\bifuncindex{int} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2023 | \end{cfuncdesc} |
| 2024 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2025 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2026 | 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] | 2027 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2028 | expression \samp{long(\var{o})}.\bifuncindex{long} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2029 | \end{cfuncdesc} |
| 2030 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2031 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2032 | Returns the \var{o} converted to a float object on success, or |
| 2033 | \NULL{} on failure. This is the equivalent of the Python expression |
| 2034 | \samp{float(\var{o})}.\bifuncindex{float} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2035 | \end{cfuncdesc} |
| 2036 | |
| 2037 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2038 | \section{Sequence Protocol \label{sequence}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2039 | |
| 2040 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2041 | Return \code{1} if the object provides sequence protocol, and |
| 2042 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2043 | \end{cfuncdesc} |
| 2044 | |
Fred Drake | c6a3cb4 | 2001-04-04 01:25:17 +0000 | [diff] [blame] | 2045 | \begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2046 | Returns the number of objects in sequence \var{o} on success, and |
| 2047 | \code{-1} on failure. For objects that do not provide sequence |
| 2048 | protocol, this is equivalent to the Python expression |
| 2049 | \samp{len(\var{o})}.\bifuncindex{len} |
| 2050 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2051 | |
Fred Drake | c6a3cb4 | 2001-04-04 01:25:17 +0000 | [diff] [blame] | 2052 | \begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o} |
| 2053 | Alternate name for \cfunction{PySequence_Size()}. |
| 2054 | \end{cfuncdesc} |
| 2055 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2056 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2057 | 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] | 2058 | failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2059 | expression \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2060 | \end{cfuncdesc} |
| 2061 | |
| 2062 | |
| 2063 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2064 | Return the result of repeating sequence object |
| 2065 | \var{o} \var{count} times, or \NULL{} on failure. This is the |
| 2066 | equivalent of the Python expression \samp{\var{o} * \var{count}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2067 | \end{cfuncdesc} |
| 2068 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 2069 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1, |
| 2070 | PyObject *o2} |
Fred Drake | 7740a01 | 2000-09-12 20:27:05 +0000 | [diff] [blame] | 2071 | Return the concatenation of \var{o1} and \var{o2} on success, and \NULL{} on |
| 2072 | failure. The operation is done \emph{in-place} when \var{o1} supports it. |
| 2073 | This is the equivalent of the Python expression \samp{\var{o1} += \var{o2}}. |
| 2074 | \end{cfuncdesc} |
| 2075 | |
| 2076 | |
| 2077 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count} |
| 2078 | Return the result of repeating sequence object \var{o} \var{count} times, or |
| 2079 | \NULL{} on failure. The operation is done \emph{in-place} when \var{o} |
| 2080 | supports it. This is the equivalent of the Python expression \samp{\var{o} |
| 2081 | *= \var{count}}. |
| 2082 | \end{cfuncdesc} |
| 2083 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2084 | |
| 2085 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2086 | Return the \var{i}th element of \var{o}, or \NULL{} on failure. This |
| 2087 | 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] | 2088 | \end{cfuncdesc} |
| 2089 | |
| 2090 | |
| 2091 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2092 | Return the slice of sequence object \var{o} between \var{i1} and |
| 2093 | \var{i2}, or \NULL{} on failure. This is the equivalent of the Python |
| 2094 | expression \samp{\var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2095 | \end{cfuncdesc} |
| 2096 | |
| 2097 | |
| 2098 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2099 | Assign object \var{v} to the \var{i}th element of \var{o}. |
| 2100 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 2101 | statement \samp{\var{o}[\var{i}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2102 | \end{cfuncdesc} |
| 2103 | |
| 2104 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
Fred Drake | 5566c1c | 2001-01-19 22:48:33 +0000 | [diff] [blame] | 2105 | Delete the \var{i}th element of object \var{o}. Returns |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2106 | \code{-1} on failure. This is the equivalent of the Python |
| 2107 | statement \samp{del \var{o}[\var{i}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2108 | \end{cfuncdesc} |
| 2109 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2110 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, |
| 2111 | int i2, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2112 | Assign the sequence object \var{v} to the slice in sequence |
| 2113 | object \var{o} from \var{i1} to \var{i2}. This is the equivalent of |
| 2114 | 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] | 2115 | \end{cfuncdesc} |
| 2116 | |
| 2117 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2118 | Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}. |
| 2119 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 2120 | statement \samp{del \var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2121 | \end{cfuncdesc} |
| 2122 | |
| 2123 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2124 | 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] | 2125 | This is equivalent to the Python expression \samp{tuple(\var{o})}. |
| 2126 | \bifuncindex{tuple} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2127 | \end{cfuncdesc} |
| 2128 | |
| 2129 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2130 | Return the number of occurrences of \var{value} in \var{o}, that is, |
| 2131 | return the number of keys for which \code{\var{o}[\var{key}] == |
| 2132 | \var{value}}. On failure, return \code{-1}. This is equivalent to |
| 2133 | the Python expression \samp{\var{o}.count(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2134 | \end{cfuncdesc} |
| 2135 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2136 | \begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2137 | Determine if \var{o} contains \var{value}. If an item in \var{o} is |
| 2138 | equal to \var{value}, return \code{1}, otherwise return \code{0}. On |
| 2139 | error, return \code{-1}. This is equivalent to the Python expression |
| 2140 | \samp{\var{value} in \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2141 | \end{cfuncdesc} |
| 2142 | |
| 2143 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2144 | Return the first index \var{i} for which \code{\var{o}[\var{i}] == |
| 2145 | \var{value}}. On error, return \code{-1}. This is equivalent to |
| 2146 | the Python expression \samp{\var{o}.index(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2147 | \end{cfuncdesc} |
| 2148 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 2149 | \begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o} |
| 2150 | Return a list object with the same contents as the arbitrary sequence |
| 2151 | \var{o}. The returned list is guaranteed to be new. |
| 2152 | \end{cfuncdesc} |
| 2153 | |
| 2154 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 2155 | Return a tuple object with the same contents as the arbitrary sequence |
| 2156 | \var{o}. If \var{o} is a tuple, a new reference will be returned, |
| 2157 | otherwise a tuple will be constructed with the appropriate contents. |
| 2158 | \end{cfuncdesc} |
| 2159 | |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2160 | |
Fred Drake | 81cccb7 | 2000-09-12 15:22:05 +0000 | [diff] [blame] | 2161 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m} |
| 2162 | Returns the sequence \var{o} as a tuple, unless it is already a |
| 2163 | tuple or list, in which case \var{o} is returned. Use |
| 2164 | \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the |
| 2165 | result. Returns \NULL{} on failure. If the object is not a sequence, |
| 2166 | raises \exception{TypeError} with \var{m} as the message text. |
| 2167 | \end{cfuncdesc} |
| 2168 | |
| 2169 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i} |
| 2170 | Return the \var{i}th element of \var{o}, assuming that \var{o} was |
| 2171 | returned by \cfunction{PySequence_Fast()}, and that \var{i} is within |
| 2172 | bounds. The caller is expected to get the length of the sequence by |
Fred Drake | 96a2a80 | 2001-05-29 18:51:41 +0000 | [diff] [blame] | 2173 | calling \cfunction{PySequence_Size()} on \var{o}, since lists and tuples |
Fred Drake | 81cccb7 | 2000-09-12 15:22:05 +0000 | [diff] [blame] | 2174 | are guaranteed to always return their true length. |
| 2175 | \end{cfuncdesc} |
| 2176 | |
| 2177 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2178 | \section{Mapping Protocol \label{mapping}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2179 | |
| 2180 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2181 | Return \code{1} if the object provides mapping protocol, and |
| 2182 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2183 | \end{cfuncdesc} |
| 2184 | |
| 2185 | |
| 2186 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2187 | Returns the number of keys in object \var{o} on success, and |
| 2188 | \code{-1} on failure. For objects that do not provide mapping |
| 2189 | protocol, this is equivalent to the Python expression |
| 2190 | \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2191 | \end{cfuncdesc} |
| 2192 | |
| 2193 | |
| 2194 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2195 | Remove the mapping for object \var{key} from the object \var{o}. |
| 2196 | Return \code{-1} on failure. This is equivalent to |
| 2197 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2198 | \end{cfuncdesc} |
| 2199 | |
| 2200 | |
| 2201 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2202 | Remove the mapping for object \var{key} from the object \var{o}. |
| 2203 | Return \code{-1} on failure. This is equivalent to |
| 2204 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2205 | \end{cfuncdesc} |
| 2206 | |
| 2207 | |
| 2208 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2209 | On success, return \code{1} if the mapping object has the key |
| 2210 | \var{key} and \code{0} otherwise. This is equivalent to the Python |
| 2211 | expression \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2212 | This function always succeeds. |
| 2213 | \end{cfuncdesc} |
| 2214 | |
| 2215 | |
| 2216 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2217 | Return \code{1} if the mapping object has the key \var{key} and |
| 2218 | \code{0} otherwise. This is equivalent to the Python expression |
| 2219 | \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2220 | This function always succeeds. |
| 2221 | \end{cfuncdesc} |
| 2222 | |
| 2223 | |
| 2224 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2225 | 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] | 2226 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2227 | expression \samp{\var{o}.keys()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2228 | \end{cfuncdesc} |
| 2229 | |
| 2230 | |
| 2231 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2232 | 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] | 2233 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2234 | expression \samp{\var{o}.values()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2235 | \end{cfuncdesc} |
| 2236 | |
| 2237 | |
| 2238 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2239 | 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] | 2240 | each item is a tuple containing a key-value pair. On |
| 2241 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2242 | expression \samp{\var{o}.items()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2243 | \end{cfuncdesc} |
| 2244 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2245 | |
| 2246 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2247 | Return element of \var{o} corresponding to the object \var{key} or |
| 2248 | \NULL{} on failure. This is the equivalent of the Python expression |
| 2249 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2250 | \end{cfuncdesc} |
| 2251 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2252 | \begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, |
| 2253 | PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2254 | Map the object \var{key} to the value \var{v} in object \var{o}. |
| 2255 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 2256 | statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2257 | \end{cfuncdesc} |
| 2258 | |
| 2259 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2260 | \section{Iterator Protocol \label{iterator}} |
| 2261 | |
Fred Drake | a8e0827 | 2001-05-07 17:47:07 +0000 | [diff] [blame] | 2262 | \versionadded{2.2} |
| 2263 | |
Fred Drake | dbcaeda | 2001-05-07 17:42:18 +0000 | [diff] [blame] | 2264 | There are only a couple of functions specifically for working with |
| 2265 | iterators. |
| 2266 | |
| 2267 | \begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o} |
| 2268 | Return true if the object \var{o} supports the iterator protocol. |
| 2269 | \end{cfuncdesc} |
| 2270 | |
| 2271 | \begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o} |
| 2272 | Return the next value from the iteration \var{o}. If the object is |
| 2273 | an iterator, this retrieves the next value from the iteration, and |
| 2274 | returns \NULL{} with no exception set if there are no remaining |
| 2275 | items. If the object is not an iterator, \exception{TypeError} is |
| 2276 | raised, or if there is an error in retrieving the item, returns |
| 2277 | \NULL{} and passes along the exception. |
| 2278 | \end{cfuncdesc} |
| 2279 | |
| 2280 | To write a loop which iterates over an iterator, the C code should |
| 2281 | look something like this: |
| 2282 | |
| 2283 | \begin{verbatim} |
| 2284 | PyObject *iterator = ...; |
| 2285 | PyObject *item; |
| 2286 | |
| 2287 | while (item = PyIter_Next(iter)) { |
| 2288 | /* do something with item */ |
| 2289 | } |
| 2290 | if (PyErr_Occurred()) { |
| 2291 | /* propogate error */ |
| 2292 | } |
| 2293 | else { |
| 2294 | /* continue doing useful work */ |
| 2295 | } |
| 2296 | \end{verbatim} |
| 2297 | |
| 2298 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2299 | \chapter{Concrete Objects Layer \label{concrete}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2300 | |
| 2301 | The functions in this chapter are specific to certain Python object |
| 2302 | types. Passing them an object of the wrong type is not a good idea; |
| 2303 | if you receive an object from a Python program and you are not sure |
| 2304 | 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] | 2305 | for example, to check that an object is a dictionary, use |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2306 | \cfunction{PyDict_Check()}. The chapter is structured like the |
| 2307 | ``family tree'' of Python object types. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2308 | |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2309 | \strong{Warning:} |
| 2310 | While the functions described in this chapter carefully check the type |
| 2311 | of the objects which are passed in, many of them do not check for |
| 2312 | \NULL{} being passed instead of a valid object. Allowing \NULL{} to |
| 2313 | be passed in can cause memory access violations and immediate |
| 2314 | termination of the interpreter. |
| 2315 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2316 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2317 | \section{Fundamental Objects \label{fundamental}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2318 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2319 | This section describes Python type objects and the singleton object |
| 2320 | \code{None}. |
| 2321 | |
| 2322 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2323 | \subsection{Type Objects \label{typeObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2324 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2325 | \obindex{type} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2326 | \begin{ctypedesc}{PyTypeObject} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2327 | The C structure of the objects used to describe built-in types. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2328 | \end{ctypedesc} |
| 2329 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2330 | \begin{cvardesc}{PyObject*}{PyType_Type} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2331 | This is the type object for type objects; it is the same object as |
| 2332 | \code{types.TypeType} in the Python layer. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2333 | \withsubitem{(in module types)}{\ttindex{TypeType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2334 | \end{cvardesc} |
| 2335 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2336 | \begin{cfuncdesc}{int}{PyType_Check}{PyObject *o} |
| 2337 | Returns true is the object \var{o} is a type object. |
| 2338 | \end{cfuncdesc} |
| 2339 | |
| 2340 | \begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature} |
| 2341 | Returns true if the type object \var{o} sets the feature |
Fred Drake | f0e08ef | 2001-02-03 01:11:26 +0000 | [diff] [blame] | 2342 | \var{feature}. Type features are denoted by single bit flags. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2343 | \end{cfuncdesc} |
| 2344 | |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 2345 | \begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b} |
| 2346 | Returns true if \var{a} is a subtype of \var{b}. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 2347 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 2348 | \end{cfuncdesc} |
| 2349 | |
| 2350 | \begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type, |
| 2351 | int nitems} |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 2352 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 2353 | \end{cfuncdesc} |
| 2354 | |
| 2355 | \begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type, |
| 2356 | PyObject *args, PyObject *kwds} |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 2357 | \versionadded{2.2} |
| 2358 | \end{cfuncdesc} |
| 2359 | |
| 2360 | \begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type} |
| 2361 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 2362 | \end{cfuncdesc} |
| 2363 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2364 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2365 | \subsection{The None Object \label{noneObject}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2366 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2367 | \obindex{None@\texttt{None}} |
| 2368 | Note that the \ctype{PyTypeObject} for \code{None} is not directly |
| 2369 | exposed in the Python/C API. Since \code{None} is a singleton, |
| 2370 | testing for object identity (using \samp{==} in C) is sufficient. |
| 2371 | There is no \cfunction{PyNone_Check()} function for the same reason. |
| 2372 | |
| 2373 | \begin{cvardesc}{PyObject*}{Py_None} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2374 | The Python \code{None} object, denoting lack of value. This object has |
| 2375 | no methods. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2376 | \end{cvardesc} |
| 2377 | |
| 2378 | |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2379 | \section{Numeric Objects \label{numericObjects}} |
| 2380 | |
| 2381 | \obindex{numeric} |
| 2382 | |
| 2383 | |
| 2384 | \subsection{Plain Integer Objects \label{intObjects}} |
| 2385 | |
| 2386 | \obindex{integer} |
| 2387 | \begin{ctypedesc}{PyIntObject} |
| 2388 | This subtype of \ctype{PyObject} represents a Python integer object. |
| 2389 | \end{ctypedesc} |
| 2390 | |
| 2391 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} |
| 2392 | This instance of \ctype{PyTypeObject} represents the Python plain |
| 2393 | integer type. This is the same object as \code{types.IntType}. |
| 2394 | \withsubitem{(in modules types)}{\ttindex{IntType}} |
| 2395 | \end{cvardesc} |
| 2396 | |
| 2397 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2398 | Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype of |
| 2399 | \cdata{PyInt_Type}. |
| 2400 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2401 | \end{cfuncdesc} |
| 2402 | |
| 2403 | \begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o} |
| 2404 | Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a |
| 2405 | subtype of \cdata{PyInt_Type}. |
| 2406 | \versionadded{2.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2407 | \end{cfuncdesc} |
| 2408 | |
| 2409 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival} |
| 2410 | Creates a new integer object with a value of \var{ival}. |
| 2411 | |
| 2412 | The current implementation keeps an array of integer objects for all |
| 2413 | integers between \code{-1} and \code{100}, when you create an int in |
| 2414 | that range you actually just get back a reference to the existing |
| 2415 | object. So it should be possible to change the value of \code{1}. I |
| 2416 | suspect the behaviour of Python in this case is undefined. :-) |
| 2417 | \end{cfuncdesc} |
| 2418 | |
| 2419 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} |
| 2420 | Will first attempt to cast the object to a \ctype{PyIntObject}, if |
| 2421 | it is not already one, and then return its value. |
| 2422 | \end{cfuncdesc} |
| 2423 | |
| 2424 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io} |
| 2425 | Returns the value of the object \var{io}. No error checking is |
| 2426 | performed. |
| 2427 | \end{cfuncdesc} |
| 2428 | |
| 2429 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} |
| 2430 | Returns the system's idea of the largest integer it can handle |
| 2431 | (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system |
| 2432 | header files). |
| 2433 | \end{cfuncdesc} |
| 2434 | |
| 2435 | |
| 2436 | \subsection{Long Integer Objects \label{longObjects}} |
| 2437 | |
| 2438 | \obindex{long integer} |
| 2439 | \begin{ctypedesc}{PyLongObject} |
| 2440 | This subtype of \ctype{PyObject} represents a Python long integer |
| 2441 | object. |
| 2442 | \end{ctypedesc} |
| 2443 | |
| 2444 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} |
| 2445 | This instance of \ctype{PyTypeObject} represents the Python long |
| 2446 | integer type. This is the same object as \code{types.LongType}. |
| 2447 | \withsubitem{(in modules types)}{\ttindex{LongType}} |
| 2448 | \end{cvardesc} |
| 2449 | |
| 2450 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2451 | Returns true if its argument is a \ctype{PyLongObject} or a subtype of |
| 2452 | \ctype{PyLongObject}. |
| 2453 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2454 | \end{cfuncdesc} |
| 2455 | |
| 2456 | \begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p} |
| 2457 | Returns true if its argument is a \ctype{PyLongObject}, but not a |
| 2458 | subtype of \ctype{PyLongObject}. |
| 2459 | \versionadded{2.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2460 | \end{cfuncdesc} |
| 2461 | |
| 2462 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
| 2463 | Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{} on |
| 2464 | failure. |
| 2465 | \end{cfuncdesc} |
| 2466 | |
| 2467 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v} |
| 2468 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned |
| 2469 | long}, or \NULL{} on failure. |
| 2470 | \end{cfuncdesc} |
| 2471 | |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2472 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v} |
| 2473 | Returns a new \ctype{PyLongObject} object from a C \ctype{long long}, |
| 2474 | or \NULL{} on failure. |
| 2475 | \end{cfuncdesc} |
| 2476 | |
| 2477 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v} |
| 2478 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned |
| 2479 | long long}, or \NULL{} on failure. |
| 2480 | \end{cfuncdesc} |
| 2481 | |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2482 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
| 2483 | Returns a new \ctype{PyLongObject} object from the integer part of |
| 2484 | \var{v}, or \NULL{} on failure. |
| 2485 | \end{cfuncdesc} |
| 2486 | |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2487 | \begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend, |
| 2488 | int base} |
| 2489 | Return a new \ctype{PyLongObject} based on the string value in |
| 2490 | \var{str}, which is interpreted according to the radix in \var{base}. |
| 2491 | If \var{pend} is non-\NULL, \code{*\var{pend}} will point to the first |
| 2492 | character in \var{str} which follows the representation of the |
| 2493 | number. If \var{base} is \code{0}, the radix will be determined base |
| 2494 | on the leading characters of \var{str}: if \var{str} starts with |
| 2495 | \code{'0x'} or \code{'0X'}, radix 16 will be used; if \var{str} starts |
| 2496 | with \code{'0'}, radix 8 will be used; otherwise radix 10 will be |
| 2497 | used. If \var{base} is not \code{0}, it must be between \code{2} and |
| 2498 | \code{36}, inclusive. Leading spaces are ignored. If there are no |
| 2499 | digits, \exception{ValueError} will be raised. |
| 2500 | \end{cfuncdesc} |
| 2501 | |
| 2502 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u, |
| 2503 | int length, int base} |
| 2504 | Convert a sequence of Unicode digits to a Python long integer value. |
| 2505 | The first parameter, \var{u}, points to the first character of the |
| 2506 | Unicode string, \var{length} gives the number of characters, and |
| 2507 | \var{base} is the radix for the conversion. The radix must be in the |
| 2508 | range [2, 36]; if it is out of range, \exception{ValueError} will be |
| 2509 | raised. |
| 2510 | \versionadded{1.6} |
| 2511 | \end{cfuncdesc} |
| 2512 | |
| 2513 | \begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p} |
| 2514 | Create a Python integer or long integer from the pointer \var{p}. The |
| 2515 | pointer value can be retrieved from the resulting value using |
| 2516 | \cfunction{PyLong_AsVoidPtr()}. |
| 2517 | \versionadded{1.5.2} |
| 2518 | \end{cfuncdesc} |
| 2519 | |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2520 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong} |
| 2521 | Returns a C \ctype{long} representation of the contents of |
| 2522 | \var{pylong}. If \var{pylong} is greater than |
| 2523 | \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError} is |
| 2524 | raised.\withsubitem{(built-in exception)}{\ttindex{OverflowError}} |
| 2525 | \end{cfuncdesc} |
| 2526 | |
| 2527 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong} |
| 2528 | Returns a C \ctype{unsigned long} representation of the contents of |
| 2529 | \var{pylong}. If \var{pylong} is greater than |
| 2530 | \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an \exception{OverflowError} |
| 2531 | is raised.\withsubitem{(built-in exception)}{\ttindex{OverflowError}} |
| 2532 | \end{cfuncdesc} |
| 2533 | |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2534 | \begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong} |
| 2535 | Return a C \ctype{long long} from a Python long integer. If |
| 2536 | \var{pylong} cannot be represented as a \ctype{long long}, an |
| 2537 | \exception{OverflowError} will be raised. |
| 2538 | \versionadded{2.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2539 | \end{cfuncdesc} |
| 2540 | |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2541 | \begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject |
| 2542 | *pylong} |
| 2543 | Return a C \ctype{unsigned long long} from a Python long integer. If |
| 2544 | \var{pylong} cannot be represented as an \ctype{unsigned long long}, |
| 2545 | an \exception{OverflowError} will be raised if the value is positive, |
| 2546 | or a \exception{TypeError} will be raised if the value is negative. |
| 2547 | \versionadded{2.2} |
| 2548 | \end{cfuncdesc} |
| 2549 | |
| 2550 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong} |
| 2551 | Returns a C \ctype{double} representation of the contents of |
| 2552 | \var{pylong}. If \var{pylong} cannot be approximately represented as |
| 2553 | a \ctype{double}, an \exception{OverflowError} exception is raised and |
| 2554 | \code{-1.0} will be returned. |
| 2555 | \end{cfuncdesc} |
| 2556 | |
| 2557 | \begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong} |
| 2558 | Convert a Python integer or long integer \var{pylong} to a C |
| 2559 | \ctype{void} pointer. If \var{pylong} cannot be converted, an |
| 2560 | \exception{OverflowError} will be raised. This is only assured to |
| 2561 | produce a usable \ctype{void} pointer for values created with |
| 2562 | \cfunction{PyLong_FromVoidPtr()}. |
| 2563 | \versionadded{1.5.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2564 | \end{cfuncdesc} |
| 2565 | |
| 2566 | |
| 2567 | \subsection{Floating Point Objects \label{floatObjects}} |
| 2568 | |
| 2569 | \obindex{floating point} |
| 2570 | \begin{ctypedesc}{PyFloatObject} |
| 2571 | This subtype of \ctype{PyObject} represents a Python floating point |
| 2572 | object. |
| 2573 | \end{ctypedesc} |
| 2574 | |
| 2575 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} |
| 2576 | This instance of \ctype{PyTypeObject} represents the Python floating |
| 2577 | point type. This is the same object as \code{types.FloatType}. |
| 2578 | \withsubitem{(in modules types)}{\ttindex{FloatType}} |
| 2579 | \end{cvardesc} |
| 2580 | |
| 2581 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2582 | Returns true if its argument is a \ctype{PyFloatObject} or a subtype |
| 2583 | of \ctype{PyFloatObject}. |
| 2584 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2585 | \end{cfuncdesc} |
| 2586 | |
| 2587 | \begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p} |
| 2588 | Returns true if its argument is a \ctype{PyFloatObject}, but not a |
| 2589 | subtype of \ctype{PyFloatObject}. |
| 2590 | \versionadded{2.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2591 | \end{cfuncdesc} |
| 2592 | |
| 2593 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
| 2594 | Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on |
| 2595 | failure. |
| 2596 | \end{cfuncdesc} |
| 2597 | |
| 2598 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat} |
| 2599 | Returns a C \ctype{double} representation of the contents of \var{pyfloat}. |
| 2600 | \end{cfuncdesc} |
| 2601 | |
| 2602 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat} |
| 2603 | Returns a C \ctype{double} representation of the contents of |
| 2604 | \var{pyfloat}, but without error checking. |
| 2605 | \end{cfuncdesc} |
| 2606 | |
| 2607 | |
| 2608 | \subsection{Complex Number Objects \label{complexObjects}} |
| 2609 | |
| 2610 | \obindex{complex number} |
| 2611 | Python's complex number objects are implemented as two distinct types |
| 2612 | when viewed from the C API: one is the Python object exposed to |
| 2613 | Python programs, and the other is a C structure which represents the |
| 2614 | actual complex number value. The API provides functions for working |
| 2615 | with both. |
| 2616 | |
| 2617 | \subsubsection{Complex Numbers as C Structures} |
| 2618 | |
| 2619 | Note that the functions which accept these structures as parameters |
| 2620 | and return them as results do so \emph{by value} rather than |
| 2621 | dereferencing them through pointers. This is consistent throughout |
| 2622 | the API. |
| 2623 | |
| 2624 | \begin{ctypedesc}{Py_complex} |
| 2625 | The C structure which corresponds to the value portion of a Python |
| 2626 | complex number object. Most of the functions for dealing with complex |
| 2627 | number objects use structures of this type as input or output values, |
| 2628 | as appropriate. It is defined as: |
| 2629 | |
| 2630 | \begin{verbatim} |
| 2631 | typedef struct { |
| 2632 | double real; |
| 2633 | double imag; |
| 2634 | } Py_complex; |
| 2635 | \end{verbatim} |
| 2636 | \end{ctypedesc} |
| 2637 | |
| 2638 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right} |
| 2639 | Return the sum of two complex numbers, using the C |
| 2640 | \ctype{Py_complex} representation. |
| 2641 | \end{cfuncdesc} |
| 2642 | |
| 2643 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right} |
| 2644 | Return the difference between two complex numbers, using the C |
| 2645 | \ctype{Py_complex} representation. |
| 2646 | \end{cfuncdesc} |
| 2647 | |
| 2648 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex} |
| 2649 | Return the negation of the complex number \var{complex}, using the C |
| 2650 | \ctype{Py_complex} representation. |
| 2651 | \end{cfuncdesc} |
| 2652 | |
| 2653 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right} |
| 2654 | Return the product of two complex numbers, using the C |
| 2655 | \ctype{Py_complex} representation. |
| 2656 | \end{cfuncdesc} |
| 2657 | |
| 2658 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend, |
| 2659 | Py_complex divisor} |
| 2660 | Return the quotient of two complex numbers, using the C |
| 2661 | \ctype{Py_complex} representation. |
| 2662 | \end{cfuncdesc} |
| 2663 | |
| 2664 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp} |
| 2665 | Return the exponentiation of \var{num} by \var{exp}, using the C |
| 2666 | \ctype{Py_complex} representation. |
| 2667 | \end{cfuncdesc} |
| 2668 | |
| 2669 | |
| 2670 | \subsubsection{Complex Numbers as Python Objects} |
| 2671 | |
| 2672 | \begin{ctypedesc}{PyComplexObject} |
| 2673 | This subtype of \ctype{PyObject} represents a Python complex number object. |
| 2674 | \end{ctypedesc} |
| 2675 | |
| 2676 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} |
| 2677 | This instance of \ctype{PyTypeObject} represents the Python complex |
| 2678 | number type. |
| 2679 | \end{cvardesc} |
| 2680 | |
| 2681 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2682 | Returns true if its argument is a \ctype{PyComplexObject} or a subtype |
| 2683 | of \ctype{PyComplexObject}. |
| 2684 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2685 | \end{cfuncdesc} |
| 2686 | |
| 2687 | \begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p} |
| 2688 | Returns true if its argument is a \ctype{PyComplexObject}, but not a |
| 2689 | subtype of \ctype{PyComplexObject}. |
| 2690 | \versionadded{2.2} |
Fred Drake | fa77487 | 2001-07-11 20:35:37 +0000 | [diff] [blame] | 2691 | \end{cfuncdesc} |
| 2692 | |
| 2693 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v} |
| 2694 | Create a new Python complex number object from a C |
| 2695 | \ctype{Py_complex} value. |
| 2696 | \end{cfuncdesc} |
| 2697 | |
| 2698 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag} |
| 2699 | Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}. |
| 2700 | \end{cfuncdesc} |
| 2701 | |
| 2702 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} |
| 2703 | Returns the real part of \var{op} as a C \ctype{double}. |
| 2704 | \end{cfuncdesc} |
| 2705 | |
| 2706 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} |
| 2707 | Returns the imaginary part of \var{op} as a C \ctype{double}. |
| 2708 | \end{cfuncdesc} |
| 2709 | |
| 2710 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} |
| 2711 | Returns the \ctype{Py_complex} value of the complex number \var{op}. |
| 2712 | \end{cfuncdesc} |
| 2713 | |
| 2714 | |
| 2715 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2716 | \section{Sequence Objects \label{sequenceObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2717 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2718 | \obindex{sequence} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2719 | Generic operations on sequence objects were discussed in the previous |
| 2720 | chapter; this section deals with the specific kinds of sequence |
| 2721 | objects that are intrinsic to the Python language. |
| 2722 | |
| 2723 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2724 | \subsection{String Objects \label{stringObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2725 | |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2726 | These functions raise \exception{TypeError} when expecting a string |
| 2727 | parameter and are called with a non-string parameter. |
| 2728 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2729 | \obindex{string} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2730 | \begin{ctypedesc}{PyStringObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2731 | This subtype of \ctype{PyObject} represents a Python string object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2732 | \end{ctypedesc} |
| 2733 | |
| 2734 | \begin{cvardesc}{PyTypeObject}{PyString_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2735 | This instance of \ctype{PyTypeObject} represents the Python string |
| 2736 | type; it is the same object as \code{types.TypeType} in the Python |
| 2737 | layer.\withsubitem{(in module types)}{\ttindex{StringType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2738 | \end{cvardesc} |
| 2739 | |
| 2740 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2741 | Returns true if the object \var{o} is a string object or an instance |
| 2742 | of a subtype of the string type. |
| 2743 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2744 | \end{cfuncdesc} |
| 2745 | |
| 2746 | \begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o} |
| 2747 | Returns true if the object \var{o} is a string object, but not an |
| 2748 | instance of a subtype of the string type. |
| 2749 | \versionadded{2.2} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 2750 | \end{cfuncdesc} |
| 2751 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2752 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2753 | Returns a new string object with the value \var{v} on success, and |
| 2754 | \NULL{} on failure. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2755 | \end{cfuncdesc} |
| 2756 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2757 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v, |
| 2758 | int len} |
| 2759 | Returns a new string object with the value \var{v} and length |
| 2760 | \var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{}, |
| 2761 | the contents of the string are uninitialized. |
| 2762 | \end{cfuncdesc} |
| 2763 | |
Barry Warsaw | c86aa57 | 2001-08-28 02:31:28 +0000 | [diff] [blame] | 2764 | \begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...} |
| 2765 | Takes a C \code{printf}-style \var{format} string and a variable |
| 2766 | number of arguments, calculates the size of the resulting Python |
| 2767 | string and returns a string with the values formatted into it. The |
| 2768 | variable arguments must be C types and must correspond exactly to the |
| 2769 | format characters in the \var{format} string. The following format |
| 2770 | characters are allowed: |
| 2771 | \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment} |
| 2772 | \lineiii{\%\%}{\emph{n/a}}{The literal \% character.} |
| 2773 | \lineiii{\%c}{int}{A single character, represented as an C int.} |
| 2774 | \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.} |
| 2775 | \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.} |
| 2776 | \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.} |
| 2777 | \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.} |
| 2778 | \lineiii{\%s}{char*}{A null-terminated C character array.} |
| 2779 | \lineiii{\%p}{void*}{The hex representation of a C pointer. |
| 2780 | Mostly equivalent to \code{printf("\%p")} except that it is |
| 2781 | guaranteed to start with the literal \code{0x} regardless of |
| 2782 | what the platform's \code{printf} yields.} |
| 2783 | \end{tableiii} |
| 2784 | \end{cfuncdesc} |
| 2785 | |
| 2786 | \begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format, |
| 2787 | va_list vargs} |
| 2788 | Identical to \function{PyString_FromFormat()} except that it takes |
| 2789 | exactly two arguments. |
| 2790 | \end{cfuncdesc} |
| 2791 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2792 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2793 | Returns the length of the string in string object \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2794 | \end{cfuncdesc} |
| 2795 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2796 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string} |
Fred Drake | 5d64421 | 2000-10-07 12:31:50 +0000 | [diff] [blame] | 2797 | Macro form of \cfunction{PyString_Size()} but without error |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2798 | checking. |
| 2799 | \end{cfuncdesc} |
| 2800 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2801 | \begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2802 | Returns a null-terminated representation of the contents of |
| 2803 | \var{string}. The pointer refers to the internal buffer of |
Fred Drake | 8902442 | 2000-10-23 16:00:54 +0000 | [diff] [blame] | 2804 | \var{string}, not a copy. The data must not be modified in any way, |
| 2805 | unless the string was just created using |
| 2806 | \code{PyString_FromStringAndSize(NULL, \var{size})}. |
| 2807 | It must not be deallocated. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2808 | \end{cfuncdesc} |
| 2809 | |
| 2810 | \begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string} |
| 2811 | Macro form of \cfunction{PyString_AsString()} but without error |
| 2812 | checking. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2813 | \end{cfuncdesc} |
| 2814 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 2815 | \begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj, |
| 2816 | char **buffer, |
| 2817 | int *length} |
| 2818 | Returns a null-terminated representation of the contents of the object |
| 2819 | \var{obj} through the output variables \var{buffer} and \var{length}. |
| 2820 | |
| 2821 | The function accepts both string and Unicode objects as input. For |
| 2822 | Unicode objects it returns the default encoded version of the object. |
| 2823 | If \var{length} is set to \NULL{}, the resulting buffer may not contain |
| 2824 | null characters; if it does, the function returns -1 and a |
| 2825 | TypeError is raised. |
| 2826 | |
| 2827 | 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] | 2828 | copy. The data must not be modified in any way, unless the string was |
| 2829 | just created using \code{PyString_FromStringAndSize(NULL, |
| 2830 | \var{size})}. It must not be deallocated. |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 2831 | \end{cfuncdesc} |
| 2832 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2833 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string, |
| 2834 | PyObject *newpart} |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 2835 | Creates a new string object in \var{*string} containing the |
Fred Drake | ddc6c27 | 2000-03-31 18:22:38 +0000 | [diff] [blame] | 2836 | contents of \var{newpart} appended to \var{string}; the caller will |
| 2837 | own the new reference. The reference to the old value of \var{string} |
| 2838 | will be stolen. If the new string |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 2839 | cannot be created, the old reference to \var{string} will still be |
| 2840 | discarded and the value of \var{*string} will be set to |
| 2841 | \NULL{}; the appropriate exception will be set. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2842 | \end{cfuncdesc} |
| 2843 | |
| 2844 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string, |
| 2845 | PyObject *newpart} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2846 | Creates a new string object in \var{*string} containing the contents |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2847 | of \var{newpart} appended to \var{string}. This version decrements |
| 2848 | the reference count of \var{newpart}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2849 | \end{cfuncdesc} |
| 2850 | |
| 2851 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2852 | A way to resize a string object even though it is ``immutable''. |
| 2853 | Only use this to build up a brand new string object; don't use this if |
| 2854 | the string may already be known in other parts of the code. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2855 | \end{cfuncdesc} |
| 2856 | |
| 2857 | \begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format, |
| 2858 | PyObject *args} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2859 | Returns a new string object from \var{format} and \var{args}. Analogous |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2860 | 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] | 2861 | a tuple. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2862 | \end{cfuncdesc} |
| 2863 | |
| 2864 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2865 | Intern the argument \var{*string} in place. The argument must be the |
| 2866 | address of a pointer variable pointing to a Python string object. |
| 2867 | If there is an existing interned string that is the same as |
| 2868 | \var{*string}, it sets \var{*string} to it (decrementing the reference |
| 2869 | count of the old string object and incrementing the reference count of |
| 2870 | the interned string object), otherwise it leaves \var{*string} alone |
| 2871 | and interns it (incrementing its reference count). (Clarification: |
| 2872 | 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] | 2873 | this function as reference-count-neutral; you own the object after |
| 2874 | the call if and only if you owned it before the call.) |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2875 | \end{cfuncdesc} |
| 2876 | |
| 2877 | \begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2878 | A combination of \cfunction{PyString_FromString()} and |
| 2879 | \cfunction{PyString_InternInPlace()}, returning either a new string object |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2880 | that has been interned, or a new (``owned'') reference to an earlier |
| 2881 | interned string object with the same value. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2882 | \end{cfuncdesc} |
| 2883 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2884 | \begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s, |
| 2885 | int size, |
| 2886 | const char *encoding, |
| 2887 | const char *errors} |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2888 | Creates an object by decoding \var{size} bytes of the encoded |
| 2889 | buffer \var{s} using the codec registered |
| 2890 | for \var{encoding}. \var{encoding} and \var{errors} have the same meaning |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2891 | as the parameters of the same name in the unicode() builtin |
| 2892 | function. The codec to be used is looked up using the Python codec |
| 2893 | registry. Returns \NULL{} in case an exception was raised by the |
| 2894 | codec. |
| 2895 | \end{cfuncdesc} |
| 2896 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2897 | \begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str, |
| 2898 | const char *encoding, |
| 2899 | const char *errors} |
| 2900 | Decodes a string object by passing it to the codec registered |
| 2901 | for \var{encoding} and returns the result as Python |
| 2902 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 2903 | parameters of the same name in the string .encode() method. The codec |
| 2904 | to be used is looked up using the Python codec registry. Returns |
| 2905 | \NULL{} in case an exception was raised by the codec. |
| 2906 | \end{cfuncdesc} |
| 2907 | |
| 2908 | \begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s, |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2909 | int size, |
| 2910 | const char *encoding, |
| 2911 | const char *errors} |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2912 | Encodes the \ctype{char} buffer of the given size by passing it to |
| 2913 | the codec registered for \var{encoding} and returns a Python object. |
| 2914 | \var{encoding} and \var{errors} have the same |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2915 | meaning as the parameters of the same name in the string .encode() |
| 2916 | method. The codec to be used is looked up using the Python codec |
| 2917 | registry. Returns \NULL{} in case an exception was raised by the |
| 2918 | codec. |
| 2919 | \end{cfuncdesc} |
| 2920 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2921 | \begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str, |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2922 | const char *encoding, |
| 2923 | const char *errors} |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2924 | Encodes a string object using the codec registered |
| 2925 | for \var{encoding} and returns the result as Python |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2926 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 2927 | parameters of the same name in the string .encode() method. The codec |
| 2928 | to be used is looked up using the Python codec registry. Returns |
| 2929 | \NULL{} in case an exception was raised by the codec. |
| 2930 | \end{cfuncdesc} |
| 2931 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2932 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2933 | \subsection{Unicode Objects \label{unicodeObjects}} |
| 2934 | \sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com} |
| 2935 | |
| 2936 | %--- Unicode Type ------------------------------------------------------- |
| 2937 | |
| 2938 | These are the basic Unicode object types used for the Unicode |
| 2939 | implementation in Python: |
| 2940 | |
| 2941 | \begin{ctypedesc}{Py_UNICODE} |
| 2942 | This type represents a 16-bit unsigned storage type which is used by |
| 2943 | Python internally as basis for holding Unicode ordinals. On platforms |
| 2944 | where \ctype{wchar_t} is available and also has 16-bits, |
| 2945 | \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance |
| 2946 | native platform compatibility. On all other platforms, |
| 2947 | \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}. |
| 2948 | \end{ctypedesc} |
| 2949 | |
| 2950 | \begin{ctypedesc}{PyUnicodeObject} |
| 2951 | This subtype of \ctype{PyObject} represents a Python Unicode object. |
| 2952 | \end{ctypedesc} |
| 2953 | |
| 2954 | \begin{cvardesc}{PyTypeObject}{PyUnicode_Type} |
| 2955 | This instance of \ctype{PyTypeObject} represents the Python Unicode type. |
| 2956 | \end{cvardesc} |
| 2957 | |
| 2958 | %--- These are really C macros... is there a macrodesc TeX macro ? |
| 2959 | |
| 2960 | The following APIs are really C macros and can be used to do fast |
| 2961 | checks and to access internal read-only data of Unicode objects: |
| 2962 | |
| 2963 | \begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 2964 | Returns true if the object \var{o} is a Unicode object or an instance |
| 2965 | of a Unicode subtype. |
| 2966 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 2967 | \end{cfuncdesc} |
| 2968 | |
| 2969 | \begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o} |
| 2970 | Returns true if the object \var{o} is a Unicode object, but not an |
| 2971 | instance of a subtype. |
| 2972 | \versionadded{2.2} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2973 | \end{cfuncdesc} |
| 2974 | |
| 2975 | \begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o} |
| 2976 | Returns the size of the object. o has to be a |
| 2977 | PyUnicodeObject (not checked). |
| 2978 | \end{cfuncdesc} |
| 2979 | |
| 2980 | \begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o} |
| 2981 | Returns the size of the object's internal buffer in bytes. o has to be |
| 2982 | a PyUnicodeObject (not checked). |
| 2983 | \end{cfuncdesc} |
| 2984 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2985 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2986 | Returns a pointer to the internal Py_UNICODE buffer of the object. o |
| 2987 | has to be a PyUnicodeObject (not checked). |
| 2988 | \end{cfuncdesc} |
| 2989 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2990 | \begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2991 | Returns a (const char *) pointer to the internal buffer of the object. |
| 2992 | o has to be a PyUnicodeObject (not checked). |
| 2993 | \end{cfuncdesc} |
| 2994 | |
| 2995 | % --- Unicode character properties --------------------------------------- |
| 2996 | |
| 2997 | Unicode provides many different character properties. The most often |
| 2998 | needed ones are available through these macros which are mapped to C |
| 2999 | functions depending on the Python configuration. |
| 3000 | |
| 3001 | \begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch} |
| 3002 | Returns 1/0 depending on whether \var{ch} is a whitespace character. |
| 3003 | \end{cfuncdesc} |
| 3004 | |
| 3005 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch} |
| 3006 | Returns 1/0 depending on whether \var{ch} is a lowercase character. |
| 3007 | \end{cfuncdesc} |
| 3008 | |
| 3009 | \begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch} |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 3010 | Returns 1/0 depending on whether \var{ch} is an uppercase character. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3011 | \end{cfuncdesc} |
| 3012 | |
| 3013 | \begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch} |
| 3014 | Returns 1/0 depending on whether \var{ch} is a titlecase character. |
| 3015 | \end{cfuncdesc} |
| 3016 | |
| 3017 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch} |
| 3018 | Returns 1/0 depending on whether \var{ch} is a linebreak character. |
| 3019 | \end{cfuncdesc} |
| 3020 | |
| 3021 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch} |
| 3022 | Returns 1/0 depending on whether \var{ch} is a decimal character. |
| 3023 | \end{cfuncdesc} |
| 3024 | |
| 3025 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch} |
| 3026 | Returns 1/0 depending on whether \var{ch} is a digit character. |
| 3027 | \end{cfuncdesc} |
| 3028 | |
| 3029 | \begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch} |
| 3030 | Returns 1/0 depending on whether \var{ch} is a numeric character. |
| 3031 | \end{cfuncdesc} |
| 3032 | |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 3033 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch} |
| 3034 | Returns 1/0 depending on whether \var{ch} is an alphabetic character. |
| 3035 | \end{cfuncdesc} |
| 3036 | |
| 3037 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch} |
| 3038 | Returns 1/0 depending on whether \var{ch} is an alphanumeric character. |
| 3039 | \end{cfuncdesc} |
| 3040 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3041 | These APIs can be used for fast direct character conversions: |
| 3042 | |
| 3043 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch} |
| 3044 | Returns the character \var{ch} converted to lower case. |
| 3045 | \end{cfuncdesc} |
| 3046 | |
| 3047 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch} |
| 3048 | Returns the character \var{ch} converted to upper case. |
| 3049 | \end{cfuncdesc} |
| 3050 | |
| 3051 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch} |
| 3052 | Returns the character \var{ch} converted to title case. |
| 3053 | \end{cfuncdesc} |
| 3054 | |
| 3055 | \begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch} |
| 3056 | Returns the character \var{ch} converted to a decimal positive integer. |
| 3057 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 3058 | \end{cfuncdesc} |
| 3059 | |
| 3060 | \begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch} |
| 3061 | Returns the character \var{ch} converted to a single digit integer. |
| 3062 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 3063 | \end{cfuncdesc} |
| 3064 | |
| 3065 | \begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch} |
| 3066 | Returns the character \var{ch} converted to a (positive) double. |
| 3067 | Returns -1.0 in case this is not possible. Does not raise exceptions. |
| 3068 | \end{cfuncdesc} |
| 3069 | |
| 3070 | % --- Plain Py_UNICODE --------------------------------------------------- |
| 3071 | |
| 3072 | To create Unicode objects and access their basic sequence properties, |
| 3073 | use these APIs: |
| 3074 | |
| 3075 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u, |
| 3076 | int size} |
| 3077 | |
| 3078 | Create a Unicode Object from the Py_UNICODE buffer \var{u} of the |
| 3079 | given size. \var{u} may be \NULL{} which causes the contents to be |
| 3080 | 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] | 3081 | The buffer is copied into the new object. If the buffer is not \NULL{}, |
| 3082 | the return value might be a shared object. Therefore, modification of |
| 3083 | the resulting Unicode Object is only allowed when \var{u} is \NULL{}. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3084 | \end{cfuncdesc} |
| 3085 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3086 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3087 | Return a read-only pointer to the Unicode object's internal |
| 3088 | \ctype{Py_UNICODE} buffer. |
| 3089 | \end{cfuncdesc} |
| 3090 | |
| 3091 | \begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode} |
| 3092 | Return the length of the Unicode object. |
| 3093 | \end{cfuncdesc} |
| 3094 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3095 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj, |
| 3096 | const char *encoding, |
| 3097 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3098 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3099 | Coerce an encoded object obj to an Unicode object and return a |
| 3100 | reference with incremented refcount. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3101 | |
| 3102 | Coercion is done in the following way: |
| 3103 | \begin{enumerate} |
| 3104 | \item Unicode objects are passed back as-is with incremented |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3105 | refcount. Note: these cannot be decoded; passing a non-NULL |
| 3106 | value for encoding will result in a TypeError. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3107 | |
| 3108 | \item String and other char buffer compatible objects are decoded |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3109 | according to the given encoding and using the error handling |
| 3110 | defined by errors. Both can be NULL to have the interface use |
| 3111 | the default values (see the next section for details). |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3112 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3113 | \item All other objects cause an exception. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3114 | \end{enumerate} |
| 3115 | The API returns NULL in case of an error. The caller is responsible |
| 3116 | for decref'ing the returned objects. |
| 3117 | \end{cfuncdesc} |
| 3118 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 3119 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj} |
| 3120 | |
| 3121 | Shortcut for PyUnicode_FromEncodedObject(obj, NULL, ``strict'') |
| 3122 | which is used throughout the interpreter whenever coercion to |
| 3123 | Unicode is needed. |
| 3124 | \end{cfuncdesc} |
| 3125 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3126 | % --- wchar_t support for platforms which support it --------------------- |
| 3127 | |
| 3128 | If the platform supports \ctype{wchar_t} and provides a header file |
| 3129 | wchar.h, Python can interface directly to this type using the |
| 3130 | following functions. Support is optimized if Python's own |
| 3131 | \ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}. |
| 3132 | |
| 3133 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w, |
| 3134 | int size} |
| 3135 | Create a Unicode Object from the \ctype{whcar_t} buffer \var{w} of the |
| 3136 | given size. Returns \NULL{} on failure. |
| 3137 | \end{cfuncdesc} |
| 3138 | |
| 3139 | \begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode, |
| 3140 | wchar_t *w, |
| 3141 | int size} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3142 | Copies the Unicode Object contents into the \ctype{whcar_t} buffer |
| 3143 | \var{w}. At most \var{size} \ctype{whcar_t} characters are copied. |
| 3144 | Returns the number of \ctype{whcar_t} characters copied or -1 in case |
| 3145 | of an error. |
| 3146 | \end{cfuncdesc} |
| 3147 | |
| 3148 | |
| 3149 | \subsubsection{Builtin Codecs \label{builtinCodecs}} |
| 3150 | |
| 3151 | Python provides a set of builtin codecs which are written in C |
| 3152 | for speed. All of these codecs are directly usable via the |
| 3153 | following functions. |
| 3154 | |
| 3155 | Many of the following APIs take two arguments encoding and |
| 3156 | errors. These parameters encoding and errors have the same semantics |
| 3157 | as the ones of the builtin unicode() Unicode object constructor. |
| 3158 | |
| 3159 | Setting encoding to NULL causes the default encoding to be used which |
Martin v. Löwis | 7c82a3e0 | 2001-09-05 17:09:48 +0000 | [diff] [blame] | 3160 | is \ASCII{}. The file system calls should use |
| 3161 | \var{Py_FileSystemDefaultEncoding} as the encoding for file |
| 3162 | names. This variable should be treated as read-only: On some systems, |
| 3163 | it will be a pointer to a static string, on others, it will change at |
| 3164 | run-time, e.g. when the application invokes setlocale. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3165 | |
| 3166 | Error handling is set by errors which may also be set to NULL meaning |
| 3167 | to use the default handling defined for the codec. Default error |
| 3168 | handling for all builtin codecs is ``strict'' (ValueErrors are raised). |
| 3169 | |
| 3170 | The codecs all use a similar interface. Only deviation from the |
| 3171 | following generic ones are documented for simplicity. |
| 3172 | |
| 3173 | % --- Generic Codecs ----------------------------------------------------- |
| 3174 | |
| 3175 | These are the generic codec APIs: |
| 3176 | |
| 3177 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s, |
| 3178 | int size, |
| 3179 | const char *encoding, |
| 3180 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3181 | Create a Unicode object by decoding \var{size} bytes of the encoded |
| 3182 | string \var{s}. \var{encoding} and \var{errors} have the same meaning |
| 3183 | as the parameters of the same name in the unicode() builtin |
| 3184 | function. The codec to be used is looked up using the Python codec |
| 3185 | registry. Returns \NULL{} in case an exception was raised by the |
| 3186 | codec. |
| 3187 | \end{cfuncdesc} |
| 3188 | |
| 3189 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s, |
| 3190 | int size, |
| 3191 | const char *encoding, |
| 3192 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3193 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a |
| 3194 | Python string object. \var{encoding} and \var{errors} have the same |
| 3195 | meaning as the parameters of the same name in the Unicode .encode() |
| 3196 | method. The codec to be used is looked up using the Python codec |
| 3197 | registry. Returns \NULL{} in case an exception was raised by the |
| 3198 | codec. |
| 3199 | \end{cfuncdesc} |
| 3200 | |
| 3201 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode, |
| 3202 | const char *encoding, |
| 3203 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3204 | Encodes a Unicode object and returns the result as Python string |
| 3205 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 3206 | parameters of the same name in the Unicode .encode() method. The codec |
| 3207 | to be used is looked up using the Python codec registry. Returns |
| 3208 | \NULL{} in case an exception was raised by the codec. |
| 3209 | \end{cfuncdesc} |
| 3210 | |
| 3211 | % --- UTF-8 Codecs ------------------------------------------------------- |
| 3212 | |
| 3213 | These are the UTF-8 codec APIs: |
| 3214 | |
| 3215 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s, |
| 3216 | int size, |
| 3217 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3218 | Creates a Unicode object by decoding \var{size} bytes of the UTF-8 |
| 3219 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 3220 | raised by the codec. |
| 3221 | \end{cfuncdesc} |
| 3222 | |
| 3223 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s, |
| 3224 | int size, |
| 3225 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3226 | Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8 |
| 3227 | and returns a Python string object. Returns \NULL{} in case an |
| 3228 | exception was raised by the codec. |
| 3229 | \end{cfuncdesc} |
| 3230 | |
| 3231 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3232 | Encodes a Unicode objects using UTF-8 and returns the result as Python |
| 3233 | string object. Error handling is ``strict''. Returns |
| 3234 | \NULL{} in case an exception was raised by the codec. |
| 3235 | \end{cfuncdesc} |
| 3236 | |
| 3237 | % --- UTF-16 Codecs ------------------------------------------------------ */ |
| 3238 | |
| 3239 | These are the UTF-16 codec APIs: |
| 3240 | |
| 3241 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s, |
| 3242 | int size, |
| 3243 | const char *errors, |
| 3244 | int *byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3245 | Decodes \var{length} bytes from a UTF-16 encoded buffer string and |
| 3246 | returns the corresponding Unicode object. |
| 3247 | |
| 3248 | \var{errors} (if non-NULL) defines the error handling. It defaults |
| 3249 | to ``strict''. |
| 3250 | |
| 3251 | If \var{byteorder} is non-\NULL{}, the decoder starts decoding using |
| 3252 | the given byte order: |
| 3253 | |
| 3254 | \begin{verbatim} |
| 3255 | *byteorder == -1: little endian |
| 3256 | *byteorder == 0: native order |
| 3257 | *byteorder == 1: big endian |
| 3258 | \end{verbatim} |
| 3259 | |
| 3260 | and then switches according to all byte order marks (BOM) it finds in |
| 3261 | the input data. BOM marks are not copied into the resulting Unicode |
| 3262 | string. After completion, \var{*byteorder} is set to the current byte |
| 3263 | order at the end of input data. |
| 3264 | |
| 3265 | If \var{byteorder} is \NULL{}, the codec starts in native order mode. |
| 3266 | |
| 3267 | Returns \NULL{} in case an exception was raised by the codec. |
| 3268 | \end{cfuncdesc} |
| 3269 | |
| 3270 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s, |
| 3271 | int size, |
| 3272 | const char *errors, |
| 3273 | int byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3274 | Returns a Python string object holding the UTF-16 encoded value of the |
| 3275 | Unicode data in \var{s}. |
| 3276 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3277 | 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] | 3278 | following byte order: |
| 3279 | |
| 3280 | \begin{verbatim} |
| 3281 | byteorder == -1: little endian |
| 3282 | byteorder == 0: native byte order (writes a BOM mark) |
| 3283 | byteorder == 1: big endian |
| 3284 | \end{verbatim} |
| 3285 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3286 | 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] | 3287 | Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is |
| 3288 | prepended. |
| 3289 | |
| 3290 | Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16 |
| 3291 | reduced to UCS-2. This trick makes it possible to add full UTF-16 |
| 3292 | capabilities at a later point without comprimising the APIs. |
| 3293 | |
| 3294 | Returns \NULL{} in case an exception was raised by the codec. |
| 3295 | \end{cfuncdesc} |
| 3296 | |
| 3297 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3298 | Returns a Python string using the UTF-16 encoding in native byte |
| 3299 | order. The string always starts with a BOM mark. Error handling is |
| 3300 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 3301 | codec. |
| 3302 | \end{cfuncdesc} |
| 3303 | |
| 3304 | % --- Unicode-Escape Codecs ---------------------------------------------- |
| 3305 | |
| 3306 | These are the ``Unicode Esacpe'' codec APIs: |
| 3307 | |
| 3308 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s, |
| 3309 | int size, |
| 3310 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3311 | Creates a Unicode object by decoding \var{size} bytes of the Unicode-Esacpe |
| 3312 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 3313 | raised by the codec. |
| 3314 | \end{cfuncdesc} |
| 3315 | |
| 3316 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s, |
| 3317 | int size, |
| 3318 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3319 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Unicode-Escape |
| 3320 | and returns a Python string object. Returns \NULL{} in case an |
| 3321 | exception was raised by the codec. |
| 3322 | \end{cfuncdesc} |
| 3323 | |
| 3324 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3325 | Encodes a Unicode objects using Unicode-Escape and returns the result |
| 3326 | as Python string object. Error handling is ``strict''. Returns |
| 3327 | \NULL{} in case an exception was raised by the codec. |
| 3328 | \end{cfuncdesc} |
| 3329 | |
| 3330 | % --- Raw-Unicode-Escape Codecs ------------------------------------------ |
| 3331 | |
| 3332 | These are the ``Raw Unicode Esacpe'' codec APIs: |
| 3333 | |
| 3334 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s, |
| 3335 | int size, |
| 3336 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3337 | Creates a Unicode object by decoding \var{size} bytes of the Raw-Unicode-Esacpe |
| 3338 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 3339 | raised by the codec. |
| 3340 | \end{cfuncdesc} |
| 3341 | |
| 3342 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s, |
| 3343 | int size, |
| 3344 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3345 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Raw-Unicode-Escape |
| 3346 | and returns a Python string object. Returns \NULL{} in case an |
| 3347 | exception was raised by the codec. |
| 3348 | \end{cfuncdesc} |
| 3349 | |
| 3350 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3351 | Encodes a Unicode objects using Raw-Unicode-Escape and returns the result |
| 3352 | as Python string object. Error handling is ``strict''. Returns |
| 3353 | \NULL{} in case an exception was raised by the codec. |
| 3354 | \end{cfuncdesc} |
| 3355 | |
| 3356 | % --- Latin-1 Codecs ----------------------------------------------------- |
| 3357 | |
| 3358 | These are the Latin-1 codec APIs: |
| 3359 | |
| 3360 | Latin-1 corresponds to the first 256 Unicode ordinals and only these |
| 3361 | are accepted by the codecs during encoding. |
| 3362 | |
| 3363 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3364 | int size, |
| 3365 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3366 | Creates a Unicode object by decoding \var{size} bytes of the Latin-1 |
| 3367 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 3368 | raised by the codec. |
| 3369 | \end{cfuncdesc} |
| 3370 | |
| 3371 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3372 | int size, |
| 3373 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3374 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Latin-1 |
| 3375 | and returns a Python string object. Returns \NULL{} in case an |
| 3376 | exception was raised by the codec. |
| 3377 | \end{cfuncdesc} |
| 3378 | |
| 3379 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3380 | Encodes a Unicode objects using Latin-1 and returns the result as |
| 3381 | Python string object. Error handling is ``strict''. Returns |
| 3382 | \NULL{} in case an exception was raised by the codec. |
| 3383 | \end{cfuncdesc} |
| 3384 | |
| 3385 | % --- ASCII Codecs ------------------------------------------------------- |
| 3386 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3387 | These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is |
| 3388 | accepted. All other codes generate errors. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3389 | |
| 3390 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3391 | int size, |
| 3392 | const char *errors} |
| 3393 | Creates a Unicode object by decoding \var{size} bytes of the |
| 3394 | \ASCII{} encoded string \var{s}. Returns \NULL{} in case an exception |
| 3395 | was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3396 | \end{cfuncdesc} |
| 3397 | |
| 3398 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3399 | int size, |
| 3400 | const char *errors} |
| 3401 | Encodes the \ctype{Py_UNICODE} buffer of the given size using |
| 3402 | \ASCII{} and returns a Python string object. Returns \NULL{} in case |
| 3403 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3404 | \end{cfuncdesc} |
| 3405 | |
| 3406 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3407 | Encodes a Unicode objects using \ASCII{} and returns the result as Python |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3408 | string object. Error handling is ``strict''. Returns |
| 3409 | \NULL{} in case an exception was raised by the codec. |
| 3410 | \end{cfuncdesc} |
| 3411 | |
| 3412 | % --- Character Map Codecs ----------------------------------------------- |
| 3413 | |
| 3414 | These are the mapping codec APIs: |
| 3415 | |
| 3416 | This codec is special in that it can be used to implement many |
| 3417 | different codecs (and this is in fact what was done to obtain most of |
| 3418 | the standard codecs included in the \module{encodings} package). The |
| 3419 | codec uses mapping to encode and decode characters. |
| 3420 | |
| 3421 | Decoding mappings must map single string characters to single Unicode |
| 3422 | characters, integers (which are then interpreted as Unicode ordinals) |
| 3423 | or None (meaning "undefined mapping" and causing an error). |
| 3424 | |
| 3425 | Encoding mappings must map single Unicode characters to single string |
| 3426 | characters, integers (which are then interpreted as Latin-1 ordinals) |
| 3427 | or None (meaning "undefined mapping" and causing an error). |
| 3428 | |
| 3429 | The mapping objects provided must only support the __getitem__ mapping |
| 3430 | interface. |
| 3431 | |
| 3432 | If a character lookup fails with a LookupError, the character is |
| 3433 | copied as-is meaning that its ordinal value will be interpreted as |
| 3434 | Unicode or Latin-1 ordinal resp. Because of this, mappings only need |
| 3435 | to contain those mappings which map characters to different code |
| 3436 | points. |
| 3437 | |
| 3438 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s, |
| 3439 | int size, |
| 3440 | PyObject *mapping, |
| 3441 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3442 | Creates a Unicode object by decoding \var{size} bytes of the encoded |
| 3443 | string \var{s} using the given \var{mapping} object. Returns \NULL{} |
| 3444 | in case an exception was raised by the codec. |
| 3445 | \end{cfuncdesc} |
| 3446 | |
| 3447 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s, |
| 3448 | int size, |
| 3449 | PyObject *mapping, |
| 3450 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3451 | Encodes the \ctype{Py_UNICODE} buffer of the given size using the |
| 3452 | given \var{mapping} object and returns a Python string object. |
| 3453 | Returns \NULL{} in case an exception was raised by the codec. |
| 3454 | \end{cfuncdesc} |
| 3455 | |
| 3456 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode, |
| 3457 | PyObject *mapping} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3458 | Encodes a Unicode objects using the given \var{mapping} object and |
| 3459 | returns the result as Python string object. Error handling is |
| 3460 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 3461 | codec. |
| 3462 | \end{cfuncdesc} |
| 3463 | |
| 3464 | The following codec API is special in that maps Unicode to Unicode. |
| 3465 | |
| 3466 | \begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s, |
| 3467 | int size, |
| 3468 | PyObject *table, |
| 3469 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3470 | Translates a \ctype{Py_UNICODE} buffer of the given length by applying |
| 3471 | a character mapping \var{table} to it and returns the resulting |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3472 | Unicode object. Returns \NULL{} when an exception was raised by the |
| 3473 | codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3474 | |
| 3475 | The \var{mapping} table must map Unicode ordinal integers to Unicode |
| 3476 | ordinal integers or None (causing deletion of the character). |
| 3477 | |
| 3478 | Mapping tables must only provide the __getitem__ interface, |
| 3479 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 3480 | which cause a LookupError) are left untouched and are copied as-is. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3481 | \end{cfuncdesc} |
| 3482 | |
| 3483 | % --- MBCS codecs for Windows -------------------------------------------- |
| 3484 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3485 | These are the MBCS codec APIs. They are currently only available on |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3486 | Windows and use the Win32 MBCS converters to implement the |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3487 | conversions. Note that MBCS (or DBCS) is a class of encodings, not |
| 3488 | just one. The target encoding is defined by the user settings on the |
| 3489 | machine running the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3490 | |
| 3491 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s, |
| 3492 | int size, |
| 3493 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3494 | Creates a Unicode object by decoding \var{size} bytes of the MBCS |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3495 | encoded string \var{s}. Returns \NULL{} in case an exception was |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3496 | raised by the codec. |
| 3497 | \end{cfuncdesc} |
| 3498 | |
| 3499 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s, |
| 3500 | int size, |
| 3501 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3502 | Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS |
| 3503 | and returns a Python string object. Returns \NULL{} in case an |
| 3504 | exception was raised by the codec. |
| 3505 | \end{cfuncdesc} |
| 3506 | |
| 3507 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3508 | Encodes a Unicode objects using MBCS and returns the result as Python |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3509 | string object. Error handling is ``strict''. Returns \NULL{} in case |
| 3510 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3511 | \end{cfuncdesc} |
| 3512 | |
| 3513 | % --- Methods & Slots ---------------------------------------------------- |
| 3514 | |
| 3515 | \subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}} |
| 3516 | |
| 3517 | The following APIs are capable of handling Unicode objects and strings |
| 3518 | on input (we refer to them as strings in the descriptions) and return |
| 3519 | Unicode objects or integers as apporpriate. |
| 3520 | |
| 3521 | They all return \NULL{} or -1 in case an exception occurrs. |
| 3522 | |
| 3523 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left, |
| 3524 | PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3525 | Concat two strings giving a new Unicode string. |
| 3526 | \end{cfuncdesc} |
| 3527 | |
| 3528 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s, |
| 3529 | PyObject *sep, |
| 3530 | int maxsplit} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3531 | Split a string giving a list of Unicode strings. |
| 3532 | |
| 3533 | If sep is NULL, splitting will be done at all whitespace |
| 3534 | substrings. Otherwise, splits occur at the given separator. |
| 3535 | |
| 3536 | At most maxsplit splits will be done. If negative, no limit is set. |
| 3537 | |
| 3538 | Separators are not included in the resulting list. |
| 3539 | \end{cfuncdesc} |
| 3540 | |
| 3541 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s, |
| 3542 | int maxsplit} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3543 | Split a Unicode string at line breaks, returning a list of Unicode |
| 3544 | strings. CRLF is considered to be one line break. The Line break |
| 3545 | characters are not included in the resulting strings. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3546 | \end{cfuncdesc} |
| 3547 | |
| 3548 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str, |
| 3549 | PyObject *table, |
| 3550 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3551 | Translate a string by applying a character mapping table to it and |
| 3552 | return the resulting Unicode object. |
| 3553 | |
| 3554 | The mapping table must map Unicode ordinal integers to Unicode ordinal |
| 3555 | integers or None (causing deletion of the character). |
| 3556 | |
| 3557 | Mapping tables must only provide the __getitem__ interface, |
| 3558 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 3559 | which cause a LookupError) are left untouched and are copied as-is. |
| 3560 | |
| 3561 | \var{errors} has the usual meaning for codecs. It may be \NULL{} |
| 3562 | which indicates to use the default error handling. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3563 | \end{cfuncdesc} |
| 3564 | |
| 3565 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator, |
| 3566 | PyObject *seq} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3567 | Join a sequence of strings using the given separator and return |
| 3568 | the resulting Unicode string. |
| 3569 | \end{cfuncdesc} |
| 3570 | |
| 3571 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str, |
| 3572 | PyObject *substr, |
| 3573 | int start, |
| 3574 | int end, |
| 3575 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3576 | Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at |
| 3577 | the given tail end (\var{direction} == -1 means to do a prefix match, |
| 3578 | \var{direction} == 1 a suffix match), 0 otherwise. |
| 3579 | \end{cfuncdesc} |
| 3580 | |
| 3581 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Find}{PyObject *str, |
| 3582 | PyObject *substr, |
| 3583 | int start, |
| 3584 | int end, |
| 3585 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3586 | Return the first position of \var{substr} in |
| 3587 | \var{str}[\var{start}:\var{end}] using the given \var{direction} |
| 3588 | (\var{direction} == 1 means to do a forward search, |
| 3589 | \var{direction} == -1 a backward search), 0 otherwise. |
| 3590 | \end{cfuncdesc} |
| 3591 | |
| 3592 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Count}{PyObject *str, |
| 3593 | PyObject *substr, |
| 3594 | int start, |
| 3595 | int end} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3596 | Count the number of occurrences of \var{substr} in |
| 3597 | \var{str}[\var{start}:\var{end}] |
| 3598 | \end{cfuncdesc} |
| 3599 | |
| 3600 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str, |
| 3601 | PyObject *substr, |
| 3602 | PyObject *replstr, |
| 3603 | int maxcount} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3604 | Replace at most \var{maxcount} occurrences of \var{substr} in |
| 3605 | \var{str} with \var{replstr} and return the resulting Unicode object. |
| 3606 | \var{maxcount} == -1 means: replace all occurrences. |
| 3607 | \end{cfuncdesc} |
| 3608 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3609 | \begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3610 | Compare two strings and return -1, 0, 1 for less than, equal, |
| 3611 | greater than resp. |
| 3612 | \end{cfuncdesc} |
| 3613 | |
| 3614 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, |
| 3615 | PyObject *args} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3616 | Returns a new string object from \var{format} and \var{args}; this is |
| 3617 | analogous to \code{\var{format} \%\ \var{args}}. The |
| 3618 | \var{args} argument must be a tuple. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3619 | \end{cfuncdesc} |
| 3620 | |
| 3621 | \begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container, |
| 3622 | PyObject *element} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3623 | Checks whether \var{element} is contained in \var{container} and |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3624 | returns true or false accordingly. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 3625 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3626 | \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] | 3627 | returned in case of an error. |
| 3628 | \end{cfuncdesc} |
| 3629 | |
| 3630 | |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3631 | \subsection{Buffer Objects \label{bufferObjects}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3632 | \sectionauthor{Greg Stein}{gstein@lyra.org} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3633 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3634 | \obindex{buffer} |
| 3635 | Python objects implemented in C can export a group of functions called |
| 3636 | the ``buffer\index{buffer interface} interface.'' These functions can |
| 3637 | be used by an object to expose its data in a raw, byte-oriented |
| 3638 | format. Clients of the object can use the buffer interface to access |
| 3639 | the object data directly, without needing to copy it first. |
| 3640 | |
| 3641 | Two examples of objects that support |
| 3642 | the buffer interface are strings and arrays. The string object exposes |
| 3643 | the character contents in the buffer interface's byte-oriented |
| 3644 | form. An array can also expose its contents, but it should be noted |
| 3645 | that array elements may be multi-byte values. |
| 3646 | |
| 3647 | An example user of the buffer interface is the file object's |
| 3648 | \method{write()} method. Any object that can export a series of bytes |
| 3649 | through the buffer interface can be written to a file. There are a |
Fred Drake | 88fdaa7 | 2001-07-20 20:56:11 +0000 | [diff] [blame] | 3650 | number of format codes to \cfunction{PyArg_ParseTuple()} that operate |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3651 | against an object's buffer interface, returning data from the target |
| 3652 | object. |
| 3653 | |
| 3654 | More information on the buffer interface is provided in the section |
| 3655 | ``Buffer Object Structures'' (section \ref{buffer-structs}), under |
| 3656 | the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}. |
| 3657 | |
| 3658 | A ``buffer object'' is defined in the \file{bufferobject.h} header |
| 3659 | (included by \file{Python.h}). These objects look very similar to |
| 3660 | string objects at the Python programming level: they support slicing, |
| 3661 | indexing, concatenation, and some other standard string |
| 3662 | operations. However, their data can come from one of two sources: from |
| 3663 | a block of memory, or from another object which exports the buffer |
| 3664 | interface. |
| 3665 | |
| 3666 | Buffer objects are useful as a way to expose the data from another |
| 3667 | object's buffer interface to the Python programmer. They can also be |
| 3668 | used as a zero-copy slicing mechanism. Using their ability to |
| 3669 | reference a block of memory, it is possible to expose any data to the |
| 3670 | Python programmer quite easily. The memory could be a large, constant |
| 3671 | array in a C extension, it could be a raw block of memory for |
| 3672 | manipulation before passing to an operating system library, or it |
| 3673 | could be used to pass around structured data in its native, in-memory |
| 3674 | format. |
| 3675 | |
| 3676 | \begin{ctypedesc}{PyBufferObject} |
| 3677 | This subtype of \ctype{PyObject} represents a buffer object. |
| 3678 | \end{ctypedesc} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3679 | |
| 3680 | \begin{cvardesc}{PyTypeObject}{PyBuffer_Type} |
| 3681 | The instance of \ctype{PyTypeObject} which represents the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3682 | buffer type; it is the same object as \code{types.BufferType} in the |
| 3683 | Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3684 | \end{cvardesc} |
| 3685 | |
| 3686 | \begin{cvardesc}{int}{Py_END_OF_BUFFER} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3687 | This constant may be passed as the \var{size} parameter to |
| 3688 | \cfunction{PyBuffer_FromObject()} or |
| 3689 | \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the new |
| 3690 | \ctype{PyBufferObject} should refer to \var{base} object from the |
| 3691 | specified \var{offset} to the end of its exported buffer. Using this |
| 3692 | enables the caller to avoid querying the \var{base} object for its |
| 3693 | length. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3694 | \end{cvardesc} |
| 3695 | |
| 3696 | \begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p} |
| 3697 | Return true if the argument has type \cdata{PyBuffer_Type}. |
| 3698 | \end{cfuncdesc} |
| 3699 | |
| 3700 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base, |
| 3701 | int offset, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3702 | Return a new read-only buffer object. This raises |
| 3703 | \exception{TypeError} if \var{base} doesn't support the read-only |
| 3704 | buffer protocol or doesn't provide exactly one buffer segment, or it |
| 3705 | raises \exception{ValueError} if \var{offset} is less than zero. The |
| 3706 | buffer will hold a reference to the \var{base} object, and the |
| 3707 | buffer's contents will refer to the \var{base} object's buffer |
| 3708 | interface, starting as position \var{offset} and extending for |
| 3709 | \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then |
| 3710 | the new buffer's contents extend to the length of the |
| 3711 | \var{base} object's exported buffer data. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3712 | \end{cfuncdesc} |
| 3713 | |
| 3714 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base, |
| 3715 | int offset, |
| 3716 | int size} |
| 3717 | Return a new writable buffer object. Parameters and exceptions are |
| 3718 | similar to those for \cfunction{PyBuffer_FromObject()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3719 | If the \var{base} object does not export the writeable buffer |
| 3720 | protocol, then \exception{TypeError} is raised. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3721 | \end{cfuncdesc} |
| 3722 | |
| 3723 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3724 | Return a new read-only buffer object that reads from a specified |
| 3725 | location in memory, with a specified size. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3726 | The caller is responsible for ensuring that the memory buffer, passed |
| 3727 | in as \var{ptr}, is not deallocated while the returned buffer object |
| 3728 | exists. Raises \exception{ValueError} if \var{size} is less than |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3729 | zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be passed |
| 3730 | for the \var{size} parameter; \exception{ValueError} will be raised in |
| 3731 | that case. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3732 | \end{cfuncdesc} |
| 3733 | |
| 3734 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3735 | Similar to \cfunction{PyBuffer_FromMemory()}, but the returned buffer |
| 3736 | is writable. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3737 | \end{cfuncdesc} |
| 3738 | |
| 3739 | \begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size} |
| 3740 | Returns a new writable buffer object that maintains its own memory |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3741 | buffer of \var{size} bytes. \exception{ValueError} is returned if |
| 3742 | \var{size} is not zero or positive. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 3743 | \end{cfuncdesc} |
| 3744 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3745 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3746 | \subsection{Tuple Objects \label{tupleObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3747 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3748 | \obindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3749 | \begin{ctypedesc}{PyTupleObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3750 | This subtype of \ctype{PyObject} represents a Python tuple object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3751 | \end{ctypedesc} |
| 3752 | |
| 3753 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3754 | This instance of \ctype{PyTypeObject} represents the Python tuple |
| 3755 | type; it is the same object as \code{types.TupleType} in the Python |
| 3756 | layer.\withsubitem{(in module types)}{\ttindex{TupleType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3757 | \end{cvardesc} |
| 3758 | |
| 3759 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 3760 | Return true if \var{p} is a tuple object or an instance of a subtype |
| 3761 | of the tuple type. |
| 3762 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 3763 | \end{cfuncdesc} |
| 3764 | |
| 3765 | \begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p} |
| 3766 | Return true if \var{p} is a tuple object, but not an instance of |
| 3767 | a subtype of the tuple type. |
| 3768 | \versionadded{2.2} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3769 | \end{cfuncdesc} |
| 3770 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3771 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len} |
| 3772 | 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] | 3773 | \end{cfuncdesc} |
| 3774 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3775 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3776 | Takes a pointer to a tuple object, and returns the size |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3777 | of that tuple. |
| 3778 | \end{cfuncdesc} |
| 3779 | |
Fred Drake | 0e40c3d | 2001-08-20 16:48:59 +0000 | [diff] [blame] | 3780 | \begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p} |
| 3781 | Return the size of the tuple \var{p}, which must be non-\NULL{} and |
| 3782 | point to a tuple; no error checking is performed. |
| 3783 | \end{cfuncdesc} |
| 3784 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3785 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3786 | Returns the object at position \var{pos} in the tuple pointed |
| 3787 | 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] | 3788 | sets an \exception{IndexError} exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3789 | \end{cfuncdesc} |
| 3790 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3791 | \begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos} |
Fred Drake | fac312f | 2001-05-29 15:13:00 +0000 | [diff] [blame] | 3792 | Like \cfunction{PyTuple_GetItem()}, but does no checking of its |
| 3793 | arguments. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3794 | \end{cfuncdesc} |
| 3795 | |
Fred Drake | a05460c | 2001-02-12 17:38:18 +0000 | [diff] [blame] | 3796 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p, |
| 3797 | int low, int high} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3798 | Takes a slice of the tuple pointed to by \var{p} from |
| 3799 | \var{low} to \var{high} and returns it as a new tuple. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3800 | \end{cfuncdesc} |
| 3801 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3802 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p, |
| 3803 | int pos, PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3804 | Inserts a reference to object \var{o} at position \var{pos} of |
| 3805 | 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] | 3806 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3807 | \end{cfuncdesc} |
| 3808 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3809 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p, |
| 3810 | int pos, PyObject *o} |
Fred Drake | fac312f | 2001-05-29 15:13:00 +0000 | [diff] [blame] | 3811 | Like \cfunction{PyTuple_SetItem()}, but does no error checking, and |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3812 | should \emph{only} be used to fill in brand new tuples. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3813 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3814 | \end{cfuncdesc} |
| 3815 | |
Fred Drake | fac312f | 2001-05-29 15:13:00 +0000 | [diff] [blame] | 3816 | \begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3817 | Can be used to resize a tuple. \var{newsize} will be the new length |
| 3818 | of the tuple. Because tuples are \emph{supposed} to be immutable, |
| 3819 | this should only be used if there is only one reference to the object. |
| 3820 | Do \emph{not} use this if the tuple may already be known to some other |
Fred Drake | fac312f | 2001-05-29 15:13:00 +0000 | [diff] [blame] | 3821 | part of the code. The tuple will always grow or shrink at the end. |
| 3822 | Think of this as destroying the old tuple and creating a new one, only |
| 3823 | more efficiently. Returns \code{0} on success. Client code should |
| 3824 | never assume that the resulting value of \code{*\var{p}} will be the |
| 3825 | same as before calling this function. If the object referenced by |
| 3826 | \code{*\var{p}} is replaced, the original \code{*\var{p}} is |
| 3827 | destroyed. On failure, returns \code{-1} and sets \code{*\var{p}} to |
| 3828 | \NULL, and raises \exception{MemoryError} or \exception{SystemError}. |
| 3829 | \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3830 | \end{cfuncdesc} |
| 3831 | |
| 3832 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3833 | \subsection{List Objects \label{listObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3834 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3835 | \obindex{list} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3836 | \begin{ctypedesc}{PyListObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3837 | This subtype of \ctype{PyObject} represents a Python list object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3838 | \end{ctypedesc} |
| 3839 | |
| 3840 | \begin{cvardesc}{PyTypeObject}{PyList_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3841 | This instance of \ctype{PyTypeObject} represents the Python list |
| 3842 | type. This is the same object as \code{types.ListType}. |
| 3843 | \withsubitem{(in module types)}{\ttindex{ListType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3844 | \end{cvardesc} |
| 3845 | |
| 3846 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3847 | Returns true if its argument is a \ctype{PyListObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3848 | \end{cfuncdesc} |
| 3849 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3850 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int len} |
| 3851 | 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] | 3852 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3853 | \end{cfuncdesc} |
| 3854 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3855 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3856 | Returns the length of the list object in \var{list}; this is |
| 3857 | equivalent to \samp{len(\var{list})} on a list object. |
| 3858 | \bifuncindex{len} |
| 3859 | \end{cfuncdesc} |
| 3860 | |
| 3861 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} |
Fred Drake | 5d64421 | 2000-10-07 12:31:50 +0000 | [diff] [blame] | 3862 | Macro form of \cfunction{PyList_Size()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3863 | \end{cfuncdesc} |
| 3864 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3865 | \begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3866 | Returns the object at position \var{pos} in the list pointed |
| 3867 | 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] | 3868 | sets an \exception{IndexError} exception. |
| 3869 | \end{cfuncdesc} |
| 3870 | |
| 3871 | \begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i} |
| 3872 | Macro form of \cfunction{PyList_GetItem()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3873 | \end{cfuncdesc} |
| 3874 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3875 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index, |
| 3876 | PyObject *item} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 3877 | Sets the item at index \var{index} in list to \var{item}. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3878 | Returns \code{0} on success or \code{-1} on failure. |
Fred Drake | 00d0cb6 | 2001-06-03 03:12:57 +0000 | [diff] [blame] | 3879 | \strong{Note:} This function ``steals'' a reference to \var{item} and |
| 3880 | discards a reference to an item already in the list at the affected |
| 3881 | position. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3882 | \end{cfuncdesc} |
| 3883 | |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3884 | \begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3885 | PyObject *o} |
| 3886 | Macro form of \cfunction{PyList_SetItem()} without error checking. |
Fred Drake | 00d0cb6 | 2001-06-03 03:12:57 +0000 | [diff] [blame] | 3887 | \strong{Note:} This function ``steals'' a reference to \var{item}, |
| 3888 | and, unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3889 | reference to any item that it being replaced; any reference in |
| 3890 | \var{list} at position \var{i} will be leaked. This is normally only |
| 3891 | used to fill in new lists where there is no previous content. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3892 | \end{cfuncdesc} |
| 3893 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3894 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3895 | PyObject *item} |
| 3896 | 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] | 3897 | \var{index}. Returns \code{0} if successful; returns \code{-1} and |
| 3898 | raises an exception if unsuccessful. Analogous to |
| 3899 | \code{\var{list}.insert(\var{index}, \var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3900 | \end{cfuncdesc} |
| 3901 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3902 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3903 | 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] | 3904 | \code{0} if successful; returns \code{-1} and sets an exception if |
| 3905 | unsuccessful. Analogous to \code{\var{list}.append(\var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3906 | \end{cfuncdesc} |
| 3907 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3908 | \begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list, |
| 3909 | int low, int high} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 3910 | 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] | 3911 | \emph{between} \var{low} and \var{high}. Returns NULL and sets an |
| 3912 | exception if unsuccessful. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3913 | Analogous to \code{\var{list}[\var{low}:\var{high}]}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3914 | \end{cfuncdesc} |
| 3915 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3916 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list, |
| 3917 | int low, int high, |
| 3918 | PyObject *itemlist} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3919 | Sets the slice of \var{list} between \var{low} and \var{high} to the |
| 3920 | contents of \var{itemlist}. Analogous to |
| 3921 | \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns |
| 3922 | \code{0} on success, \code{-1} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3923 | \end{cfuncdesc} |
| 3924 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3925 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3926 | Sorts the items of \var{list} in place. Returns \code{0} on success, |
| 3927 | \code{-1} on failure. This is equivalent to |
| 3928 | \samp{\var{list}.sort()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3929 | \end{cfuncdesc} |
| 3930 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3931 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3932 | Reverses the items of \var{list} in place. Returns \code{0} on |
| 3933 | success, \code{-1} on failure. This is the equivalent of |
| 3934 | \samp{\var{list}.reverse()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3935 | \end{cfuncdesc} |
| 3936 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3937 | \begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3938 | Returns a new tuple object containing the contents of \var{list}; |
| 3939 | equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3940 | \end{cfuncdesc} |
| 3941 | |
| 3942 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3943 | \section{Mapping Objects \label{mapObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3944 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3945 | \obindex{mapping} |
| 3946 | |
| 3947 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3948 | \subsection{Dictionary Objects \label{dictObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3949 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3950 | \obindex{dictionary} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3951 | \begin{ctypedesc}{PyDictObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3952 | This subtype of \ctype{PyObject} represents a Python dictionary object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3953 | \end{ctypedesc} |
| 3954 | |
| 3955 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3956 | This instance of \ctype{PyTypeObject} represents the Python dictionary |
| 3957 | type. This is exposed to Python programs as \code{types.DictType} and |
| 3958 | \code{types.DictionaryType}. |
| 3959 | \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3960 | \end{cvardesc} |
| 3961 | |
| 3962 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3963 | Returns true if its argument is a \ctype{PyDictObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3964 | \end{cfuncdesc} |
| 3965 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3966 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3967 | Returns a new empty dictionary, or \NULL{} on failure. |
| 3968 | \end{cfuncdesc} |
| 3969 | |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 3970 | \begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict} |
| 3971 | Return a proxy object for a mapping which enforces read-only |
| 3972 | behavior. This is normally used to create a proxy to prevent |
| 3973 | modification of the dictionary for non-dynamic class types. |
| 3974 | \versionadded{2.2} |
| 3975 | \end{cfuncdesc} |
| 3976 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3977 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p} |
| 3978 | Empties an existing dictionary of all key-value pairs. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3979 | \end{cfuncdesc} |
| 3980 | |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 3981 | \begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p} |
Fred Drake | 0e40c3d | 2001-08-20 16:48:59 +0000 | [diff] [blame] | 3982 | Returns a new dictionary that contains the same key-value pairs as |
| 3983 | \var{p}. |
Fred Drake | 11ee902 | 2001-08-10 21:31:12 +0000 | [diff] [blame] | 3984 | \versionadded{1.6} |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 3985 | \end{cfuncdesc} |
| 3986 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3987 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key, |
| 3988 | PyObject *val} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3989 | Inserts \var{value} into the dictionary \var{p} with a key of \var{key}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3990 | \var{key} must be hashable; if it isn't, \exception{TypeError} will be |
| 3991 | raised. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3992 | Returns \code{0} on success or \code{-1} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3993 | \end{cfuncdesc} |
| 3994 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 3995 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p, |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3996 | char *key, |
| 3997 | PyObject *val} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 3998 | Inserts \var{value} into the dictionary \var{p} using \var{key} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3999 | 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] | 4000 | created using \code{PyString_FromString(\var{key})}. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 4001 | Returns \code{0} on success or \code{-1} on failure. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4002 | \ttindex{PyString_FromString()} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4003 | \end{cfuncdesc} |
| 4004 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4005 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4006 | Removes the entry in dictionary \var{p} with key \var{key}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4007 | \var{key} must be hashable; if it isn't, \exception{TypeError} is |
| 4008 | raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4009 | \end{cfuncdesc} |
| 4010 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4011 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4012 | Removes the entry in dictionary \var{p} which has a key |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4013 | specified by the string \var{key}. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 4014 | Returns \code{0} on success or \code{-1} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4015 | \end{cfuncdesc} |
| 4016 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4017 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4018 | Returns the object from dictionary \var{p} which has a key |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4019 | \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] | 4020 | \emph{without} setting an exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4021 | \end{cfuncdesc} |
| 4022 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4023 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4024 | This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4025 | specified as a \ctype{char*}, rather than a \ctype{PyObject*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4026 | \end{cfuncdesc} |
| 4027 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4028 | \begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4029 | Returns a \ctype{PyListObject} containing all the items |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4030 | from the dictionary, as in the dictinoary method \method{items()} (see |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 4031 | the \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4032 | \end{cfuncdesc} |
| 4033 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4034 | \begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4035 | Returns a \ctype{PyListObject} containing all the keys |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4036 | from the dictionary, as in the dictionary method \method{keys()} (see the |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 4037 | \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4038 | \end{cfuncdesc} |
| 4039 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4040 | \begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4041 | Returns a \ctype{PyListObject} containing all the values |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4042 | from the dictionary \var{p}, as in the dictionary method |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 4043 | \method{values()} (see the \citetitle[../lib/lib.html]{Python Library |
| 4044 | Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4045 | \end{cfuncdesc} |
| 4046 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4047 | \begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p} |
| 4048 | Returns the number of items in the dictionary. This is equivalent to |
| 4049 | \samp{len(\var{p})} on a dictionary.\bifuncindex{len} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4050 | \end{cfuncdesc} |
| 4051 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 4052 | \begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos, |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 4053 | PyObject **pkey, PyObject **pvalue} |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 4054 | Iterate over all key-value pairs in the dictionary \var{p}. The |
| 4055 | \ctype{int} referred to by \var{ppos} must be initialized to \code{0} |
| 4056 | prior to the first call to this function to start the iteration; the |
| 4057 | function returns true for each pair in the dictionary, and false once |
| 4058 | all pairs have been reported. The parameters \var{pkey} and |
| 4059 | \var{pvalue} should either point to \ctype{PyObject*} variables that |
| 4060 | 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] | 4061 | \NULL. |
| 4062 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 4063 | For example: |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4064 | |
Fred Drake | 83e01bf | 2001-03-16 15:41:29 +0000 | [diff] [blame] | 4065 | \begin{verbatim} |
| 4066 | PyObject *key, *value; |
| 4067 | int pos = 0; |
| 4068 | |
| 4069 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 4070 | /* do something interesting with the values... */ |
| 4071 | ... |
| 4072 | } |
| 4073 | \end{verbatim} |
Fred Drake | 8d00a0f | 2001-04-13 17:55:02 +0000 | [diff] [blame] | 4074 | |
| 4075 | The dictionary \var{p} should not be mutated during iteration. It is |
| 4076 | safe (since Python 2.1) to modify the values of the keys as you |
Fred Drake | 11ee902 | 2001-08-10 21:31:12 +0000 | [diff] [blame] | 4077 | iterate over the dictionary, but only so long as the set of keys does |
| 4078 | not change. For example: |
Fred Drake | 8d00a0f | 2001-04-13 17:55:02 +0000 | [diff] [blame] | 4079 | |
| 4080 | \begin{verbatim} |
| 4081 | PyObject *key, *value; |
| 4082 | int pos = 0; |
| 4083 | |
| 4084 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 4085 | int i = PyInt_AS_LONG(value) + 1; |
| 4086 | PyObject *o = PyInt_FromLong(i); |
| 4087 | if (o == NULL) |
| 4088 | return -1; |
| 4089 | if (PyDict_SetItem(self->dict, key, o) < 0) { |
| 4090 | Py_DECREF(o); |
| 4091 | return -1; |
| 4092 | } |
| 4093 | Py_DECREF(o); |
| 4094 | } |
| 4095 | \end{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4096 | \end{cfuncdesc} |
| 4097 | |
Fred Drake | 11ee902 | 2001-08-10 21:31:12 +0000 | [diff] [blame] | 4098 | \begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override} |
| 4099 | Iterate over dictionary \var{b} adding key-value pairs to dictionary |
| 4100 | \var{a}. If \var{override} is true, existing pairs in \var{a} will be |
| 4101 | replaced if a matching key is found in \var{b}, otherwise pairs will |
| 4102 | only be added if there is not a matching key in \var{a}. Returns |
| 4103 | \code{0} on success or \code{-1} if an exception was raised. |
| 4104 | \versionadded{2.2} |
| 4105 | \end{cfuncdesc} |
| 4106 | |
| 4107 | \begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b} |
| 4108 | This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C, or |
| 4109 | \code{\var{a}.update(\var{b})} in Python. Returns \code{0} on success |
| 4110 | or \code{-1} if an exception was raised. |
| 4111 | \versionadded{2.2} |
| 4112 | \end{cfuncdesc} |
| 4113 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4114 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4115 | \section{Other Objects \label{otherObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4116 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4117 | \subsection{File Objects \label{fileObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4118 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4119 | \obindex{file} |
| 4120 | Python's built-in file objects are implemented entirely on the |
| 4121 | \ctype{FILE*} support from the C standard library. This is an |
| 4122 | implementation detail and may change in future releases of Python. |
| 4123 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4124 | \begin{ctypedesc}{PyFileObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4125 | This subtype of \ctype{PyObject} represents a Python file object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4126 | \end{ctypedesc} |
| 4127 | |
| 4128 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4129 | This instance of \ctype{PyTypeObject} represents the Python file |
| 4130 | type. This is exposed to Python programs as \code{types.FileType}. |
| 4131 | \withsubitem{(in module types)}{\ttindex{FileType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4132 | \end{cvardesc} |
| 4133 | |
| 4134 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 4135 | Returns true if its argument is a \ctype{PyFileObject} or a subtype of |
| 4136 | \ctype{PyFileObject}. |
| 4137 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 4138 | \end{cfuncdesc} |
| 4139 | |
| 4140 | \begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p} |
| 4141 | Returns true if its argument is a \ctype{PyFileObject}, but not a |
| 4142 | subtype of \ctype{PyFileObject}. |
| 4143 | \versionadded{2.2} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4144 | \end{cfuncdesc} |
| 4145 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4146 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode} |
| 4147 | On success, returns a new file object that is opened on the |
| 4148 | file given by \var{filename}, with a file mode given by \var{mode}, |
| 4149 | where \var{mode} has the same semantics as the standard C routine |
| 4150 | \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4151 | \end{cfuncdesc} |
| 4152 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4153 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4154 | char *name, char *mode, |
| 4155 | int (*close)(FILE*)} |
| 4156 | Creates a new \ctype{PyFileObject} from the already-open standard C |
| 4157 | file pointer, \var{fp}. The function \var{close} will be called when |
| 4158 | the file should be closed. Returns \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4159 | \end{cfuncdesc} |
| 4160 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4161 | \begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p} |
| 4162 | Returns the file object associated with \var{p} as a \ctype{FILE*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4163 | \end{cfuncdesc} |
| 4164 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4165 | \begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4166 | Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this |
| 4167 | function reads one line from the object \var{p}. \var{p} may be a |
| 4168 | file object or any object with a \method{readline()} method. If |
| 4169 | \var{n} is \code{0}, exactly one line is read, regardless of the |
| 4170 | length of the line. If \var{n} is greater than \code{0}, no more than |
| 4171 | \var{n} bytes will be read from the file; a partial line can be |
| 4172 | returned. In both cases, an empty string is returned if the end of |
| 4173 | the file is reached immediately. If \var{n} is less than \code{0}, |
| 4174 | however, one line is read regardless of length, but |
| 4175 | \exception{EOFError} is raised if the end of the file is reached |
| 4176 | immediately. |
| 4177 | \withsubitem{(built-in exception)}{\ttindex{EOFError}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4178 | \end{cfuncdesc} |
| 4179 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4180 | \begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4181 | 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] | 4182 | \end{cfuncdesc} |
| 4183 | |
| 4184 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4185 | Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()} |
| 4186 | only. This should only be called immediately after file object |
| 4187 | creation. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4188 | \end{cfuncdesc} |
| 4189 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4190 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag} |
| 4191 | This function exists for internal use by the interpreter. |
| 4192 | Sets the \member{softspace} attribute of \var{p} to \var{newflag} and |
| 4193 | \withsubitem{(file attribute)}{\ttindex{softspace}}returns the |
| 4194 | previous value. \var{p} does not have to be a file object |
| 4195 | for this function to work properly; any object is supported (thought |
| 4196 | its only interesting if the \member{softspace} attribute can be set). |
| 4197 | This function clears any errors, and will return \code{0} as the |
| 4198 | previous value if the attribute either does not exist or if there were |
| 4199 | errors in retrieving it. There is no way to detect errors from this |
| 4200 | function, but doing so should not be needed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4201 | \end{cfuncdesc} |
| 4202 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4203 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p, |
| 4204 | int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4205 | Writes object \var{obj} to file object \var{p}. The only supported |
| 4206 | flag for \var{flags} is \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; |
| 4207 | if given, the \function{str()} of the object is written instead of the |
| 4208 | \function{repr()}. Returns \code{0} on success or \code{-1} on |
| 4209 | failure; the appropriate exception will be set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4210 | \end{cfuncdesc} |
| 4211 | |
Fred Drake | 024ef6f | 2001-08-10 14:27:38 +0000 | [diff] [blame] | 4212 | \begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4213 | Writes string \var{s} to file object \var{p}. Returns \code{0} on |
| 4214 | success or \code{-1} on failure; the appropriate exception will be |
| 4215 | set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4216 | \end{cfuncdesc} |
| 4217 | |
| 4218 | |
Fred Drake | 5838d0f | 2001-01-28 06:39:35 +0000 | [diff] [blame] | 4219 | \subsection{Instance Objects \label{instanceObjects}} |
| 4220 | |
| 4221 | \obindex{instance} |
| 4222 | There are very few functions specific to instance objects. |
| 4223 | |
| 4224 | \begin{cvardesc}{PyTypeObject}{PyInstance_Type} |
| 4225 | Type object for class instances. |
| 4226 | \end{cvardesc} |
| 4227 | |
| 4228 | \begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj} |
| 4229 | Returns true if \var{obj} is an instance. |
| 4230 | \end{cfuncdesc} |
| 4231 | |
| 4232 | \begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class, |
| 4233 | PyObject *arg, |
| 4234 | PyObject *kw} |
| 4235 | Create a new instance of a specific class. The parameters \var{arg} |
| 4236 | and \var{kw} are used as the positional and keyword parameters to |
| 4237 | the object's constructor. |
| 4238 | \end{cfuncdesc} |
| 4239 | |
| 4240 | \begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class, |
| 4241 | PyObject *dict} |
| 4242 | Create a new instance of a specific class without calling it's |
| 4243 | constructor. \var{class} is the class of new object. The |
| 4244 | \var{dict} parameter will be used as the object's \member{__dict__}; |
| 4245 | if \NULL, a new dictionary will be created for the instance. |
| 4246 | \end{cfuncdesc} |
| 4247 | |
| 4248 | |
Fred Drake | f8d7a5d | 2001-09-06 17:12:44 +0000 | [diff] [blame] | 4249 | \subsection{Method Objects \label{method-objects}} |
| 4250 | |
| 4251 | \obindex{method} |
| 4252 | There are some useful functions that are useful for working with |
| 4253 | method objects. |
| 4254 | |
| 4255 | \begin{cvardesc}{PyTypeObject}{PyMethod_Type} |
| 4256 | This instance of \ctype{PyTypeObject} represents the Python method |
| 4257 | type. This is exposed to Python programs as \code{types.MethodType}. |
| 4258 | \withsubitem{(in module types)}{\ttindex{MethodType}} |
| 4259 | \end{cvardesc} |
| 4260 | |
| 4261 | \begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o} |
| 4262 | Return true if \var{o} is a method object (has type |
| 4263 | \cdata{PyMethod_Type}). The parameter must not be \NULL. |
| 4264 | \end{cfuncdesc} |
| 4265 | |
| 4266 | \begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func. |
| 4267 | PyObject *self, PyObject *class} |
| 4268 | Return a new method object, with \var{func} being any callable |
| 4269 | object; this is the function that will be called when the method is |
| 4270 | called. If this method should be bound to an instance, \var{self} |
| 4271 | should be the instance and \var{class} should be the class of |
| 4272 | \var{self}, otherwise \var{self} should be \NULL{} and \var{class} |
| 4273 | should be the class which provides the unbound method.. |
| 4274 | \end{cfuncdesc} |
| 4275 | |
| 4276 | \begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth} |
| 4277 | Return the class object from which the method \var{meth} was |
| 4278 | created; if this was created from an instance, it will be the class |
| 4279 | of the instance. |
| 4280 | \end{cfuncdesc} |
| 4281 | |
| 4282 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth} |
| 4283 | Macro version of \cfunction{PyMethod_Class()} which avoids error |
| 4284 | checking. |
| 4285 | \end{cfuncdesc} |
| 4286 | |
| 4287 | \begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth} |
| 4288 | Return the function object associated with the method \var{meth}. |
| 4289 | \end{cfuncdesc} |
| 4290 | |
| 4291 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth} |
| 4292 | Macro version of \cfunction{PyMethod_Function()} which avoids error |
| 4293 | checking. |
| 4294 | \end{cfuncdesc} |
| 4295 | |
| 4296 | \begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth} |
| 4297 | Return the instance associated with the method \var{meth} if it is |
| 4298 | bound, otherwise return \NULL. |
| 4299 | \end{cfuncdesc} |
| 4300 | |
| 4301 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth} |
| 4302 | Macro version of \cfunction{PyMethod_Self()} which avoids error |
| 4303 | checking. |
| 4304 | \end{cfuncdesc} |
| 4305 | |
| 4306 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4307 | \subsection{Module Objects \label{moduleObjects}} |
| 4308 | |
| 4309 | \obindex{module} |
| 4310 | There are only a few functions special to module objects. |
| 4311 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4312 | \begin{cvardesc}{PyTypeObject}{PyModule_Type} |
| 4313 | This instance of \ctype{PyTypeObject} represents the Python module |
| 4314 | type. This is exposed to Python programs as \code{types.ModuleType}. |
| 4315 | \withsubitem{(in module types)}{\ttindex{ModuleType}} |
| 4316 | \end{cvardesc} |
| 4317 | |
| 4318 | \begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p} |
Fred Drake | f47d8ef | 2001-09-20 19:18:52 +0000 | [diff] [blame] | 4319 | Returns true if \var{p} is a module object, or a subtype of a |
| 4320 | module object. |
| 4321 | \versionchanged[Allowed subtypes to be accepted]{2.2} |
| 4322 | \end{cfuncdesc} |
| 4323 | |
| 4324 | \begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p} |
| 4325 | Returns true if \var{p} is a module object, but not a subtype of |
| 4326 | \cdata{PyModule_Type}. |
| 4327 | \versionadded{2.2} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4328 | \end{cfuncdesc} |
| 4329 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4330 | \begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name} |
| 4331 | Return a new module object with the \member{__name__} attribute set to |
| 4332 | \var{name}. Only the module's \member{__doc__} and |
| 4333 | \member{__name__} attributes are filled in; the caller is responsible |
| 4334 | for providing a \member{__file__} attribute. |
| 4335 | \withsubitem{(module attribute)}{ |
| 4336 | \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}} |
| 4337 | \end{cfuncdesc} |
| 4338 | |
| 4339 | \begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4340 | Return the dictionary object that implements \var{module}'s namespace; |
| 4341 | this object is the same as the \member{__dict__} attribute of the |
| 4342 | module object. This function never fails. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4343 | \withsubitem{(module attribute)}{\ttindex{__dict__}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4344 | \end{cfuncdesc} |
| 4345 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4346 | \begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4347 | Return \var{module}'s \member{__name__} value. If the module does not |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4348 | provide one, or if it is not a string, \exception{SystemError} is |
| 4349 | raised and \NULL{} is returned. |
| 4350 | \withsubitem{(module attribute)}{\ttindex{__name__}} |
| 4351 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4352 | \end{cfuncdesc} |
| 4353 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4354 | \begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4355 | Return the name of the file from which \var{module} was loaded using |
| 4356 | \var{module}'s \member{__file__} attribute. If this is not defined, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4357 | or if it is not a string, raise \exception{SystemError} and return |
| 4358 | \NULL. |
| 4359 | \withsubitem{(module attribute)}{\ttindex{__file__}} |
| 4360 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4361 | \end{cfuncdesc} |
| 4362 | |
Fred Drake | 891150b | 2000-09-23 03:25:42 +0000 | [diff] [blame] | 4363 | \begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module, |
| 4364 | char *name, PyObject *value} |
| 4365 | Add an object to \var{module} as \var{name}. This is a convenience |
| 4366 | function which can be used from the module's initialization function. |
| 4367 | This steals a reference to \var{value}. Returns \code{-1} on error, |
| 4368 | \code{0} on success. |
| 4369 | \versionadded{2.0} |
| 4370 | \end{cfuncdesc} |
| 4371 | |
| 4372 | \begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module, |
| 4373 | char *name, int value} |
| 4374 | Add an integer constant to \var{module} as \var{name}. This convenience |
| 4375 | function can be used from the module's initialization function. |
| 4376 | Returns \code{-1} on error, \code{0} on success. |
| 4377 | \versionadded{2.0} |
| 4378 | \end{cfuncdesc} |
| 4379 | |
| 4380 | \begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module, |
| 4381 | char *name, char *value} |
| 4382 | Add a string constant to \var{module} as \var{name}. This convenience |
| 4383 | function can be used from the module's initialization function. The |
| 4384 | string \var{value} must be null-terminated. Returns \code{-1} on |
| 4385 | error, \code{0} on success. |
| 4386 | \versionadded{2.0} |
| 4387 | \end{cfuncdesc} |
| 4388 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4389 | |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4390 | \subsection{Iterator Objects \label{iterator-objects}} |
| 4391 | |
| 4392 | Python provides two general-purpose iterator objects. The first, a |
| 4393 | sequence iterator, works with an arbitrary sequence supporting the |
| 4394 | \method{__getitem__()} method. The second works with a callable |
| 4395 | object and a sentinel value, calling the callable for each item in the |
| 4396 | sequence, and ending the iteration when the sentinel value is |
| 4397 | returned. |
| 4398 | |
| 4399 | \begin{cvardesc}{PyTypeObject}{PySeqIter_Type} |
| 4400 | Type object for iterator objects returned by |
| 4401 | \cfunction{PySeqIter_New()} and the one-argument form of the |
| 4402 | \function{iter()} built-in function for built-in sequence types. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4403 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4404 | \end{cvardesc} |
| 4405 | |
| 4406 | \begin{cfuncdesc}{int}{PySeqIter_Check}{op} |
| 4407 | Return true if the type of \var{op} is \cdata{PySeqIter_Type}. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4408 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4409 | \end{cfuncdesc} |
| 4410 | |
| 4411 | \begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq} |
| 4412 | Return an iterator that works with a general sequence object, |
| 4413 | \var{seq}. The iteration ends when the sequence raises |
| 4414 | \exception{IndexError} for the subscripting operation. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4415 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4416 | \end{cfuncdesc} |
| 4417 | |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4418 | \begin{cvardesc}{PyTypeObject}{PyCallIter_Type} |
| 4419 | Type object for iterator objects returned by |
| 4420 | \cfunction{PyCallIter_New()} and the two-argument form of the |
| 4421 | \function{iter()} built-in function. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4422 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4423 | \end{cvardesc} |
| 4424 | |
| 4425 | \begin{cfuncdesc}{int}{PyCallIter_Check}{op} |
| 4426 | Return true if the type of \var{op} is \cdata{PyCallIter_Type}. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4427 | \versionadded{2.2} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4428 | \end{cfuncdesc} |
| 4429 | |
| 4430 | \begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable, |
| 4431 | PyObject *sentinel} |
| 4432 | Return a new iterator. The first parameter, \var{callable}, can be |
| 4433 | any Python callable object that can be called with no parameters; |
| 4434 | each call to it should return the next item in the iteration. When |
| 4435 | \var{callable} returns a value equal to \var{sentinel}, the |
| 4436 | iteration will be terminated. |
Fred Drake | f244b2e | 2001-09-24 15:31:50 +0000 | [diff] [blame] | 4437 | \versionadded{2.2} |
| 4438 | \end{cfuncdesc} |
| 4439 | |
| 4440 | |
| 4441 | \subsection{Descriptor Objects \label{descriptor-objects}} |
| 4442 | |
| 4443 | \begin{cvardesc}{PyTypeObject}{PyProperty_Type} |
| 4444 | The type object for a descriptor. |
| 4445 | \versionadded{2.2} |
| 4446 | \end{cvardesc} |
| 4447 | |
| 4448 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type, |
| 4449 | PyGetSetDef *getset} |
| 4450 | \versionadded{2.2} |
| 4451 | \end{cfuncdesc} |
| 4452 | |
| 4453 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type, |
| 4454 | PyMemberDef *meth} |
| 4455 | \versionadded{2.2} |
| 4456 | \end{cfuncdesc} |
| 4457 | |
| 4458 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type, |
| 4459 | PyMethodDef *meth} |
| 4460 | \versionadded{2.2} |
| 4461 | \end{cfuncdesc} |
| 4462 | |
| 4463 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type, |
| 4464 | struct wrapperbase *wrapper, |
| 4465 | void *wrapped} |
| 4466 | \versionadded{2.2} |
| 4467 | \end{cfuncdesc} |
| 4468 | |
| 4469 | \begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr} |
| 4470 | Returns true if the descriptor objects \var{descr} describes a data |
| 4471 | attribute, or false if it describes a method. \var{descr} must be a |
| 4472 | descriptor object; there is no error checking. |
| 4473 | \versionadded{2.2} |
| 4474 | \end{cfuncdesc} |
| 4475 | |
| 4476 | \begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *} |
| 4477 | \versionadded{2.2} |
| 4478 | \end{cfuncdesc} |
| 4479 | |
| 4480 | |
| 4481 | \subsection{Slice Objects \label{slice-objects}} |
| 4482 | |
| 4483 | \begin{cvardesc}{PyTypeObject}{PySlice_Type} |
| 4484 | The type object for slice objects. This is the same as |
| 4485 | \code{types.SliceType}. |
| 4486 | \withsubitem{(in module types)}{\ttindex{SliceType}} |
| 4487 | \end{cvardesc} |
| 4488 | |
| 4489 | \begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob} |
| 4490 | Returns true if \var{ob} is a slice object; \var{ob} must not be |
| 4491 | \NULL. |
| 4492 | \end{cfuncdesc} |
| 4493 | |
| 4494 | \begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop, |
| 4495 | PyObject *step} |
| 4496 | Return a new slice object with the given values. The \var{start}, |
| 4497 | \var{stop}, and \var{step} parameters are used as the values of the |
| 4498 | slice object attributes of the same names. Any of the values may be |
| 4499 | \NULL, in which case the \code{None} will be used for the |
| 4500 | corresponding attribute. Returns \NULL{} if the new object could |
| 4501 | not be allocated. |
| 4502 | \end{cfuncdesc} |
| 4503 | |
| 4504 | \begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length, |
| 4505 | int *start, int *stop, int *step} |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4506 | \end{cfuncdesc} |
| 4507 | |
| 4508 | |
Fred Drake | bf88b68 | 2001-10-05 22:03:58 +0000 | [diff] [blame^] | 4509 | \subsection{Weak Reference Objects \label{weakref-objects}} |
| 4510 | |
| 4511 | Python supports \emph{weak references} as first-class objects. There |
| 4512 | are two specific object types which directly implement weak |
| 4513 | references. The first is a simple reference object, and the second |
| 4514 | acts as a proxy for the original object as much as it can. |
| 4515 | |
| 4516 | \begin{cfuncdesc}{int}{PyWeakref_Check}{ob} |
| 4517 | Return true if \var{ob} is either a reference or proxy object. |
| 4518 | \versionadded{2.2} |
| 4519 | \end{cfuncdesc} |
| 4520 | |
| 4521 | \begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob} |
| 4522 | Return true if \var{ob} is a reference object. |
| 4523 | \versionadded{2.2} |
| 4524 | \end{cfuncdesc} |
| 4525 | |
| 4526 | \begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob} |
| 4527 | Return true if \var{ob} is a proxy object. |
| 4528 | \versionadded{2.2} |
| 4529 | \end{cfuncdesc} |
| 4530 | |
| 4531 | \begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob, |
| 4532 | PyObject *callback} |
| 4533 | Return a weak reference object for the object \var{ob}. This will |
| 4534 | always return a new reference, but is not guaranteed to create a new |
| 4535 | object; an existing reference object may be returned. The second |
| 4536 | parameter, \var{callback}, can be a callable object that receives |
| 4537 | notification when \var{ob} is garbage collected; it should accept a |
| 4538 | single paramter, which will be the weak reference object itself. |
| 4539 | \var{callback} may also be \code{None} or \NULL. If \var{ob} |
| 4540 | is not a weakly-referencable object, or if \var{callback} is not |
| 4541 | callable, \code{None}, or \NULL, this will return \NULL{} and |
| 4542 | raise \exception{TypeError}. |
| 4543 | \versionadded{2.2} |
| 4544 | \end{cfuncdesc} |
| 4545 | |
| 4546 | \begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob, |
| 4547 | PyObject *callback} |
| 4548 | Return a weak reference proxy object for the object \var{ob}. This |
| 4549 | will always return a new reference, but is not guaranteed to create |
| 4550 | a new object; an existing proxy object may be returned. The second |
| 4551 | parameter, \var{callback}, can be a callable object that receives |
| 4552 | notification when \var{ob} is garbage collected; it should accept a |
| 4553 | single paramter, which will be the weak reference object itself. |
| 4554 | \var{callback} may also be \code{None} or \NULL. If \var{ob} is not |
| 4555 | a weakly-referencable object, or if \var{callback} is not callable, |
| 4556 | \code{None}, or \NULL, this will return \NULL{} and raise |
| 4557 | \exception{TypeError}. |
| 4558 | \versionadded{2.2} |
| 4559 | \end{cfuncdesc} |
| 4560 | |
| 4561 | \begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref} |
| 4562 | Returns the referenced object from a weak reference, \var{ref}. If |
| 4563 | the referent is no longer live, returns \NULL. |
| 4564 | \versionadded{2.2} |
| 4565 | \end{cfuncdesc} |
| 4566 | |
| 4567 | \begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref} |
| 4568 | Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a |
| 4569 | macro that does no error checking. |
| 4570 | \versionadded{2.2} |
| 4571 | \end{cfuncdesc} |
| 4572 | |
| 4573 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4574 | \subsection{CObjects \label{cObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4575 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4576 | \obindex{CObject} |
| 4577 | Refer to \emph{Extending and Embedding the Python Interpreter}, |
Fred Drake | d61d0d3 | 2001-09-23 02:05:26 +0000 | [diff] [blame] | 4578 | section 1.12 (``Providing a C API for an Extension Module), for more |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4579 | information on using these objects. |
| 4580 | |
| 4581 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4582 | \begin{ctypedesc}{PyCObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4583 | This subtype of \ctype{PyObject} represents an opaque value, useful for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4584 | C extension modules who need to pass an opaque value (as a |
| 4585 | \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] | 4586 | often used to make a C function pointer defined in one module |
| 4587 | available to other modules, so the regular import mechanism can be |
| 4588 | used to access C APIs defined in dynamically loaded modules. |
| 4589 | \end{ctypedesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4590 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4591 | \begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p} |
| 4592 | Returns true if its argument is a \ctype{PyCObject}. |
| 4593 | \end{cfuncdesc} |
| 4594 | |
| 4595 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 4596 | void (*destr)(void *)} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 4597 | Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The |
Fred Drake | dab4468 | 1999-05-13 18:41:14 +0000 | [diff] [blame] | 4598 | \var{destr} function will be called when the object is reclaimed, unless |
| 4599 | it is \NULL. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4600 | \end{cfuncdesc} |
| 4601 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4602 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj, |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 4603 | void* desc, void (*destr)(void *, void *) } |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4604 | Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The |
| 4605 | \var{destr} function will be called when the object is reclaimed. The |
| 4606 | \var{desc} argument can be used to pass extra callback data for the |
| 4607 | destructor function. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4608 | \end{cfuncdesc} |
| 4609 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4610 | \begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self} |
| 4611 | Returns the object \ctype{void *} that the |
| 4612 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4613 | \end{cfuncdesc} |
| 4614 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4615 | \begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self} |
| 4616 | Returns the description \ctype{void *} that the |
| 4617 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 4618 | \end{cfuncdesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 4619 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4620 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4621 | \chapter{Initialization, Finalization, and Threads |
| 4622 | \label{initialization}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4623 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4624 | \begin{cfuncdesc}{void}{Py_Initialize}{} |
| 4625 | Initialize the Python interpreter. In an application embedding |
| 4626 | Python, this should be called before using any other Python/C API |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4627 | functions; with the exception of |
| 4628 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, |
| 4629 | \cfunction{PyEval_InitThreads()}\ttindex{PyEval_InitThreads()}, |
| 4630 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()}, |
| 4631 | and \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()}. |
| 4632 | This initializes the table of loaded modules (\code{sys.modules}), and |
| 4633 | \withsubitem{(in module sys)}{\ttindex{modules}\ttindex{path}}creates the |
| 4634 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4635 | \module{__main__}\refbimodindex{__main__} and |
| 4636 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4637 | search\indexiii{module}{search}{path} path (\code{sys.path}). |
| 4638 | It does not set \code{sys.argv}; use |
| 4639 | \cfunction{PySys_SetArgv()}\ttindex{PySys_SetArgv()} for that. This |
| 4640 | is a no-op when called for a second time (without calling |
| 4641 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} first). There is no |
| 4642 | return value; it is a fatal error if the initialization fails. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4643 | \end{cfuncdesc} |
| 4644 | |
| 4645 | \begin{cfuncdesc}{int}{Py_IsInitialized}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4646 | Return true (nonzero) when the Python interpreter has been |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4647 | initialized, false (zero) if not. After \cfunction{Py_Finalize()} is |
| 4648 | called, this returns false until \cfunction{Py_Initialize()} is called |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4649 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4650 | \end{cfuncdesc} |
| 4651 | |
| 4652 | \begin{cfuncdesc}{void}{Py_Finalize}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4653 | Undo all initializations made by \cfunction{Py_Initialize()} and |
| 4654 | subsequent use of Python/C API functions, and destroy all |
| 4655 | sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were |
| 4656 | created and not yet destroyed since the last call to |
| 4657 | \cfunction{Py_Initialize()}. Ideally, this frees all memory allocated |
| 4658 | by the Python interpreter. This is a no-op when called for a second |
| 4659 | time (without calling \cfunction{Py_Initialize()} again first). There |
| 4660 | is no return value; errors during finalization are ignored. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4661 | |
| 4662 | This function is provided for a number of reasons. An embedding |
| 4663 | application might want to restart Python without having to restart the |
| 4664 | application itself. An application that has loaded the Python |
| 4665 | interpreter from a dynamically loadable library (or DLL) might want to |
| 4666 | free all memory allocated by Python before unloading the DLL. During a |
| 4667 | hunt for memory leaks in an application a developer might want to free |
| 4668 | all memory allocated by Python before exiting from the application. |
| 4669 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4670 | \strong{Bugs and caveats:} The destruction of modules and objects in |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4671 | modules is done in random order; this may cause destructors |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4672 | (\method{__del__()} methods) to fail when they depend on other objects |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4673 | (even functions) or modules. Dynamically loaded extension modules |
| 4674 | loaded by Python are not unloaded. Small amounts of memory allocated |
| 4675 | by the Python interpreter may not be freed (if you find a leak, please |
| 4676 | report it). Memory tied up in circular references between objects is |
| 4677 | not freed. Some memory allocated by extension modules may not be |
| 4678 | freed. Some extension may not work properly if their initialization |
| 4679 | routine is called more than once; this can happen if an applcation |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4680 | calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more |
| 4681 | than once. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4682 | \end{cfuncdesc} |
| 4683 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4684 | \begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4685 | Create a new sub-interpreter. This is an (almost) totally separate |
| 4686 | environment for the execution of Python code. In particular, the new |
| 4687 | interpreter has separate, independent versions of all imported |
| 4688 | modules, including the fundamental modules |
| 4689 | \module{__builtin__}\refbimodindex{__builtin__}, |
| 4690 | \module{__main__}\refbimodindex{__main__} and |
| 4691 | \module{sys}\refbimodindex{sys}. The table of loaded modules |
| 4692 | (\code{sys.modules}) and the module search path (\code{sys.path}) are |
| 4693 | also separate. The new environment has no \code{sys.argv} variable. |
| 4694 | It has new standard I/O stream file objects \code{sys.stdin}, |
| 4695 | \code{sys.stdout} and \code{sys.stderr} (however these refer to the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4696 | same underlying \ctype{FILE} structures in the C library). |
| 4697 | \withsubitem{(in module sys)}{ |
| 4698 | \ttindex{stdout}\ttindex{stderr}\ttindex{stdin}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4699 | |
| 4700 | The return value points to the first thread state created in the new |
| 4701 | sub-interpreter. This thread state is made the current thread state. |
| 4702 | Note that no actual thread is created; see the discussion of thread |
| 4703 | states below. If creation of the new interpreter is unsuccessful, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4704 | \NULL{} is returned; no exception is set since the exception state |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4705 | is stored in the current thread state and there may not be a current |
| 4706 | thread state. (Like all other Python/C API functions, the global |
| 4707 | interpreter lock must be held before calling this function and is |
| 4708 | still held when it returns; however, unlike most other Python/C API |
| 4709 | functions, there needn't be a current thread state on entry.) |
| 4710 | |
| 4711 | Extension modules are shared between (sub-)interpreters as follows: |
| 4712 | the first time a particular extension is imported, it is initialized |
| 4713 | normally, and a (shallow) copy of its module's dictionary is |
| 4714 | squirreled away. When the same extension is imported by another |
| 4715 | (sub-)interpreter, a new module is initialized and filled with the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4716 | contents of this copy; the extension's \code{init} function is not |
| 4717 | called. Note that this is different from what happens when an |
| 4718 | extension is imported after the interpreter has been completely |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4719 | re-initialized by calling |
| 4720 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 4721 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}; in that case, |
| 4722 | the extension's \code{init\var{module}} function \emph{is} called |
| 4723 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4724 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4725 | \strong{Bugs and caveats:} Because sub-interpreters (and the main |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4726 | interpreter) are part of the same process, the insulation between them |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4727 | isn't perfect --- for example, using low-level file operations like |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4728 | \withsubitem{(in module os)}{\ttindex{close()}} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4729 | \function{os.close()} they can (accidentally or maliciously) affect each |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4730 | other's open files. Because of the way extensions are shared between |
| 4731 | (sub-)interpreters, some extensions may not work properly; this is |
| 4732 | especially likely when the extension makes use of (static) global |
| 4733 | variables, or when the extension manipulates its module's dictionary |
| 4734 | after its initialization. It is possible to insert objects created in |
| 4735 | one sub-interpreter into a namespace of another sub-interpreter; this |
| 4736 | should be done with great care to avoid sharing user-defined |
| 4737 | functions, methods, instances or classes between sub-interpreters, |
| 4738 | since import operations executed by such objects may affect the |
| 4739 | wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is |
| 4740 | a hard-to-fix bug that will be addressed in a future release.) |
| 4741 | \end{cfuncdesc} |
| 4742 | |
| 4743 | \begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate} |
| 4744 | Destroy the (sub-)interpreter represented by the given thread state. |
| 4745 | The given thread state must be the current thread state. See the |
| 4746 | discussion of thread states below. When the call returns, the current |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4747 | thread state is \NULL{}. All thread states associated with this |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4748 | interpreted are destroyed. (The global interpreter lock must be held |
| 4749 | before calling this function and is still held when it returns.) |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4750 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} will destroy all |
| 4751 | sub-interpreters that haven't been explicitly destroyed at that point. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4752 | \end{cfuncdesc} |
| 4753 | |
| 4754 | \begin{cfuncdesc}{void}{Py_SetProgramName}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4755 | This function should be called before |
| 4756 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()} is called |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4757 | 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] | 4758 | the value of the \code{argv[0]} argument to the |
| 4759 | \cfunction{main()}\ttindex{main()} function of the program. This is |
| 4760 | used by \cfunction{Py_GetPath()}\ttindex{Py_GetPath()} and some other |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4761 | functions below to find the Python run-time libraries relative to the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4762 | interpreter executable. The default value is \code{'python'}. The |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4763 | argument should point to a zero-terminated character string in static |
| 4764 | storage whose contents will not change for the duration of the |
| 4765 | program's execution. No code in the Python interpreter will change |
| 4766 | the contents of this storage. |
| 4767 | \end{cfuncdesc} |
| 4768 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4769 | \begin{cfuncdesc}{char*}{Py_GetProgramName}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4770 | Return the program name set with |
| 4771 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, or the |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4772 | default. The returned string points into static storage; the caller |
| 4773 | should not modify its value. |
| 4774 | \end{cfuncdesc} |
| 4775 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4776 | \begin{cfuncdesc}{char*}{Py_GetPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4777 | Return the \emph{prefix} for installed platform-independent files. This |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4778 | is derived through a number of complicated rules from the program name |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4779 | set with \cfunction{Py_SetProgramName()} and some environment variables; |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4780 | for example, if the program name is \code{'/usr/local/bin/python'}, |
| 4781 | the prefix is \code{'/usr/local'}. The returned string points into |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4782 | static storage; the caller should not modify its value. This |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 4783 | corresponds to the \makevar{prefix} variable in the top-level |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4784 | \file{Makefile} and the \longprogramopt{prefix} argument to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4785 | \program{configure} script at build time. The value is available to |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4786 | 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] | 4787 | also the next function. |
| 4788 | \end{cfuncdesc} |
| 4789 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4790 | \begin{cfuncdesc}{char*}{Py_GetExecPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4791 | Return the \emph{exec-prefix} for installed platform-\emph{de}pendent |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4792 | files. This is derived through a number of complicated rules from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4793 | program name set with \cfunction{Py_SetProgramName()} and some environment |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4794 | variables; for example, if the program name is |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4795 | \code{'/usr/local/bin/python'}, the exec-prefix is |
| 4796 | \code{'/usr/local'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4797 | the caller should not modify its value. This corresponds to the |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 4798 | \makevar{exec_prefix} variable in the top-level \file{Makefile} and the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4799 | \longprogramopt{exec-prefix} argument to the |
Fred Drake | 310ee61 | 1999-11-09 17:31:42 +0000 | [diff] [blame] | 4800 | \program{configure} script at build time. The value is available to |
| 4801 | 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] | 4802 | |
| 4803 | Background: The exec-prefix differs from the prefix when platform |
| 4804 | dependent files (such as executables and shared libraries) are |
| 4805 | installed in a different directory tree. In a typical installation, |
| 4806 | platform dependent files may be installed in the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4807 | \file{/usr/local/plat} subtree while platform independent may be |
| 4808 | installed in \file{/usr/local}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4809 | |
| 4810 | Generally speaking, a platform is a combination of hardware and |
| 4811 | software families, e.g. Sparc machines running the Solaris 2.x |
| 4812 | operating system are considered the same platform, but Intel machines |
| 4813 | running Solaris 2.x are another platform, and Intel machines running |
| 4814 | Linux are yet another platform. Different major revisions of the same |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4815 | operating system generally also form different platforms. Non-\UNIX{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4816 | operating systems are a different story; the installation strategies |
| 4817 | on those systems are so different that the prefix and exec-prefix are |
| 4818 | meaningless, and set to the empty string. Note that compiled Python |
| 4819 | bytecode files are platform independent (but not independent from the |
| 4820 | Python version by which they were compiled!). |
| 4821 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4822 | System administrators will know how to configure the \program{mount} or |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4823 | \program{automount} programs to share \file{/usr/local} between platforms |
| 4824 | 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] | 4825 | platform. |
| 4826 | \end{cfuncdesc} |
| 4827 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4828 | \begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4829 | Return the full program name of the Python executable; this is |
| 4830 | computed as a side-effect of deriving the default module search path |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4831 | from the program name (set by |
| 4832 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()} above). |
| 4833 | The returned string points into static storage; the caller should not |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4834 | modify its value. The value is available to Python code as |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 4835 | \code{sys.executable}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4836 | \withsubitem{(in module sys)}{\ttindex{executable}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4837 | \end{cfuncdesc} |
| 4838 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4839 | \begin{cfuncdesc}{char*}{Py_GetPath}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 4840 | \indexiii{module}{search}{path} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4841 | Return the default module search path; this is computed from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4842 | program name (set by \cfunction{Py_SetProgramName()} above) and some |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4843 | environment variables. The returned string consists of a series of |
| 4844 | directory names separated by a platform dependent delimiter character. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 4845 | The delimiter character is \character{:} on \UNIX{}, \character{;} on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4846 | DOS/Windows, and \character{\e n} (the \ASCII{} newline character) on |
Fred Drake | e5bc497 | 1998-02-12 23:36:49 +0000 | [diff] [blame] | 4847 | Macintosh. The returned string points into static storage; the caller |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4848 | should not modify its value. The value is available to Python code |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4849 | as the list \code{sys.path}\withsubitem{(in module sys)}{\ttindex{path}}, |
| 4850 | which may be modified to change the future search path for loaded |
| 4851 | modules. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4852 | |
| 4853 | % XXX should give the exact rules |
| 4854 | \end{cfuncdesc} |
| 4855 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4856 | \begin{cfuncdesc}{const char*}{Py_GetVersion}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4857 | Return the version of this Python interpreter. This is a string that |
| 4858 | looks something like |
| 4859 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 4860 | \begin{verbatim} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4861 | "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] | 4862 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4863 | |
| 4864 | The first word (up to the first space character) is the current Python |
| 4865 | version; the first three characters are the major and minor version |
| 4866 | separated by a period. The returned string points into static storage; |
| 4867 | the caller should not modify its value. The value is available to |
| 4868 | Python code as the list \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4869 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4870 | \end{cfuncdesc} |
| 4871 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4872 | \begin{cfuncdesc}{const char*}{Py_GetPlatform}{} |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 4873 | Return the platform identifier for the current platform. On \UNIX{}, |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4874 | this is formed from the ``official'' name of the operating system, |
| 4875 | converted to lower case, followed by the major revision number; e.g., |
| 4876 | 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] | 4877 | \code{'sunos5'}. On Macintosh, it is \code{'mac'}. On Windows, it |
| 4878 | is \code{'win'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4879 | the caller should not modify its value. The value is available to |
| 4880 | Python code as \code{sys.platform}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4881 | \withsubitem{(in module sys)}{\ttindex{platform}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4882 | \end{cfuncdesc} |
| 4883 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4884 | \begin{cfuncdesc}{const char*}{Py_GetCopyright}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4885 | Return the official copyright string for the current Python version, |
| 4886 | for example |
| 4887 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4888 | \code{'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4889 | |
| 4890 | The returned string points into static storage; the caller should not |
| 4891 | modify its value. The value is available to Python code as the list |
| 4892 | \code{sys.copyright}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4893 | \withsubitem{(in module sys)}{\ttindex{copyright}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4894 | \end{cfuncdesc} |
| 4895 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4896 | \begin{cfuncdesc}{const char*}{Py_GetCompiler}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4897 | Return an indication of the compiler used to build the current Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4898 | version, in square brackets, for example: |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4899 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4900 | \begin{verbatim} |
| 4901 | "[GCC 2.7.2.2]" |
| 4902 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4903 | |
| 4904 | The returned string points into static storage; the caller should not |
| 4905 | modify its value. The value is available to Python code as part of |
| 4906 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4907 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4908 | \end{cfuncdesc} |
| 4909 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4910 | \begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4911 | Return information about the sequence number and build date and time |
| 4912 | of the current Python interpreter instance, for example |
| 4913 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 4914 | \begin{verbatim} |
| 4915 | "#67, Aug 1 1997, 22:34:28" |
| 4916 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4917 | |
| 4918 | The returned string points into static storage; the caller should not |
| 4919 | modify its value. The value is available to Python code as part of |
| 4920 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4921 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4922 | \end{cfuncdesc} |
| 4923 | |
| 4924 | \begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4925 | Set \code{sys.argv} based on \var{argc} and \var{argv}. These |
| 4926 | parameters are similar to those passed to the program's |
| 4927 | \cfunction{main()}\ttindex{main()} function with the difference that |
| 4928 | the first entry should refer to the script file to be executed rather |
| 4929 | than the executable hosting the Python interpreter. If there isn't a |
| 4930 | script that will be run, the first entry in \var{argv} can be an empty |
| 4931 | string. If this function fails to initialize \code{sys.argv}, a fatal |
| 4932 | condition is signalled using |
| 4933 | \cfunction{Py_FatalError()}\ttindex{Py_FatalError()}. |
| 4934 | \withsubitem{(in module sys)}{\ttindex{argv}} |
| 4935 | % XXX impl. doesn't seem consistent in allowing 0/NULL for the params; |
| 4936 | % check w/ Guido. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4937 | \end{cfuncdesc} |
| 4938 | |
| 4939 | % XXX Other PySys thingies (doesn't really belong in this chapter) |
| 4940 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4941 | \section{Thread State and the Global Interpreter Lock |
| 4942 | \label{threads}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4943 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4944 | \index{global interpreter lock} |
| 4945 | \index{interpreter lock} |
| 4946 | \index{lock, interpreter} |
| 4947 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4948 | The Python interpreter is not fully thread safe. In order to support |
| 4949 | multi-threaded Python programs, there's a global lock that must be |
| 4950 | held by the current thread before it can safely access Python objects. |
| 4951 | Without the lock, even the simplest operations could cause problems in |
Fred Drake | 7baf3d4 | 1998-02-20 00:45:52 +0000 | [diff] [blame] | 4952 | a multi-threaded program: for example, when two threads simultaneously |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4953 | increment the reference count of the same object, the reference count |
| 4954 | could end up being incremented only once instead of twice. |
| 4955 | |
| 4956 | Therefore, the rule exists that only the thread that has acquired the |
| 4957 | global interpreter lock may operate on Python objects or call Python/C |
| 4958 | API functions. In order to support multi-threaded Python programs, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4959 | the interpreter regularly releases and reacquires the lock --- by |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4960 | default, every ten bytecode instructions (this can be changed with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4961 | \withsubitem{(in module sys)}{\ttindex{setcheckinterval()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4962 | \function{sys.setcheckinterval()}). The lock is also released and |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4963 | reacquired around potentially blocking I/O operations like reading or |
| 4964 | writing a file, so that other threads can run while the thread that |
| 4965 | requests the I/O is waiting for the I/O operation to complete. |
| 4966 | |
| 4967 | The Python interpreter needs to keep some bookkeeping information |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4968 | separate per thread --- for this it uses a data structure called |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4969 | \ctype{PyThreadState}\ttindex{PyThreadState}. This is new in Python |
| 4970 | 1.5; in earlier versions, such state was stored in global variables, |
| 4971 | and switching threads could cause problems. In particular, exception |
| 4972 | handling is now thread safe, when the application uses |
| 4973 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
| 4974 | \function{sys.exc_info()} to access the exception last raised in the |
| 4975 | current thread. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4976 | |
| 4977 | There's one global variable left, however: the pointer to the current |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4978 | \ctype{PyThreadState}\ttindex{PyThreadState} structure. While most |
| 4979 | thread packages have a way to store ``per-thread global data,'' |
| 4980 | Python's internal platform independent thread abstraction doesn't |
| 4981 | support this yet. Therefore, the current thread state must be |
| 4982 | manipulated explicitly. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4983 | |
| 4984 | This is easy enough in most cases. Most code manipulating the global |
| 4985 | interpreter lock has the following simple structure: |
| 4986 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4987 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4988 | Save the thread state in a local variable. |
| 4989 | Release the interpreter lock. |
| 4990 | ...Do some blocking I/O operation... |
| 4991 | Reacquire the interpreter lock. |
| 4992 | Restore the thread state from the local variable. |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4993 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4994 | |
| 4995 | This is so common that a pair of macros exists to simplify it: |
| 4996 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 4997 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4998 | Py_BEGIN_ALLOW_THREADS |
| 4999 | ...Do some blocking I/O operation... |
| 5000 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 5001 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5002 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5003 | The \code{Py_BEGIN_ALLOW_THREADS}\ttindex{Py_BEGIN_ALLOW_THREADS} macro |
| 5004 | opens a new block and declares a hidden local variable; the |
| 5005 | \code{Py_END_ALLOW_THREADS}\ttindex{Py_END_ALLOW_THREADS} macro closes |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5006 | the block. Another advantage of using these two macros is that when |
| 5007 | Python is compiled without thread support, they are defined empty, |
| 5008 | thus saving the thread state and lock manipulations. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5009 | |
| 5010 | When thread support is enabled, the block above expands to the |
| 5011 | following code: |
| 5012 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 5013 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5014 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5015 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5016 | _save = PyEval_SaveThread(); |
| 5017 | ...Do some blocking I/O operation... |
| 5018 | PyEval_RestoreThread(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 5019 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5020 | |
| 5021 | Using even lower level primitives, we can get roughly the same effect |
| 5022 | as follows: |
| 5023 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 5024 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5025 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5026 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5027 | _save = PyThreadState_Swap(NULL); |
| 5028 | PyEval_ReleaseLock(); |
| 5029 | ...Do some blocking I/O operation... |
| 5030 | PyEval_AcquireLock(); |
| 5031 | PyThreadState_Swap(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 5032 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5033 | |
| 5034 | There are some subtle differences; in particular, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5035 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()} saves |
| 5036 | and restores the value of the global variable |
| 5037 | \cdata{errno}\ttindex{errno}, since the lock manipulation does not |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 5038 | guarantee that \cdata{errno} is left alone. Also, when thread support |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5039 | is disabled, |
| 5040 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} and |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5041 | \cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5042 | case, \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} and |
| 5043 | \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()} are not |
| 5044 | available. This is done so that dynamically loaded extensions |
| 5045 | compiled with thread support enabled can be loaded by an interpreter |
| 5046 | that was compiled with disabled thread support. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5047 | |
| 5048 | The global interpreter lock is used to protect the pointer to the |
| 5049 | current thread state. When releasing the lock and saving the thread |
| 5050 | state, the current thread state pointer must be retrieved before the |
| 5051 | lock is released (since another thread could immediately acquire the |
| 5052 | lock and store its own thread state in the global variable). |
Fred Drake | ffe58ca | 2000-09-29 17:31:54 +0000 | [diff] [blame] | 5053 | Conversely, when acquiring the lock and restoring the thread state, |
| 5054 | the lock must be acquired before storing the thread state pointer. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5055 | |
| 5056 | 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] | 5057 | 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] | 5058 | lock, nor is there a thread state data structure for them. Such |
| 5059 | threads must bootstrap themselves into existence, by first creating a |
| 5060 | thread state data structure, then acquiring the lock, and finally |
| 5061 | storing their thread state pointer, before they can start using the |
| 5062 | Python/C API. When they are done, they should reset the thread state |
| 5063 | pointer, release the lock, and finally free their thread state data |
| 5064 | structure. |
| 5065 | |
| 5066 | When creating a thread data structure, you need to provide an |
| 5067 | interpreter state data structure. The interpreter state data |
| 5068 | structure hold global data that is shared by all threads in an |
| 5069 | interpreter, for example the module administration |
| 5070 | (\code{sys.modules}). Depending on your needs, you can either create |
| 5071 | a new interpreter state data structure, or share the interpreter state |
| 5072 | data structure used by the Python main thread (to access the latter, |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 5073 | 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] | 5074 | this must be done by a thread that is created by Python or by the main |
| 5075 | thread after Python is initialized). |
| 5076 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5077 | |
| 5078 | \begin{ctypedesc}{PyInterpreterState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5079 | This data structure represents the state shared by a number of |
| 5080 | cooperating threads. Threads belonging to the same interpreter |
| 5081 | share their module administration and a few other internal items. |
| 5082 | There are no public members in this structure. |
| 5083 | |
| 5084 | Threads belonging to different interpreters initially share nothing, |
| 5085 | except process state like available memory, open file descriptors and |
| 5086 | such. The global interpreter lock is also shared by all threads, |
| 5087 | regardless of to which interpreter they belong. |
| 5088 | \end{ctypedesc} |
| 5089 | |
| 5090 | \begin{ctypedesc}{PyThreadState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5091 | This data structure represents the state of a single thread. The only |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 5092 | public data member is \ctype{PyInterpreterState *}\member{interp}, |
| 5093 | which points to this thread's interpreter state. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5094 | \end{ctypedesc} |
| 5095 | |
| 5096 | \begin{cfuncdesc}{void}{PyEval_InitThreads}{} |
| 5097 | Initialize and acquire the global interpreter lock. It should be |
| 5098 | called in the main thread before creating a second thread or engaging |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5099 | in any other thread operations such as |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5100 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} or |
| 5101 | \code{PyEval_ReleaseThread(\var{tstate})}\ttindex{PyEval_ReleaseThread()}. |
| 5102 | It is not needed before calling |
| 5103 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} or |
| 5104 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5105 | |
| 5106 | 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] | 5107 | this function before calling |
| 5108 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5109 | |
| 5110 | When only the main thread exists, no lock operations are needed. This |
| 5111 | is a common situation (most Python programs do not use threads), and |
| 5112 | the lock operations slow the interpreter down a bit. Therefore, the |
| 5113 | lock is not created initially. This situation is equivalent to having |
| 5114 | acquired the lock: when there is only a single thread, all object |
| 5115 | accesses are safe. Therefore, when this function initializes the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 5116 | lock, it also acquires it. Before the Python |
| 5117 | \module{thread}\refbimodindex{thread} module creates a new thread, |
| 5118 | knowing that either it has the lock or the lock hasn't been created |
| 5119 | yet, it calls \cfunction{PyEval_InitThreads()}. When this call |
| 5120 | returns, it is guaranteed that the lock has been created and that it |
| 5121 | has acquired it. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5122 | |
| 5123 | It is \strong{not} safe to call this function when it is unknown which |
| 5124 | thread (if any) currently has the global interpreter lock. |
| 5125 | |
| 5126 | This function is not available when thread support is disabled at |
| 5127 | compile time. |
| 5128 | \end{cfuncdesc} |
| 5129 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5130 | \begin{cfuncdesc}{void}{PyEval_AcquireLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5131 | Acquire the global interpreter lock. The lock must have been created |
| 5132 | earlier. If this thread already has the lock, a deadlock ensues. |
| 5133 | This function is not available when thread support is disabled at |
| 5134 | compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5135 | \end{cfuncdesc} |
| 5136 | |
| 5137 | \begin{cfuncdesc}{void}{PyEval_ReleaseLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5138 | Release the global interpreter lock. The lock must have been created |
| 5139 | earlier. This function is not available when thread support is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5140 | disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5141 | \end{cfuncdesc} |
| 5142 | |
| 5143 | \begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5144 | Acquire the global interpreter lock and then set the current thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 5145 | 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] | 5146 | have been created earlier. If this thread already has the lock, |
| 5147 | deadlock ensues. This function is not available when thread support |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5148 | is disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5149 | \end{cfuncdesc} |
| 5150 | |
| 5151 | \begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 5152 | Reset the current thread state to \NULL{} and release the global |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5153 | interpreter lock. The lock must have been created earlier and must be |
| 5154 | 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] | 5155 | be \NULL{}, is only used to check that it represents the current |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5156 | thread state --- if it isn't, a fatal error is reported. This |
| 5157 | function is not available when thread support is disabled at compile |
| 5158 | time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5159 | \end{cfuncdesc} |
| 5160 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5161 | \begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5162 | Release the interpreter lock (if it has been created and thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 5163 | support is enabled) and reset the thread state to \NULL{}, |
| 5164 | returning the previous thread state (which is not \NULL{}). If |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5165 | the lock has been created, the current thread must have acquired it. |
| 5166 | (This function is available even when thread support is disabled at |
| 5167 | compile time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5168 | \end{cfuncdesc} |
| 5169 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5170 | \begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5171 | Acquire the interpreter lock (if it has been created and thread |
| 5172 | 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] | 5173 | 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] | 5174 | thread must not have acquired it, otherwise deadlock ensues. (This |
| 5175 | function is available even when thread support is disabled at compile |
| 5176 | time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5177 | \end{cfuncdesc} |
| 5178 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5179 | The following macros are normally used without a trailing semicolon; |
| 5180 | look for example usage in the Python source distribution. |
| 5181 | |
| 5182 | \begin{csimplemacrodesc}{Py_BEGIN_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5183 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5184 | \samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5185 | Note that it contains an opening brace; it must be matched with a |
| 5186 | following \code{Py_END_ALLOW_THREADS} macro. See above for further |
| 5187 | discussion of this macro. It is a no-op when thread support is |
| 5188 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5189 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5190 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5191 | \begin{csimplemacrodesc}{Py_END_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5192 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5193 | \samp{PyEval_RestoreThread(_save); \}}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5194 | Note that it contains a closing brace; it must be matched with an |
| 5195 | earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further |
| 5196 | discussion of this macro. It is a no-op when thread support is |
| 5197 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5198 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5199 | |
Thomas Wouters | e30ac57 | 2001-07-09 14:35:01 +0000 | [diff] [blame] | 5200 | \begin{csimplemacrodesc}{Py_BLOCK_THREADS} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5201 | This macro expands to \samp{PyEval_RestoreThread(_save);}: it |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5202 | is equivalent to \code{Py_END_ALLOW_THREADS} without the closing |
| 5203 | brace. It is a no-op when thread support is disabled at compile |
| 5204 | time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5205 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5206 | |
Thomas Wouters | e30ac57 | 2001-07-09 14:35:01 +0000 | [diff] [blame] | 5207 | \begin{csimplemacrodesc}{Py_UNBLOCK_THREADS} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5208 | This macro expands to \samp{_save = PyEval_SaveThread();}: it is |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5209 | equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace |
| 5210 | and variable declaration. It is a no-op when thread support is |
| 5211 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5212 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5213 | |
| 5214 | All of the following functions are only available when thread support |
| 5215 | is enabled at compile time, and must be called only when the |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 5216 | interpreter lock has been created. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5217 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5218 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{} |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 5219 | Create a new interpreter state object. The interpreter lock need not |
| 5220 | be held, but may be held if it is necessary to serialize calls to this |
| 5221 | function. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5222 | \end{cfuncdesc} |
| 5223 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5224 | \begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp} |
| 5225 | Reset all information in an interpreter state object. The interpreter |
| 5226 | lock must be held. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5227 | \end{cfuncdesc} |
| 5228 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5229 | \begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp} |
| 5230 | Destroy an interpreter state object. The interpreter lock need not be |
| 5231 | held. The interpreter state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5232 | call to \cfunction{PyInterpreterState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5233 | \end{cfuncdesc} |
| 5234 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5235 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5236 | Create a new thread state object belonging to the given interpreter |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 5237 | object. The interpreter lock need not be held, but may be held if it |
| 5238 | is necessary to serialize calls to this function. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5239 | \end{cfuncdesc} |
| 5240 | |
| 5241 | \begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate} |
| 5242 | Reset all information in a thread state object. The interpreter lock |
| 5243 | must be held. |
| 5244 | \end{cfuncdesc} |
| 5245 | |
| 5246 | \begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate} |
| 5247 | Destroy a thread state object. The interpreter lock need not be |
| 5248 | held. The thread state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5249 | call to \cfunction{PyThreadState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5250 | \end{cfuncdesc} |
| 5251 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5252 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5253 | Return the current thread state. The interpreter lock must be held. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 5254 | When the current thread state is \NULL{}, this issues a fatal |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 5255 | error (so that the caller needn't check for \NULL{}). |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5256 | \end{cfuncdesc} |
| 5257 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5258 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5259 | 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] | 5260 | argument \var{tstate}, which may be \NULL{}. The interpreter lock |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5261 | must be held. |
| 5262 | \end{cfuncdesc} |
| 5263 | |
Fred Drake | 24e6219 | 2001-05-21 15:56:55 +0000 | [diff] [blame] | 5264 | \begin{cfuncdesc}{PyObject*}{PyThreadState_GetDict}{} |
| 5265 | Return a dictionary in which extensions can store thread-specific |
| 5266 | state information. Each extension should use a unique key to use to |
| 5267 | store state in the dictionary. If this function returns \NULL, an |
| 5268 | exception has been raised and the caller should allow it to |
| 5269 | propogate. |
| 5270 | \end{cfuncdesc} |
| 5271 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 5272 | |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5273 | \section{Profiling and Tracing \label{profiling}} |
| 5274 | |
| 5275 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 5276 | |
| 5277 | The Python interpreter provides some low-level support for attaching |
| 5278 | profiling and execution tracing facilities. These are used for |
| 5279 | profiling, debugging, and coverage analysis tools. |
| 5280 | |
| 5281 | Starting with Python 2.2, the implementation of this facility was |
| 5282 | substantially revised, and an interface from C was added. This C |
| 5283 | interface allows the profiling or tracing code to avoid the overhead |
| 5284 | of calling through Python-level callable objects, making a direct C |
| 5285 | function call instead. The essential attributes of the facility have |
| 5286 | not changed; the interface allows trace functions to be installed |
| 5287 | per-thread, and the basic events reported to the trace function are |
| 5288 | the same as had been reported to the Python-level trace functions in |
| 5289 | previous versions. |
| 5290 | |
| 5291 | \begin{ctypedesc}[Py_tracefunc]{int (*Py_tracefunc)(PyObject *obj, |
| 5292 | PyFrameObject *frame, int what, |
| 5293 | PyObject *arg)} |
| 5294 | The type of the trace function registered using |
| 5295 | \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}. |
| 5296 | The first parameter is the object passed to the registration |
Fred Drake | 6f3d826 | 2001-10-03 21:52:51 +0000 | [diff] [blame] | 5297 | function as \var{obj}, \var{frame} is the frame object to which the |
| 5298 | event pertains, \var{what} is one of the constants |
| 5299 | \constant{PyTrace_CALL}, \constant{PyTrace_EXCEPT}, |
| 5300 | \constant{PyTrace_LINE} or \constant{PyTrace_RETURN}, and \var{arg} |
| 5301 | depends on the value of \var{what}: |
| 5302 | |
| 5303 | \begin{tableii}{l|l}{constant}{Value of \var{what}}{Meaning of \var{arg}} |
| 5304 | \lineii{PyTrace_CALL}{Always \NULL.} |
| 5305 | \lineii{PyTrace_EXCEPT}{Exception information as returned by |
| 5306 | \function{sys.exc_info()}.} |
| 5307 | \lineii{PyTrace_LINE}{Always \NULL.} |
| 5308 | \lineii{PyTrace_RETURN}{Value being returned to the caller.} |
| 5309 | \end{tableii} |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5310 | \end{ctypedesc} |
| 5311 | |
| 5312 | \begin{cvardesc}{int}{PyTrace_CALL} |
| 5313 | The value of the \var{what} parameter to a \ctype{Py_tracefunc} |
Fred Drake | 6f3d826 | 2001-10-03 21:52:51 +0000 | [diff] [blame] | 5314 | function when a new call to a function or method is being reported, |
| 5315 | or a new entry into a generator. Note that the creation of the |
| 5316 | iterator for a generator function is not reported as there is no |
| 5317 | control transfer to the Python bytecode in the corresponding frame. |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5318 | \end{cvardesc} |
| 5319 | |
| 5320 | \begin{cvardesc}{int}{PyTrace_EXCEPT} |
Fred Drake | 6f3d826 | 2001-10-03 21:52:51 +0000 | [diff] [blame] | 5321 | The value of the \var{what} parameter to a \ctype{Py_tracefunc} |
| 5322 | function when an exception has been raised by Python code as the |
| 5323 | result of an operation. The operation may have explictly intended |
| 5324 | to raise the operation (as with a \keyword{raise} statement), or may |
| 5325 | have triggered an exception in the runtime as a result of the |
| 5326 | specific operation. |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5327 | \end{cvardesc} |
| 5328 | |
| 5329 | \begin{cvardesc}{int}{PyTrace_LINE} |
| 5330 | The value passed as the \var{what} parameter to a trace function |
| 5331 | (but not a profiling function) when a line-number event is being |
| 5332 | reported. |
| 5333 | \end{cvardesc} |
| 5334 | |
| 5335 | \begin{cvardesc}{int}{PyTrace_RETURN} |
| 5336 | The value for the \var{what} parameter to \ctype{Py_tracefunc} |
| 5337 | functions when a call is returning without propogating an exception. |
| 5338 | \end{cvardesc} |
| 5339 | |
| 5340 | \begin{cfuncdesc}{void}{PyEval_SetProfile}{Py_tracefunc func, PyObject *obj} |
Fred Drake | f90490e | 2001-08-02 18:00:28 +0000 | [diff] [blame] | 5341 | Set the profiler function to \var{func}. The \var{obj} parameter is |
| 5342 | passed to the function as its first parameter, and may be any Python |
| 5343 | object, or \NULL. If the profile function needs to maintain state, |
| 5344 | using a different value for \var{obj} for each thread provides a |
| 5345 | convenient and thread-safe place to store it. The profile function |
| 5346 | is called for all monitored events except the line-number events. |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5347 | \end{cfuncdesc} |
| 5348 | |
| 5349 | \begin{cfuncdesc}{void}{PyEval_SetTrace}{Py_tracefunc func, PyObject *obj} |
Fred Drake | f90490e | 2001-08-02 18:00:28 +0000 | [diff] [blame] | 5350 | Set the the tracing function to \var{func}. This is similar to |
| 5351 | \cfunction{PyEval_SetProfile()}, except the tracing function does |
| 5352 | receive line-number events. |
Fred Drake | 68db730 | 2001-07-17 19:48:30 +0000 | [diff] [blame] | 5353 | \end{cfuncdesc} |
| 5354 | |
| 5355 | |
Fred Drake | 0197858 | 2001-08-08 19:14:53 +0000 | [diff] [blame] | 5356 | \section{Advanced Debugger Support \label{advanced-debugging}} |
| 5357 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 5358 | |
| 5359 | These functions are only intended to be used by advanced debugging |
| 5360 | tools. |
| 5361 | |
| 5362 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_Head}{} |
| 5363 | Return the interpreter state object at the head of the list of all |
| 5364 | such objects. |
| 5365 | \versionadded{2.2} |
| 5366 | \end{cfuncdesc} |
| 5367 | |
| 5368 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_Next}{PyInterpreterState *interp} |
| 5369 | Return the next interpreter state object after \var{interp} from the |
| 5370 | list of all such objects. |
| 5371 | \versionadded{2.2} |
| 5372 | \end{cfuncdesc} |
| 5373 | |
| 5374 | \begin{cfuncdesc}{PyThreadState *}{PyInterpreterState_ThreadHead}{PyInterpreterState *interp} |
| 5375 | Return the a pointer to the first \ctype{PyThreadState} object in the |
| 5376 | list of threads associated with the interpreter \var{interp}. |
| 5377 | \versionadded{2.2} |
| 5378 | \end{cfuncdesc} |
| 5379 | |
| 5380 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Next}{PyThreadState *tstate} |
| 5381 | Return the next thread state object after \var{tstate} from the list |
| 5382 | of all such objects belonging to the same \ctype{PyInterpreterState} |
| 5383 | object. |
| 5384 | \versionadded{2.2} |
| 5385 | \end{cfuncdesc} |
| 5386 | |
| 5387 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5388 | \chapter{Memory Management \label{memory}} |
| 5389 | \sectionauthor{Vladimir Marangozov}{Vladimir.Marangozov@inrialpes.fr} |
| 5390 | |
| 5391 | |
| 5392 | \section{Overview \label{memoryOverview}} |
| 5393 | |
| 5394 | Memory management in Python involves a private heap containing all |
| 5395 | Python objects and data structures. The management of this private |
| 5396 | heap is ensured internally by the \emph{Python memory manager}. The |
| 5397 | Python memory manager has different components which deal with various |
| 5398 | dynamic storage management aspects, like sharing, segmentation, |
| 5399 | preallocation or caching. |
| 5400 | |
| 5401 | At the lowest level, a raw memory allocator ensures that there is |
| 5402 | enough room in the private heap for storing all Python-related data |
| 5403 | by interacting with the memory manager of the operating system. On top |
| 5404 | of the raw memory allocator, several object-specific allocators |
| 5405 | operate on the same heap and implement distinct memory management |
| 5406 | policies adapted to the peculiarities of every object type. For |
| 5407 | example, integer objects are managed differently within the heap than |
| 5408 | strings, tuples or dictionaries because integers imply different |
| 5409 | storage requirements and speed/space tradeoffs. The Python memory |
| 5410 | manager thus delegates some of the work to the object-specific |
| 5411 | allocators, but ensures that the latter operate within the bounds of |
| 5412 | the private heap. |
| 5413 | |
| 5414 | It is important to understand that the management of the Python heap |
| 5415 | is performed by the interpreter itself and that the user has no |
| 5416 | control on it, even if she regularly manipulates object pointers to |
| 5417 | memory blocks inside that heap. The allocation of heap space for |
| 5418 | Python objects and other internal buffers is performed on demand by |
| 5419 | the Python memory manager through the Python/C API functions listed in |
| 5420 | this document. |
| 5421 | |
| 5422 | To avoid memory corruption, extension writers should never try to |
| 5423 | operate on Python objects with the functions exported by the C |
| 5424 | library: \cfunction{malloc()}\ttindex{malloc()}, |
| 5425 | \cfunction{calloc()}\ttindex{calloc()}, |
| 5426 | \cfunction{realloc()}\ttindex{realloc()} and |
| 5427 | \cfunction{free()}\ttindex{free()}. This will result in |
| 5428 | mixed calls between the C allocator and the Python memory manager |
| 5429 | with fatal consequences, because they implement different algorithms |
| 5430 | and operate on different heaps. However, one may safely allocate and |
| 5431 | release memory blocks with the C library allocator for individual |
| 5432 | purposes, as shown in the following example: |
| 5433 | |
| 5434 | \begin{verbatim} |
| 5435 | PyObject *res; |
| 5436 | char *buf = (char *) malloc(BUFSIZ); /* for I/O */ |
| 5437 | |
| 5438 | if (buf == NULL) |
| 5439 | return PyErr_NoMemory(); |
| 5440 | ...Do some I/O operation involving buf... |
| 5441 | res = PyString_FromString(buf); |
| 5442 | free(buf); /* malloc'ed */ |
| 5443 | return res; |
| 5444 | \end{verbatim} |
| 5445 | |
| 5446 | In this example, the memory request for the I/O buffer is handled by |
| 5447 | the C library allocator. The Python memory manager is involved only |
| 5448 | in the allocation of the string object returned as a result. |
| 5449 | |
| 5450 | In most situations, however, it is recommended to allocate memory from |
| 5451 | the Python heap specifically because the latter is under control of |
| 5452 | the Python memory manager. For example, this is required when the |
| 5453 | interpreter is extended with new object types written in C. Another |
| 5454 | reason for using the Python heap is the desire to \emph{inform} the |
| 5455 | Python memory manager about the memory needs of the extension module. |
| 5456 | Even when the requested memory is used exclusively for internal, |
| 5457 | highly-specific purposes, delegating all memory requests to the Python |
| 5458 | memory manager causes the interpreter to have a more accurate image of |
| 5459 | its memory footprint as a whole. Consequently, under certain |
| 5460 | circumstances, the Python memory manager may or may not trigger |
| 5461 | appropriate actions, like garbage collection, memory compaction or |
| 5462 | other preventive procedures. Note that by using the C library |
| 5463 | allocator as shown in the previous example, the allocated memory for |
| 5464 | the I/O buffer escapes completely the Python memory manager. |
| 5465 | |
| 5466 | |
| 5467 | \section{Memory Interface \label{memoryInterface}} |
| 5468 | |
| 5469 | The following function sets, modeled after the ANSI C standard, are |
| 5470 | available for allocating and releasing memory from the Python heap: |
| 5471 | |
| 5472 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 5473 | \begin{cfuncdesc}{void*}{PyMem_Malloc}{size_t n} |
| 5474 | Allocates \var{n} bytes and returns a pointer of type \ctype{void*} to |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5475 | the allocated memory, or \NULL{} if the request fails. Requesting zero |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5476 | bytes returns a non-\NULL{} pointer. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5477 | The memory will not have been initialized in any way. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5478 | \end{cfuncdesc} |
| 5479 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 5480 | \begin{cfuncdesc}{void*}{PyMem_Realloc}{void *p, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5481 | Resizes the memory block pointed to by \var{p} to \var{n} bytes. The |
| 5482 | contents will be unchanged to the minimum of the old and the new |
| 5483 | sizes. If \var{p} is \NULL{}, the call is equivalent to |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5484 | \cfunction{PyMem_Malloc(\var{n})}; if \var{n} is equal to zero, the |
| 5485 | memory block is resized but is not freed, and the returned pointer is |
| 5486 | non-\NULL{}. Unless \var{p} is \NULL{}, it must have been returned by |
| 5487 | a previous call to \cfunction{PyMem_Malloc()} or |
| 5488 | \cfunction{PyMem_Realloc()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5489 | \end{cfuncdesc} |
| 5490 | |
Fred Drake | 7d45d34 | 2000-08-11 17:07:32 +0000 | [diff] [blame] | 5491 | \begin{cfuncdesc}{void}{PyMem_Free}{void *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5492 | Frees the memory block pointed to by \var{p}, which must have been |
| 5493 | returned by a previous call to \cfunction{PyMem_Malloc()} or |
| 5494 | \cfunction{PyMem_Realloc()}. Otherwise, or if |
| 5495 | \cfunction{PyMem_Free(p)} has been called before, undefined behaviour |
| 5496 | occurs. If \var{p} is \NULL{}, no operation is performed. |
| 5497 | \end{cfuncdesc} |
| 5498 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5499 | The following type-oriented macros are provided for convenience. Note |
| 5500 | that \var{TYPE} refers to any C type. |
| 5501 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5502 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_New}{TYPE, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5503 | Same as \cfunction{PyMem_Malloc()}, but allocates \code{(\var{n} * |
| 5504 | sizeof(\var{TYPE}))} bytes of memory. Returns a pointer cast to |
| 5505 | \ctype{\var{TYPE}*}. |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5506 | The memory will not have been initialized in any way. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5507 | \end{cfuncdesc} |
| 5508 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5509 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_Resize}{void *p, TYPE, size_t n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5510 | Same as \cfunction{PyMem_Realloc()}, but the memory block is resized |
| 5511 | to \code{(\var{n} * sizeof(\var{TYPE}))} bytes. Returns a pointer |
| 5512 | cast to \ctype{\var{TYPE}*}. |
| 5513 | \end{cfuncdesc} |
| 5514 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5515 | \begin{cfuncdesc}{void}{PyMem_Del}{void *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5516 | Same as \cfunction{PyMem_Free()}. |
| 5517 | \end{cfuncdesc} |
| 5518 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5519 | In addition, the following macro sets are provided for calling the |
| 5520 | Python memory allocator directly, without involving the C API functions |
| 5521 | listed above. However, note that their use does not preserve binary |
| 5522 | compatibility accross Python versions and is therefore deprecated in |
| 5523 | extension modules. |
| 5524 | |
| 5525 | \cfunction{PyMem_MALLOC()}, \cfunction{PyMem_REALLOC()}, \cfunction{PyMem_FREE()}. |
| 5526 | |
| 5527 | \cfunction{PyMem_NEW()}, \cfunction{PyMem_RESIZE()}, \cfunction{PyMem_DEL()}. |
| 5528 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5529 | |
| 5530 | \section{Examples \label{memoryExamples}} |
| 5531 | |
| 5532 | Here is the example from section \ref{memoryOverview}, rewritten so |
| 5533 | that the I/O buffer is allocated from the Python heap by using the |
| 5534 | first function set: |
| 5535 | |
| 5536 | \begin{verbatim} |
| 5537 | PyObject *res; |
| 5538 | char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */ |
| 5539 | |
| 5540 | if (buf == NULL) |
| 5541 | return PyErr_NoMemory(); |
| 5542 | /* ...Do some I/O operation involving buf... */ |
| 5543 | res = PyString_FromString(buf); |
| 5544 | PyMem_Free(buf); /* allocated with PyMem_Malloc */ |
| 5545 | return res; |
| 5546 | \end{verbatim} |
| 5547 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5548 | The same code using the type-oriented function set: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5549 | |
| 5550 | \begin{verbatim} |
| 5551 | PyObject *res; |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5552 | char *buf = PyMem_New(char, BUFSIZ); /* for I/O */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5553 | |
| 5554 | if (buf == NULL) |
| 5555 | return PyErr_NoMemory(); |
| 5556 | /* ...Do some I/O operation involving buf... */ |
| 5557 | res = PyString_FromString(buf); |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5558 | PyMem_Del(buf); /* allocated with PyMem_New */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5559 | return res; |
| 5560 | \end{verbatim} |
| 5561 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5562 | Note that in the two examples above, the buffer is always |
| 5563 | manipulated via functions belonging to the same set. Indeed, it |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5564 | is required to use the same memory API family for a given |
| 5565 | memory block, so that the risk of mixing different allocators is |
| 5566 | reduced to a minimum. The following code sequence contains two errors, |
| 5567 | one of which is labeled as \emph{fatal} because it mixes two different |
| 5568 | allocators operating on different heaps. |
| 5569 | |
| 5570 | \begin{verbatim} |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5571 | char *buf1 = PyMem_New(char, BUFSIZ); |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5572 | char *buf2 = (char *) malloc(BUFSIZ); |
| 5573 | char *buf3 = (char *) PyMem_Malloc(BUFSIZ); |
| 5574 | ... |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5575 | PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5576 | free(buf2); /* Right -- allocated via malloc() */ |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5577 | free(buf1); /* Fatal -- should be PyMem_Del() */ |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5578 | \end{verbatim} |
| 5579 | |
| 5580 | In addition to the functions aimed at handling raw memory blocks from |
| 5581 | the Python heap, objects in Python are allocated and released with |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5582 | \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()} and |
| 5583 | \cfunction{PyObject_Del()}, or with their corresponding macros |
| 5584 | \cfunction{PyObject_NEW()}, \cfunction{PyObject_NEW_VAR()} and |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 5585 | \cfunction{PyObject_DEL()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5586 | |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 5587 | These will be explained in the next chapter on defining and |
| 5588 | implementing new object types in C. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5589 | |
| 5590 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 5591 | \chapter{Defining New Object Types \label{newTypes}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 5592 | |
Fred Drake | 88fdaa7 | 2001-07-20 20:56:11 +0000 | [diff] [blame] | 5593 | |
| 5594 | \section{Allocating Objects on the Heap |
| 5595 | \label{allocating-objects}} |
| 5596 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 5597 | \begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5598 | \end{cfuncdesc} |
| 5599 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5600 | \begin{cfuncdesc}{PyVarObject*}{_PyObject_NewVar}{PyTypeObject *type, int size} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5601 | \end{cfuncdesc} |
| 5602 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5603 | \begin{cfuncdesc}{void}{_PyObject_Del}{PyObject *op} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5604 | \end{cfuncdesc} |
| 5605 | |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5606 | \begin{cfuncdesc}{PyObject*}{PyObject_Init}{PyObject *op, |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5607 | PyTypeObject *type} |
| 5608 | Initialize a newly-allocated object \var{op} with its type and |
| 5609 | initial reference. Returns the initialized object. If \var{type} |
| 5610 | indicates that the object participates in the cyclic garbage |
| 5611 | detector, it it added to the detector's set of observed objects. |
| 5612 | Other fields of the object are not affected. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5613 | \end{cfuncdesc} |
| 5614 | |
| 5615 | \begin{cfuncdesc}{PyVarObject*}{PyObject_InitVar}{PyVarObject *op, |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5616 | PyTypeObject *type, int size} |
| 5617 | This does everything \cfunction{PyObject_Init()} does, and also |
| 5618 | initializes the length information for a variable-size object. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5619 | \end{cfuncdesc} |
| 5620 | |
| 5621 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_New}{TYPE, PyTypeObject *type} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5622 | Allocate a new Python object using the C structure type \var{TYPE} |
| 5623 | and the Python type object \var{type}. Fields not defined by the |
| 5624 | Python object header are not initialized; the object's reference |
| 5625 | count will be one. The size of the memory |
| 5626 | allocation is determined from the \member{tp_basicsize} field of the |
| 5627 | type object. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5628 | \end{cfuncdesc} |
| 5629 | |
| 5630 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NewVar}{TYPE, PyTypeObject *type, |
| 5631 | int size} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5632 | Allocate a new Python object using the C structure type \var{TYPE} |
| 5633 | and the Python type object \var{type}. Fields not defined by the |
| 5634 | Python object header are not initialized. The allocated memory |
| 5635 | allows for the \var{TYPE} structure plus \var{size} fields of the |
| 5636 | size given by the \member{tp_itemsize} field of \var{type}. This is |
| 5637 | useful for implementing objects like tuples, which are able to |
| 5638 | determine their size at construction time. Embedding the array of |
| 5639 | fields into the same allocation decreases the number of allocations, |
| 5640 | improving the memory management efficiency. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5641 | \end{cfuncdesc} |
| 5642 | |
| 5643 | \begin{cfuncdesc}{void}{PyObject_Del}{PyObject *op} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5644 | Releases memory allocated to an object using |
| 5645 | \cfunction{PyObject_New()} or \cfunction{PyObject_NewVar()}. This |
| 5646 | is normally called from the \member{tp_dealloc} handler specified in |
| 5647 | the object's type. The fields of the object should not be accessed |
| 5648 | after this call as the memory is no longer a valid Python object. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5649 | \end{cfuncdesc} |
| 5650 | |
| 5651 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW}{TYPE, PyTypeObject *type} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5652 | Macro version of \cfunction{PyObject_New()}, to gain performance at |
| 5653 | the expense of safety. This does not check \var{type} for a \NULL{} |
| 5654 | value. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5655 | \end{cfuncdesc} |
| 5656 | |
| 5657 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_NEW_VAR}{TYPE, PyTypeObject *type, |
| 5658 | int size} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5659 | Macro version of \cfunction{PyObject_NewVar()}, to gain performance |
| 5660 | at the expense of safety. This does not check \var{type} for a |
| 5661 | \NULL{} value. |
Fred Drake | f913e54 | 2000-09-12 20:17:17 +0000 | [diff] [blame] | 5662 | \end{cfuncdesc} |
| 5663 | |
| 5664 | \begin{cfuncdesc}{void}{PyObject_DEL}{PyObject *op} |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5665 | Macro version of \cfunction{PyObject_Del()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 5666 | \end{cfuncdesc} |
| 5667 | |
Fred Drake | ee814bf | 2000-11-28 22:34:32 +0000 | [diff] [blame] | 5668 | \begin{cfuncdesc}{PyObject*}{Py_InitModule}{char *name, |
| 5669 | PyMethodDef *methods} |
| 5670 | Create a new module object based on a name and table of functions, |
| 5671 | returning the new module object. |
| 5672 | \end{cfuncdesc} |
| 5673 | |
| 5674 | \begin{cfuncdesc}{PyObject*}{Py_InitModule3}{char *name, |
| 5675 | PyMethodDef *methods, |
| 5676 | char *doc} |
| 5677 | Create a new module object based on a name and table of functions, |
| 5678 | returning the new module object. If \var{doc} is non-\NULL, it will |
| 5679 | be used to define the docstring for the module. |
| 5680 | \end{cfuncdesc} |
| 5681 | |
| 5682 | \begin{cfuncdesc}{PyObject*}{Py_InitModule4}{char *name, |
| 5683 | PyMethodDef *methods, |
| 5684 | char *doc, PyObject *self, |
| 5685 | int apiver} |
| 5686 | Create a new module object based on a name and table of functions, |
| 5687 | returning the new module object. If \var{doc} is non-\NULL, it will |
| 5688 | be used to define the docstring for the module. If \var{self} is |
| 5689 | non-\NULL, it will passed to the functions of the module as their |
| 5690 | (otherwise \NULL) first parameter. (This was added as an |
| 5691 | experimental feature, and there are no known uses in the current |
| 5692 | version of Python.) For \var{apiver}, the only value which should |
| 5693 | be passed is defined by the constant \constant{PYTHON_API_VERSION}. |
| 5694 | |
| 5695 | \strong{Note:} Most uses of this function should probably be using |
| 5696 | the \cfunction{Py_InitModule3()} instead; only use this if you are |
| 5697 | sure you need it. |
| 5698 | \end{cfuncdesc} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 5699 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5700 | DL_IMPORT |
| 5701 | |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 5702 | \begin{cvardesc}{PyObject}{_Py_NoneStruct} |
| 5703 | Object which is visible in Python as \code{None}. This should only |
| 5704 | be accessed using the \code{Py_None} macro, which evaluates to a |
| 5705 | pointer to this object. |
| 5706 | \end{cvardesc} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5707 | |
| 5708 | |
| 5709 | \section{Common Object Structures \label{common-structs}} |
| 5710 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5711 | PyObject, PyVarObject |
| 5712 | |
| 5713 | PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD |
| 5714 | |
| 5715 | Typedefs: |
| 5716 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 5717 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5718 | destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 5719 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 5720 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 5721 | \begin{ctypedesc}{PyCFunction} |
| 5722 | Type of the functions used to implement most Python callables in C. |
| 5723 | \end{ctypedesc} |
| 5724 | |
| 5725 | \begin{ctypedesc}{PyMethodDef} |
| 5726 | Structure used to describe a method of an extension type. This |
| 5727 | structure has four fields: |
| 5728 | |
| 5729 | \begin{tableiii}{l|l|l}{member}{Field}{C Type}{Meaning} |
| 5730 | \lineiii{ml_name}{char *}{name of the method} |
| 5731 | \lineiii{ml_meth}{PyCFunction}{pointer to the C implementation} |
| 5732 | \lineiii{ml_flags}{int}{flag bits indicating how the call should be |
| 5733 | constructed} |
| 5734 | \lineiii{ml_doc}{char *}{points to the contents of the docstring} |
| 5735 | \end{tableiii} |
| 5736 | \end{ctypedesc} |
| 5737 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5738 | The \var{ml_meth} is a C function pointer. The functions may be of |
| 5739 | different types, but they always return \ctype{PyObject*}. If the |
| 5740 | function is not of the \ctype{PyCFunction}, the compiler will require |
| 5741 | a cast in the method table. Even though \ctype{PyCFunction} defines |
| 5742 | the first parameter as \ctype{PyObject*}, it is common that the method |
| 5743 | implementation uses a the specific C type of the \var{self} object. |
| 5744 | |
| 5745 | The flags can have the following values. Only METH_VARARGS and |
| 5746 | METH_KEYWORDS can be combined; the others can't. |
| 5747 | |
| 5748 | \begin{datadesc}{METH_VARARGS} |
| 5749 | |
| 5750 | This is the typical calling convention, where the methods have the |
| 5751 | type \ctype{PyMethodDef}. The function expects two \ctype{PyObject*}. |
| 5752 | The first one is the \var{self} object for methods; for module |
| 5753 | functions, it has the value given to \cfunction{PyInitModule4} (or |
| 5754 | \NULL{} if \cfunction{PyInitModule} was used). The second parameter |
| 5755 | (often called \var{args}) is a tuple object representing all |
| 5756 | arguments. This parameter is typically processed using |
| 5757 | \cfunction{PyArg_ParseTuple}. |
| 5758 | |
| 5759 | \end{datadesc} |
| 5760 | |
| 5761 | \begin{datadesc}{METH_KEYWORDS} |
| 5762 | |
| 5763 | Methods with these flags must be of type |
| 5764 | \ctype{PyCFunctionWithKeywords}. The function expects three |
| 5765 | parameters: \var{self}, \var{args}, and a dictionary of all the keyword |
| 5766 | arguments. The flag is typically combined with METH_VARARGS, and the |
| 5767 | parameters are typically processed using |
| 5768 | \cfunction{PyArg_ParseTupleAndKeywords}. |
| 5769 | |
| 5770 | \end{datadesc} |
| 5771 | |
| 5772 | \begin{datadesc}{METH_NOARGS} |
| 5773 | |
| 5774 | Methods without parameters don't need to check whether arguments are |
| 5775 | given if they are listed with the \code{METH_NOARGS} flag. They need |
| 5776 | to be of type \ctype{PyNoArgsFunction}, i.e. they expect a single |
| 5777 | \var{self} parameter. |
| 5778 | |
| 5779 | \end{datadesc} |
| 5780 | |
| 5781 | \begin{datadesc}{METH_O} |
| 5782 | |
| 5783 | Methods with a single object argument can be listed with the |
| 5784 | \code{METH_O} flag, instead of invoking \cfunction{PyArg_ParseTuple} |
| 5785 | with a \code{``O''} argument. They have the type \ctype{PyCFunction}, |
| 5786 | with the \var{self} parameter, and a \ctype{PyObject*} parameter |
| 5787 | representing the single argument. |
| 5788 | |
| 5789 | \end{datadesc} |
| 5790 | |
| 5791 | \begin{datadesc}{METH_OLDARGS} |
| 5792 | |
| 5793 | This calling convention is deprecated. The method must be of type |
| 5794 | \ctype{PyCFunction}. The second argument is \NULL{} if no arguments |
| 5795 | are given, a single object if exactly one argument is given, and a |
| 5796 | tuple of objects if more than one argument is given. |
| 5797 | |
| 5798 | \end{datadesc} |
| 5799 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 5800 | \begin{cfuncdesc}{PyObject*}{Py_FindMethod}{PyMethodDef[] table, |
| 5801 | PyObject *ob, char *name} |
| 5802 | Return a bound method object for an extension type implemented in C. |
| 5803 | This function also handles the special attribute \member{__methods__}, |
| 5804 | returning a list of all the method names defined in \var{table}. |
| 5805 | \end{cfuncdesc} |
| 5806 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5807 | |
| 5808 | \section{Mapping Object Structures \label{mapping-structs}} |
| 5809 | |
| 5810 | \begin{ctypedesc}{PyMappingMethods} |
| 5811 | Structure used to hold pointers to the functions used to implement the |
| 5812 | mapping protocol for an extension type. |
| 5813 | \end{ctypedesc} |
| 5814 | |
| 5815 | |
| 5816 | \section{Number Object Structures \label{number-structs}} |
| 5817 | |
| 5818 | \begin{ctypedesc}{PyNumberMethods} |
| 5819 | Structure used to hold pointers to the functions an extension type |
| 5820 | uses to implement the number protocol. |
| 5821 | \end{ctypedesc} |
| 5822 | |
| 5823 | |
| 5824 | \section{Sequence Object Structures \label{sequence-structs}} |
| 5825 | |
| 5826 | \begin{ctypedesc}{PySequenceMethods} |
| 5827 | Structure used to hold pointers to the functions which an object uses |
| 5828 | to implement the sequence protocol. |
| 5829 | \end{ctypedesc} |
| 5830 | |
| 5831 | |
| 5832 | \section{Buffer Object Structures \label{buffer-structs}} |
| 5833 | \sectionauthor{Greg J. Stein}{greg@lyra.org} |
| 5834 | |
| 5835 | The buffer interface exports a model where an object can expose its |
| 5836 | internal data as a set of chunks of data, where each chunk is |
| 5837 | specified as a pointer/length pair. These chunks are called |
| 5838 | \dfn{segments} and are presumed to be non-contiguous in memory. |
| 5839 | |
| 5840 | If an object does not export the buffer interface, then its |
| 5841 | \member{tp_as_buffer} member in the \ctype{PyTypeObject} structure |
| 5842 | should be \NULL{}. Otherwise, the \member{tp_as_buffer} will point to |
| 5843 | a \ctype{PyBufferProcs} structure. |
| 5844 | |
| 5845 | \strong{Note:} It is very important that your |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5846 | \ctype{PyTypeObject} structure uses \constant{Py_TPFLAGS_DEFAULT} for |
| 5847 | 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] | 5848 | tells the Python runtime that your \ctype{PyBufferProcs} structure |
| 5849 | contains the \member{bf_getcharbuffer} slot. Older versions of Python |
| 5850 | did not have this member, so a new Python interpreter using an old |
| 5851 | extension needs to be able to test for its presence before using it. |
| 5852 | |
| 5853 | \begin{ctypedesc}{PyBufferProcs} |
| 5854 | Structure used to hold the function pointers which define an |
| 5855 | implementation of the buffer protocol. |
| 5856 | |
| 5857 | The first slot is \member{bf_getreadbuffer}, of type |
| 5858 | \ctype{getreadbufferproc}. If this slot is \NULL{}, then the object |
| 5859 | does not support reading from the internal data. This is |
| 5860 | non-sensical, so implementors should fill this in, but callers should |
| 5861 | test that the slot contains a non-\NULL{} value. |
| 5862 | |
| 5863 | The next slot is \member{bf_getwritebuffer} having type |
| 5864 | \ctype{getwritebufferproc}. This slot may be \NULL{} if the object |
| 5865 | does not allow writing into its returned buffers. |
| 5866 | |
| 5867 | The third slot is \member{bf_getsegcount}, with type |
| 5868 | \ctype{getsegcountproc}. This slot must not be \NULL{} and is used to |
| 5869 | inform the caller how many segments the object contains. Simple |
| 5870 | objects such as \ctype{PyString_Type} and |
| 5871 | \ctype{PyBuffer_Type} objects contain a single segment. |
| 5872 | |
| 5873 | The last slot is \member{bf_getcharbuffer}, of type |
| 5874 | \ctype{getcharbufferproc}. This slot will only be present if the |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5875 | \constant{Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5876 | \member{tp_flags} field of the object's \ctype{PyTypeObject}. Before using |
| 5877 | this slot, the caller should test whether it is present by using the |
| 5878 | \cfunction{PyType_HasFeature()}\ttindex{PyType_HasFeature()} function. |
| 5879 | If present, it may be \NULL, indicating that the object's contents |
| 5880 | cannot be used as \emph{8-bit characters}. |
| 5881 | The slot function may also raise an error if the object's contents |
| 5882 | cannot be interpreted as 8-bit characters. For example, if the object |
| 5883 | is an array which is configured to hold floating point values, an |
| 5884 | exception may be raised if a caller attempts to use |
| 5885 | \member{bf_getcharbuffer} to fetch a sequence of 8-bit characters. |
| 5886 | This notion of exporting the internal buffers as ``text'' is used to |
| 5887 | distinguish between objects that are binary in nature, and those which |
| 5888 | have character-based content. |
| 5889 | |
| 5890 | \strong{Note:} The current policy seems to state that these characters |
| 5891 | may be multi-byte characters. This implies that a buffer size of |
| 5892 | \var{N} does not mean there are \var{N} characters present. |
| 5893 | \end{ctypedesc} |
| 5894 | |
| 5895 | \begin{datadesc}{Py_TPFLAGS_HAVE_GETCHARBUFFER} |
| 5896 | Flag bit set in the type structure to indicate that the |
| 5897 | \member{bf_getcharbuffer} slot is known. This being set does not |
| 5898 | indicate that the object supports the buffer interface or that the |
| 5899 | \member{bf_getcharbuffer} slot is non-\NULL. |
| 5900 | \end{datadesc} |
| 5901 | |
| 5902 | \begin{ctypedesc}[getreadbufferproc]{int (*getreadbufferproc) |
| 5903 | (PyObject *self, int segment, void **ptrptr)} |
| 5904 | Return a pointer to a readable segment of the buffer. This function |
| 5905 | is allowed to raise an exception, in which case it must return |
| 5906 | \code{-1}. The \var{segment} which is passed must be zero or |
| 5907 | positive, and strictly less than the number of segments returned by |
Greg Stein | 4d4d003 | 2001-04-07 16:14:49 +0000 | [diff] [blame] | 5908 | the \member{bf_getsegcount} slot function. On success, it returns the |
| 5909 | length of the buffer memory, and sets \code{*\var{ptrptr}} to a |
| 5910 | pointer to that memory. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5911 | \end{ctypedesc} |
| 5912 | |
| 5913 | \begin{ctypedesc}[getwritebufferproc]{int (*getwritebufferproc) |
| 5914 | (PyObject *self, int segment, void **ptrptr)} |
Greg Stein | 4d4d003 | 2001-04-07 16:14:49 +0000 | [diff] [blame] | 5915 | Return a pointer to a writable memory buffer in \code{*\var{ptrptr}}, |
| 5916 | and the length of that segment as the function return value. |
| 5917 | The memory buffer must correspond to buffer segment \var{segment}. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 5918 | Must return \code{-1} and set an exception on error. |
| 5919 | \exception{TypeError} should be raised if the object only supports |
| 5920 | read-only buffers, and \exception{SystemError} should be raised when |
| 5921 | \var{segment} specifies a segment that doesn't exist. |
| 5922 | % Why doesn't it raise ValueError for this one? |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5923 | % GJS: because you shouldn't be calling it with an invalid |
| 5924 | % segment. That indicates a blatant programming error in the C |
| 5925 | % code. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 5926 | \end{ctypedesc} |
| 5927 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5928 | \begin{ctypedesc}[getsegcountproc]{int (*getsegcountproc) |
| 5929 | (PyObject *self, int *lenp)} |
| 5930 | Return the number of memory segments which comprise the buffer. If |
| 5931 | \var{lenp} is not \NULL, the implementation must report the sum of the |
| 5932 | sizes (in bytes) of all segments in \code{*\var{lenp}}. |
| 5933 | The function cannot fail. |
| 5934 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5935 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 5936 | \begin{ctypedesc}[getcharbufferproc]{int (*getcharbufferproc) |
| 5937 | (PyObject *self, int segment, const char **ptrptr)} |
| 5938 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5939 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 5940 | |
Fred Drake | f90490e | 2001-08-02 18:00:28 +0000 | [diff] [blame] | 5941 | \section{Supporting the Iterator Protocol |
| 5942 | \label{supporting-iteration}} |
| 5943 | |
| 5944 | |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5945 | \section{Supporting Cyclic Garbarge Collection |
| 5946 | \label{supporting-cycle-detection}} |
| 5947 | |
| 5948 | Python's support for detecting and collecting garbage which involves |
| 5949 | circular references requires support from object types which are |
| 5950 | ``containers'' for other objects which may also be containers. Types |
| 5951 | which do not store references to other objects, or which only store |
| 5952 | references to atomic types (such as numbers or strings), do not need |
| 5953 | to provide any explicit support for garbage collection. |
| 5954 | |
| 5955 | To create a container type, the \member{tp_flags} field of the type |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 5956 | object must include the \constant{Py_TPFLAGS_HAVE_GC} and provide an |
| 5957 | implementation of the \member{tp_traverse} handler. If instances of the |
| 5958 | type are mutable, a \member{tp_clear} implementation must also be |
| 5959 | provided. |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5960 | |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 5961 | \begin{datadesc}{Py_TPFLAGS_HAVE_GC} |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5962 | Objects with a type with this flag set must conform with the rules |
| 5963 | documented here. For convenience these objects will be referred to |
| 5964 | as container objects. |
| 5965 | \end{datadesc} |
| 5966 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5967 | Constructors for container types must conform to two rules: |
| 5968 | |
| 5969 | \begin{enumerate} |
| 5970 | \item The memory for the object must be allocated using |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 5971 | \cfunction{PyObject_GC_New()} or \cfunction{PyObject_GC_VarNew()}. |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5972 | |
| 5973 | \item Once all the fields which may contain references to other |
| 5974 | containers are initialized, it must call |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 5975 | \cfunction{PyObject_GC_Track()}. |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 5976 | \end{enumerate} |
| 5977 | |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 5978 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_GC_New}{TYPE, PyTypeObject *type} |
| 5979 | Analogous to \cfunction{PyObject_New()} but for container objects with |
| 5980 | the \constant{Py_TPFLAGS_HAVE_GC} flag set. |
| 5981 | \end{cfuncdesc} |
| 5982 | |
| 5983 | \begin{cfuncdesc}{\var{TYPE}*}{PyObject_GC_NewVar}{TYPE, PyTypeObject *type, |
| 5984 | int size} |
| 5985 | Analogous to \cfunction{PyObject_NewVar()} but for container objects |
| 5986 | with the \constant{Py_TPFLAGS_HAVE_GC} flag set. |
| 5987 | \end{cfuncdesc} |
| 5988 | |
| 5989 | \begin{cfuncdesc}{PyVarObject *}{PyObject_GC_Resize}{PyVarObject *op, int} |
| 5990 | Resize an object allocated by \cfunction{PyObject_NewVar()}. Returns |
| 5991 | the resized object or \NULL{} on failure. |
| 5992 | \end{cfuncdesc} |
| 5993 | |
| 5994 | \begin{cfuncdesc}{void}{PyObject_GC_Track}{PyObject *op} |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 5995 | Adds the object \var{op} to the set of container objects tracked by |
| 5996 | the collector. The collector can run at unexpected times so objects |
| 5997 | must be valid while being tracked. This should be called once all |
| 5998 | the fields followed by the \member{tp_traverse} handler become valid, |
| 5999 | usually near the end of the constructor. |
| 6000 | \end{cfuncdesc} |
| 6001 | |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6002 | \begin{cfuncdesc}{void}{_PyObject_GC_TRACK}{PyObject *op} |
| 6003 | A macro version of \cfunction{PyObject_GC_Track()}. It should not be |
| 6004 | used for extension modules. |
| 6005 | \end{cfuncdesc} |
| 6006 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6007 | Similarly, the deallocator for the object must conform to a similar |
| 6008 | pair of rules: |
| 6009 | |
| 6010 | \begin{enumerate} |
| 6011 | \item Before fields which refer to other containers are invalidated, |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6012 | \cfunction{PyObject_GC_UnTrack()} must be called. |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6013 | |
| 6014 | \item The object's memory must be deallocated using |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6015 | \cfunction{PyObject_GC_Del()}. |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6016 | \end{enumerate} |
| 6017 | |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6018 | \begin{cfuncdesc}{void}{PyObject_GC_Del}{PyObject *op} |
| 6019 | Releases memory allocated to an object using |
| 6020 | \cfunction{PyObject_GC_New()} or \cfunction{PyObject_GC_NewVar()}. |
| 6021 | \end{cfuncdesc} |
| 6022 | |
| 6023 | \begin{cfuncdesc}{void}{PyObject_GC_UnTrack}{PyObject *op} |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 6024 | Remove the object \var{op} from the set of container objects tracked |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6025 | by the collector. Note that \cfunction{PyObject_GC_Track()} can be |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 6026 | called again on this object to add it back to the set of tracked |
| 6027 | objects. The deallocator (\member{tp_dealloc} handler) should call |
| 6028 | this for the object before any of the fields used by the |
| 6029 | \member{tp_traverse} handler become invalid. |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6030 | \end{cfuncdesc} |
Fred Drake | 8f6df46 | 2001-03-23 17:42:09 +0000 | [diff] [blame] | 6031 | |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6032 | \begin{cfuncdesc}{void}{_PyObject_GC_UNTRACK}{PyObject *op} |
| 6033 | A macro version of \cfunction{PyObject_GC_UnTrack()}. It should not be |
| 6034 | used for extension modules. |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 6035 | \end{cfuncdesc} |
| 6036 | |
| 6037 | The \member{tp_traverse} handler accepts a function parameter of this |
| 6038 | type: |
| 6039 | |
| 6040 | \begin{ctypedesc}[visitproc]{int (*visitproc)(PyObject *object, void *arg)} |
| 6041 | Type of the visitor function passed to the \member{tp_traverse} |
| 6042 | handler. The function should be called with an object to traverse |
| 6043 | as \var{object} and the third parameter to the \member{tp_traverse} |
| 6044 | handler as \var{arg}. |
| 6045 | \end{ctypedesc} |
| 6046 | |
| 6047 | The \member{tp_traverse} handler must have the following type: |
| 6048 | |
| 6049 | \begin{ctypedesc}[traverseproc]{int (*traverseproc)(PyObject *self, |
| 6050 | visitproc visit, void *arg)} |
| 6051 | Traversal function for a container object. Implementations must |
| 6052 | call the \var{visit} function for each object directly contained by |
| 6053 | \var{self}, with the parameters to \var{visit} being the contained |
| 6054 | object and the \var{arg} value passed to the handler. If |
| 6055 | \var{visit} returns a non-zero value then an error has occurred and |
| 6056 | that value should be returned immediately. |
| 6057 | \end{ctypedesc} |
| 6058 | |
| 6059 | The \member{tp_clear} handler must be of the \ctype{inquiry} type, or |
| 6060 | \NULL{} if the object is immutable. |
| 6061 | |
| 6062 | \begin{ctypedesc}[inquiry]{int (*inquiry)(PyObject *self)} |
| 6063 | Drop references that may have created reference cycles. Immutable |
| 6064 | objects do not have to define this method since they can never |
| 6065 | directly create reference cycles. Note that the object must still |
Fred Drake | bab2965 | 2001-07-10 16:10:08 +0000 | [diff] [blame] | 6066 | be valid after calling this method (don't just call |
Fred Drake | c392b57 | 2001-03-21 22:15:01 +0000 | [diff] [blame] | 6067 | \cfunction{Py_DECREF()} on a reference). The collector will call |
| 6068 | this method if it detects that this object is involved in a |
| 6069 | reference cycle. |
| 6070 | \end{ctypedesc} |
| 6071 | |
| 6072 | |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6073 | \subsection{Example Cycle Collector Support |
| 6074 | \label{example-cycle-support}} |
| 6075 | |
| 6076 | This example shows only enough of the implementation of an extension |
| 6077 | type to show how the garbage collector support needs to be added. It |
| 6078 | shows the definition of the object structure, the |
| 6079 | \member{tp_traverse}, \member{tp_clear} and \member{tp_dealloc} |
| 6080 | implementations, the type structure, and a constructor --- the module |
| 6081 | initialization needed to export the constructor to Python is not shown |
| 6082 | as there are no special considerations there for the collector. To |
| 6083 | make this interesting, assume that the module exposes ways for the |
| 6084 | \member{container} field of the object to be modified. Note that |
| 6085 | since no checks are made on the type of the object used to initialize |
| 6086 | \member{container}, we have to assume that it may be a container. |
| 6087 | |
| 6088 | \begin{verbatim} |
| 6089 | #include "Python.h" |
| 6090 | |
| 6091 | typedef struct { |
| 6092 | PyObject_HEAD |
| 6093 | PyObject *container; |
| 6094 | } MyObject; |
| 6095 | |
| 6096 | static int |
| 6097 | my_traverse(MyObject *self, visitproc visit, void *arg) |
| 6098 | { |
| 6099 | if (self->container != NULL) |
| 6100 | return visit(self->container, arg); |
| 6101 | else |
| 6102 | return 0; |
| 6103 | } |
| 6104 | |
| 6105 | static int |
| 6106 | my_clear(MyObject *self) |
| 6107 | { |
| 6108 | Py_XDECREF(self->container); |
| 6109 | self->container = NULL; |
| 6110 | |
| 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | static void |
| 6115 | my_dealloc(MyObject *self) |
| 6116 | { |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6117 | PyObject_GC_UnTrack((PyObject *) self); |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6118 | Py_XDECREF(self->container); |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6119 | PyObject_GC_Del(self); |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6120 | } |
| 6121 | \end{verbatim} |
| 6122 | |
| 6123 | \begin{verbatim} |
| 6124 | statichere PyTypeObject |
| 6125 | MyObject_Type = { |
| 6126 | PyObject_HEAD_INIT(NULL) |
| 6127 | 0, |
| 6128 | "MyObject", |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6129 | sizeof(MyObject), |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6130 | 0, |
| 6131 | (destructor)my_dealloc, /* tp_dealloc */ |
| 6132 | 0, /* tp_print */ |
| 6133 | 0, /* tp_getattr */ |
| 6134 | 0, /* tp_setattr */ |
| 6135 | 0, /* tp_compare */ |
| 6136 | 0, /* tp_repr */ |
| 6137 | 0, /* tp_as_number */ |
| 6138 | 0, /* tp_as_sequence */ |
| 6139 | 0, /* tp_as_mapping */ |
| 6140 | 0, /* tp_hash */ |
| 6141 | 0, /* tp_call */ |
| 6142 | 0, /* tp_str */ |
| 6143 | 0, /* tp_getattro */ |
| 6144 | 0, /* tp_setattro */ |
| 6145 | 0, /* tp_as_buffer */ |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6146 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6147 | 0, /* tp_doc */ |
| 6148 | (traverseproc)my_traverse, /* tp_traverse */ |
| 6149 | (inquiry)my_clear, /* tp_clear */ |
| 6150 | 0, /* tp_richcompare */ |
| 6151 | 0, /* tp_weaklistoffset */ |
| 6152 | }; |
| 6153 | |
| 6154 | /* This constructor should be made accessible from Python. */ |
| 6155 | static PyObject * |
| 6156 | new_object(PyObject *unused, PyObject *args) |
| 6157 | { |
| 6158 | PyObject *container = NULL; |
| 6159 | MyObject *result = NULL; |
| 6160 | |
| 6161 | if (PyArg_ParseTuple(args, "|O:new_object", &container)) { |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6162 | result = PyObject_GC_New(MyObject, &MyObject_Type); |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6163 | if (result != NULL) { |
| 6164 | result->container = container; |
Neil Schemenauer | 55cdc88 | 2001-08-30 15:24:17 +0000 | [diff] [blame] | 6165 | PyObject_GC_Track(result); |
Fred Drake | e28d8ae | 2001-03-22 16:30:17 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | return (PyObject *) result; |
| 6169 | } |
| 6170 | \end{verbatim} |
| 6171 | |
| 6172 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 6173 | % \chapter{Debugging \label{debugging}} |
| 6174 | % |
| 6175 | % XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 6176 | |
| 6177 | |
Fred Drake | ed773ef | 2000-09-21 21:35:22 +0000 | [diff] [blame] | 6178 | \appendix |
| 6179 | \chapter{Reporting Bugs} |
| 6180 | \input{reportingbugs} |
| 6181 | |
Fred Drake | 490d34d | 2001-06-20 21:39:12 +0000 | [diff] [blame] | 6182 | \chapter{History and License} |
| 6183 | \input{license} |
| 6184 | |
Marc-André Lemburg | a544ea2 | 2001-01-17 18:04:31 +0000 | [diff] [blame] | 6185 | \input{api.ind} % Index -- must be last |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 6186 | |
| 6187 | \end{document} |