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 | |
| 7 | \makeindex % tell \index to actually write the .idx file |
| 8 | |
| 9 | |
| 10 | \begin{document} |
| 11 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 12 | \maketitle |
| 13 | |
Fred Drake | 9f86b66 | 1998-07-28 21:55:19 +0000 | [diff] [blame] | 14 | \ifhtml |
| 15 | \chapter*{Front Matter\label{front}} |
| 16 | \fi |
| 17 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 18 | \input{copyright} |
| 19 | |
| 20 | \begin{abstract} |
| 21 | |
| 22 | \noindent |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 23 | This manual documents the API used by C and \Cpp{} programmers who |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 24 | want to write extension modules or embed Python. It is a companion to |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 25 | \citetitle[../ext/ext.html]{Extending and Embedding the Python |
| 26 | Interpreter}, which describes the general principles of extension |
| 27 | writing but does not document the API functions in detail. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 29 | \strong{Warning:} The current version of this document is incomplete. |
| 30 | I hope that it is nevertheless useful. I will continue to work on it, |
| 31 | and release new versions from time to time, independent from Python |
| 32 | source code releases. |
| 33 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 34 | \end{abstract} |
| 35 | |
Fred Drake | 4d4f9e7 | 1998-01-13 22:25:02 +0000 | [diff] [blame] | 36 | \tableofcontents |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 38 | % XXX Consider moving all this back to ext.tex and giving api.tex |
| 39 | % XXX a *really* short intro only. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 40 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 41 | \chapter{Introduction \label{intro}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 42 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 43 | The Application Programmer's Interface to Python gives C and |
| 44 | \Cpp{} programmers access to the Python interpreter at a variety of |
| 45 | levels. The API is equally usable from \Cpp{}, but for brevity it is |
| 46 | generally referred to as the Python/C API. There are two |
| 47 | fundamentally different reasons for using the Python/C API. The first |
| 48 | reason is to write \emph{extension modules} for specific purposes; |
| 49 | these are C modules that extend the Python interpreter. This is |
| 50 | probably the most common use. The second reason is to use Python as a |
| 51 | component in a larger application; this technique is generally |
| 52 | referred to as \dfn{embedding} Python in an application. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 54 | Writing an extension module is a relatively well-understood process, |
| 55 | where a ``cookbook'' approach works well. There are several tools |
| 56 | that automate the process to some extent. While people have embedded |
| 57 | Python in other applications since its early existence, the process of |
| 58 | embedding Python is less straightforward that writing an extension. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 60 | Many API functions are useful independent of whether you're embedding |
| 61 | or extending Python; moreover, most applications that embed Python |
| 62 | will need to provide a custom extension as well, so it's probably a |
| 63 | good idea to become familiar with writing an extension before |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 64 | attempting to embed Python in a real application. |
| 65 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 66 | |
| 67 | \section{Include Files \label{includes}} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 68 | |
| 69 | All function, type and macro definitions needed to use the Python/C |
| 70 | API are included in your code by the following line: |
| 71 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 72 | \begin{verbatim} |
| 73 | #include "Python.h" |
| 74 | \end{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 75 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 76 | This implies inclusion of the following standard headers: |
| 77 | \code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and |
| 78 | \code{<stdlib.h>} (if available). |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 79 | |
| 80 | All user visible names defined by Python.h (except those defined by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 81 | the included standard headers) have one of the prefixes \samp{Py} or |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 82 | \samp{_Py}. Names beginning with \samp{_Py} are for internal use by |
| 83 | the Python implementation and should not be used by extension writers. |
| 84 | Structure member names do not have a reserved prefix. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 85 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 86 | \strong{Important:} user code should never define names that begin |
| 87 | with \samp{Py} or \samp{_Py}. This confuses the reader, and |
| 88 | jeopardizes the portability of the user code to future Python |
| 89 | versions, which may define additional names beginning with one of |
| 90 | these prefixes. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 91 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 92 | The header files are typically installed with Python. On \UNIX, these |
| 93 | are located in the directories |
| 94 | \file{\envvar{prefix}/include/python\var{version}/} and |
| 95 | \file{\envvar{exec_prefix}/include/python\var{version}/}, where |
| 96 | \envvar{prefix} and \envvar{exec_prefix} are defined by the |
| 97 | corresponding parameters to Python's \program{configure} script and |
| 98 | \var{version} is \code{sys.version[:3]}. On Windows, the headers are |
| 99 | installed in \file{\envvar{prefix}/include}, where \envvar{prefix} is |
| 100 | the installation directory specified to the installer. |
| 101 | |
| 102 | To include the headers, place both directories (if different) on your |
| 103 | compiler's search path for includes. Do \emph{not} place the parent |
| 104 | directories on the search path and then use |
| 105 | \samp{\#include <python1.5/Python.h>}; this will break on |
| 106 | multi-platform builds since the platform independent headers under |
| 107 | \envvar{prefix} include the platform specific headers from |
| 108 | \envvar{exec_prefix}. |
| 109 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 110 | |
| 111 | \section{Objects, Types and Reference Counts \label{objects}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 113 | Most Python/C API functions have one or more arguments as well as a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 114 | return value of type \ctype{PyObject*}. This type is a pointer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 115 | to an opaque data type representing an arbitrary Python |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 116 | object. Since all Python object types are treated the same way by the |
| 117 | Python language in most situations (e.g., assignments, scope rules, |
| 118 | and argument passing), it is only fitting that they should be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 119 | represented by a single C type. Almost all Python objects live on the |
| 120 | heap: you never declare an automatic or static variable of type |
| 121 | \ctype{PyObject}, only pointer variables of type \ctype{PyObject*} can |
| 122 | be declared. The sole exception are the type objects\obindex{type}; |
| 123 | since these must never be deallocated, they are typically static |
| 124 | \ctype{PyTypeObject} objects. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 125 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 126 | All Python objects (even Python integers) have a \dfn{type} and a |
| 127 | \dfn{reference count}. An object's type determines what kind of object |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 128 | it is (e.g., an integer, a list, or a user-defined function; there are |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 129 | many more as explained in the \citetitle[../ref/ref.html]{Python |
| 130 | Reference Manual}). For each of the well-known types there is a macro |
| 131 | to check whether an object is of that type; for instance, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 132 | \samp{PyList_Check(\var{a})} is true if (and only if) the object |
| 133 | pointed to by \var{a} is a Python list. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 134 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 135 | |
| 136 | \subsection{Reference Counts \label{refcounts}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 138 | The reference count is important because today's computers have a |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 139 | finite (and often severely limited) memory size; it counts how many |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 140 | different places there are that have a reference to an object. Such a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 141 | place could be another object, or a global (or static) C variable, or |
| 142 | a local variable in some C function. When an object's reference count |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 143 | becomes zero, the object is deallocated. If it contains references to |
| 144 | other objects, their reference count is decremented. Those other |
| 145 | objects may be deallocated in turn, if this decrement makes their |
| 146 | reference count become zero, and so on. (There's an obvious problem |
| 147 | with objects that reference each other here; for now, the solution is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 148 | ``don't do that.'') |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 150 | Reference counts are always manipulated explicitly. The normal way is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 151 | to use the macro \cfunction{Py_INCREF()}\ttindex{Py_INCREF()} to |
| 152 | increment an object's reference count by one, and |
| 153 | \cfunction{Py_DECREF()}\ttindex{Py_DECREF()} to decrement it by |
| 154 | one. The \cfunction{Py_DECREF()} macro is considerably more complex |
| 155 | than the incref one, since it must check whether the reference count |
| 156 | becomes zero and then cause the object's deallocator to be called. |
| 157 | The deallocator is a function pointer contained in the object's type |
| 158 | structure. The type-specific deallocator takes care of decrementing |
| 159 | the reference counts for other objects contained in the object if this |
| 160 | is a compound object type, such as a list, as well as performing any |
| 161 | additional finalization that's needed. There's no chance that the |
| 162 | reference count can overflow; at least as many bits are used to hold |
| 163 | the reference count as there are distinct memory locations in virtual |
| 164 | memory (assuming \code{sizeof(long) >= sizeof(char*)}). Thus, the |
| 165 | reference count increment is a simple operation. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 167 | It is not necessary to increment an object's reference count for every |
| 168 | local variable that contains a pointer to an object. In theory, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 169 | object's reference count goes up by one when the variable is made to |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 170 | point to it and it goes down by one when the variable goes out of |
| 171 | scope. However, these two cancel each other out, so at the end the |
| 172 | reference count hasn't changed. The only real reason to use the |
| 173 | reference count is to prevent the object from being deallocated as |
| 174 | long as our variable is pointing to it. If we know that there is at |
| 175 | least one other reference to the object that lives at least as long as |
| 176 | our variable, there is no need to increment the reference count |
| 177 | temporarily. An important situation where this arises is in objects |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 178 | that are passed as arguments to C functions in an extension module |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 179 | that are called from Python; the call mechanism guarantees to hold a |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 180 | reference to every argument for the duration of the call. |
| 181 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 182 | However, a common pitfall is to extract an object from a list and |
| 183 | hold on to it for a while without incrementing its reference count. |
| 184 | Some other operation might conceivably remove the object from the |
| 185 | list, decrementing its reference count and possible deallocating it. |
| 186 | The real danger is that innocent-looking operations may invoke |
| 187 | arbitrary Python code which could do this; there is a code path which |
| 188 | allows control to flow back to the user from a \cfunction{Py_DECREF()}, |
| 189 | so almost any operation is potentially dangerous. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 191 | A safe approach is to always use the generic operations (functions |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 192 | whose name begins with \samp{PyObject_}, \samp{PyNumber_}, |
| 193 | \samp{PySequence_} or \samp{PyMapping_}). These operations always |
| 194 | increment the reference count of the object they return. This leaves |
| 195 | the caller with the responsibility to call |
| 196 | \cfunction{Py_DECREF()} when they are done with the result; this soon |
| 197 | becomes second nature. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 198 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 199 | |
| 200 | \subsubsection{Reference Count Details \label{refcountDetails}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 201 | |
| 202 | The reference count behavior of functions in the Python/C API is best |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 203 | explained in terms of \emph{ownership of references}. Note that we |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 204 | talk of owning references, never of owning objects; objects are always |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 205 | shared! When a function owns a reference, it has to dispose of it |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 206 | properly --- either by passing ownership on (usually to its caller) or |
| 207 | by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When |
| 208 | a function passes ownership of a reference on to its caller, the |
| 209 | caller is said to receive a \emph{new} reference. When no ownership |
| 210 | is transferred, the caller is said to \emph{borrow} the reference. |
| 211 | Nothing needs to be done for a borrowed reference. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 212 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 213 | Conversely, when a calling function passes it a reference to an |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 214 | object, there are two possibilities: the function \emph{steals} a |
| 215 | reference to the object, or it does not. Few functions steal |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 216 | references; the two notable exceptions are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 217 | \cfunction{PyList_SetItem()}\ttindex{PyList_SetItem()} and |
| 218 | \cfunction{PyTuple_SetItem()}\ttindex{PyTuple_SetItem()}, which |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 219 | steal a reference to the item (but not to the tuple or list into which |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 220 | the item is put!). These functions were designed to steal a reference |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 221 | because of a common idiom for populating a tuple or list with newly |
| 222 | created objects; for example, the code to create the tuple \code{(1, |
| 223 | 2, "three")} could look like this (forgetting about error handling for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 224 | the moment; a better way to code this is shown below): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 225 | |
| 226 | \begin{verbatim} |
| 227 | PyObject *t; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 229 | t = PyTuple_New(3); |
| 230 | PyTuple_SetItem(t, 0, PyInt_FromLong(1L)); |
| 231 | PyTuple_SetItem(t, 1, PyInt_FromLong(2L)); |
| 232 | PyTuple_SetItem(t, 2, PyString_FromString("three")); |
| 233 | \end{verbatim} |
| 234 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 235 | Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to |
| 236 | set tuple items; \cfunction{PySequence_SetItem()} and |
| 237 | \cfunction{PyObject_SetItem()} refuse to do this since tuples are an |
| 238 | immutable data type. You should only use |
| 239 | \cfunction{PyTuple_SetItem()} for tuples that you are creating |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 240 | yourself. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 241 | |
| 242 | Equivalent code for populating a list can be written using |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 243 | \cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code |
| 244 | can also use \cfunction{PySequence_SetItem()}; this illustrates the |
| 245 | difference between the two (the extra \cfunction{Py_DECREF()} calls): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 246 | |
| 247 | \begin{verbatim} |
| 248 | PyObject *l, *x; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 250 | l = PyList_New(3); |
| 251 | x = PyInt_FromLong(1L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 252 | PySequence_SetItem(l, 0, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 253 | x = PyInt_FromLong(2L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 254 | PySequence_SetItem(l, 1, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 255 | x = PyString_FromString("three"); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 256 | PySequence_SetItem(l, 2, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 257 | \end{verbatim} |
| 258 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 259 | You might find it strange that the ``recommended'' approach takes more |
| 260 | code. However, in practice, you will rarely use these ways of |
| 261 | creating and populating a tuple or list. There's a generic function, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 262 | \cfunction{Py_BuildValue()}, that can create most common objects from |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 263 | C values, directed by a \dfn{format string}. For example, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 264 | above two blocks of code could be replaced by the following (which |
| 265 | also takes care of the error checking): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 266 | |
| 267 | \begin{verbatim} |
| 268 | PyObject *t, *l; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 270 | t = Py_BuildValue("(iis)", 1, 2, "three"); |
| 271 | l = Py_BuildValue("[iis]", 1, 2, "three"); |
| 272 | \end{verbatim} |
| 273 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 274 | It is much more common to use \cfunction{PyObject_SetItem()} and |
| 275 | friends with items whose references you are only borrowing, like |
| 276 | arguments that were passed in to the function you are writing. In |
| 277 | that case, their behaviour regarding reference counts is much saner, |
| 278 | since you don't have to increment a reference count so you can give a |
| 279 | reference away (``have it be stolen''). For example, this function |
| 280 | sets all items of a list (actually, any mutable sequence) to a given |
| 281 | item: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 282 | |
| 283 | \begin{verbatim} |
| 284 | int set_all(PyObject *target, PyObject *item) |
| 285 | { |
| 286 | int i, n; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 288 | n = PyObject_Length(target); |
| 289 | if (n < 0) |
| 290 | return -1; |
| 291 | for (i = 0; i < n; i++) { |
| 292 | if (PyObject_SetItem(target, i, item) < 0) |
| 293 | return -1; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 298 | \ttindex{set_all()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 299 | |
| 300 | The situation is slightly different for function return values. |
| 301 | While passing a reference to most functions does not change your |
| 302 | ownership responsibilities for that reference, many functions that |
| 303 | return a referece to an object give you ownership of the reference. |
| 304 | The reason is simple: in many cases, the returned object is created |
| 305 | on the fly, and the reference you get is the only reference to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 306 | object. Therefore, the generic functions that return object |
| 307 | references, like \cfunction{PyObject_GetItem()} and |
| 308 | \cfunction{PySequence_GetItem()}, always return a new reference (i.e., |
| 309 | the caller becomes the owner of the reference). |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 310 | |
| 311 | It is important to realize that whether you own a reference returned |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 312 | by a function depends on which function you call only --- \emph{the |
| 313 | plumage} (i.e., the type of the type of the object passed as an |
| 314 | argument to the function) \emph{doesn't enter into it!} Thus, if you |
| 315 | extract an item from a list using \cfunction{PyList_GetItem()}, you |
| 316 | don't own the reference --- but if you obtain the same item from the |
| 317 | same list using \cfunction{PySequence_GetItem()} (which happens to |
| 318 | take exactly the same arguments), you do own a reference to the |
| 319 | returned object. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 320 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 321 | Here is an example of how you could write a function that computes the |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 322 | sum of the items in a list of integers; once using |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 323 | \cfunction{PyList_GetItem()}\ttindex{PyList_GetItem()}, and once using |
| 324 | \cfunction{PySequence_GetItem()}\ttindex{PySequence_GetItem()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 325 | |
| 326 | \begin{verbatim} |
| 327 | long sum_list(PyObject *list) |
| 328 | { |
| 329 | int i, n; |
| 330 | long total = 0; |
| 331 | PyObject *item; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 333 | n = PyList_Size(list); |
| 334 | if (n < 0) |
| 335 | return -1; /* Not a list */ |
| 336 | for (i = 0; i < n; i++) { |
| 337 | item = PyList_GetItem(list, i); /* Can't fail */ |
| 338 | if (!PyInt_Check(item)) continue; /* Skip non-integers */ |
| 339 | total += PyInt_AsLong(item); |
| 340 | } |
| 341 | return total; |
| 342 | } |
| 343 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 344 | \ttindex{sum_list()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 345 | |
| 346 | \begin{verbatim} |
| 347 | long sum_sequence(PyObject *sequence) |
| 348 | { |
| 349 | int i, n; |
| 350 | long total = 0; |
| 351 | PyObject *item; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 352 | n = PySequence_Length(sequence); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 353 | if (n < 0) |
| 354 | return -1; /* Has no length */ |
| 355 | for (i = 0; i < n; i++) { |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 356 | item = PySequence_GetItem(sequence, i); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 357 | if (item == NULL) |
| 358 | return -1; /* Not a sequence, or other failure */ |
| 359 | if (PyInt_Check(item)) |
| 360 | total += PyInt_AsLong(item); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 361 | Py_DECREF(item); /* Discard reference ownership */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 362 | } |
| 363 | return total; |
| 364 | } |
| 365 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 366 | \ttindex{sum_sequence()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 367 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 368 | |
| 369 | \subsection{Types \label{types}} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 370 | |
| 371 | There are few other data types that play a significant role in |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 372 | the Python/C API; most are simple C types such as \ctype{int}, |
| 373 | \ctype{long}, \ctype{double} and \ctype{char*}. A few structure types |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 374 | are used to describe static tables used to list the functions exported |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 375 | by a module or the data attributes of a new object type, and another |
| 376 | is used to describe the value of a complex number. These will |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 377 | be discussed together with the functions that use them. |
| 378 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 379 | |
| 380 | \section{Exceptions \label{exceptions}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 382 | The Python programmer only needs to deal with exceptions if specific |
| 383 | error handling is required; unhandled exceptions are automatically |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 384 | propagated to the caller, then to the caller's caller, and so on, until |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 385 | they reach the top-level interpreter, where they are reported to the |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 386 | user accompanied by a stack traceback. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 387 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 388 | For C programmers, however, error checking always has to be explicit. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 389 | All functions in the Python/C API can raise exceptions, unless an |
| 390 | explicit claim is made otherwise in a function's documentation. In |
| 391 | general, when a function encounters an error, it sets an exception, |
| 392 | discards any object references that it owns, and returns an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 393 | error indicator --- usually \NULL{} or \code{-1}. A few functions |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 394 | return a Boolean true/false result, with false indicating an error. |
| 395 | Very few functions return no explicit error indicator or have an |
| 396 | ambiguous return value, and require explicit testing for errors with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 397 | \cfunction{PyErr_Occurred()}\ttindex{PyErr_Occurred()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 398 | |
| 399 | Exception state is maintained in per-thread storage (this is |
| 400 | equivalent to using global storage in an unthreaded application). A |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 401 | thread can be in one of two states: an exception has occurred, or not. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 402 | The function \cfunction{PyErr_Occurred()} can be used to check for |
| 403 | this: it returns a borrowed reference to the exception type object |
| 404 | when an exception has occurred, and \NULL{} otherwise. There are a |
| 405 | number of functions to set the exception state: |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 406 | \cfunction{PyErr_SetString()}\ttindex{PyErr_SetString()} is the most |
| 407 | common (though not the most general) function to set the exception |
| 408 | state, and \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} clears the |
| 409 | exception state. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 410 | |
| 411 | The full exception state consists of three objects (all of which can |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 412 | be \NULL{}): the exception type, the corresponding exception |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 413 | value, and the traceback. These have the same meanings as the Python |
| 414 | \withsubitem{(in module sys)}{ |
| 415 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
| 416 | objects \code{sys.exc_type}, \code{sys.exc_value}, and |
| 417 | \code{sys.exc_traceback}; however, they are not the same: the Python |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 418 | objects represent the last exception being handled by a Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 419 | \keyword{try} \ldots\ \keyword{except} statement, while the C level |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 420 | exception state only exists while an exception is being passed on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 421 | between C functions until it reaches the Python bytecode interpreter's |
| 422 | main loop, which takes care of transferring it to \code{sys.exc_type} |
| 423 | and friends. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 424 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 425 | Note that starting with Python 1.5, the preferred, thread-safe way to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 426 | access the exception state from Python code is to call the function |
| 427 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 428 | \function{sys.exc_info()}, which returns the per-thread exception state |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 429 | for Python code. Also, the semantics of both ways to access the |
| 430 | exception state have changed so that a function which catches an |
| 431 | exception will save and restore its thread's exception state so as to |
| 432 | preserve the exception state of its caller. This prevents common bugs |
| 433 | in exception handling code caused by an innocent-looking function |
| 434 | overwriting the exception being handled; it also reduces the often |
| 435 | unwanted lifetime extension for objects that are referenced by the |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 436 | stack frames in the traceback. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 437 | |
| 438 | As a general principle, a function that calls another function to |
| 439 | perform some task should check whether the called function raised an |
| 440 | exception, and if so, pass the exception state on to its caller. It |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 441 | should discard any object references that it owns, and return an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 442 | error indicator, but it should \emph{not} set another exception --- |
| 443 | that would overwrite the exception that was just raised, and lose |
| 444 | important information about the exact cause of the error. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 445 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 446 | A simple example of detecting exceptions and passing them on is shown |
| 447 | in the \cfunction{sum_sequence()}\ttindex{sum_sequence()} example |
| 448 | above. It so happens that that example doesn't need to clean up any |
| 449 | owned references when it detects an error. The following example |
| 450 | function shows some error cleanup. First, to remind you why you like |
| 451 | Python, we show the equivalent Python code: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 452 | |
| 453 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 454 | def incr_item(dict, key): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 455 | try: |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 456 | item = dict[key] |
| 457 | except KeyError: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 458 | item = 0 |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 459 | return item + 1 |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 460 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 461 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 462 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 463 | Here is the corresponding C code, in all its glory: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 464 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 465 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 466 | int incr_item(PyObject *dict, PyObject *key) |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 467 | { |
| 468 | /* Objects all initialized to NULL for Py_XDECREF */ |
| 469 | PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL; |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 470 | int rv = -1; /* Return value initialized to -1 (failure) */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 472 | item = PyObject_GetItem(dict, key); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 473 | if (item == NULL) { |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 474 | /* Handle KeyError only: */ |
| 475 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 476 | |
| 477 | /* Clear the error and use zero: */ |
| 478 | PyErr_Clear(); |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 479 | item = PyInt_FromLong(0L); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 480 | if (item == NULL) goto error; |
| 481 | } |
| 482 | |
| 483 | const_one = PyInt_FromLong(1L); |
| 484 | if (const_one == NULL) goto error; |
| 485 | |
| 486 | incremented_item = PyNumber_Add(item, const_one); |
| 487 | if (incremented_item == NULL) goto error; |
| 488 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 489 | if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 490 | rv = 0; /* Success */ |
| 491 | /* Continue with cleanup code */ |
| 492 | |
| 493 | error: |
| 494 | /* Cleanup code, shared by success and failure path */ |
| 495 | |
| 496 | /* Use Py_XDECREF() to ignore NULL references */ |
| 497 | Py_XDECREF(item); |
| 498 | Py_XDECREF(const_one); |
| 499 | Py_XDECREF(incremented_item); |
| 500 | |
| 501 | return rv; /* -1 for error, 0 for success */ |
| 502 | } |
| 503 | \end{verbatim} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 504 | \ttindex{incr_item()} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 505 | |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 506 | This example represents an endorsed use of the \keyword{goto} statement |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 507 | in C! It illustrates the use of |
| 508 | \cfunction{PyErr_ExceptionMatches()}\ttindex{PyErr_ExceptionMatches()} and |
| 509 | \cfunction{PyErr_Clear()}\ttindex{PyErr_Clear()} to |
| 510 | handle specific exceptions, and the use of |
| 511 | \cfunction{Py_XDECREF()}\ttindex{Py_XDECREF()} to |
| 512 | dispose of owned references that may be \NULL{} (note the |
| 513 | \character{X} in the name; \cfunction{Py_DECREF()} would crash when |
| 514 | confronted with a \NULL{} reference). It is important that the |
| 515 | variables used to hold owned references are initialized to \NULL{} for |
| 516 | this to work; likewise, the proposed return value is initialized to |
| 517 | \code{-1} (failure) and only set to success after the final call made |
| 518 | is successful. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 519 | |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 520 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 521 | \section{Embedding Python \label{embedding}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 523 | The one important task that only embedders (as opposed to extension |
| 524 | writers) of the Python interpreter have to worry about is the |
| 525 | initialization, and possibly the finalization, of the Python |
| 526 | interpreter. Most functionality of the interpreter can only be used |
| 527 | after the interpreter has been initialized. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 528 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 529 | The basic initialization function is |
| 530 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 531 | This initializes the table of loaded modules, and creates the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 532 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
| 533 | \module{__main__}\refbimodindex{__main__} and |
| 534 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 535 | search path (\code{sys.path}).% |
| 536 | \indexiii{module}{search}{path} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 537 | \withsubitem{(in module sys)}{\ttindex{path}} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 538 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 539 | \cfunction{Py_Initialize()} does not set the ``script argument list'' |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 540 | (\code{sys.argv}). If this variable is needed by Python code that |
| 541 | 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] | 542 | \code{PySys_SetArgv(\var{argc}, |
| 543 | \var{argv})}\ttindex{PySys_SetArgv()} subsequent to the call to |
| 544 | \cfunction{Py_Initialize()}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 545 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 546 | On most systems (in particular, on \UNIX{} and Windows, although the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 547 | details are slightly different), |
| 548 | \cfunction{Py_Initialize()} calculates the module search path based |
| 549 | upon its best guess for the location of the standard Python |
| 550 | interpreter executable, assuming that the Python library is found in a |
| 551 | fixed location relative to the Python interpreter executable. In |
| 552 | particular, it looks for a directory named |
Fred Drake | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 553 | \file{lib/python1.5} (replacing \file{1.5} with the current |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 554 | interpreter version) relative to the parent directory where the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 555 | executable named \file{python} is found on the shell command search |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 556 | path (the environment variable \envvar{PATH}). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 557 | |
| 558 | For instance, if the Python executable is found in |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 559 | \file{/usr/local/bin/python}, it will assume that the libraries are in |
Fred Drake | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 560 | \file{/usr/local/lib/python1.5}. (In fact, this particular path |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 561 | is also the ``fallback'' location, used when no executable file named |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 562 | \file{python} is found along \envvar{PATH}.) The user can override |
| 563 | this behavior by setting the environment variable \envvar{PYTHONHOME}, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 564 | or insert additional directories in front of the standard path by |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 565 | setting \envvar{PYTHONPATH}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 567 | The embedding application can steer the search by calling |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 568 | \code{Py_SetProgramName(\var{file})}\ttindex{Py_SetProgramName()} \emph{before} calling |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 569 | \cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still |
| 570 | overrides this and \envvar{PYTHONPATH} is still inserted in front of |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 571 | the standard path. An application that requires total control has to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 572 | provide its own implementation of |
| 573 | \cfunction{Py_GetPath()}\ttindex{Py_GetPath()}, |
| 574 | \cfunction{Py_GetPrefix()}\ttindex{Py_GetPrefix()}, |
| 575 | \cfunction{Py_GetExecPrefix()}\ttindex{Py_GetExecPrefix()}, and |
| 576 | \cfunction{Py_GetProgramFullPath()}\ttindex{Py_GetProgramFullPath()} (all |
| 577 | defined in \file{Modules/getpath.c}). |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 579 | Sometimes, it is desirable to ``uninitialize'' Python. For instance, |
| 580 | the application may want to start over (make another call to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 581 | \cfunction{Py_Initialize()}) or the application is simply done with its |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 582 | 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] | 583 | can be accomplished by calling \cfunction{Py_Finalize()}. The function |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 584 | \cfunction{Py_IsInitialized()}\ttindex{Py_IsInitialized()} returns |
| 585 | true if Python is currently in the initialized state. More |
| 586 | information about these functions is given in a later chapter. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 588 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 589 | \chapter{The Very High Level Layer \label{veryhigh}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 590 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 591 | The functions in this chapter will let you execute Python source code |
| 592 | given in a file or a buffer, but they will not let you interact in a |
| 593 | more detailed way with the interpreter. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 594 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 595 | Several of these functions accept a start symbol from the grammar as a |
| 596 | parameter. The available start symbols are \constant{Py_eval_input}, |
| 597 | \constant{Py_file_input}, and \constant{Py_single_input}. These are |
| 598 | described following the functions which accept them as parameters. |
| 599 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 600 | \begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 601 | If \var{fp} refers to a file associated with an interactive device |
| 602 | (console or terminal input or \UNIX{} pseudo-terminal), return the |
| 603 | value of \cfunction{PyRun_InteractiveLoop()}, otherwise return the |
| 604 | result of \cfunction{PyRun_SimpleFile()}. If \var{filename} is |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 605 | \NULL{}, this function uses \code{'???'} as the filename. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 606 | \end{cfuncdesc} |
| 607 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 608 | \begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 609 | Executes the Python source code from \var{command} in the |
| 610 | \module{__main__} module. If \module{__main__} does not already |
| 611 | exist, it is created. Returns \code{0} on success or \code{-1} if |
| 612 | an exception was raised. If there was an error, there is no way to |
| 613 | get the exception information. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 614 | \end{cfuncdesc} |
| 615 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 616 | \begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 617 | Similar to \cfunction{PyRun_SimpleString()}, but the Python source |
| 618 | code is read from \var{fp} instead of an in-memory string. |
| 619 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 620 | \end{cfuncdesc} |
| 621 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 622 | \begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 623 | \end{cfuncdesc} |
| 624 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 625 | \begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 626 | \end{cfuncdesc} |
| 627 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 628 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str, |
| 629 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 630 | Parse Python source code from \var{str} using the start token |
| 631 | \var{start}. The result can be used to create a code object which |
| 632 | can be evaluated efficiently. This is useful if a code fragment |
| 633 | must be evaluated many times. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 634 | \end{cfuncdesc} |
| 635 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 636 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp, |
| 637 | char *filename, int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 638 | Similar to \cfunction{PyParser_SimpleParseString()}, but the Python |
| 639 | source code is read from \var{fp} instead of an in-memory string. |
| 640 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 641 | \end{cfuncdesc} |
| 642 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 643 | \begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start, |
| 644 | PyObject *globals, |
| 645 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 646 | Execute Python source code from \var{str} in the context specified |
| 647 | by the dictionaries \var{globals} and \var{locals}. The parameter |
| 648 | \var{start} specifies the start token that should be used to parse |
| 649 | the source code. |
| 650 | |
| 651 | Returns the result of executing the code as a Python object, or |
| 652 | \NULL{} if an exception was raised. |
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}{PyObject*}{PyRun_File}{FILE *fp, char *filename, |
| 656 | int start, PyObject *globals, |
| 657 | PyObject *locals} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 658 | Similar to \cfunction{PyRun_String()}, but the Python source code is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 659 | read from \var{fp} instead of an in-memory string. |
| 660 | \var{filename} should be the name of the file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 661 | \end{cfuncdesc} |
| 662 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 663 | \begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename, |
| 664 | int start} |
Fred Drake | 0041a94 | 1999-04-29 04:20:46 +0000 | [diff] [blame] | 665 | Parse and compile the Python source code in \var{str}, returning the |
| 666 | resulting code object. The start token is given by \var{start}; |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 667 | this can be used to constrain the code which can be compiled and should |
| 668 | be \constant{Py_eval_input}, \constant{Py_file_input}, or |
| 669 | \constant{Py_single_input}. The filename specified by |
| 670 | \var{filename} is used to construct the code object and may appear |
| 671 | in tracebacks or \exception{SyntaxError} exception messages. This |
| 672 | returns \NULL{} if the code cannot be parsed or compiled. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 673 | \end{cfuncdesc} |
| 674 | |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 675 | \begin{cvardesc}{int}{Py_eval_input} |
| 676 | The start symbol from the Python grammar for isolated expressions; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 677 | for use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 678 | \end{cvardesc} |
| 679 | |
| 680 | \begin{cvardesc}{int}{Py_file_input} |
| 681 | The start symbol from the Python grammar for sequences of statements |
| 682 | as read from a file or other source; for use with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 683 | \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. This is |
| 684 | the symbol to use when compiling arbitrarily long Python source code. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 685 | \end{cvardesc} |
| 686 | |
| 687 | \begin{cvardesc}{int}{Py_single_input} |
| 688 | The start symbol from the Python grammar for a single statement; for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 689 | use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. |
| 690 | This is the symbol used for the interactive interpreter loop. |
Fred Drake | c924b8d | 1999-08-23 18:57:25 +0000 | [diff] [blame] | 691 | \end{cvardesc} |
| 692 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 693 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 694 | \chapter{Reference Counting \label{countingRefs}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 695 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 696 | The macros in this section are used for managing reference counts |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 697 | of Python objects. |
| 698 | |
| 699 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 700 | Increment the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 701 | 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] | 702 | \cfunction{Py_XINCREF()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 703 | \end{cfuncdesc} |
| 704 | |
| 705 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 706 | 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] | 707 | \NULL{}, in which case the macro has no effect. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 708 | \end{cfuncdesc} |
| 709 | |
| 710 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 711 | Decrement the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 712 | 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] | 713 | \cfunction{Py_XDECREF()}. If the reference count reaches zero, the |
| 714 | object's type's deallocation function (which must not be \NULL{}) is |
| 715 | invoked. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 716 | |
| 717 | \strong{Warning:} The deallocation function can cause arbitrary Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 718 | code to be invoked (e.g. when a class instance with a |
| 719 | \method{__del__()} method is deallocated). While exceptions in such |
| 720 | code are not propagated, the executed code has free access to all |
| 721 | Python global variables. This means that any object that is reachable |
| 722 | from a global variable should be in a consistent state before |
| 723 | \cfunction{Py_DECREF()} is invoked. For example, code to delete an |
| 724 | object from a list should copy a reference to the deleted object in a |
| 725 | temporary variable, update the list data structure, and then call |
| 726 | \cfunction{Py_DECREF()} for the temporary variable. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 727 | \end{cfuncdesc} |
| 728 | |
| 729 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 730 | Decrement the reference count for object \var{o}. The object may be |
| 731 | \NULL{}, in which case the macro has no effect; otherwise the effect |
| 732 | 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] | 733 | applies. |
| 734 | \end{cfuncdesc} |
| 735 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 736 | The following functions or macros are only for use within the |
| 737 | interpreter core: \cfunction{_Py_Dealloc()}, |
| 738 | \cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as |
| 739 | well as the global variable \cdata{_Py_RefTotal}. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 741 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 742 | \chapter{Exception Handling \label{exceptionHandling}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 743 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 744 | 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] | 745 | exceptions. It is important to understand some of the basics of |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 746 | Python exception handling. It works somewhat like the |
| 747 | \UNIX{} \cdata{errno} variable: there is a global indicator (per |
| 748 | thread) of the last error that occurred. Most functions don't clear |
| 749 | this on success, but will set it to indicate the cause of the error on |
| 750 | failure. Most functions also return an error indicator, usually |
| 751 | \NULL{} if they are supposed to return a pointer, or \code{-1} if they |
| 752 | return an integer (exception: the \cfunction{PyArg_Parse*()} functions |
| 753 | return \code{1} for success and \code{0} for failure). When a |
| 754 | function must fail because some function it called failed, it |
| 755 | generally doesn't set the error indicator; the function it called |
| 756 | already set it. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 757 | |
| 758 | The error indicator consists of three Python objects corresponding to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 759 | \withsubitem{(in module sys)}{ |
| 760 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 761 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and |
| 762 | \code{sys.exc_traceback}. API functions exist to interact with the |
| 763 | error indicator in various ways. There is a separate error indicator |
| 764 | for each thread. |
| 765 | |
| 766 | % XXX Order of these should be more thoughtful. |
| 767 | % Either alphabetical or some kind of structure. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 768 | |
| 769 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 770 | Print a standard traceback to \code{sys.stderr} and clear the error |
| 771 | indicator. Call this function only when the error indicator is set. |
| 772 | (Otherwise it will cause a fatal error!) |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 773 | \end{cfuncdesc} |
| 774 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 775 | \begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 776 | Test whether the error indicator is set. If set, return the exception |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 777 | \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] | 778 | \cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 779 | not set, return \NULL{}. You do not own a reference to the return |
| 780 | value, so you do not need to \cfunction{Py_DECREF()} it. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 781 | \strong{Note:} Do not compare the return value to a specific |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 782 | exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 783 | below. (The comparison could easily fail since the exception may be |
| 784 | an instance instead of a class, in the case of a class exception, or |
| 785 | it may the a subclass of the expected exception.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 786 | \end{cfuncdesc} |
| 787 | |
| 788 | \begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 789 | Equivalent to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 790 | \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 791 | This should only be called when an exception is actually set; a memory |
| 792 | access violation will occur if no exception has been raised. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 793 | \end{cfuncdesc} |
| 794 | |
| 795 | \begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 796 | Return true if the \var{given} exception matches the exception in |
| 797 | \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] | 798 | 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] | 799 | exceptions in the tuple (and recursively in subtuples) are searched |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 800 | for a match. If \var{given} is \NULL, a memory access violation will |
| 801 | occur. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 802 | \end{cfuncdesc} |
| 803 | |
| 804 | \begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 805 | Under certain circumstances, the values returned by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 806 | \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that |
| 807 | \code{*\var{exc}} is a class object but \code{*\var{val}} is not an |
| 808 | instance of the same class. This function can be used to instantiate |
| 809 | the class in that case. If the values are already normalized, nothing |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 810 | happens. The delayed normalization is implemented to improve |
| 811 | performance. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 812 | \end{cfuncdesc} |
| 813 | |
| 814 | \begin{cfuncdesc}{void}{PyErr_Clear}{} |
| 815 | Clear the error indicator. If the error indicator is not set, there |
| 816 | is no effect. |
| 817 | \end{cfuncdesc} |
| 818 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 819 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, |
| 820 | PyObject **ptraceback} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 821 | Retrieve the error indicator into three variables whose addresses are |
| 822 | passed. If the error indicator is not set, set all three variables to |
| 823 | \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] | 824 | each object retrieved. The value and traceback object may be |
| 825 | \NULL{} even when the type object is not. \strong{Note:} This |
| 826 | function is normally only used by code that needs to handle exceptions |
| 827 | or by code that needs to save and restore the error indicator |
| 828 | temporarily. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 829 | \end{cfuncdesc} |
| 830 | |
| 831 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback} |
| 832 | Set the error indicator from the three objects. If the error |
| 833 | indicator is already set, it is cleared first. If the objects are |
| 834 | \NULL{}, the error indicator is cleared. Do not pass a \NULL{} type |
| 835 | and non-\NULL{} value or traceback. The exception type should be a |
| 836 | string or class; if it is a class, the value should be an instance of |
| 837 | that class. Do not pass an invalid exception type or value. |
| 838 | (Violating these rules will cause subtle problems later.) This call |
| 839 | takes away a reference to each object, i.e. you must own a reference |
| 840 | to each object before the call and after the call you no longer own |
| 841 | these references. (If you don't understand this, don't use this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 842 | function. I warned you.) \strong{Note:} This function is normally |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 843 | only used by code that needs to save and restore the error indicator |
| 844 | temporarily. |
| 845 | \end{cfuncdesc} |
| 846 | |
| 847 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message} |
| 848 | This is the most common way to set the error indicator. The first |
| 849 | argument specifies the exception type; it is normally one of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 850 | standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 851 | increment its reference count. The second argument is an error |
| 852 | message; it is converted to a string object. |
| 853 | \end{cfuncdesc} |
| 854 | |
| 855 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 856 | This function is similar to \cfunction{PyErr_SetString()} but lets you |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 857 | specify an arbitrary Python object for the ``value'' of the exception. |
| 858 | You need not increment its reference count. |
| 859 | \end{cfuncdesc} |
| 860 | |
Fred Drake | 7357770 | 2000-04-10 18:50:14 +0000 | [diff] [blame] | 861 | \begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception, |
| 862 | const char *format, ...} |
Jeremy Hylton | 98605b5 | 2000-04-10 18:40:57 +0000 | [diff] [blame] | 863 | This function sets the error indicator using a printf-style format |
| 864 | string. The first argument specifies the exception type and the |
| 865 | second argument specifies the format string for the exception. Any |
| 866 | subsequent arguments are converted to output by the C library's |
| 867 | \cfunction{vsprintf()} function. The buffer used internally by |
Fred Drake | 7357770 | 2000-04-10 18:50:14 +0000 | [diff] [blame] | 868 | \cfunction{PyErr_Format()} is 500 bytes long. The caller is |
| 869 | responsible for guaranteeing that the formatted output does not |
| 870 | overflow the buffer. |
Jeremy Hylton | 98605b5 | 2000-04-10 18:40:57 +0000 | [diff] [blame] | 871 | \end{cfuncdesc} |
| 872 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 873 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 874 | 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] | 875 | \end{cfuncdesc} |
| 876 | |
| 877 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 878 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 879 | \var{message})}, where \var{message} indicates that a built-in operation |
| 880 | was invoked with an illegal argument. It is mostly for internal use. |
| 881 | \end{cfuncdesc} |
| 882 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 883 | \begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 884 | This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 885 | returns \NULL{} so an object allocation function can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 886 | \samp{return PyErr_NoMemory();} when it runs out of memory. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 887 | \end{cfuncdesc} |
| 888 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 889 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 890 | This is a convenience function to raise an exception when a C library |
| 891 | 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] | 892 | It constructs a tuple object whose first item is the integer |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 893 | \cdata{errno} value and whose second item is the corresponding error |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 894 | message (gotten from \cfunction{strerror()}\ttindex{strerror()}), and |
| 895 | then calls |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 896 | \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 897 | the \cdata{errno} value is \constant{EINTR}, indicating an interrupted |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 898 | system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 899 | the error indicator, leaves it set to that. The function always |
| 900 | returns \NULL{}, so a wrapper function around a system call can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 901 | \samp{return PyErr_SetFromErrno();} when the system call returns an |
| 902 | error. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 903 | \end{cfuncdesc} |
| 904 | |
| 905 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 906 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 907 | \var{message})}, where \var{message} indicates that an internal |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 908 | 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] | 909 | argument. It is mostly for internal use. |
| 910 | \end{cfuncdesc} |
| 911 | |
| 912 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{} |
| 913 | This function interacts with Python's signal handling. It checks |
| 914 | 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] | 915 | corresponding signal handler. If the |
| 916 | \module{signal}\refbimodindex{signal} module is supported, this can |
| 917 | invoke a signal handler written in Python. In all cases, the default |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 918 | effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the |
| 919 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 920 | \exception{KeyboardInterrupt} exception. If an exception is raised the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 921 | error indicator is set and the function returns \code{1}; otherwise |
| 922 | the function returns \code{0}. The error indicator may or may not be |
| 923 | cleared if it was previously set. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 924 | \end{cfuncdesc} |
| 925 | |
| 926 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 927 | This function is obsolete. It simulates the effect of a |
| 928 | \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 929 | \cfunction{PyErr_CheckSignals()} is called, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 930 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}} |
| 931 | \exception{KeyboardInterrupt} will be raised. |
| 932 | It may be called without holding the interpreter lock. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 933 | \end{cfuncdesc} |
| 934 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 935 | \begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name, |
| 936 | PyObject *base, |
| 937 | PyObject *dict} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 938 | This utility function creates and returns a new exception object. The |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 939 | \var{name} argument must be the name of the new exception, a C string |
| 940 | of the form \code{module.class}. The \var{base} and |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 941 | \var{dict} arguments are normally \NULL{}. This creates a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 942 | class object derived from the root for all exceptions, the built-in |
| 943 | name \exception{Exception} (accessible in C as |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 944 | \cdata{PyExc_Exception}). The \member{__module__} attribute of the |
| 945 | new class is set to the first part (up to the last dot) of the |
| 946 | \var{name} argument, and the class name is set to the last part (after |
| 947 | the last dot). The \var{base} argument can be used to specify an |
| 948 | alternate base class. The \var{dict} argument can be used to specify |
| 949 | a dictionary of class variables and methods. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 950 | \end{cfuncdesc} |
| 951 | |
| 952 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 953 | \section{Standard Exceptions \label{standardExceptions}} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 954 | |
| 955 | All standard Python exceptions are available as global variables whose |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 956 | names are \samp{PyExc_} followed by the Python exception name. These |
| 957 | have the type \ctype{PyObject*}; they are all class objects. For |
| 958 | completeness, here are all the variables: |
| 959 | |
| 960 | \begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes} |
| 961 | \lineiii{PyExc_Exception}{\exception{Exception}}{(1)} |
| 962 | \lineiii{PyExc_StandardError}{\exception{StandardError}}{(1)} |
| 963 | \lineiii{PyExc_ArithmeticError}{\exception{ArithmeticError}}{(1)} |
| 964 | \lineiii{PyExc_LookupError}{\exception{LookupError}}{(1)} |
| 965 | \lineiii{PyExc_AssertionError}{\exception{AssertionError}}{} |
| 966 | \lineiii{PyExc_AttributeError}{\exception{AttributeError}}{} |
| 967 | \lineiii{PyExc_EOFError}{\exception{EOFError}}{} |
| 968 | \lineiii{PyExc_EnvironmentError}{\exception{EnvironmentError}}{(1)} |
| 969 | \lineiii{PyExc_FloatingPointError}{\exception{FloatingPointError}}{} |
| 970 | \lineiii{PyExc_IOError}{\exception{IOError}}{} |
| 971 | \lineiii{PyExc_ImportError}{\exception{ImportError}}{} |
| 972 | \lineiii{PyExc_IndexError}{\exception{IndexError}}{} |
| 973 | \lineiii{PyExc_KeyError}{\exception{KeyError}}{} |
| 974 | \lineiii{PyExc_KeyboardInterrupt}{\exception{KeyboardInterrupt}}{} |
| 975 | \lineiii{PyExc_MemoryError}{\exception{MemoryError}}{} |
| 976 | \lineiii{PyExc_NameError}{\exception{NameError}}{} |
| 977 | \lineiii{PyExc_NotImplementedError}{\exception{NotImplementedError}}{} |
| 978 | \lineiii{PyExc_OSError}{\exception{OSError}}{} |
| 979 | \lineiii{PyExc_OverflowError}{\exception{OverflowError}}{} |
| 980 | \lineiii{PyExc_RuntimeError}{\exception{RuntimeError}}{} |
| 981 | \lineiii{PyExc_SyntaxError}{\exception{SyntaxError}}{} |
| 982 | \lineiii{PyExc_SystemError}{\exception{SystemError}}{} |
| 983 | \lineiii{PyExc_SystemExit}{\exception{SystemExit}}{} |
| 984 | \lineiii{PyExc_TypeError}{\exception{TypeError}}{} |
| 985 | \lineiii{PyExc_ValueError}{\exception{ValueError}}{} |
| 986 | \lineiii{PyExc_ZeroDivisionError}{\exception{ZeroDivisionError}}{} |
| 987 | \end{tableiii} |
| 988 | |
| 989 | \noindent |
| 990 | Note: |
| 991 | \begin{description} |
| 992 | \item[(1)] |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 993 | This is a base class for other standard exceptions. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 994 | \end{description} |
| 995 | |
| 996 | |
| 997 | \section{Deprecation of String Exceptions} |
| 998 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 999 | All exceptions built into Python or provided in the standard library |
| 1000 | are derived from \exception{Exception}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1001 | \withsubitem{(built-in exception)}{\ttindex{Exception}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1002 | |
Fred Drake | d04038d | 2000-06-29 20:15:14 +0000 | [diff] [blame] | 1003 | String exceptions are still supported in the interpreter to allow |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1004 | existing code to run unmodified, but this will also change in a future |
| 1005 | release. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1006 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1007 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1008 | \chapter{Utilities \label{utilities}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1009 | |
| 1010 | The functions in this chapter perform various utility tasks, such as |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1011 | parsing function arguments and constructing Python values from C |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1012 | values. |
| 1013 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1014 | \section{OS Utilities \label{os}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1015 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1016 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1017 | Return true (nonzero) if the standard I/O file \var{fp} with name |
| 1018 | \var{filename} is deemed interactive. This is the case for files for |
| 1019 | which \samp{isatty(fileno(\var{fp}))} is true. If the global flag |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1020 | \cdata{Py_InteractiveFlag} is true, this function also returns true if |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1021 | the \var{name} pointer is \NULL{} or if the name is equal to one of |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 1022 | the strings \code{'<stdin>'} or \code{'???'}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1023 | \end{cfuncdesc} |
| 1024 | |
| 1025 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1026 | Return the time of last modification of the file \var{filename}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1027 | 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] | 1028 | the standard C library function \cfunction{time()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1029 | \end{cfuncdesc} |
| 1030 | |
Fred Drake | cabbc3b | 2000-06-28 15:53:13 +0000 | [diff] [blame] | 1031 | \begin{cfuncdesc}{void}{PyOS_AfterFork}{} |
| 1032 | Function to update some internal state after a process fork; this |
| 1033 | should be called in the new process if the Python interpreter will |
| 1034 | continue to be used. If a new executable is loaded into the new |
| 1035 | process, this function does not need to be called. |
| 1036 | \end{cfuncdesc} |
| 1037 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1038 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1039 | \section{Process Control \label{processControl}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1040 | |
| 1041 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 1042 | Print a fatal error message and kill the process. No cleanup is |
| 1043 | performed. This function should only be invoked when a condition is |
| 1044 | detected that would make it dangerous to continue using the Python |
| 1045 | interpreter; e.g., when the object administration appears to be |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1046 | corrupted. On \UNIX{}, the standard C library function |
| 1047 | \cfunction{abort()}\ttindex{abort()} is called which will attempt to |
| 1048 | produce a \file{core} file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1049 | \end{cfuncdesc} |
| 1050 | |
| 1051 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1052 | Exit the current process. This calls |
| 1053 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 1054 | then calls the standard C library function |
| 1055 | \code{exit(\var{status})}\ttindex{exit()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1056 | \end{cfuncdesc} |
| 1057 | |
| 1058 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1059 | Register a cleanup function to be called by |
| 1060 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1061 | The cleanup function will be called with no arguments and should |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1062 | return no value. At most 32 \index{cleanup functions}cleanup |
| 1063 | functions can be registered. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1064 | When the registration is successful, \cfunction{Py_AtExit()} returns |
| 1065 | \code{0}; on failure, it returns \code{-1}. The cleanup function |
| 1066 | registered last is called first. Each cleanup function will be called |
| 1067 | at most once. Since Python's internal finallization will have |
| 1068 | completed before the cleanup function, no Python APIs should be called |
| 1069 | by \var{func}. |
| 1070 | \end{cfuncdesc} |
| 1071 | |
| 1072 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1073 | \section{Importing Modules \label{importing}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1074 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1075 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1076 | This is a simplified interface to |
| 1077 | \cfunction{PyImport_ImportModuleEx()} below, leaving the |
| 1078 | \var{globals} and \var{locals} arguments set to \NULL{}. When the |
| 1079 | \var{name} argument contains a dot (i.e., when it specifies a |
| 1080 | submodule of a package), the \var{fromlist} argument is set to the |
| 1081 | list \code{['*']} so that the return value is the named module rather |
| 1082 | than the top-level package containing it as would otherwise be the |
| 1083 | case. (Unfortunately, this has an additional side effect when |
| 1084 | \var{name} in fact specifies a subpackage instead of a submodule: the |
| 1085 | submodules specified in the package's \code{__all__} variable are |
| 1086 | \index{package variable!\code{__all__}} |
| 1087 | \withsubitem{(package variable)}{\ttindex{__all__}}loaded.) Return a |
| 1088 | new reference to the imported module, or |
| 1089 | \NULL{} with an exception set on failure (the module may still be |
| 1090 | created in this case --- examine \code{sys.modules} to find out). |
| 1091 | \withsubitem{(in module sys)}{\ttindex{modules}} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1092 | \end{cfuncdesc} |
| 1093 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1094 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1095 | 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] | 1096 | Python function \function{__import__()}\bifuncindex{__import__}, as |
| 1097 | the standard \function{__import__()} function calls this function |
| 1098 | directly. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1099 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1100 | 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] | 1101 | top-level package, or \NULL{} with an exception set on failure |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 1102 | (the module may still be created in this case). Like for |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1103 | \function{__import__()}, the return value when a submodule of a |
| 1104 | package was requested is normally the top-level package, unless a |
| 1105 | non-empty \var{fromlist} was given. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1106 | \end{cfuncdesc} |
| 1107 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1108 | \begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1109 | This is a higher-level interface that calls the current ``import hook |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1110 | function''. It invokes the \function{__import__()} function from the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1111 | \code{__builtins__} of the current globals. This means that the |
| 1112 | import is done using whatever import hooks are installed in the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 1113 | current environment, e.g. by \module{rexec}\refstmodindex{rexec} or |
| 1114 | \module{ihooks}\refstmodindex{ihooks}. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1115 | \end{cfuncdesc} |
| 1116 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1117 | \begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1118 | 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] | 1119 | Python function \function{reload()}\bifuncindex{reload}, as the standard |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1120 | \function{reload()} function calls this function directly. Return a |
| 1121 | new reference to the reloaded module, or \NULL{} with an exception set |
| 1122 | on failure (the module still exists in this case). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1123 | \end{cfuncdesc} |
| 1124 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1125 | \begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1126 | Return the module object corresponding to a module name. The |
| 1127 | \var{name} argument may be of the form \code{package.module}). First |
| 1128 | 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] | 1129 | a new one and insert in in the modules dictionary. |
Guido van Rossum | a096a2e | 1998-11-02 17:02:42 +0000 | [diff] [blame] | 1130 | Warning: this function does not load or import the module; if the |
| 1131 | module wasn't already loaded, you will get an empty module object. |
| 1132 | Use \cfunction{PyImport_ImportModule()} or one of its variants to |
| 1133 | import a module. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1134 | Return \NULL{} with an exception set on failure. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1135 | \end{cfuncdesc} |
| 1136 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1137 | \begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1138 | Given a module name (possibly of the form \code{package.module}) and a |
| 1139 | code object read from a Python bytecode file or obtained from the |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1140 | built-in function \function{compile()}\bifuncindex{compile}, load the |
| 1141 | module. Return a new reference to the module object, or \NULL{} with |
| 1142 | an exception set if an error occurred (the module may still be created |
| 1143 | in this case). (This function would reload the module if it was |
| 1144 | already imported.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1145 | \end{cfuncdesc} |
| 1146 | |
| 1147 | \begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1148 | Return the magic number for Python bytecode files (a.k.a. |
| 1149 | \file{.pyc} and \file{.pyo} files). The magic number should be |
| 1150 | present in the first four bytes of the bytecode file, in little-endian |
| 1151 | byte order. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1152 | \end{cfuncdesc} |
| 1153 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1154 | \begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1155 | Return the dictionary used for the module administration |
| 1156 | (a.k.a. \code{sys.modules}). Note that this is a per-interpreter |
| 1157 | variable. |
| 1158 | \end{cfuncdesc} |
| 1159 | |
| 1160 | \begin{cfuncdesc}{void}{_PyImport_Init}{} |
| 1161 | Initialize the import mechanism. For internal use only. |
| 1162 | \end{cfuncdesc} |
| 1163 | |
| 1164 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 1165 | Empty the module table. For internal use only. |
| 1166 | \end{cfuncdesc} |
| 1167 | |
| 1168 | \begin{cfuncdesc}{void}{_PyImport_Fini}{} |
| 1169 | Finalize the import mechanism. For internal use only. |
| 1170 | \end{cfuncdesc} |
| 1171 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1172 | \begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1173 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1174 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1175 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1176 | \begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1177 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1178 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1179 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 1180 | \begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name} |
| 1181 | Load a frozen module named \var{name}. Return \code{1} for success, |
| 1182 | \code{0} if the module is not found, and \code{-1} with an exception |
| 1183 | set if the initialization failed. To access the imported module on a |
| 1184 | successful load, use \cfunction{PyImport_ImportModule()}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1185 | (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] | 1186 | already imported.) |
| 1187 | \end{cfuncdesc} |
| 1188 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1189 | \begin{ctypedesc}[_frozen]{struct _frozen} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1190 | This is the structure type definition for frozen module descriptors, |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1191 | as generated by the \program{freeze}\index{freeze utility} utility |
| 1192 | (see \file{Tools/freeze/} in the Python source distribution). Its |
| 1193 | definition is: |
| 1194 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1195 | \begin{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1196 | struct _frozen { |
Fred Drake | 36fbe76 | 1997-10-13 18:18:33 +0000 | [diff] [blame] | 1197 | char *name; |
| 1198 | unsigned char *code; |
| 1199 | int size; |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1200 | }; |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1201 | \end{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1202 | \end{ctypedesc} |
| 1203 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1204 | \begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1205 | This pointer is initialized to point to an array of \ctype{struct |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1206 | _frozen} records, terminated by one whose members are all |
| 1207 | \NULL{} or zero. When a frozen module is imported, it is searched in |
| 1208 | 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] | 1209 | dynamically created collection of frozen modules. |
| 1210 | \end{cvardesc} |
| 1211 | |
| 1212 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1213 | \chapter{Abstract Objects Layer \label{abstract}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1214 | |
| 1215 | The functions in this chapter interact with Python objects regardless |
| 1216 | of their type, or with wide classes of object types (e.g. all |
| 1217 | numerical types, or all sequence types). When used on object types |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1218 | 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] | 1219 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1220 | \section{Object Protocol \label{object}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1221 | |
| 1222 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1223 | Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error. |
| 1224 | The flags argument is used to enable certain printing options. The |
| 1225 | only option currently supported is \constant{Py_PRINT_RAW}; if given, |
| 1226 | the \function{str()} of the object is written instead of the |
| 1227 | \function{repr()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1228 | \end{cfuncdesc} |
| 1229 | |
| 1230 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1231 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1232 | \code{0} otherwise. This is equivalent to the Python expression |
| 1233 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1234 | This function always succeeds. |
| 1235 | \end{cfuncdesc} |
| 1236 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1237 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, |
| 1238 | char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1239 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1240 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1241 | This is the equivalent of the Python expression |
| 1242 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1243 | \end{cfuncdesc} |
| 1244 | |
| 1245 | |
| 1246 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1247 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1248 | \code{0} otherwise. This is equivalent to the Python expression |
| 1249 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1250 | This function always succeeds. |
| 1251 | \end{cfuncdesc} |
| 1252 | |
| 1253 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1254 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, |
| 1255 | PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1256 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1257 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1258 | This is the equivalent of the Python expression |
| 1259 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1260 | \end{cfuncdesc} |
| 1261 | |
| 1262 | |
| 1263 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1264 | Set the value of the attribute named \var{attr_name}, for object |
| 1265 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This is |
| 1266 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1267 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1268 | \end{cfuncdesc} |
| 1269 | |
| 1270 | |
| 1271 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1272 | Set the value of the attribute named \var{attr_name}, for |
| 1273 | object \var{o}, |
| 1274 | to the value \var{v}. Returns \code{-1} on failure. This is |
| 1275 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1276 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1277 | \end{cfuncdesc} |
| 1278 | |
| 1279 | |
| 1280 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1281 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1282 | \code{-1} on failure. This is the equivalent of the Python |
| 1283 | statement: \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1284 | \end{cfuncdesc} |
| 1285 | |
| 1286 | |
| 1287 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1288 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1289 | \code{-1} on failure. This is the equivalent of the Python |
| 1290 | statement \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1291 | \end{cfuncdesc} |
| 1292 | |
| 1293 | |
| 1294 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1295 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1296 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1297 | \var{o2}. The result of the comparison is returned in \var{result}. |
| 1298 | Returns \code{-1} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1299 | statement\bifuncindex{cmp} \samp{\var{result} = cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1300 | \end{cfuncdesc} |
| 1301 | |
| 1302 | |
| 1303 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1304 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1305 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1306 | \var{o2}. Returns the result of the comparison on success. On error, |
| 1307 | the value returned is undefined; use \cfunction{PyErr_Occurred()} to |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1308 | detect an error. This is equivalent to the Python |
| 1309 | expression\bifuncindex{cmp} \samp{cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1310 | \end{cfuncdesc} |
| 1311 | |
| 1312 | |
| 1313 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1314 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1315 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1316 | the equivalent of the Python expression \samp{repr(\var{o})}. |
| 1317 | Called by the \function{repr()}\bifuncindex{repr} built-in function |
| 1318 | and by reverse quotes. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1319 | \end{cfuncdesc} |
| 1320 | |
| 1321 | |
| 1322 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1323 | Compute a string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1324 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1325 | the equivalent of the Python expression \samp{str(\var{o})}. |
| 1326 | Called by the \function{str()}\bifuncindex{str} built-in function and |
| 1327 | by the \keyword{print} statement. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1328 | \end{cfuncdesc} |
| 1329 | |
| 1330 | |
| 1331 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1332 | 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] | 1333 | object is callable and \code{0} otherwise. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1334 | This function always succeeds. |
| 1335 | \end{cfuncdesc} |
| 1336 | |
| 1337 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1338 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, |
| 1339 | PyObject *args} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1340 | Call a callable Python object \var{callable_object}, with |
| 1341 | arguments given by the tuple \var{args}. If no arguments are |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1342 | 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] | 1343 | call on success, or \NULL{} on failure. This is the equivalent |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1344 | of the Python expression \samp{apply(\var{o}, \var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1345 | \bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1346 | \end{cfuncdesc} |
| 1347 | |
| 1348 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1349 | Call a callable Python object \var{callable_object}, with a |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1350 | variable number of C arguments. The C arguments are described |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1351 | using a \cfunction{Py_BuildValue()} style format string. The format may |
| 1352 | be \NULL{}, indicating that no arguments are provided. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1353 | result of the call on success, or \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1354 | the equivalent of the Python expression \samp{apply(\var{o}, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1355 | \var{args})}.\bifuncindex{apply} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1356 | \end{cfuncdesc} |
| 1357 | |
| 1358 | |
| 1359 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1360 | 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] | 1361 | of C arguments. The C arguments are described by a |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1362 | \cfunction{Py_BuildValue()} format string. The format may be \NULL{}, |
| 1363 | indicating that no arguments are provided. Returns the result of the |
| 1364 | call on success, or \NULL{} on failure. This is the equivalent of the |
| 1365 | Python expression \samp{\var{o}.\var{method}(\var{args})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1366 | Note that special method names, such as \method{__add__()}, |
| 1367 | \method{__getitem__()}, and so on are not supported. The specific |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1368 | abstract-object routines for these must be used. |
| 1369 | \end{cfuncdesc} |
| 1370 | |
| 1371 | |
| 1372 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1373 | Compute and return the hash value of an object \var{o}. On |
| 1374 | failure, return \code{-1}. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1375 | expression \samp{hash(\var{o})}.\bifuncindex{hash} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1376 | \end{cfuncdesc} |
| 1377 | |
| 1378 | |
| 1379 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1380 | Returns \code{1} if the object \var{o} is considered to be true, and |
| 1381 | \code{0} otherwise. This is equivalent to the Python expression |
| 1382 | \samp{not not \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1383 | This function always succeeds. |
| 1384 | \end{cfuncdesc} |
| 1385 | |
| 1386 | |
| 1387 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 1388 | On success, returns a type object corresponding to the object |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1389 | type of object \var{o}. On failure, returns \NULL{}. This is |
| 1390 | equivalent to the Python expression \samp{type(\var{o})}. |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1391 | \bifuncindex{type} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1392 | \end{cfuncdesc} |
| 1393 | |
| 1394 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1395 | 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] | 1396 | both sequence and mapping protocols, the sequence length is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1397 | returned. On error, \code{-1} is returned. This is the equivalent |
| 1398 | to the Python expression \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1399 | \end{cfuncdesc} |
| 1400 | |
| 1401 | |
| 1402 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1403 | Return element of \var{o} corresponding to the object \var{key} or |
| 1404 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1405 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1406 | \end{cfuncdesc} |
| 1407 | |
| 1408 | |
| 1409 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1410 | Map the object \var{key} to the value \var{v}. |
| 1411 | Returns \code{-1} on failure. This is the equivalent |
| 1412 | of the Python statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1413 | \end{cfuncdesc} |
| 1414 | |
| 1415 | |
Guido van Rossum | d1dbf63 | 1999-01-22 20:10:49 +0000 | [diff] [blame] | 1416 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1417 | Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on |
| 1418 | failure. This is the equivalent of the Python statement \samp{del |
| 1419 | \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1420 | \end{cfuncdesc} |
| 1421 | |
Andrew M. Kuchling | 8c46b30 | 2000-07-13 23:58:16 +0000 | [diff] [blame] | 1422 | \begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o} |
| 1423 | Derives a file-descriptor from a Python object. If the object |
| 1424 | is an integer or long integer, its value is returned. If not, the |
| 1425 | object's \method{fileno()} method is called if it exists; the method |
| 1426 | must return an integer or long integer, which is returned as the file |
| 1427 | descriptor value. Returns \code{-1} on failure. |
| 1428 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1429 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1430 | \section{Number Protocol \label{number}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1431 | |
| 1432 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1433 | 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] | 1434 | false otherwise. |
| 1435 | This function always succeeds. |
| 1436 | \end{cfuncdesc} |
| 1437 | |
| 1438 | |
| 1439 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1440 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 1441 | failure. This is the equivalent of the Python expression |
| 1442 | \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1443 | \end{cfuncdesc} |
| 1444 | |
| 1445 | |
| 1446 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1447 | Returns the result of subtracting \var{o2} from \var{o1}, or |
| 1448 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1449 | \samp{\var{o1} - \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1450 | \end{cfuncdesc} |
| 1451 | |
| 1452 | |
| 1453 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1454 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1455 | failure. This is the equivalent of the Python expression |
| 1456 | \samp{\var{o1} * \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1457 | \end{cfuncdesc} |
| 1458 | |
| 1459 | |
| 1460 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1461 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1462 | failure. |
| 1463 | This is the equivalent of the Python expression \samp{\var{o1} / |
| 1464 | \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1465 | \end{cfuncdesc} |
| 1466 | |
| 1467 | |
| 1468 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1469 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1470 | failure. This is the equivalent of the Python expression |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1471 | \samp{\var{o1} \%\ \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1472 | \end{cfuncdesc} |
| 1473 | |
| 1474 | |
| 1475 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1476 | See the built-in function \function{divmod()}\bifuncindex{divmod}. |
| 1477 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 1478 | expression \samp{divmod(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1479 | \end{cfuncdesc} |
| 1480 | |
| 1481 | |
| 1482 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1483 | See the built-in function \function{pow()}\bifuncindex{pow}. Returns |
| 1484 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1485 | \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] | 1486 | If \var{o3} is to be ignored, pass \cdata{Py_None} in its place |
| 1487 | (passing \NULL{} for \var{o3} would cause an illegal memory access). |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1488 | \end{cfuncdesc} |
| 1489 | |
| 1490 | |
| 1491 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1492 | Returns the negation of \var{o} on success, or \NULL{} on failure. |
| 1493 | This is the equivalent of the Python expression \samp{-\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1494 | \end{cfuncdesc} |
| 1495 | |
| 1496 | |
| 1497 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1498 | Returns \var{o} on success, or \NULL{} on failure. |
| 1499 | This is the equivalent of the Python expression \samp{+\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1500 | \end{cfuncdesc} |
| 1501 | |
| 1502 | |
| 1503 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1504 | Returns the absolute value of \var{o}, or \NULL{} on failure. This is |
| 1505 | the equivalent of the Python expression \samp{abs(\var{o})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1506 | \bifuncindex{abs} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1507 | \end{cfuncdesc} |
| 1508 | |
| 1509 | |
| 1510 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1511 | Returns the bitwise negation of \var{o} on success, or \NULL{} on |
| 1512 | failure. This is the equivalent of the Python expression |
| 1513 | \samp{\~\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1514 | \end{cfuncdesc} |
| 1515 | |
| 1516 | |
| 1517 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1518 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 1519 | or \NULL{} on failure. This is the equivalent of the Python |
| 1520 | expression \samp{\var{o1} << \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1521 | \end{cfuncdesc} |
| 1522 | |
| 1523 | |
| 1524 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1525 | Returns the result of right shifting \var{o1} by \var{o2} on success, |
| 1526 | or \NULL{} on failure. This is the equivalent of the Python |
| 1527 | expression \samp{\var{o1} >> \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1528 | \end{cfuncdesc} |
| 1529 | |
| 1530 | |
| 1531 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1532 | Returns the result of ``anding'' \var{o2} and \var{o2} on success and |
| 1533 | \NULL{} on failure. This is the equivalent of the Python |
| 1534 | expression \samp{\var{o1} and \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1535 | \end{cfuncdesc} |
| 1536 | |
| 1537 | |
| 1538 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1539 | Returns the bitwise exclusive or of \var{o1} by \var{o2} on success, |
| 1540 | or \NULL{} on failure. This is the equivalent of the Python |
| 1541 | expression \samp{\var{o1} \^{ }\var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1542 | \end{cfuncdesc} |
| 1543 | |
| 1544 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1545 | Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on |
| 1546 | failure. This is the equivalent of the Python expression |
| 1547 | \samp{\var{o1} or \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1548 | \end{cfuncdesc} |
| 1549 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1550 | \begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1551 | This function takes the addresses of two variables of type |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1552 | \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}} and |
| 1553 | \code{*\var{p2}} have the same type, increment their reference count |
| 1554 | and return \code{0} (success). If the objects can be converted to a |
| 1555 | common numeric type, replace \code{*p1} and \code{*p2} by their |
| 1556 | converted value (with 'new' reference counts), and return \code{0}. |
| 1557 | If no conversion is possible, or if some other error occurs, return |
| 1558 | \code{-1} (failure) and don't increment the reference counts. The |
| 1559 | call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 1560 | statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}. |
| 1561 | \bifuncindex{coerce} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1562 | \end{cfuncdesc} |
| 1563 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1564 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1565 | 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] | 1566 | \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1567 | expression \samp{int(\var{o})}.\bifuncindex{int} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1568 | \end{cfuncdesc} |
| 1569 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1570 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1571 | 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] | 1572 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1573 | expression \samp{long(\var{o})}.\bifuncindex{long} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1574 | \end{cfuncdesc} |
| 1575 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1576 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1577 | Returns the \var{o} converted to a float object on success, or |
| 1578 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1579 | \samp{float(\var{o})}.\bifuncindex{float} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1580 | \end{cfuncdesc} |
| 1581 | |
| 1582 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1583 | \section{Sequence Protocol \label{sequence}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1584 | |
| 1585 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1586 | Return \code{1} if the object provides sequence protocol, and |
| 1587 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1588 | \end{cfuncdesc} |
| 1589 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1590 | \begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o} |
| 1591 | Returns the number of objects in sequence \var{o} on success, and |
| 1592 | \code{-1} on failure. For objects that do not provide sequence |
| 1593 | protocol, this is equivalent to the Python expression |
| 1594 | \samp{len(\var{o})}.\bifuncindex{len} |
| 1595 | \end{cfuncdesc} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1596 | |
| 1597 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1598 | 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] | 1599 | failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1600 | expression \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1601 | \end{cfuncdesc} |
| 1602 | |
| 1603 | |
| 1604 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1605 | Return the result of repeating sequence object |
| 1606 | \var{o} \var{count} times, or \NULL{} on failure. This is the |
| 1607 | equivalent of the Python expression \samp{\var{o} * \var{count}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1608 | \end{cfuncdesc} |
| 1609 | |
| 1610 | |
| 1611 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1612 | Return the \var{i}th element of \var{o}, or \NULL{} on failure. This |
| 1613 | 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] | 1614 | \end{cfuncdesc} |
| 1615 | |
| 1616 | |
| 1617 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1618 | Return the slice of sequence object \var{o} between \var{i1} and |
| 1619 | \var{i2}, or \NULL{} on failure. This is the equivalent of the Python |
| 1620 | expression \samp{\var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1621 | \end{cfuncdesc} |
| 1622 | |
| 1623 | |
| 1624 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1625 | Assign object \var{v} to the \var{i}th element of \var{o}. |
| 1626 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1627 | statement \samp{\var{o}[\var{i}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1628 | \end{cfuncdesc} |
| 1629 | |
| 1630 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1631 | Delete the \var{i}th element of object \var{v}. Returns |
| 1632 | \code{-1} on failure. This is the equivalent of the Python |
| 1633 | statement \samp{del \var{o}[\var{i}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1634 | \end{cfuncdesc} |
| 1635 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1636 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, |
| 1637 | int i2, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1638 | Assign the sequence object \var{v} to the slice in sequence |
| 1639 | object \var{o} from \var{i1} to \var{i2}. This is the equivalent of |
| 1640 | 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] | 1641 | \end{cfuncdesc} |
| 1642 | |
| 1643 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1644 | Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}. |
| 1645 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1646 | statement \samp{del \var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1647 | \end{cfuncdesc} |
| 1648 | |
| 1649 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1650 | 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] | 1651 | This is equivalent to the Python expression \samp{tuple(\var{o})}. |
| 1652 | \bifuncindex{tuple} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1653 | \end{cfuncdesc} |
| 1654 | |
| 1655 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1656 | Return the number of occurrences of \var{value} in \var{o}, that is, |
| 1657 | return the number of keys for which \code{\var{o}[\var{key}] == |
| 1658 | \var{value}}. On failure, return \code{-1}. This is equivalent to |
| 1659 | the Python expression \samp{\var{o}.count(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1660 | \end{cfuncdesc} |
| 1661 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1662 | \begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1663 | Determine if \var{o} contains \var{value}. If an item in \var{o} is |
| 1664 | equal to \var{value}, return \code{1}, otherwise return \code{0}. On |
| 1665 | error, return \code{-1}. This is equivalent to the Python expression |
| 1666 | \samp{\var{value} in \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1667 | \end{cfuncdesc} |
| 1668 | |
| 1669 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1670 | Return the first index \var{i} for which \code{\var{o}[\var{i}] == |
| 1671 | \var{value}}. On error, return \code{-1}. This is equivalent to |
| 1672 | the Python expression \samp{\var{o}.index(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1673 | \end{cfuncdesc} |
| 1674 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 1675 | \begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o} |
| 1676 | Return a list object with the same contents as the arbitrary sequence |
| 1677 | \var{o}. The returned list is guaranteed to be new. |
| 1678 | \end{cfuncdesc} |
| 1679 | |
| 1680 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 1681 | Return a tuple object with the same contents as the arbitrary sequence |
| 1682 | \var{o}. If \var{o} is a tuple, a new reference will be returned, |
| 1683 | otherwise a tuple will be constructed with the appropriate contents. |
| 1684 | \end{cfuncdesc} |
| 1685 | |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1686 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1687 | \section{Mapping Protocol \label{mapping}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1688 | |
| 1689 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1690 | Return \code{1} if the object provides mapping protocol, and |
| 1691 | \code{0} otherwise. This function always succeeds. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1692 | \end{cfuncdesc} |
| 1693 | |
| 1694 | |
| 1695 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1696 | Returns the number of keys in object \var{o} on success, and |
| 1697 | \code{-1} on failure. For objects that do not provide mapping |
| 1698 | protocol, this is equivalent to the Python expression |
| 1699 | \samp{len(\var{o})}.\bifuncindex{len} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1700 | \end{cfuncdesc} |
| 1701 | |
| 1702 | |
| 1703 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1704 | Remove the mapping for object \var{key} from the object \var{o}. |
| 1705 | Return \code{-1} on failure. This is equivalent to |
| 1706 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1707 | \end{cfuncdesc} |
| 1708 | |
| 1709 | |
| 1710 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1711 | Remove the mapping for object \var{key} from the object \var{o}. |
| 1712 | Return \code{-1} on failure. This is equivalent to |
| 1713 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1714 | \end{cfuncdesc} |
| 1715 | |
| 1716 | |
| 1717 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1718 | On success, return \code{1} if the mapping object has the key |
| 1719 | \var{key} and \code{0} otherwise. This is equivalent to the Python |
| 1720 | expression \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1721 | This function always succeeds. |
| 1722 | \end{cfuncdesc} |
| 1723 | |
| 1724 | |
| 1725 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1726 | Return \code{1} if the mapping object has the key \var{key} and |
| 1727 | \code{0} otherwise. This is equivalent to the Python expression |
| 1728 | \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1729 | This function always succeeds. |
| 1730 | \end{cfuncdesc} |
| 1731 | |
| 1732 | |
| 1733 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1734 | 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] | 1735 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1736 | expression \samp{\var{o}.keys()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1737 | \end{cfuncdesc} |
| 1738 | |
| 1739 | |
| 1740 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1741 | 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] | 1742 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1743 | expression \samp{\var{o}.values()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1744 | \end{cfuncdesc} |
| 1745 | |
| 1746 | |
| 1747 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1748 | 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] | 1749 | each item is a tuple containing a key-value pair. On |
| 1750 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1751 | expression \samp{\var{o}.items()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1752 | \end{cfuncdesc} |
| 1753 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1754 | |
| 1755 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1756 | Return element of \var{o} corresponding to the object \var{key} or |
| 1757 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1758 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1759 | \end{cfuncdesc} |
| 1760 | |
Guido van Rossum | 0a0f11b | 1998-10-16 17:43:53 +0000 | [diff] [blame] | 1761 | \begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1762 | Map the object \var{key} to the value \var{v} in object \var{o}. |
| 1763 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1764 | statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1765 | \end{cfuncdesc} |
| 1766 | |
| 1767 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1768 | \chapter{Concrete Objects Layer \label{concrete}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1769 | |
| 1770 | The functions in this chapter are specific to certain Python object |
| 1771 | types. Passing them an object of the wrong type is not a good idea; |
| 1772 | if you receive an object from a Python program and you are not sure |
| 1773 | that it has the right type, you must perform a type check first; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1774 | for example. to check that an object is a dictionary, use |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1775 | \cfunction{PyDict_Check()}. The chapter is structured like the |
| 1776 | ``family tree'' of Python object types. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1777 | |
| 1778 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1779 | \section{Fundamental Objects \label{fundamental}} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1780 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1781 | This section describes Python type objects and the singleton object |
| 1782 | \code{None}. |
| 1783 | |
| 1784 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1785 | \subsection{Type Objects \label{typeObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1786 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1787 | \obindex{type} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1788 | \begin{ctypedesc}{PyTypeObject} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1789 | The C structure of the objects used to describe built-in types. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1790 | \end{ctypedesc} |
| 1791 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1792 | \begin{cvardesc}{PyObject*}{PyType_Type} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1793 | This is the type object for type objects; it is the same object as |
| 1794 | \code{types.TypeType} in the Python layer. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1795 | \withsubitem{(in module types)}{\ttindex{TypeType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1796 | \end{cvardesc} |
| 1797 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1798 | \begin{cfuncdesc}{int}{PyType_Check}{PyObject *o} |
| 1799 | Returns true is the object \var{o} is a type object. |
| 1800 | \end{cfuncdesc} |
| 1801 | |
| 1802 | \begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature} |
| 1803 | Returns true if the type object \var{o} sets the feature |
| 1804 | \var{feature}. Type features are denoted by single bit flags. The |
| 1805 | only defined feature flag is \constant{Py_TPFLAGS_HAVE_GETCHARBUFFER}, |
| 1806 | described in section \ref{buffer-structs}. |
| 1807 | \end{cfuncdesc} |
| 1808 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1809 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1810 | \subsection{The None Object \label{noneObject}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1811 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1812 | \obindex{None@\texttt{None}} |
| 1813 | Note that the \ctype{PyTypeObject} for \code{None} is not directly |
| 1814 | exposed in the Python/C API. Since \code{None} is a singleton, |
| 1815 | testing for object identity (using \samp{==} in C) is sufficient. |
| 1816 | There is no \cfunction{PyNone_Check()} function for the same reason. |
| 1817 | |
| 1818 | \begin{cvardesc}{PyObject*}{Py_None} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1819 | The Python \code{None} object, denoting lack of value. This object has |
| 1820 | no methods. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1821 | \end{cvardesc} |
| 1822 | |
| 1823 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1824 | \section{Sequence Objects \label{sequenceObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1825 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1826 | \obindex{sequence} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1827 | Generic operations on sequence objects were discussed in the previous |
| 1828 | chapter; this section deals with the specific kinds of sequence |
| 1829 | objects that are intrinsic to the Python language. |
| 1830 | |
| 1831 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 1832 | \subsection{String Objects \label{stringObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1833 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1834 | \obindex{string} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1835 | \begin{ctypedesc}{PyStringObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1836 | This subtype of \ctype{PyObject} represents a Python string object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1837 | \end{ctypedesc} |
| 1838 | |
| 1839 | \begin{cvardesc}{PyTypeObject}{PyString_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1840 | This instance of \ctype{PyTypeObject} represents the Python string |
| 1841 | type; it is the same object as \code{types.TypeType} in the Python |
| 1842 | layer.\withsubitem{(in module types)}{\ttindex{StringType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1843 | \end{cvardesc} |
| 1844 | |
| 1845 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1846 | Returns true if the object \var{o} is a string object. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1847 | \end{cfuncdesc} |
| 1848 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1849 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1850 | Returns a new string object with the value \var{v} on success, and |
| 1851 | \NULL{} on failure. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1852 | \end{cfuncdesc} |
| 1853 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1854 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v, |
| 1855 | int len} |
| 1856 | Returns a new string object with the value \var{v} and length |
| 1857 | \var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{}, |
| 1858 | the contents of the string are uninitialized. |
| 1859 | \end{cfuncdesc} |
| 1860 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1861 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1862 | Returns the length of the string in string object \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1863 | \end{cfuncdesc} |
| 1864 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1865 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string} |
| 1866 | Macro form of \cfunction{PyString_GetSize()} but without error |
| 1867 | checking. |
| 1868 | \end{cfuncdesc} |
| 1869 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1870 | \begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1871 | Returns a null-terminated representation of the contents of |
| 1872 | \var{string}. The pointer refers to the internal buffer of |
| 1873 | \var{string}, not a copy. The data must not be modified in any way. |
| 1874 | It must not be de-allocated. |
| 1875 | \end{cfuncdesc} |
| 1876 | |
| 1877 | \begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string} |
| 1878 | Macro form of \cfunction{PyString_AsString()} but without error |
| 1879 | checking. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1880 | \end{cfuncdesc} |
| 1881 | |
| 1882 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string, |
| 1883 | PyObject *newpart} |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 1884 | Creates a new string object in \var{*string} containing the |
Fred Drake | ddc6c27 | 2000-03-31 18:22:38 +0000 | [diff] [blame] | 1885 | contents of \var{newpart} appended to \var{string}; the caller will |
| 1886 | own the new reference. The reference to the old value of \var{string} |
| 1887 | will be stolen. If the new string |
Fred Drake | 66b989c | 1999-02-15 20:15:39 +0000 | [diff] [blame] | 1888 | cannot be created, the old reference to \var{string} will still be |
| 1889 | discarded and the value of \var{*string} will be set to |
| 1890 | \NULL{}; the appropriate exception will be set. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1891 | \end{cfuncdesc} |
| 1892 | |
| 1893 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string, |
| 1894 | PyObject *newpart} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1895 | Creates a new string object in \var{*string} containing the contents |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1896 | of \var{newpart} appended to \var{string}. This version decrements |
| 1897 | the reference count of \var{newpart}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1898 | \end{cfuncdesc} |
| 1899 | |
| 1900 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1901 | A way to resize a string object even though it is ``immutable''. |
| 1902 | Only use this to build up a brand new string object; don't use this if |
| 1903 | the string may already be known in other parts of the code. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1904 | \end{cfuncdesc} |
| 1905 | |
| 1906 | \begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format, |
| 1907 | PyObject *args} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1908 | Returns a new string object from \var{format} and \var{args}. Analogous |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 1909 | 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] | 1910 | a tuple. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1911 | \end{cfuncdesc} |
| 1912 | |
| 1913 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1914 | Intern the argument \var{*string} in place. The argument must be the |
| 1915 | address of a pointer variable pointing to a Python string object. |
| 1916 | If there is an existing interned string that is the same as |
| 1917 | \var{*string}, it sets \var{*string} to it (decrementing the reference |
| 1918 | count of the old string object and incrementing the reference count of |
| 1919 | the interned string object), otherwise it leaves \var{*string} alone |
| 1920 | and interns it (incrementing its reference count). (Clarification: |
| 1921 | 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] | 1922 | this function as reference-count-neutral; you own the object after |
| 1923 | the call if and only if you owned it before the call.) |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1924 | \end{cfuncdesc} |
| 1925 | |
| 1926 | \begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1927 | A combination of \cfunction{PyString_FromString()} and |
| 1928 | \cfunction{PyString_InternInPlace()}, returning either a new string object |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1929 | that has been interned, or a new (``owned'') reference to an earlier |
| 1930 | interned string object with the same value. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1931 | \end{cfuncdesc} |
| 1932 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 1933 | \begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s, |
| 1934 | int size, |
| 1935 | const char *encoding, |
| 1936 | const char *errors} |
| 1937 | Create a string object by decoding \var{size} bytes of the encoded |
| 1938 | buffer \var{s}. \var{encoding} and \var{errors} have the same meaning |
| 1939 | as the parameters of the same name in the unicode() builtin |
| 1940 | function. The codec to be used is looked up using the Python codec |
| 1941 | registry. Returns \NULL{} in case an exception was raised by the |
| 1942 | codec. |
| 1943 | \end{cfuncdesc} |
| 1944 | |
| 1945 | \begin{cfuncdesc}{PyObject*}{PyString_Encode}{const Py_UNICODE *s, |
| 1946 | int size, |
| 1947 | const char *encoding, |
| 1948 | const char *errors} |
| 1949 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a |
| 1950 | Python string object. \var{encoding} and \var{errors} have the same |
| 1951 | meaning as the parameters of the same name in the string .encode() |
| 1952 | method. The codec to be used is looked up using the Python codec |
| 1953 | registry. Returns \NULL{} in case an exception was raised by the |
| 1954 | codec. |
| 1955 | \end{cfuncdesc} |
| 1956 | |
| 1957 | \begin{cfuncdesc}{PyObject*}{PyString_AsEncodedString}{PyObject *unicode, |
| 1958 | const char *encoding, |
| 1959 | const char *errors} |
| 1960 | Encodes a string object and returns the result as Python string |
| 1961 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 1962 | parameters of the same name in the string .encode() method. The codec |
| 1963 | to be used is looked up using the Python codec registry. Returns |
| 1964 | \NULL{} in case an exception was raised by the codec. |
| 1965 | \end{cfuncdesc} |
| 1966 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1967 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 1968 | \subsection{Unicode Objects \label{unicodeObjects}} |
| 1969 | \sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com} |
| 1970 | |
| 1971 | %--- Unicode Type ------------------------------------------------------- |
| 1972 | |
| 1973 | These are the basic Unicode object types used for the Unicode |
| 1974 | implementation in Python: |
| 1975 | |
| 1976 | \begin{ctypedesc}{Py_UNICODE} |
| 1977 | This type represents a 16-bit unsigned storage type which is used by |
| 1978 | Python internally as basis for holding Unicode ordinals. On platforms |
| 1979 | where \ctype{wchar_t} is available and also has 16-bits, |
| 1980 | \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance |
| 1981 | native platform compatibility. On all other platforms, |
| 1982 | \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}. |
| 1983 | \end{ctypedesc} |
| 1984 | |
| 1985 | \begin{ctypedesc}{PyUnicodeObject} |
| 1986 | This subtype of \ctype{PyObject} represents a Python Unicode object. |
| 1987 | \end{ctypedesc} |
| 1988 | |
| 1989 | \begin{cvardesc}{PyTypeObject}{PyUnicode_Type} |
| 1990 | This instance of \ctype{PyTypeObject} represents the Python Unicode type. |
| 1991 | \end{cvardesc} |
| 1992 | |
| 1993 | %--- These are really C macros... is there a macrodesc TeX macro ? |
| 1994 | |
| 1995 | The following APIs are really C macros and can be used to do fast |
| 1996 | checks and to access internal read-only data of Unicode objects: |
| 1997 | |
| 1998 | \begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o} |
| 1999 | Returns true if the object \var{o} is a Unicode object. |
| 2000 | \end{cfuncdesc} |
| 2001 | |
| 2002 | \begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o} |
| 2003 | Returns the size of the object. o has to be a |
| 2004 | PyUnicodeObject (not checked). |
| 2005 | \end{cfuncdesc} |
| 2006 | |
| 2007 | \begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o} |
| 2008 | Returns the size of the object's internal buffer in bytes. o has to be |
| 2009 | a PyUnicodeObject (not checked). |
| 2010 | \end{cfuncdesc} |
| 2011 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2012 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2013 | Returns a pointer to the internal Py_UNICODE buffer of the object. o |
| 2014 | has to be a PyUnicodeObject (not checked). |
| 2015 | \end{cfuncdesc} |
| 2016 | |
Fred Drake | 992fe5a | 2000-06-16 21:04:15 +0000 | [diff] [blame] | 2017 | \begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2018 | Returns a (const char *) pointer to the internal buffer of the object. |
| 2019 | o has to be a PyUnicodeObject (not checked). |
| 2020 | \end{cfuncdesc} |
| 2021 | |
| 2022 | % --- Unicode character properties --------------------------------------- |
| 2023 | |
| 2024 | Unicode provides many different character properties. The most often |
| 2025 | needed ones are available through these macros which are mapped to C |
| 2026 | functions depending on the Python configuration. |
| 2027 | |
| 2028 | \begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch} |
| 2029 | Returns 1/0 depending on whether \var{ch} is a whitespace character. |
| 2030 | \end{cfuncdesc} |
| 2031 | |
| 2032 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch} |
| 2033 | Returns 1/0 depending on whether \var{ch} is a lowercase character. |
| 2034 | \end{cfuncdesc} |
| 2035 | |
| 2036 | \begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch} |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 2037 | Returns 1/0 depending on whether \var{ch} is an uppercase character. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2038 | \end{cfuncdesc} |
| 2039 | |
| 2040 | \begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch} |
| 2041 | Returns 1/0 depending on whether \var{ch} is a titlecase character. |
| 2042 | \end{cfuncdesc} |
| 2043 | |
| 2044 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch} |
| 2045 | Returns 1/0 depending on whether \var{ch} is a linebreak character. |
| 2046 | \end{cfuncdesc} |
| 2047 | |
| 2048 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch} |
| 2049 | Returns 1/0 depending on whether \var{ch} is a decimal character. |
| 2050 | \end{cfuncdesc} |
| 2051 | |
| 2052 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch} |
| 2053 | Returns 1/0 depending on whether \var{ch} is a digit character. |
| 2054 | \end{cfuncdesc} |
| 2055 | |
| 2056 | \begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch} |
| 2057 | Returns 1/0 depending on whether \var{ch} is a numeric character. |
| 2058 | \end{cfuncdesc} |
| 2059 | |
Fred Drake | ae96aab | 2000-07-03 13:38:10 +0000 | [diff] [blame] | 2060 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch} |
| 2061 | Returns 1/0 depending on whether \var{ch} is an alphabetic character. |
| 2062 | \end{cfuncdesc} |
| 2063 | |
| 2064 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch} |
| 2065 | Returns 1/0 depending on whether \var{ch} is an alphanumeric character. |
| 2066 | \end{cfuncdesc} |
| 2067 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2068 | These APIs can be used for fast direct character conversions: |
| 2069 | |
| 2070 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch} |
| 2071 | Returns the character \var{ch} converted to lower case. |
| 2072 | \end{cfuncdesc} |
| 2073 | |
| 2074 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch} |
| 2075 | Returns the character \var{ch} converted to upper case. |
| 2076 | \end{cfuncdesc} |
| 2077 | |
| 2078 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch} |
| 2079 | Returns the character \var{ch} converted to title case. |
| 2080 | \end{cfuncdesc} |
| 2081 | |
| 2082 | \begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch} |
| 2083 | Returns the character \var{ch} converted to a decimal positive integer. |
| 2084 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 2085 | \end{cfuncdesc} |
| 2086 | |
| 2087 | \begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch} |
| 2088 | Returns the character \var{ch} converted to a single digit integer. |
| 2089 | Returns -1 in case this is not possible. Does not raise exceptions. |
| 2090 | \end{cfuncdesc} |
| 2091 | |
| 2092 | \begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch} |
| 2093 | Returns the character \var{ch} converted to a (positive) double. |
| 2094 | Returns -1.0 in case this is not possible. Does not raise exceptions. |
| 2095 | \end{cfuncdesc} |
| 2096 | |
| 2097 | % --- Plain Py_UNICODE --------------------------------------------------- |
| 2098 | |
| 2099 | To create Unicode objects and access their basic sequence properties, |
| 2100 | use these APIs: |
| 2101 | |
| 2102 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u, |
| 2103 | int size} |
| 2104 | |
| 2105 | Create a Unicode Object from the Py_UNICODE buffer \var{u} of the |
| 2106 | given size. \var{u} may be \NULL{} which causes the contents to be |
| 2107 | undefined. It is the user's responsibility to fill in the needed data. |
| 2108 | The buffer is copied into the new object. |
| 2109 | \end{cfuncdesc} |
| 2110 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2111 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2112 | Return a read-only pointer to the Unicode object's internal |
| 2113 | \ctype{Py_UNICODE} buffer. |
| 2114 | \end{cfuncdesc} |
| 2115 | |
| 2116 | \begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode} |
| 2117 | Return the length of the Unicode object. |
| 2118 | \end{cfuncdesc} |
| 2119 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2120 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj, |
| 2121 | const char *encoding, |
| 2122 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2123 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2124 | Coerce an encoded object obj to an Unicode object and return a |
| 2125 | reference with incremented refcount. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2126 | |
| 2127 | Coercion is done in the following way: |
| 2128 | \begin{enumerate} |
| 2129 | \item Unicode objects are passed back as-is with incremented |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2130 | refcount. Note: these cannot be decoded; passing a non-NULL |
| 2131 | value for encoding will result in a TypeError. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2132 | |
| 2133 | \item String and other char buffer compatible objects are decoded |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2134 | according to the given encoding and using the error handling |
| 2135 | defined by errors. Both can be NULL to have the interface use |
| 2136 | the default values (see the next section for details). |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2137 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2138 | \item All other objects cause an exception. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2139 | \end{enumerate} |
| 2140 | The API returns NULL in case of an error. The caller is responsible |
| 2141 | for decref'ing the returned objects. |
| 2142 | \end{cfuncdesc} |
| 2143 | |
Marc-André Lemburg | 5a20b21 | 2000-07-07 15:47:06 +0000 | [diff] [blame] | 2144 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj} |
| 2145 | |
| 2146 | Shortcut for PyUnicode_FromEncodedObject(obj, NULL, ``strict'') |
| 2147 | which is used throughout the interpreter whenever coercion to |
| 2148 | Unicode is needed. |
| 2149 | \end{cfuncdesc} |
| 2150 | |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2151 | % --- wchar_t support for platforms which support it --------------------- |
| 2152 | |
| 2153 | If the platform supports \ctype{wchar_t} and provides a header file |
| 2154 | wchar.h, Python can interface directly to this type using the |
| 2155 | following functions. Support is optimized if Python's own |
| 2156 | \ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}. |
| 2157 | |
| 2158 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w, |
| 2159 | int size} |
| 2160 | Create a Unicode Object from the \ctype{whcar_t} buffer \var{w} of the |
| 2161 | given size. Returns \NULL{} on failure. |
| 2162 | \end{cfuncdesc} |
| 2163 | |
| 2164 | \begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode, |
| 2165 | wchar_t *w, |
| 2166 | int size} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2167 | Copies the Unicode Object contents into the \ctype{whcar_t} buffer |
| 2168 | \var{w}. At most \var{size} \ctype{whcar_t} characters are copied. |
| 2169 | Returns the number of \ctype{whcar_t} characters copied or -1 in case |
| 2170 | of an error. |
| 2171 | \end{cfuncdesc} |
| 2172 | |
| 2173 | |
| 2174 | \subsubsection{Builtin Codecs \label{builtinCodecs}} |
| 2175 | |
| 2176 | Python provides a set of builtin codecs which are written in C |
| 2177 | for speed. All of these codecs are directly usable via the |
| 2178 | following functions. |
| 2179 | |
| 2180 | Many of the following APIs take two arguments encoding and |
| 2181 | errors. These parameters encoding and errors have the same semantics |
| 2182 | as the ones of the builtin unicode() Unicode object constructor. |
| 2183 | |
| 2184 | Setting encoding to NULL causes the default encoding to be used which |
| 2185 | is UTF-8. |
| 2186 | |
| 2187 | Error handling is set by errors which may also be set to NULL meaning |
| 2188 | to use the default handling defined for the codec. Default error |
| 2189 | handling for all builtin codecs is ``strict'' (ValueErrors are raised). |
| 2190 | |
| 2191 | The codecs all use a similar interface. Only deviation from the |
| 2192 | following generic ones are documented for simplicity. |
| 2193 | |
| 2194 | % --- Generic Codecs ----------------------------------------------------- |
| 2195 | |
| 2196 | These are the generic codec APIs: |
| 2197 | |
| 2198 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s, |
| 2199 | int size, |
| 2200 | const char *encoding, |
| 2201 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2202 | Create a Unicode object by decoding \var{size} bytes of the encoded |
| 2203 | string \var{s}. \var{encoding} and \var{errors} have the same meaning |
| 2204 | as the parameters of the same name in the unicode() builtin |
| 2205 | function. The codec to be used is looked up using the Python codec |
| 2206 | registry. Returns \NULL{} in case an exception was raised by the |
| 2207 | codec. |
| 2208 | \end{cfuncdesc} |
| 2209 | |
| 2210 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s, |
| 2211 | int size, |
| 2212 | const char *encoding, |
| 2213 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2214 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns a |
| 2215 | Python string object. \var{encoding} and \var{errors} have the same |
| 2216 | meaning as the parameters of the same name in the Unicode .encode() |
| 2217 | method. The codec to be used is looked up using the Python codec |
| 2218 | registry. Returns \NULL{} in case an exception was raised by the |
| 2219 | codec. |
| 2220 | \end{cfuncdesc} |
| 2221 | |
| 2222 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode, |
| 2223 | const char *encoding, |
| 2224 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2225 | Encodes a Unicode object and returns the result as Python string |
| 2226 | object. \var{encoding} and \var{errors} have the same meaning as the |
| 2227 | parameters of the same name in the Unicode .encode() method. The codec |
| 2228 | to be used is looked up using the Python codec registry. Returns |
| 2229 | \NULL{} in case an exception was raised by the codec. |
| 2230 | \end{cfuncdesc} |
| 2231 | |
| 2232 | % --- UTF-8 Codecs ------------------------------------------------------- |
| 2233 | |
| 2234 | These are the UTF-8 codec APIs: |
| 2235 | |
| 2236 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s, |
| 2237 | int size, |
| 2238 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2239 | Creates a Unicode object by decoding \var{size} bytes of the UTF-8 |
| 2240 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2241 | raised by the codec. |
| 2242 | \end{cfuncdesc} |
| 2243 | |
| 2244 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s, |
| 2245 | int size, |
| 2246 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2247 | Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8 |
| 2248 | and returns a Python string object. Returns \NULL{} in case an |
| 2249 | exception was raised by the codec. |
| 2250 | \end{cfuncdesc} |
| 2251 | |
| 2252 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2253 | Encodes a Unicode objects using UTF-8 and returns the result as Python |
| 2254 | string object. Error handling is ``strict''. Returns |
| 2255 | \NULL{} in case an exception was raised by the codec. |
| 2256 | \end{cfuncdesc} |
| 2257 | |
| 2258 | % --- UTF-16 Codecs ------------------------------------------------------ */ |
| 2259 | |
| 2260 | These are the UTF-16 codec APIs: |
| 2261 | |
| 2262 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s, |
| 2263 | int size, |
| 2264 | const char *errors, |
| 2265 | int *byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2266 | Decodes \var{length} bytes from a UTF-16 encoded buffer string and |
| 2267 | returns the corresponding Unicode object. |
| 2268 | |
| 2269 | \var{errors} (if non-NULL) defines the error handling. It defaults |
| 2270 | to ``strict''. |
| 2271 | |
| 2272 | If \var{byteorder} is non-\NULL{}, the decoder starts decoding using |
| 2273 | the given byte order: |
| 2274 | |
| 2275 | \begin{verbatim} |
| 2276 | *byteorder == -1: little endian |
| 2277 | *byteorder == 0: native order |
| 2278 | *byteorder == 1: big endian |
| 2279 | \end{verbatim} |
| 2280 | |
| 2281 | and then switches according to all byte order marks (BOM) it finds in |
| 2282 | the input data. BOM marks are not copied into the resulting Unicode |
| 2283 | string. After completion, \var{*byteorder} is set to the current byte |
| 2284 | order at the end of input data. |
| 2285 | |
| 2286 | If \var{byteorder} is \NULL{}, the codec starts in native order mode. |
| 2287 | |
| 2288 | Returns \NULL{} in case an exception was raised by the codec. |
| 2289 | \end{cfuncdesc} |
| 2290 | |
| 2291 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s, |
| 2292 | int size, |
| 2293 | const char *errors, |
| 2294 | int byteorder} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2295 | Returns a Python string object holding the UTF-16 encoded value of the |
| 2296 | Unicode data in \var{s}. |
| 2297 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 2298 | 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] | 2299 | following byte order: |
| 2300 | |
| 2301 | \begin{verbatim} |
| 2302 | byteorder == -1: little endian |
| 2303 | byteorder == 0: native byte order (writes a BOM mark) |
| 2304 | byteorder == 1: big endian |
| 2305 | \end{verbatim} |
| 2306 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 2307 | 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] | 2308 | Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is |
| 2309 | prepended. |
| 2310 | |
| 2311 | Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16 |
| 2312 | reduced to UCS-2. This trick makes it possible to add full UTF-16 |
| 2313 | capabilities at a later point without comprimising the APIs. |
| 2314 | |
| 2315 | Returns \NULL{} in case an exception was raised by the codec. |
| 2316 | \end{cfuncdesc} |
| 2317 | |
| 2318 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2319 | Returns a Python string using the UTF-16 encoding in native byte |
| 2320 | order. The string always starts with a BOM mark. Error handling is |
| 2321 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 2322 | codec. |
| 2323 | \end{cfuncdesc} |
| 2324 | |
| 2325 | % --- Unicode-Escape Codecs ---------------------------------------------- |
| 2326 | |
| 2327 | These are the ``Unicode Esacpe'' codec APIs: |
| 2328 | |
| 2329 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s, |
| 2330 | int size, |
| 2331 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2332 | Creates a Unicode object by decoding \var{size} bytes of the Unicode-Esacpe |
| 2333 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2334 | raised by the codec. |
| 2335 | \end{cfuncdesc} |
| 2336 | |
| 2337 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s, |
| 2338 | int size, |
| 2339 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2340 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Unicode-Escape |
| 2341 | and returns a Python string object. Returns \NULL{} in case an |
| 2342 | exception was raised by the codec. |
| 2343 | \end{cfuncdesc} |
| 2344 | |
| 2345 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2346 | Encodes a Unicode objects using Unicode-Escape and returns the result |
| 2347 | as Python string object. Error handling is ``strict''. Returns |
| 2348 | \NULL{} in case an exception was raised by the codec. |
| 2349 | \end{cfuncdesc} |
| 2350 | |
| 2351 | % --- Raw-Unicode-Escape Codecs ------------------------------------------ |
| 2352 | |
| 2353 | These are the ``Raw Unicode Esacpe'' codec APIs: |
| 2354 | |
| 2355 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s, |
| 2356 | int size, |
| 2357 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2358 | Creates a Unicode object by decoding \var{size} bytes of the Raw-Unicode-Esacpe |
| 2359 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2360 | raised by the codec. |
| 2361 | \end{cfuncdesc} |
| 2362 | |
| 2363 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s, |
| 2364 | int size, |
| 2365 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2366 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Raw-Unicode-Escape |
| 2367 | and returns a Python string object. Returns \NULL{} in case an |
| 2368 | exception was raised by the codec. |
| 2369 | \end{cfuncdesc} |
| 2370 | |
| 2371 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2372 | Encodes a Unicode objects using Raw-Unicode-Escape and returns the result |
| 2373 | as Python string object. Error handling is ``strict''. Returns |
| 2374 | \NULL{} in case an exception was raised by the codec. |
| 2375 | \end{cfuncdesc} |
| 2376 | |
| 2377 | % --- Latin-1 Codecs ----------------------------------------------------- |
| 2378 | |
| 2379 | These are the Latin-1 codec APIs: |
| 2380 | |
| 2381 | Latin-1 corresponds to the first 256 Unicode ordinals and only these |
| 2382 | are accepted by the codecs during encoding. |
| 2383 | |
| 2384 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2385 | int size, |
| 2386 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2387 | Creates a Unicode object by decoding \var{size} bytes of the Latin-1 |
| 2388 | encoded string \var{s}. Returns \NULL{} in case an exception was |
| 2389 | raised by the codec. |
| 2390 | \end{cfuncdesc} |
| 2391 | |
| 2392 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2393 | int size, |
| 2394 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2395 | Encodes the \ctype{Py_UNICODE} buffer of the given size using Latin-1 |
| 2396 | and returns a Python string object. Returns \NULL{} in case an |
| 2397 | exception was raised by the codec. |
| 2398 | \end{cfuncdesc} |
| 2399 | |
| 2400 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2401 | Encodes a Unicode objects using Latin-1 and returns the result as |
| 2402 | Python string object. Error handling is ``strict''. Returns |
| 2403 | \NULL{} in case an exception was raised by the codec. |
| 2404 | \end{cfuncdesc} |
| 2405 | |
| 2406 | % --- ASCII Codecs ------------------------------------------------------- |
| 2407 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2408 | These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is |
| 2409 | accepted. All other codes generate errors. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2410 | |
| 2411 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2412 | int size, |
| 2413 | const char *errors} |
| 2414 | Creates a Unicode object by decoding \var{size} bytes of the |
| 2415 | \ASCII{} encoded string \var{s}. Returns \NULL{} in case an exception |
| 2416 | was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2417 | \end{cfuncdesc} |
| 2418 | |
| 2419 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s, |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2420 | int size, |
| 2421 | const char *errors} |
| 2422 | Encodes the \ctype{Py_UNICODE} buffer of the given size using |
| 2423 | \ASCII{} and returns a Python string object. Returns \NULL{} in case |
| 2424 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2425 | \end{cfuncdesc} |
| 2426 | |
| 2427 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2428 | Encodes a Unicode objects using \ASCII{} and returns the result as Python |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2429 | string object. Error handling is ``strict''. Returns |
| 2430 | \NULL{} in case an exception was raised by the codec. |
| 2431 | \end{cfuncdesc} |
| 2432 | |
| 2433 | % --- Character Map Codecs ----------------------------------------------- |
| 2434 | |
| 2435 | These are the mapping codec APIs: |
| 2436 | |
| 2437 | This codec is special in that it can be used to implement many |
| 2438 | different codecs (and this is in fact what was done to obtain most of |
| 2439 | the standard codecs included in the \module{encodings} package). The |
| 2440 | codec uses mapping to encode and decode characters. |
| 2441 | |
| 2442 | Decoding mappings must map single string characters to single Unicode |
| 2443 | characters, integers (which are then interpreted as Unicode ordinals) |
| 2444 | or None (meaning "undefined mapping" and causing an error). |
| 2445 | |
| 2446 | Encoding mappings must map single Unicode characters to single string |
| 2447 | characters, integers (which are then interpreted as Latin-1 ordinals) |
| 2448 | or None (meaning "undefined mapping" and causing an error). |
| 2449 | |
| 2450 | The mapping objects provided must only support the __getitem__ mapping |
| 2451 | interface. |
| 2452 | |
| 2453 | If a character lookup fails with a LookupError, the character is |
| 2454 | copied as-is meaning that its ordinal value will be interpreted as |
| 2455 | Unicode or Latin-1 ordinal resp. Because of this, mappings only need |
| 2456 | to contain those mappings which map characters to different code |
| 2457 | points. |
| 2458 | |
| 2459 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s, |
| 2460 | int size, |
| 2461 | PyObject *mapping, |
| 2462 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2463 | Creates a Unicode object by decoding \var{size} bytes of the encoded |
| 2464 | string \var{s} using the given \var{mapping} object. Returns \NULL{} |
| 2465 | in case an exception was raised by the codec. |
| 2466 | \end{cfuncdesc} |
| 2467 | |
| 2468 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s, |
| 2469 | int size, |
| 2470 | PyObject *mapping, |
| 2471 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2472 | Encodes the \ctype{Py_UNICODE} buffer of the given size using the |
| 2473 | given \var{mapping} object and returns a Python string object. |
| 2474 | Returns \NULL{} in case an exception was raised by the codec. |
| 2475 | \end{cfuncdesc} |
| 2476 | |
| 2477 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode, |
| 2478 | PyObject *mapping} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2479 | Encodes a Unicode objects using the given \var{mapping} object and |
| 2480 | returns the result as Python string object. Error handling is |
| 2481 | ``strict''. Returns \NULL{} in case an exception was raised by the |
| 2482 | codec. |
| 2483 | \end{cfuncdesc} |
| 2484 | |
| 2485 | The following codec API is special in that maps Unicode to Unicode. |
| 2486 | |
| 2487 | \begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s, |
| 2488 | int size, |
| 2489 | PyObject *table, |
| 2490 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2491 | Translates a \ctype{Py_UNICODE} buffer of the given length by applying |
| 2492 | a character mapping \var{table} to it and returns the resulting |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2493 | Unicode object. Returns \NULL{} when an exception was raised by the |
| 2494 | codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2495 | |
| 2496 | The \var{mapping} table must map Unicode ordinal integers to Unicode |
| 2497 | ordinal integers or None (causing deletion of the character). |
| 2498 | |
| 2499 | Mapping tables must only provide the __getitem__ interface, |
| 2500 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 2501 | which cause a LookupError) are left untouched and are copied as-is. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2502 | \end{cfuncdesc} |
| 2503 | |
| 2504 | % --- MBCS codecs for Windows -------------------------------------------- |
| 2505 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2506 | These are the MBCS codec APIs. They are currently only available on |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2507 | Windows and use the Win32 MBCS converters to implement the |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2508 | conversions. Note that MBCS (or DBCS) is a class of encodings, not |
| 2509 | just one. The target encoding is defined by the user settings on the |
| 2510 | machine running the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2511 | |
| 2512 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s, |
| 2513 | int size, |
| 2514 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2515 | Creates a Unicode object by decoding \var{size} bytes of the MBCS |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2516 | encoded string \var{s}. Returns \NULL{} in case an exception was |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2517 | raised by the codec. |
| 2518 | \end{cfuncdesc} |
| 2519 | |
| 2520 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s, |
| 2521 | int size, |
| 2522 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2523 | Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS |
| 2524 | and returns a Python string object. Returns \NULL{} in case an |
| 2525 | exception was raised by the codec. |
| 2526 | \end{cfuncdesc} |
| 2527 | |
| 2528 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2529 | Encodes a Unicode objects using MBCS and returns the result as Python |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2530 | string object. Error handling is ``strict''. Returns \NULL{} in case |
| 2531 | an exception was raised by the codec. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2532 | \end{cfuncdesc} |
| 2533 | |
| 2534 | % --- Methods & Slots ---------------------------------------------------- |
| 2535 | |
| 2536 | \subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}} |
| 2537 | |
| 2538 | The following APIs are capable of handling Unicode objects and strings |
| 2539 | on input (we refer to them as strings in the descriptions) and return |
| 2540 | Unicode objects or integers as apporpriate. |
| 2541 | |
| 2542 | They all return \NULL{} or -1 in case an exception occurrs. |
| 2543 | |
| 2544 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left, |
| 2545 | PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2546 | Concat two strings giving a new Unicode string. |
| 2547 | \end{cfuncdesc} |
| 2548 | |
| 2549 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s, |
| 2550 | PyObject *sep, |
| 2551 | int maxsplit} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2552 | Split a string giving a list of Unicode strings. |
| 2553 | |
| 2554 | If sep is NULL, splitting will be done at all whitespace |
| 2555 | substrings. Otherwise, splits occur at the given separator. |
| 2556 | |
| 2557 | At most maxsplit splits will be done. If negative, no limit is set. |
| 2558 | |
| 2559 | Separators are not included in the resulting list. |
| 2560 | \end{cfuncdesc} |
| 2561 | |
| 2562 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s, |
| 2563 | int maxsplit} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2564 | Split a Unicode string at line breaks, returning a list of Unicode |
| 2565 | strings. CRLF is considered to be one line break. The Line break |
| 2566 | characters are not included in the resulting strings. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2567 | \end{cfuncdesc} |
| 2568 | |
| 2569 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str, |
| 2570 | PyObject *table, |
| 2571 | const char *errors} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2572 | Translate a string by applying a character mapping table to it and |
| 2573 | return the resulting Unicode object. |
| 2574 | |
| 2575 | The mapping table must map Unicode ordinal integers to Unicode ordinal |
| 2576 | integers or None (causing deletion of the character). |
| 2577 | |
| 2578 | Mapping tables must only provide the __getitem__ interface, |
| 2579 | e.g. dictionaries or sequences. Unmapped character ordinals (ones |
| 2580 | which cause a LookupError) are left untouched and are copied as-is. |
| 2581 | |
| 2582 | \var{errors} has the usual meaning for codecs. It may be \NULL{} |
| 2583 | which indicates to use the default error handling. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2584 | \end{cfuncdesc} |
| 2585 | |
| 2586 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator, |
| 2587 | PyObject *seq} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2588 | Join a sequence of strings using the given separator and return |
| 2589 | the resulting Unicode string. |
| 2590 | \end{cfuncdesc} |
| 2591 | |
| 2592 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str, |
| 2593 | PyObject *substr, |
| 2594 | int start, |
| 2595 | int end, |
| 2596 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2597 | Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at |
| 2598 | the given tail end (\var{direction} == -1 means to do a prefix match, |
| 2599 | \var{direction} == 1 a suffix match), 0 otherwise. |
| 2600 | \end{cfuncdesc} |
| 2601 | |
| 2602 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Find}{PyObject *str, |
| 2603 | PyObject *substr, |
| 2604 | int start, |
| 2605 | int end, |
| 2606 | int direction} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2607 | Return the first position of \var{substr} in |
| 2608 | \var{str}[\var{start}:\var{end}] using the given \var{direction} |
| 2609 | (\var{direction} == 1 means to do a forward search, |
| 2610 | \var{direction} == -1 a backward search), 0 otherwise. |
| 2611 | \end{cfuncdesc} |
| 2612 | |
| 2613 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Count}{PyObject *str, |
| 2614 | PyObject *substr, |
| 2615 | int start, |
| 2616 | int end} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2617 | Count the number of occurrences of \var{substr} in |
| 2618 | \var{str}[\var{start}:\var{end}] |
| 2619 | \end{cfuncdesc} |
| 2620 | |
| 2621 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str, |
| 2622 | PyObject *substr, |
| 2623 | PyObject *replstr, |
| 2624 | int maxcount} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2625 | Replace at most \var{maxcount} occurrences of \var{substr} in |
| 2626 | \var{str} with \var{replstr} and return the resulting Unicode object. |
| 2627 | \var{maxcount} == -1 means: replace all occurrences. |
| 2628 | \end{cfuncdesc} |
| 2629 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2630 | \begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2631 | Compare two strings and return -1, 0, 1 for less than, equal, |
| 2632 | greater than resp. |
| 2633 | \end{cfuncdesc} |
| 2634 | |
| 2635 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, |
| 2636 | PyObject *args} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2637 | Returns a new string object from \var{format} and \var{args}; this is |
| 2638 | analogous to \code{\var{format} \%\ \var{args}}. The |
| 2639 | \var{args} argument must be a tuple. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2640 | \end{cfuncdesc} |
| 2641 | |
| 2642 | \begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container, |
| 2643 | PyObject *element} |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2644 | Checks whether \var{element} is contained in \var{container} and |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2645 | returns true or false accordingly. |
Fred Drake | a4cd261 | 2000-04-06 14:10:29 +0000 | [diff] [blame] | 2646 | |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2647 | \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] | 2648 | returned in case of an error. |
| 2649 | \end{cfuncdesc} |
| 2650 | |
| 2651 | |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2652 | \subsection{Buffer Objects \label{bufferObjects}} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2653 | \sectionauthor{Greg Stein}{gstein@lyra.org} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2654 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2655 | \obindex{buffer} |
| 2656 | Python objects implemented in C can export a group of functions called |
| 2657 | the ``buffer\index{buffer interface} interface.'' These functions can |
| 2658 | be used by an object to expose its data in a raw, byte-oriented |
| 2659 | format. Clients of the object can use the buffer interface to access |
| 2660 | the object data directly, without needing to copy it first. |
| 2661 | |
| 2662 | Two examples of objects that support |
| 2663 | the buffer interface are strings and arrays. The string object exposes |
| 2664 | the character contents in the buffer interface's byte-oriented |
| 2665 | form. An array can also expose its contents, but it should be noted |
| 2666 | that array elements may be multi-byte values. |
| 2667 | |
| 2668 | An example user of the buffer interface is the file object's |
| 2669 | \method{write()} method. Any object that can export a series of bytes |
| 2670 | through the buffer interface can be written to a file. There are a |
| 2671 | number of format codes to \cfunction{PyArgs_ParseTuple()} that operate |
| 2672 | against an object's buffer interface, returning data from the target |
| 2673 | object. |
| 2674 | |
| 2675 | More information on the buffer interface is provided in the section |
| 2676 | ``Buffer Object Structures'' (section \ref{buffer-structs}), under |
| 2677 | the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}. |
| 2678 | |
| 2679 | A ``buffer object'' is defined in the \file{bufferobject.h} header |
| 2680 | (included by \file{Python.h}). These objects look very similar to |
| 2681 | string objects at the Python programming level: they support slicing, |
| 2682 | indexing, concatenation, and some other standard string |
| 2683 | operations. However, their data can come from one of two sources: from |
| 2684 | a block of memory, or from another object which exports the buffer |
| 2685 | interface. |
| 2686 | |
| 2687 | Buffer objects are useful as a way to expose the data from another |
| 2688 | object's buffer interface to the Python programmer. They can also be |
| 2689 | used as a zero-copy slicing mechanism. Using their ability to |
| 2690 | reference a block of memory, it is possible to expose any data to the |
| 2691 | Python programmer quite easily. The memory could be a large, constant |
| 2692 | array in a C extension, it could be a raw block of memory for |
| 2693 | manipulation before passing to an operating system library, or it |
| 2694 | could be used to pass around structured data in its native, in-memory |
| 2695 | format. |
| 2696 | |
| 2697 | \begin{ctypedesc}{PyBufferObject} |
| 2698 | This subtype of \ctype{PyObject} represents a buffer object. |
| 2699 | \end{ctypedesc} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2700 | |
| 2701 | \begin{cvardesc}{PyTypeObject}{PyBuffer_Type} |
| 2702 | The instance of \ctype{PyTypeObject} which represents the Python |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2703 | buffer type; it is the same object as \code{types.BufferType} in the |
| 2704 | Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2705 | \end{cvardesc} |
| 2706 | |
| 2707 | \begin{cvardesc}{int}{Py_END_OF_BUFFER} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2708 | This constant may be passed as the \var{size} parameter to |
| 2709 | \cfunction{PyBuffer_FromObject()} or |
| 2710 | \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the new |
| 2711 | \ctype{PyBufferObject} should refer to \var{base} object from the |
| 2712 | specified \var{offset} to the end of its exported buffer. Using this |
| 2713 | enables the caller to avoid querying the \var{base} object for its |
| 2714 | length. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2715 | \end{cvardesc} |
| 2716 | |
| 2717 | \begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p} |
| 2718 | Return true if the argument has type \cdata{PyBuffer_Type}. |
| 2719 | \end{cfuncdesc} |
| 2720 | |
| 2721 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base, |
| 2722 | int offset, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2723 | Return a new read-only buffer object. This raises |
| 2724 | \exception{TypeError} if \var{base} doesn't support the read-only |
| 2725 | buffer protocol or doesn't provide exactly one buffer segment, or it |
| 2726 | raises \exception{ValueError} if \var{offset} is less than zero. The |
| 2727 | buffer will hold a reference to the \var{base} object, and the |
| 2728 | buffer's contents will refer to the \var{base} object's buffer |
| 2729 | interface, starting as position \var{offset} and extending for |
| 2730 | \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then |
| 2731 | the new buffer's contents extend to the length of the |
| 2732 | \var{base} object's exported buffer data. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2733 | \end{cfuncdesc} |
| 2734 | |
| 2735 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base, |
| 2736 | int offset, |
| 2737 | int size} |
| 2738 | Return a new writable buffer object. Parameters and exceptions are |
| 2739 | similar to those for \cfunction{PyBuffer_FromObject()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2740 | If the \var{base} object does not export the writeable buffer |
| 2741 | protocol, then \exception{TypeError} is raised. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2742 | \end{cfuncdesc} |
| 2743 | |
| 2744 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2745 | Return a new read-only buffer object that reads from a specified |
| 2746 | location in memory, with a specified size. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2747 | The caller is responsible for ensuring that the memory buffer, passed |
| 2748 | in as \var{ptr}, is not deallocated while the returned buffer object |
| 2749 | exists. Raises \exception{ValueError} if \var{size} is less than |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2750 | zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be passed |
| 2751 | for the \var{size} parameter; \exception{ValueError} will be raised in |
| 2752 | that case. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2753 | \end{cfuncdesc} |
| 2754 | |
| 2755 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2756 | Similar to \cfunction{PyBuffer_FromMemory()}, but the returned buffer |
| 2757 | is writable. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2758 | \end{cfuncdesc} |
| 2759 | |
| 2760 | \begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size} |
| 2761 | Returns a new writable buffer object that maintains its own memory |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2762 | buffer of \var{size} bytes. \exception{ValueError} is returned if |
| 2763 | \var{size} is not zero or positive. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 2764 | \end{cfuncdesc} |
| 2765 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2766 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2767 | \subsection{Tuple Objects \label{tupleObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2768 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2769 | \obindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2770 | \begin{ctypedesc}{PyTupleObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2771 | This subtype of \ctype{PyObject} represents a Python tuple object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2772 | \end{ctypedesc} |
| 2773 | |
| 2774 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2775 | This instance of \ctype{PyTypeObject} represents the Python tuple |
| 2776 | type; it is the same object as \code{types.TupleType} in the Python |
| 2777 | layer.\withsubitem{(in module types)}{\ttindex{TupleType}}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2778 | \end{cvardesc} |
| 2779 | |
| 2780 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} |
| 2781 | Return true if the argument is a tuple object. |
| 2782 | \end{cfuncdesc} |
| 2783 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2784 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len} |
| 2785 | 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] | 2786 | \end{cfuncdesc} |
| 2787 | |
| 2788 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2789 | Takes a pointer to a tuple object, and returns the size |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2790 | of that tuple. |
| 2791 | \end{cfuncdesc} |
| 2792 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2793 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyTupleObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2794 | Returns the object at position \var{pos} in the tuple pointed |
| 2795 | 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] | 2796 | sets an \exception{IndexError} exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2797 | \end{cfuncdesc} |
| 2798 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2799 | \begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2800 | Does the same, but does no checking of its arguments. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2801 | \end{cfuncdesc} |
| 2802 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2803 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyTupleObject *p, |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2804 | int low, |
| 2805 | int high} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2806 | Takes a slice of the tuple pointed to by \var{p} from |
| 2807 | \var{low} to \var{high} and returns it as a new tuple. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2808 | \end{cfuncdesc} |
| 2809 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2810 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p, |
| 2811 | int pos, PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2812 | Inserts a reference to object \var{o} at position \var{pos} of |
| 2813 | 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] | 2814 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2815 | \end{cfuncdesc} |
| 2816 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2817 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p, |
| 2818 | int pos, PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2819 | Does the same, but does no error checking, and |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2820 | should \emph{only} be used to fill in brand new tuples. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2821 | \strong{Note:} This function ``steals'' a reference to \var{o}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2822 | \end{cfuncdesc} |
| 2823 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2824 | \begin{cfuncdesc}{int}{_PyTuple_Resize}{PyTupleObject *p, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2825 | int newsize, int last_is_sticky} |
| 2826 | Can be used to resize a tuple. \var{newsize} will be the new length |
| 2827 | of the tuple. Because tuples are \emph{supposed} to be immutable, |
| 2828 | this should only be used if there is only one reference to the object. |
| 2829 | Do \emph{not} use this if the tuple may already be known to some other |
| 2830 | part of the code. \var{last_is_sticky} is a flag --- if true, the |
| 2831 | tuple will grow or shrink at the front, otherwise it will grow or |
| 2832 | shrink at the end. Think of this as destroying the old tuple and |
| 2833 | creating a new one, only more efficiently. Returns \code{0} on |
| 2834 | success and \code{-1} on failure (in which case a |
| 2835 | \exception{MemoryError} or \exception{SystemError} will be raised). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2836 | \end{cfuncdesc} |
| 2837 | |
| 2838 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2839 | \subsection{List Objects \label{listObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2840 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2841 | \obindex{list} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2842 | \begin{ctypedesc}{PyListObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2843 | This subtype of \ctype{PyObject} represents a Python list object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2844 | \end{ctypedesc} |
| 2845 | |
| 2846 | \begin{cvardesc}{PyTypeObject}{PyList_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2847 | This instance of \ctype{PyTypeObject} represents the Python list |
| 2848 | type. This is the same object as \code{types.ListType}. |
| 2849 | \withsubitem{(in module types)}{\ttindex{ListType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2850 | \end{cvardesc} |
| 2851 | |
| 2852 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2853 | Returns true if its argument is a \ctype{PyListObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2854 | \end{cfuncdesc} |
| 2855 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2856 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int len} |
| 2857 | 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] | 2858 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2859 | \end{cfuncdesc} |
| 2860 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2861 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2862 | Returns the length of the list object in \var{list}; this is |
| 2863 | equivalent to \samp{len(\var{list})} on a list object. |
| 2864 | \bifuncindex{len} |
| 2865 | \end{cfuncdesc} |
| 2866 | |
| 2867 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} |
| 2868 | Macro form of \cfunction{PyList_GetSize()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2869 | \end{cfuncdesc} |
| 2870 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2871 | \begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2872 | Returns the object at position \var{pos} in the list pointed |
| 2873 | 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] | 2874 | sets an \exception{IndexError} exception. |
| 2875 | \end{cfuncdesc} |
| 2876 | |
| 2877 | \begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i} |
| 2878 | Macro form of \cfunction{PyList_GetItem()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2879 | \end{cfuncdesc} |
| 2880 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2881 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index, |
| 2882 | PyObject *item} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2883 | Sets the item at index \var{index} in list to \var{item}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2884 | \strong{Note:} This function ``steals'' a reference to \var{item}. |
| 2885 | \end{cfuncdesc} |
| 2886 | |
| 2887 | \begin{cfuncdesc}{PyObject*}{PyList_SET_ITEM}{PyObject *list, int i, |
| 2888 | PyObject *o} |
| 2889 | Macro form of \cfunction{PyList_SetItem()} without error checking. |
| 2890 | \strong{Note:} This function ``steals'' a reference to \var{item}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2891 | \end{cfuncdesc} |
| 2892 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2893 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2894 | PyObject *item} |
| 2895 | 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] | 2896 | \var{index}. Returns \code{0} if successful; returns \code{-1} and |
| 2897 | raises an exception if unsuccessful. Analogous to |
| 2898 | \code{\var{list}.insert(\var{index}, \var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2899 | \end{cfuncdesc} |
| 2900 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2901 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2902 | 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] | 2903 | \code{0} if successful; returns \code{-1} and sets an exception if |
| 2904 | unsuccessful. Analogous to \code{\var{list}.append(\var{item})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2905 | \end{cfuncdesc} |
| 2906 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2907 | \begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list, |
| 2908 | int low, int high} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2909 | 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] | 2910 | \emph{between} \var{low} and \var{high}. Returns NULL and sets an |
| 2911 | exception if unsuccessful. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2912 | Analogous to \code{\var{list}[\var{low}:\var{high}]}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2913 | \end{cfuncdesc} |
| 2914 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2915 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list, |
| 2916 | int low, int high, |
| 2917 | PyObject *itemlist} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2918 | Sets the slice of \var{list} between \var{low} and \var{high} to the |
| 2919 | contents of \var{itemlist}. Analogous to |
| 2920 | \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns |
| 2921 | \code{0} on success, \code{-1} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2922 | \end{cfuncdesc} |
| 2923 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2924 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2925 | Sorts the items of \var{list} in place. Returns \code{0} on success, |
| 2926 | \code{-1} on failure. This is equivalent to |
| 2927 | \samp{\var{list}.sort()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2928 | \end{cfuncdesc} |
| 2929 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2930 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2931 | Reverses the items of \var{list} in place. Returns \code{0} on |
| 2932 | success, \code{-1} on failure. This is the equivalent of |
| 2933 | \samp{\var{list}.reverse()}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2934 | \end{cfuncdesc} |
| 2935 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2936 | \begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2937 | Returns a new tuple object containing the contents of \var{list}; |
| 2938 | equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2939 | \end{cfuncdesc} |
| 2940 | |
| 2941 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2942 | \section{Mapping Objects \label{mapObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2943 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2944 | \obindex{mapping} |
| 2945 | |
| 2946 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 2947 | \subsection{Dictionary Objects \label{dictObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2948 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2949 | \obindex{dictionary} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2950 | \begin{ctypedesc}{PyDictObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2951 | This subtype of \ctype{PyObject} represents a Python dictionary object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2952 | \end{ctypedesc} |
| 2953 | |
| 2954 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2955 | This instance of \ctype{PyTypeObject} represents the Python dictionary |
| 2956 | type. This is exposed to Python programs as \code{types.DictType} and |
| 2957 | \code{types.DictionaryType}. |
| 2958 | \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2959 | \end{cvardesc} |
| 2960 | |
| 2961 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2962 | Returns true if its argument is a \ctype{PyDictObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2963 | \end{cfuncdesc} |
| 2964 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2965 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2966 | Returns a new empty dictionary, or \NULL{} on failure. |
| 2967 | \end{cfuncdesc} |
| 2968 | |
| 2969 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p} |
| 2970 | Empties an existing dictionary of all key-value pairs. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2971 | \end{cfuncdesc} |
| 2972 | |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2973 | \begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2974 | Returns a new dictionary that contains the same key-value pairs as p. |
| 2975 | Empties an existing dictionary of all key-value pairs. |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2976 | \end{cfuncdesc} |
| 2977 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2978 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key, |
| 2979 | PyObject *val} |
| 2980 | Inserts \var{value} into the dictionary with a key of \var{key}. |
| 2981 | \var{key} must be hashable; if it isn't, \exception{TypeError} will be |
| 2982 | raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2983 | \end{cfuncdesc} |
| 2984 | |
| 2985 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p, |
| 2986 | char *key, |
| 2987 | PyObject *val} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2988 | Inserts \var{value} into the dictionary using \var{key} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 2989 | 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] | 2990 | created using \code{PyString_FromString(\var{key})}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2991 | \ttindex{PyString_FromString()} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2992 | \end{cfuncdesc} |
| 2993 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2994 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2995 | Removes the entry in dictionary \var{p} with key \var{key}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 2996 | \var{key} must be hashable; if it isn't, \exception{TypeError} is |
| 2997 | raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2998 | \end{cfuncdesc} |
| 2999 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3000 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3001 | Removes the entry in dictionary \var{p} which has a key |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3002 | specified by the string \var{key}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3003 | \end{cfuncdesc} |
| 3004 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3005 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3006 | Returns the object from dictionary \var{p} which has a key |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3007 | \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] | 3008 | \emph{without} setting an exception. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3009 | \end{cfuncdesc} |
| 3010 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3011 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3012 | This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3013 | specified as a \ctype{char*}, rather than a \ctype{PyObject*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3014 | \end{cfuncdesc} |
| 3015 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3016 | \begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3017 | Returns a \ctype{PyListObject} containing all the items |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3018 | from the dictionary, as in the dictinoary method \method{items()} (see |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3019 | the \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3020 | \end{cfuncdesc} |
| 3021 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3022 | \begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3023 | Returns a \ctype{PyListObject} containing all the keys |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3024 | from the dictionary, as in the dictionary method \method{keys()} (see the |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3025 | \citetitle[../lib/lib.html]{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3026 | \end{cfuncdesc} |
| 3027 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3028 | \begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3029 | Returns a \ctype{PyListObject} containing all the values |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3030 | from the dictionary \var{p}, as in the dictionary method |
Fred Drake | be48646 | 1999-11-09 17:03:03 +0000 | [diff] [blame] | 3031 | \method{values()} (see the \citetitle[../lib/lib.html]{Python Library |
| 3032 | Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3033 | \end{cfuncdesc} |
| 3034 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3035 | \begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p} |
| 3036 | Returns the number of items in the dictionary. This is equivalent to |
| 3037 | \samp{len(\var{p})} on a dictionary.\bifuncindex{len} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3038 | \end{cfuncdesc} |
| 3039 | |
| 3040 | \begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p, |
| 3041 | int ppos, |
| 3042 | PyObject **pkey, |
| 3043 | PyObject **pvalue} |
| 3044 | |
| 3045 | \end{cfuncdesc} |
| 3046 | |
| 3047 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3048 | \section{Numeric Objects \label{numericObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3049 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3050 | \obindex{numeric} |
| 3051 | |
| 3052 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3053 | \subsection{Plain Integer Objects \label{intObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3054 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3055 | \obindex{integer} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3056 | \begin{ctypedesc}{PyIntObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3057 | This subtype of \ctype{PyObject} represents a Python integer object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3058 | \end{ctypedesc} |
| 3059 | |
| 3060 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3061 | This instance of \ctype{PyTypeObject} represents the Python plain |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3062 | integer type. This is the same object as \code{types.IntType}. |
| 3063 | \withsubitem{(in modules types)}{\ttindex{IntType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3064 | \end{cvardesc} |
| 3065 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3066 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o} |
| 3067 | Returns true if \var{o} is of type \cdata{PyInt_Type}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3068 | \end{cfuncdesc} |
| 3069 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3070 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3071 | Creates a new integer object with a value of \var{ival}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3072 | |
| 3073 | The current implementation keeps an array of integer objects for all |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3074 | integers between \code{-1} and \code{100}, when you create an int in |
| 3075 | that range you actually just get back a reference to the existing |
| 3076 | object. So it should be possible to change the value of \code{1}. I |
Fred Drake | 7e9d314 | 1998-04-03 05:02:28 +0000 | [diff] [blame] | 3077 | suspect the behaviour of Python in this case is undefined. :-) |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3078 | \end{cfuncdesc} |
| 3079 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3080 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3081 | Will first attempt to cast the object to a \ctype{PyIntObject}, if |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3082 | it is not already one, and then return its value. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3083 | \end{cfuncdesc} |
| 3084 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3085 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io} |
| 3086 | Returns the value of the object \var{io}. No error checking is |
| 3087 | performed. |
| 3088 | \end{cfuncdesc} |
| 3089 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3090 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3091 | Returns the system's idea of the largest integer it can handle |
| 3092 | (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system |
| 3093 | header files). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3094 | \end{cfuncdesc} |
| 3095 | |
| 3096 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3097 | \subsection{Long Integer Objects \label{longObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3098 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3099 | \obindex{long integer} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3100 | \begin{ctypedesc}{PyLongObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3101 | This subtype of \ctype{PyObject} represents a Python long integer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3102 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3103 | \end{ctypedesc} |
| 3104 | |
| 3105 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3106 | This instance of \ctype{PyTypeObject} represents the Python long |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3107 | integer type. This is the same object as \code{types.LongType}. |
| 3108 | \withsubitem{(in modules types)}{\ttindex{LongType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3109 | \end{cvardesc} |
| 3110 | |
| 3111 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3112 | Returns true if its argument is a \ctype{PyLongObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3113 | \end{cfuncdesc} |
| 3114 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3115 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3116 | Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{} on |
| 3117 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3118 | \end{cfuncdesc} |
| 3119 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3120 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3121 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned |
| 3122 | long}, or \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3123 | \end{cfuncdesc} |
| 3124 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3125 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3126 | Returns a new \ctype{PyLongObject} object from the integer part of |
| 3127 | \var{v}, or \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3128 | \end{cfuncdesc} |
| 3129 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3130 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3131 | Returns a C \ctype{long} representation of the contents of |
| 3132 | \var{pylong}. If \var{pylong} is greater than |
| 3133 | \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError} is |
| 3134 | raised.\withsubitem{(built-in exception)}{OverflowError} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3135 | \end{cfuncdesc} |
| 3136 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3137 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3138 | Returns a C \ctype{unsigned long} representation of the contents of |
| 3139 | \var{pylong}. If \var{pylong} is greater than |
| 3140 | \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an \exception{OverflowError} |
| 3141 | is raised.\withsubitem{(built-in exception)}{OverflowError} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3142 | \end{cfuncdesc} |
| 3143 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3144 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3145 | Returns a C \ctype{double} representation of the contents of \var{pylong}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3146 | \end{cfuncdesc} |
| 3147 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3148 | \begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend, |
| 3149 | int base} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3150 | Return a new \ctype{PyLongObject} based on the string value in |
| 3151 | \var{str}, which is interpreted according to the radix in \var{base}. |
| 3152 | If \var{pend} is non-\NULL, \code{*\var{pend}} will point to the first |
| 3153 | character in \var{str} which follows the representation of the |
| 3154 | number. If \var{base} is \code{0}, the radix will be determined base |
| 3155 | on the leading characters of \var{str}: if \var{str} starts with |
| 3156 | \code{'0x'} or \code{'0X'}, radix 16 will be used; if \var{str} starts |
| 3157 | with \code{'0'}, radix 8 will be used; otherwise radix 10 will be |
| 3158 | used. If \var{base} is not \code{0}, it must be between \code{2} and |
| 3159 | \code{36}, inclusive. Leading spaces are ignored. If there are no |
| 3160 | digits, \exception{ValueError} will be raised. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3161 | \end{cfuncdesc} |
| 3162 | |
| 3163 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3164 | \subsection{Floating Point Objects \label{floatObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3165 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3166 | \obindex{floating point} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3167 | \begin{ctypedesc}{PyFloatObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3168 | This subtype of \ctype{PyObject} represents a Python floating point |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3169 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3170 | \end{ctypedesc} |
| 3171 | |
| 3172 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3173 | This instance of \ctype{PyTypeObject} represents the Python floating |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3174 | point type. This is the same object as \code{types.FloatType}. |
| 3175 | \withsubitem{(in modules types)}{\ttindex{FloatType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3176 | \end{cvardesc} |
| 3177 | |
| 3178 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3179 | Returns true if its argument is a \ctype{PyFloatObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3180 | \end{cfuncdesc} |
| 3181 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3182 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3183 | Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on |
| 3184 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3185 | \end{cfuncdesc} |
| 3186 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3187 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3188 | Returns a C \ctype{double} representation of the contents of \var{pyfloat}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3189 | \end{cfuncdesc} |
| 3190 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3191 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3192 | Returns a C \ctype{double} representation of the contents of |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3193 | \var{pyfloat}, but without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3194 | \end{cfuncdesc} |
| 3195 | |
| 3196 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3197 | \subsection{Complex Number Objects \label{complexObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3198 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3199 | \obindex{complex number} |
| 3200 | Python's complex number objects are implemented as two distinct types |
| 3201 | when viewed from the C API: one is the Python object exposed to |
| 3202 | Python programs, and the other is a C structure which represents the |
| 3203 | actual complex number value. The API provides functions for working |
| 3204 | with both. |
| 3205 | |
| 3206 | \subsubsection{Complex Numbers as C Structures} |
| 3207 | |
| 3208 | Note that the functions which accept these structures as parameters |
| 3209 | and return them as results do so \emph{by value} rather than |
| 3210 | dereferencing them through pointers. This is consistent throughout |
| 3211 | the API. |
| 3212 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3213 | \begin{ctypedesc}{Py_complex} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3214 | The C structure which corresponds to the value portion of a Python |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3215 | complex number object. Most of the functions for dealing with complex |
| 3216 | number objects use structures of this type as input or output values, |
| 3217 | as appropriate. It is defined as: |
| 3218 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3219 | \begin{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3220 | typedef struct { |
| 3221 | double real; |
| 3222 | double imag; |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3223 | } Py_complex; |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3224 | \end{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3225 | \end{ctypedesc} |
| 3226 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3227 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right} |
| 3228 | Return the sum of two complex numbers, using the C |
| 3229 | \ctype{Py_complex} representation. |
| 3230 | \end{cfuncdesc} |
| 3231 | |
| 3232 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right} |
| 3233 | Return the difference between two complex numbers, using the C |
| 3234 | \ctype{Py_complex} representation. |
| 3235 | \end{cfuncdesc} |
| 3236 | |
| 3237 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex} |
| 3238 | Return the negation of the complex number \var{complex}, using the C |
| 3239 | \ctype{Py_complex} representation. |
| 3240 | \end{cfuncdesc} |
| 3241 | |
| 3242 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right} |
| 3243 | Return the product of two complex numbers, using the C |
| 3244 | \ctype{Py_complex} representation. |
| 3245 | \end{cfuncdesc} |
| 3246 | |
| 3247 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend, |
| 3248 | Py_complex divisor} |
| 3249 | Return the quotient of two complex numbers, using the C |
| 3250 | \ctype{Py_complex} representation. |
| 3251 | \end{cfuncdesc} |
| 3252 | |
| 3253 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp} |
| 3254 | Return the exponentiation of \var{num} by \var{exp}, using the C |
| 3255 | \ctype{Py_complex} representation. |
| 3256 | \end{cfuncdesc} |
| 3257 | |
| 3258 | |
| 3259 | \subsubsection{Complex Numbers as Python Objects} |
| 3260 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3261 | \begin{ctypedesc}{PyComplexObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3262 | This subtype of \ctype{PyObject} represents a Python complex number object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3263 | \end{ctypedesc} |
| 3264 | |
| 3265 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3266 | This instance of \ctype{PyTypeObject} represents the Python complex |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3267 | number type. |
| 3268 | \end{cvardesc} |
| 3269 | |
| 3270 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3271 | Returns true if its argument is a \ctype{PyComplexObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3272 | \end{cfuncdesc} |
| 3273 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3274 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3275 | Create a new Python complex number object from a C |
| 3276 | \ctype{Py_complex} value. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3277 | \end{cfuncdesc} |
| 3278 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3279 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3280 | Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3281 | \end{cfuncdesc} |
| 3282 | |
| 3283 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3284 | Returns the real part of \var{op} as a C \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3285 | \end{cfuncdesc} |
| 3286 | |
| 3287 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3288 | Returns the imaginary part of \var{op} as a C \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3289 | \end{cfuncdesc} |
| 3290 | |
| 3291 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3292 | Returns the \ctype{Py_complex} value of the complex number \var{op}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3293 | \end{cfuncdesc} |
| 3294 | |
| 3295 | |
| 3296 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3297 | \section{Other Objects \label{otherObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3298 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3299 | \subsection{File Objects \label{fileObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3300 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3301 | \obindex{file} |
| 3302 | Python's built-in file objects are implemented entirely on the |
| 3303 | \ctype{FILE*} support from the C standard library. This is an |
| 3304 | implementation detail and may change in future releases of Python. |
| 3305 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3306 | \begin{ctypedesc}{PyFileObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3307 | This subtype of \ctype{PyObject} represents a Python file object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3308 | \end{ctypedesc} |
| 3309 | |
| 3310 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3311 | This instance of \ctype{PyTypeObject} represents the Python file |
| 3312 | type. This is exposed to Python programs as \code{types.FileType}. |
| 3313 | \withsubitem{(in module types)}{\ttindex{FileType}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3314 | \end{cvardesc} |
| 3315 | |
| 3316 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3317 | Returns true if its argument is a \ctype{PyFileObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3318 | \end{cfuncdesc} |
| 3319 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3320 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode} |
| 3321 | On success, returns a new file object that is opened on the |
| 3322 | file given by \var{filename}, with a file mode given by \var{mode}, |
| 3323 | where \var{mode} has the same semantics as the standard C routine |
| 3324 | \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3325 | \end{cfuncdesc} |
| 3326 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3327 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3328 | char *name, char *mode, |
| 3329 | int (*close)(FILE*)} |
| 3330 | Creates a new \ctype{PyFileObject} from the already-open standard C |
| 3331 | file pointer, \var{fp}. The function \var{close} will be called when |
| 3332 | the file should be closed. Returns \NULL{} on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3333 | \end{cfuncdesc} |
| 3334 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3335 | \begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p} |
| 3336 | Returns the file object associated with \var{p} as a \ctype{FILE*}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3337 | \end{cfuncdesc} |
| 3338 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3339 | \begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3340 | Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this |
| 3341 | function reads one line from the object \var{p}. \var{p} may be a |
| 3342 | file object or any object with a \method{readline()} method. If |
| 3343 | \var{n} is \code{0}, exactly one line is read, regardless of the |
| 3344 | length of the line. If \var{n} is greater than \code{0}, no more than |
| 3345 | \var{n} bytes will be read from the file; a partial line can be |
| 3346 | returned. In both cases, an empty string is returned if the end of |
| 3347 | the file is reached immediately. If \var{n} is less than \code{0}, |
| 3348 | however, one line is read regardless of length, but |
| 3349 | \exception{EOFError} is raised if the end of the file is reached |
| 3350 | immediately. |
| 3351 | \withsubitem{(built-in exception)}{\ttindex{EOFError}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3352 | \end{cfuncdesc} |
| 3353 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3354 | \begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3355 | 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] | 3356 | \end{cfuncdesc} |
| 3357 | |
| 3358 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3359 | Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()} |
| 3360 | only. This should only be called immediately after file object |
| 3361 | creation. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3362 | \end{cfuncdesc} |
| 3363 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3364 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag} |
| 3365 | This function exists for internal use by the interpreter. |
| 3366 | Sets the \member{softspace} attribute of \var{p} to \var{newflag} and |
| 3367 | \withsubitem{(file attribute)}{\ttindex{softspace}}returns the |
| 3368 | previous value. \var{p} does not have to be a file object |
| 3369 | for this function to work properly; any object is supported (thought |
| 3370 | its only interesting if the \member{softspace} attribute can be set). |
| 3371 | This function clears any errors, and will return \code{0} as the |
| 3372 | previous value if the attribute either does not exist or if there were |
| 3373 | errors in retrieving it. There is no way to detect errors from this |
| 3374 | function, but doing so should not be needed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3375 | \end{cfuncdesc} |
| 3376 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3377 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p, |
| 3378 | int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3379 | Writes object \var{obj} to file object \var{p}. The only supported |
| 3380 | flag for \var{flags} is \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; |
| 3381 | if given, the \function{str()} of the object is written instead of the |
| 3382 | \function{repr()}. Returns \code{0} on success or \code{-1} on |
| 3383 | failure; the appropriate exception will be set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3384 | \end{cfuncdesc} |
| 3385 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3386 | \begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p, |
| 3387 | int flags} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3388 | Writes string \var{s} to file object \var{p}. Returns \code{0} on |
| 3389 | success or \code{-1} on failure; the appropriate exception will be |
| 3390 | set. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3391 | \end{cfuncdesc} |
| 3392 | |
| 3393 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3394 | \subsection{Module Objects \label{moduleObjects}} |
| 3395 | |
| 3396 | \obindex{module} |
| 3397 | There are only a few functions special to module objects. |
| 3398 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3399 | \begin{cvardesc}{PyTypeObject}{PyModule_Type} |
| 3400 | This instance of \ctype{PyTypeObject} represents the Python module |
| 3401 | type. This is exposed to Python programs as \code{types.ModuleType}. |
| 3402 | \withsubitem{(in module types)}{\ttindex{ModuleType}} |
| 3403 | \end{cvardesc} |
| 3404 | |
| 3405 | \begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p} |
| 3406 | Returns true if its argument is a module object. |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3407 | \end{cfuncdesc} |
| 3408 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3409 | \begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name} |
| 3410 | Return a new module object with the \member{__name__} attribute set to |
| 3411 | \var{name}. Only the module's \member{__doc__} and |
| 3412 | \member{__name__} attributes are filled in; the caller is responsible |
| 3413 | for providing a \member{__file__} attribute. |
| 3414 | \withsubitem{(module attribute)}{ |
| 3415 | \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}} |
| 3416 | \end{cfuncdesc} |
| 3417 | |
| 3418 | \begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3419 | Return the dictionary object that implements \var{module}'s namespace; |
| 3420 | this object is the same as the \member{__dict__} attribute of the |
| 3421 | module object. This function never fails. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3422 | \withsubitem{(module attribute)}{\ttindex{__dict__}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3423 | \end{cfuncdesc} |
| 3424 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3425 | \begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3426 | Return \var{module}'s \member{__name__} value. If the module does not |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3427 | provide one, or if it is not a string, \exception{SystemError} is |
| 3428 | raised and \NULL{} is returned. |
| 3429 | \withsubitem{(module attribute)}{\ttindex{__name__}} |
| 3430 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3431 | \end{cfuncdesc} |
| 3432 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3433 | \begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3434 | Return the name of the file from which \var{module} was loaded using |
| 3435 | \var{module}'s \member{__file__} attribute. If this is not defined, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3436 | or if it is not a string, raise \exception{SystemError} and return |
| 3437 | \NULL. |
| 3438 | \withsubitem{(module attribute)}{\ttindex{__file__}} |
| 3439 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3440 | \end{cfuncdesc} |
| 3441 | |
| 3442 | |
| 3443 | \subsection{CObjects \label{cObjects}} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3444 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3445 | \obindex{CObject} |
| 3446 | Refer to \emph{Extending and Embedding the Python Interpreter}, |
| 3447 | section 1.12 (``Providing a C API for an Extension Module''), for more |
| 3448 | information on using these objects. |
| 3449 | |
| 3450 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3451 | \begin{ctypedesc}{PyCObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3452 | This subtype of \ctype{PyObject} represents an opaque value, useful for |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3453 | C extension modules who need to pass an opaque value (as a |
| 3454 | \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] | 3455 | often used to make a C function pointer defined in one module |
| 3456 | available to other modules, so the regular import mechanism can be |
| 3457 | used to access C APIs defined in dynamically loaded modules. |
| 3458 | \end{ctypedesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3459 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3460 | \begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p} |
| 3461 | Returns true if its argument is a \ctype{PyCObject}. |
| 3462 | \end{cfuncdesc} |
| 3463 | |
| 3464 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3465 | void (*destr)(void *)} |
Fred Drake | 1d15869 | 2000-06-18 05:21:21 +0000 | [diff] [blame] | 3466 | Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The |
Fred Drake | dab4468 | 1999-05-13 18:41:14 +0000 | [diff] [blame] | 3467 | \var{destr} function will be called when the object is reclaimed, unless |
| 3468 | it is \NULL. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3469 | \end{cfuncdesc} |
| 3470 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3471 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3472 | void* desc, void (*destr)(void *, void *) } |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3473 | Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The |
| 3474 | \var{destr} function will be called when the object is reclaimed. The |
| 3475 | \var{desc} argument can be used to pass extra callback data for the |
| 3476 | destructor function. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3477 | \end{cfuncdesc} |
| 3478 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3479 | \begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self} |
| 3480 | Returns the object \ctype{void *} that the |
| 3481 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3482 | \end{cfuncdesc} |
| 3483 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3484 | \begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self} |
| 3485 | Returns the description \ctype{void *} that the |
| 3486 | \ctype{PyCObject} \var{self} was created with. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 3487 | \end{cfuncdesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3488 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3489 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3490 | \chapter{Initialization, Finalization, and Threads |
| 3491 | \label{initialization}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3492 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3493 | \begin{cfuncdesc}{void}{Py_Initialize}{} |
| 3494 | Initialize the Python interpreter. In an application embedding |
| 3495 | Python, this should be called before using any other Python/C API |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3496 | functions; with the exception of |
| 3497 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, |
| 3498 | \cfunction{PyEval_InitThreads()}\ttindex{PyEval_InitThreads()}, |
| 3499 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()}, |
| 3500 | and \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()}. |
| 3501 | This initializes the table of loaded modules (\code{sys.modules}), and |
| 3502 | \withsubitem{(in module sys)}{\ttindex{modules}\ttindex{path}}creates the |
| 3503 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3504 | \module{__main__}\refbimodindex{__main__} and |
| 3505 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3506 | search\indexiii{module}{search}{path} path (\code{sys.path}). |
| 3507 | It does not set \code{sys.argv}; use |
| 3508 | \cfunction{PySys_SetArgv()}\ttindex{PySys_SetArgv()} for that. This |
| 3509 | is a no-op when called for a second time (without calling |
| 3510 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} first). There is no |
| 3511 | return value; it is a fatal error if the initialization fails. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 3512 | \end{cfuncdesc} |
| 3513 | |
| 3514 | \begin{cfuncdesc}{int}{Py_IsInitialized}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 3515 | Return true (nonzero) when the Python interpreter has been |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3516 | initialized, false (zero) if not. After \cfunction{Py_Finalize()} is |
| 3517 | called, this returns false until \cfunction{Py_Initialize()} is called |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 3518 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3519 | \end{cfuncdesc} |
| 3520 | |
| 3521 | \begin{cfuncdesc}{void}{Py_Finalize}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3522 | Undo all initializations made by \cfunction{Py_Initialize()} and |
| 3523 | subsequent use of Python/C API functions, and destroy all |
| 3524 | sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were |
| 3525 | created and not yet destroyed since the last call to |
| 3526 | \cfunction{Py_Initialize()}. Ideally, this frees all memory allocated |
| 3527 | by the Python interpreter. This is a no-op when called for a second |
| 3528 | time (without calling \cfunction{Py_Initialize()} again first). There |
| 3529 | is no return value; errors during finalization are ignored. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3530 | |
| 3531 | This function is provided for a number of reasons. An embedding |
| 3532 | application might want to restart Python without having to restart the |
| 3533 | application itself. An application that has loaded the Python |
| 3534 | interpreter from a dynamically loadable library (or DLL) might want to |
| 3535 | free all memory allocated by Python before unloading the DLL. During a |
| 3536 | hunt for memory leaks in an application a developer might want to free |
| 3537 | all memory allocated by Python before exiting from the application. |
| 3538 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3539 | \strong{Bugs and caveats:} The destruction of modules and objects in |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3540 | modules is done in random order; this may cause destructors |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3541 | (\method{__del__()} methods) to fail when they depend on other objects |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3542 | (even functions) or modules. Dynamically loaded extension modules |
| 3543 | loaded by Python are not unloaded. Small amounts of memory allocated |
| 3544 | by the Python interpreter may not be freed (if you find a leak, please |
| 3545 | report it). Memory tied up in circular references between objects is |
| 3546 | not freed. Some memory allocated by extension modules may not be |
| 3547 | freed. Some extension may not work properly if their initialization |
| 3548 | routine is called more than once; this can happen if an applcation |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3549 | calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more |
| 3550 | than once. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3551 | \end{cfuncdesc} |
| 3552 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3553 | \begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3554 | Create a new sub-interpreter. This is an (almost) totally separate |
| 3555 | environment for the execution of Python code. In particular, the new |
| 3556 | interpreter has separate, independent versions of all imported |
| 3557 | modules, including the fundamental modules |
| 3558 | \module{__builtin__}\refbimodindex{__builtin__}, |
| 3559 | \module{__main__}\refbimodindex{__main__} and |
| 3560 | \module{sys}\refbimodindex{sys}. The table of loaded modules |
| 3561 | (\code{sys.modules}) and the module search path (\code{sys.path}) are |
| 3562 | also separate. The new environment has no \code{sys.argv} variable. |
| 3563 | It has new standard I/O stream file objects \code{sys.stdin}, |
| 3564 | \code{sys.stdout} and \code{sys.stderr} (however these refer to the |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3565 | same underlying \ctype{FILE} structures in the C library). |
| 3566 | \withsubitem{(in module sys)}{ |
| 3567 | \ttindex{stdout}\ttindex{stderr}\ttindex{stdin}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3568 | |
| 3569 | The return value points to the first thread state created in the new |
| 3570 | sub-interpreter. This thread state is made the current thread state. |
| 3571 | Note that no actual thread is created; see the discussion of thread |
| 3572 | states below. If creation of the new interpreter is unsuccessful, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 3573 | \NULL{} is returned; no exception is set since the exception state |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3574 | is stored in the current thread state and there may not be a current |
| 3575 | thread state. (Like all other Python/C API functions, the global |
| 3576 | interpreter lock must be held before calling this function and is |
| 3577 | still held when it returns; however, unlike most other Python/C API |
| 3578 | functions, there needn't be a current thread state on entry.) |
| 3579 | |
| 3580 | Extension modules are shared between (sub-)interpreters as follows: |
| 3581 | the first time a particular extension is imported, it is initialized |
| 3582 | normally, and a (shallow) copy of its module's dictionary is |
| 3583 | squirreled away. When the same extension is imported by another |
| 3584 | (sub-)interpreter, a new module is initialized and filled with the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3585 | contents of this copy; the extension's \code{init} function is not |
| 3586 | called. Note that this is different from what happens when an |
| 3587 | extension is imported after the interpreter has been completely |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3588 | re-initialized by calling |
| 3589 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and |
| 3590 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}; in that case, |
| 3591 | the extension's \code{init\var{module}} function \emph{is} called |
| 3592 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3593 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3594 | \strong{Bugs and caveats:} Because sub-interpreters (and the main |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3595 | interpreter) are part of the same process, the insulation between them |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3596 | isn't perfect --- for example, using low-level file operations like |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3597 | \withsubitem{(in module os)}{\ttindex{close()}} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3598 | \function{os.close()} they can (accidentally or maliciously) affect each |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3599 | other's open files. Because of the way extensions are shared between |
| 3600 | (sub-)interpreters, some extensions may not work properly; this is |
| 3601 | especially likely when the extension makes use of (static) global |
| 3602 | variables, or when the extension manipulates its module's dictionary |
| 3603 | after its initialization. It is possible to insert objects created in |
| 3604 | one sub-interpreter into a namespace of another sub-interpreter; this |
| 3605 | should be done with great care to avoid sharing user-defined |
| 3606 | functions, methods, instances or classes between sub-interpreters, |
| 3607 | since import operations executed by such objects may affect the |
| 3608 | wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is |
| 3609 | a hard-to-fix bug that will be addressed in a future release.) |
| 3610 | \end{cfuncdesc} |
| 3611 | |
| 3612 | \begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate} |
| 3613 | Destroy the (sub-)interpreter represented by the given thread state. |
| 3614 | The given thread state must be the current thread state. See the |
| 3615 | discussion of thread states below. When the call returns, the current |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 3616 | thread state is \NULL{}. All thread states associated with this |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3617 | interpreted are destroyed. (The global interpreter lock must be held |
| 3618 | before calling this function and is still held when it returns.) |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3619 | \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} will destroy all |
| 3620 | sub-interpreters that haven't been explicitly destroyed at that point. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3621 | \end{cfuncdesc} |
| 3622 | |
| 3623 | \begin{cfuncdesc}{void}{Py_SetProgramName}{char *name} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3624 | This function should be called before |
| 3625 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()} is called |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3626 | 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] | 3627 | the value of the \code{argv[0]} argument to the |
| 3628 | \cfunction{main()}\ttindex{main()} function of the program. This is |
| 3629 | used by \cfunction{Py_GetPath()}\ttindex{Py_GetPath()} and some other |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3630 | functions below to find the Python run-time libraries relative to the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3631 | interpreter executable. The default value is \code{'python'}. The |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3632 | argument should point to a zero-terminated character string in static |
| 3633 | storage whose contents will not change for the duration of the |
| 3634 | program's execution. No code in the Python interpreter will change |
| 3635 | the contents of this storage. |
| 3636 | \end{cfuncdesc} |
| 3637 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3638 | \begin{cfuncdesc}{char*}{Py_GetProgramName}{} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3639 | Return the program name set with |
| 3640 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, or the |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3641 | default. The returned string points into static storage; the caller |
| 3642 | should not modify its value. |
| 3643 | \end{cfuncdesc} |
| 3644 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3645 | \begin{cfuncdesc}{char*}{Py_GetPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3646 | Return the \emph{prefix} for installed platform-independent files. This |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3647 | is derived through a number of complicated rules from the program name |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3648 | set with \cfunction{Py_SetProgramName()} and some environment variables; |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3649 | for example, if the program name is \code{'/usr/local/bin/python'}, |
| 3650 | the prefix is \code{'/usr/local'}. The returned string points into |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3651 | static storage; the caller should not modify its value. This |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 3652 | corresponds to the \makevar{prefix} variable in the top-level |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3653 | \file{Makefile} and the \longprogramopt{prefix} argument to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3654 | \program{configure} script at build time. The value is available to |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 3655 | 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] | 3656 | also the next function. |
| 3657 | \end{cfuncdesc} |
| 3658 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3659 | \begin{cfuncdesc}{char*}{Py_GetExecPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3660 | Return the \emph{exec-prefix} for installed platform-\emph{de}pendent |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3661 | files. This is derived through a number of complicated rules from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3662 | program name set with \cfunction{Py_SetProgramName()} and some environment |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3663 | variables; for example, if the program name is |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3664 | \code{'/usr/local/bin/python'}, the exec-prefix is |
| 3665 | \code{'/usr/local'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3666 | the caller should not modify its value. This corresponds to the |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 3667 | \makevar{exec_prefix} variable in the top-level \file{Makefile} and the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3668 | \longprogramopt{exec-prefix} argument to the |
Fred Drake | 310ee61 | 1999-11-09 17:31:42 +0000 | [diff] [blame] | 3669 | \program{configure} script at build time. The value is available to |
| 3670 | 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] | 3671 | |
| 3672 | Background: The exec-prefix differs from the prefix when platform |
| 3673 | dependent files (such as executables and shared libraries) are |
| 3674 | installed in a different directory tree. In a typical installation, |
| 3675 | platform dependent files may be installed in the |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3676 | \file{/usr/local/plat} subtree while platform independent may be |
| 3677 | installed in \file{/usr/local}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3678 | |
| 3679 | Generally speaking, a platform is a combination of hardware and |
| 3680 | software families, e.g. Sparc machines running the Solaris 2.x |
| 3681 | operating system are considered the same platform, but Intel machines |
| 3682 | running Solaris 2.x are another platform, and Intel machines running |
| 3683 | Linux are yet another platform. Different major revisions of the same |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 3684 | operating system generally also form different platforms. Non-\UNIX{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3685 | operating systems are a different story; the installation strategies |
| 3686 | on those systems are so different that the prefix and exec-prefix are |
| 3687 | meaningless, and set to the empty string. Note that compiled Python |
| 3688 | bytecode files are platform independent (but not independent from the |
| 3689 | Python version by which they were compiled!). |
| 3690 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3691 | System administrators will know how to configure the \program{mount} or |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3692 | \program{automount} programs to share \file{/usr/local} between platforms |
| 3693 | 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] | 3694 | platform. |
| 3695 | \end{cfuncdesc} |
| 3696 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3697 | \begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3698 | Return the full program name of the Python executable; this is |
| 3699 | computed as a side-effect of deriving the default module search path |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3700 | from the program name (set by |
| 3701 | \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()} above). |
| 3702 | The returned string points into static storage; the caller should not |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3703 | modify its value. The value is available to Python code as |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 3704 | \code{sys.executable}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3705 | \withsubitem{(in module sys)}{\ttindex{executable}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3706 | \end{cfuncdesc} |
| 3707 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3708 | \begin{cfuncdesc}{char*}{Py_GetPath}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3709 | \indexiii{module}{search}{path} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3710 | Return the default module search path; this is computed from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3711 | program name (set by \cfunction{Py_SetProgramName()} above) and some |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3712 | environment variables. The returned string consists of a series of |
| 3713 | directory names separated by a platform dependent delimiter character. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3714 | The delimiter character is \character{:} on \UNIX{}, \character{;} on |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3715 | DOS/Windows, and \character{\e n} (the \ASCII{} newline character) on |
Fred Drake | e5bc497 | 1998-02-12 23:36:49 +0000 | [diff] [blame] | 3716 | Macintosh. The returned string points into static storage; the caller |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3717 | should not modify its value. The value is available to Python code |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3718 | as the list \code{sys.path}\withsubitem{(in module sys)}{\ttindex{path}}, |
| 3719 | which may be modified to change the future search path for loaded |
| 3720 | modules. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3721 | |
| 3722 | % XXX should give the exact rules |
| 3723 | \end{cfuncdesc} |
| 3724 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3725 | \begin{cfuncdesc}{const char*}{Py_GetVersion}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3726 | Return the version of this Python interpreter. This is a string that |
| 3727 | looks something like |
| 3728 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 3729 | \begin{verbatim} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3730 | "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] | 3731 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3732 | |
| 3733 | The first word (up to the first space character) is the current Python |
| 3734 | version; the first three characters are the major and minor version |
| 3735 | separated by a period. The returned string points into static storage; |
| 3736 | the caller should not modify its value. The value is available to |
| 3737 | Python code as the list \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3738 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3739 | \end{cfuncdesc} |
| 3740 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3741 | \begin{cfuncdesc}{const char*}{Py_GetPlatform}{} |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 3742 | Return the platform identifier for the current platform. On \UNIX{}, |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3743 | this is formed from the ``official'' name of the operating system, |
| 3744 | converted to lower case, followed by the major revision number; e.g., |
| 3745 | 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] | 3746 | \code{'sunos5'}. On Macintosh, it is \code{'mac'}. On Windows, it |
| 3747 | is \code{'win'}. The returned string points into static storage; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3748 | the caller should not modify its value. The value is available to |
| 3749 | Python code as \code{sys.platform}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3750 | \withsubitem{(in module sys)}{\ttindex{platform}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3751 | \end{cfuncdesc} |
| 3752 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3753 | \begin{cfuncdesc}{const char*}{Py_GetCopyright}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3754 | Return the official copyright string for the current Python version, |
| 3755 | for example |
| 3756 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 3757 | \code{'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3758 | |
| 3759 | The returned string points into static storage; the caller should not |
| 3760 | modify its value. The value is available to Python code as the list |
| 3761 | \code{sys.copyright}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3762 | \withsubitem{(in module sys)}{\ttindex{copyright}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3763 | \end{cfuncdesc} |
| 3764 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3765 | \begin{cfuncdesc}{const char*}{Py_GetCompiler}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3766 | Return an indication of the compiler used to build the current Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3767 | version, in square brackets, for example: |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3768 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3769 | \begin{verbatim} |
| 3770 | "[GCC 2.7.2.2]" |
| 3771 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3772 | |
| 3773 | The returned string points into static storage; the caller should not |
| 3774 | modify its value. The value is available to Python code as part of |
| 3775 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3776 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3777 | \end{cfuncdesc} |
| 3778 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 3779 | \begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3780 | Return information about the sequence number and build date and time |
| 3781 | of the current Python interpreter instance, for example |
| 3782 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 3783 | \begin{verbatim} |
| 3784 | "#67, Aug 1 1997, 22:34:28" |
| 3785 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3786 | |
| 3787 | The returned string points into static storage; the caller should not |
| 3788 | modify its value. The value is available to Python code as part of |
| 3789 | the variable \code{sys.version}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3790 | \withsubitem{(in module sys)}{\ttindex{version}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3791 | \end{cfuncdesc} |
| 3792 | |
| 3793 | \begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv} |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3794 | Set \code{sys.argv} based on \var{argc} and \var{argv}. These |
| 3795 | parameters are similar to those passed to the program's |
| 3796 | \cfunction{main()}\ttindex{main()} function with the difference that |
| 3797 | the first entry should refer to the script file to be executed rather |
| 3798 | than the executable hosting the Python interpreter. If there isn't a |
| 3799 | script that will be run, the first entry in \var{argv} can be an empty |
| 3800 | string. If this function fails to initialize \code{sys.argv}, a fatal |
| 3801 | condition is signalled using |
| 3802 | \cfunction{Py_FatalError()}\ttindex{Py_FatalError()}. |
| 3803 | \withsubitem{(in module sys)}{\ttindex{argv}} |
| 3804 | % XXX impl. doesn't seem consistent in allowing 0/NULL for the params; |
| 3805 | % check w/ Guido. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3806 | \end{cfuncdesc} |
| 3807 | |
| 3808 | % XXX Other PySys thingies (doesn't really belong in this chapter) |
| 3809 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 3810 | \section{Thread State and the Global Interpreter Lock |
| 3811 | \label{threads}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3812 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3813 | \index{global interpreter lock} |
| 3814 | \index{interpreter lock} |
| 3815 | \index{lock, interpreter} |
| 3816 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3817 | The Python interpreter is not fully thread safe. In order to support |
| 3818 | multi-threaded Python programs, there's a global lock that must be |
| 3819 | held by the current thread before it can safely access Python objects. |
| 3820 | Without the lock, even the simplest operations could cause problems in |
Fred Drake | 7baf3d4 | 1998-02-20 00:45:52 +0000 | [diff] [blame] | 3821 | a multi-threaded program: for example, when two threads simultaneously |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3822 | increment the reference count of the same object, the reference count |
| 3823 | could end up being incremented only once instead of twice. |
| 3824 | |
| 3825 | Therefore, the rule exists that only the thread that has acquired the |
| 3826 | global interpreter lock may operate on Python objects or call Python/C |
| 3827 | API functions. In order to support multi-threaded Python programs, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3828 | the interpreter regularly releases and reacquires the lock --- by |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3829 | default, every ten bytecode instructions (this can be changed with |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3830 | \withsubitem{(in module sys)}{\ttindex{setcheckinterval()}} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3831 | \function{sys.setcheckinterval()}). The lock is also released and |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3832 | reacquired around potentially blocking I/O operations like reading or |
| 3833 | writing a file, so that other threads can run while the thread that |
| 3834 | requests the I/O is waiting for the I/O operation to complete. |
| 3835 | |
| 3836 | The Python interpreter needs to keep some bookkeeping information |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3837 | separate per thread --- for this it uses a data structure called |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3838 | \ctype{PyThreadState}\ttindex{PyThreadState}. This is new in Python |
| 3839 | 1.5; in earlier versions, such state was stored in global variables, |
| 3840 | and switching threads could cause problems. In particular, exception |
| 3841 | handling is now thread safe, when the application uses |
| 3842 | \withsubitem{(in module sys)}{\ttindex{exc_info()}} |
| 3843 | \function{sys.exc_info()} to access the exception last raised in the |
| 3844 | current thread. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3845 | |
| 3846 | There's one global variable left, however: the pointer to the current |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3847 | \ctype{PyThreadState}\ttindex{PyThreadState} structure. While most |
| 3848 | thread packages have a way to store ``per-thread global data,'' |
| 3849 | Python's internal platform independent thread abstraction doesn't |
| 3850 | support this yet. Therefore, the current thread state must be |
| 3851 | manipulated explicitly. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3852 | |
| 3853 | This is easy enough in most cases. Most code manipulating the global |
| 3854 | interpreter lock has the following simple structure: |
| 3855 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3856 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3857 | Save the thread state in a local variable. |
| 3858 | Release the interpreter lock. |
| 3859 | ...Do some blocking I/O operation... |
| 3860 | Reacquire the interpreter lock. |
| 3861 | Restore the thread state from the local variable. |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3862 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3863 | |
| 3864 | This is so common that a pair of macros exists to simplify it: |
| 3865 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3866 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3867 | Py_BEGIN_ALLOW_THREADS |
| 3868 | ...Do some blocking I/O operation... |
| 3869 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3870 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3871 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3872 | The \code{Py_BEGIN_ALLOW_THREADS}\ttindex{Py_BEGIN_ALLOW_THREADS} macro |
| 3873 | opens a new block and declares a hidden local variable; the |
| 3874 | \code{Py_END_ALLOW_THREADS}\ttindex{Py_END_ALLOW_THREADS} macro closes |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3875 | the block. Another advantage of using these two macros is that when |
| 3876 | Python is compiled without thread support, they are defined empty, |
| 3877 | thus saving the thread state and lock manipulations. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3878 | |
| 3879 | When thread support is enabled, the block above expands to the |
| 3880 | following code: |
| 3881 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3882 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3883 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3884 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3885 | _save = PyEval_SaveThread(); |
| 3886 | ...Do some blocking I/O operation... |
| 3887 | PyEval_RestoreThread(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3888 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3889 | |
| 3890 | Using even lower level primitives, we can get roughly the same effect |
| 3891 | as follows: |
| 3892 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3893 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3894 | PyThreadState *_save; |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3895 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3896 | _save = PyThreadState_Swap(NULL); |
| 3897 | PyEval_ReleaseLock(); |
| 3898 | ...Do some blocking I/O operation... |
| 3899 | PyEval_AcquireLock(); |
| 3900 | PyThreadState_Swap(_save); |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 3901 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3902 | |
| 3903 | There are some subtle differences; in particular, |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3904 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()} saves |
| 3905 | and restores the value of the global variable |
| 3906 | \cdata{errno}\ttindex{errno}, since the lock manipulation does not |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3907 | guarantee that \cdata{errno} is left alone. Also, when thread support |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3908 | is disabled, |
| 3909 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} and |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3910 | \cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3911 | case, \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} and |
| 3912 | \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()} are not |
| 3913 | available. This is done so that dynamically loaded extensions |
| 3914 | compiled with thread support enabled can be loaded by an interpreter |
| 3915 | that was compiled with disabled thread support. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3916 | |
| 3917 | The global interpreter lock is used to protect the pointer to the |
| 3918 | current thread state. When releasing the lock and saving the thread |
| 3919 | state, the current thread state pointer must be retrieved before the |
| 3920 | lock is released (since another thread could immediately acquire the |
| 3921 | lock and store its own thread state in the global variable). |
| 3922 | Reversely, when acquiring the lock and restoring the thread state, the |
| 3923 | lock must be acquired before storing the thread state pointer. |
| 3924 | |
| 3925 | 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] | 3926 | 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] | 3927 | lock, nor is there a thread state data structure for them. Such |
| 3928 | threads must bootstrap themselves into existence, by first creating a |
| 3929 | thread state data structure, then acquiring the lock, and finally |
| 3930 | storing their thread state pointer, before they can start using the |
| 3931 | Python/C API. When they are done, they should reset the thread state |
| 3932 | pointer, release the lock, and finally free their thread state data |
| 3933 | structure. |
| 3934 | |
| 3935 | When creating a thread data structure, you need to provide an |
| 3936 | interpreter state data structure. The interpreter state data |
| 3937 | structure hold global data that is shared by all threads in an |
| 3938 | interpreter, for example the module administration |
| 3939 | (\code{sys.modules}). Depending on your needs, you can either create |
| 3940 | a new interpreter state data structure, or share the interpreter state |
| 3941 | data structure used by the Python main thread (to access the latter, |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3942 | 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] | 3943 | this must be done by a thread that is created by Python or by the main |
| 3944 | thread after Python is initialized). |
| 3945 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3946 | |
| 3947 | \begin{ctypedesc}{PyInterpreterState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3948 | This data structure represents the state shared by a number of |
| 3949 | cooperating threads. Threads belonging to the same interpreter |
| 3950 | share their module administration and a few other internal items. |
| 3951 | There are no public members in this structure. |
| 3952 | |
| 3953 | Threads belonging to different interpreters initially share nothing, |
| 3954 | except process state like available memory, open file descriptors and |
| 3955 | such. The global interpreter lock is also shared by all threads, |
| 3956 | regardless of to which interpreter they belong. |
| 3957 | \end{ctypedesc} |
| 3958 | |
| 3959 | \begin{ctypedesc}{PyThreadState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3960 | This data structure represents the state of a single thread. The only |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 3961 | public data member is \ctype{PyInterpreterState *}\member{interp}, |
| 3962 | which points to this thread's interpreter state. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3963 | \end{ctypedesc} |
| 3964 | |
| 3965 | \begin{cfuncdesc}{void}{PyEval_InitThreads}{} |
| 3966 | Initialize and acquire the global interpreter lock. It should be |
| 3967 | called in the main thread before creating a second thread or engaging |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 3968 | in any other thread operations such as |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 3969 | \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()} or |
| 3970 | \code{PyEval_ReleaseThread(\var{tstate})}\ttindex{PyEval_ReleaseThread()}. |
| 3971 | It is not needed before calling |
| 3972 | \cfunction{PyEval_SaveThread()}\ttindex{PyEval_SaveThread()} or |
| 3973 | \cfunction{PyEval_RestoreThread()}\ttindex{PyEval_RestoreThread()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3974 | |
| 3975 | 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] | 3976 | this function before calling |
| 3977 | \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3978 | |
| 3979 | When only the main thread exists, no lock operations are needed. This |
| 3980 | is a common situation (most Python programs do not use threads), and |
| 3981 | the lock operations slow the interpreter down a bit. Therefore, the |
| 3982 | lock is not created initially. This situation is equivalent to having |
| 3983 | acquired the lock: when there is only a single thread, all object |
| 3984 | accesses are safe. Therefore, when this function initializes the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 3985 | lock, it also acquires it. Before the Python |
| 3986 | \module{thread}\refbimodindex{thread} module creates a new thread, |
| 3987 | knowing that either it has the lock or the lock hasn't been created |
| 3988 | yet, it calls \cfunction{PyEval_InitThreads()}. When this call |
| 3989 | returns, it is guaranteed that the lock has been created and that it |
| 3990 | has acquired it. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 3991 | |
| 3992 | It is \strong{not} safe to call this function when it is unknown which |
| 3993 | thread (if any) currently has the global interpreter lock. |
| 3994 | |
| 3995 | This function is not available when thread support is disabled at |
| 3996 | compile time. |
| 3997 | \end{cfuncdesc} |
| 3998 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 3999 | \begin{cfuncdesc}{void}{PyEval_AcquireLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4000 | Acquire the global interpreter lock. The lock must have been created |
| 4001 | earlier. If this thread already has the lock, a deadlock ensues. |
| 4002 | This function is not available when thread support is disabled at |
| 4003 | compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4004 | \end{cfuncdesc} |
| 4005 | |
| 4006 | \begin{cfuncdesc}{void}{PyEval_ReleaseLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4007 | Release the global interpreter lock. The lock must have been created |
| 4008 | earlier. This function is not available when thread support is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4009 | disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4010 | \end{cfuncdesc} |
| 4011 | |
| 4012 | \begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4013 | Acquire the global interpreter lock and then set the current thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4014 | 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] | 4015 | have been created earlier. If this thread already has the lock, |
| 4016 | deadlock ensues. This function is not available when thread support |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4017 | is disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4018 | \end{cfuncdesc} |
| 4019 | |
| 4020 | \begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4021 | Reset the current thread state to \NULL{} and release the global |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4022 | interpreter lock. The lock must have been created earlier and must be |
| 4023 | 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] | 4024 | be \NULL{}, is only used to check that it represents the current |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4025 | thread state --- if it isn't, a fatal error is reported. This |
| 4026 | function is not available when thread support is disabled at compile |
| 4027 | time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4028 | \end{cfuncdesc} |
| 4029 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4030 | \begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4031 | Release the interpreter lock (if it has been created and thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4032 | support is enabled) and reset the thread state to \NULL{}, |
| 4033 | returning the previous thread state (which is not \NULL{}). If |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4034 | the lock has been created, the current thread must have acquired it. |
| 4035 | (This function is available even when thread support is disabled at |
| 4036 | compile time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4037 | \end{cfuncdesc} |
| 4038 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4039 | \begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4040 | Acquire the interpreter lock (if it has been created and thread |
| 4041 | 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] | 4042 | 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] | 4043 | thread must not have acquired it, otherwise deadlock ensues. (This |
| 4044 | function is available even when thread support is disabled at compile |
| 4045 | time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4046 | \end{cfuncdesc} |
| 4047 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4048 | The following macros are normally used without a trailing semicolon; |
| 4049 | look for example usage in the Python source distribution. |
| 4050 | |
| 4051 | \begin{csimplemacrodesc}{Py_BEGIN_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4052 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4053 | \samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4054 | Note that it contains an opening brace; it must be matched with a |
| 4055 | following \code{Py_END_ALLOW_THREADS} macro. See above for further |
| 4056 | discussion of this macro. It is a no-op when thread support is |
| 4057 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4058 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4059 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4060 | \begin{csimplemacrodesc}{Py_END_ALLOW_THREADS} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4061 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4062 | \samp{PyEval_RestoreThread(_save); \}}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4063 | Note that it contains a closing brace; it must be matched with an |
| 4064 | earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further |
| 4065 | discussion of this macro. It is a no-op when thread support is |
| 4066 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4067 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4068 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4069 | \begin{csimplemacrodesc}{Py_BEGIN_BLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4070 | This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4071 | is equivalent to \code{Py_END_ALLOW_THREADS} without the closing |
| 4072 | brace. It is a no-op when thread support is disabled at compile |
| 4073 | time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4074 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4075 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4076 | \begin{csimplemacrodesc}{Py_BEGIN_UNBLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4077 | This macro expands to \samp{_save = PyEval_SaveThread();} i.e. it is |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4078 | equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace |
| 4079 | and variable declaration. It is a no-op when thread support is |
| 4080 | disabled at compile time. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4081 | \end{csimplemacrodesc} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4082 | |
| 4083 | All of the following functions are only available when thread support |
| 4084 | is enabled at compile time, and must be called only when the |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 4085 | interpreter lock has been created. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4086 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4087 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{} |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 4088 | Create a new interpreter state object. The interpreter lock need not |
| 4089 | be held, but may be held if it is necessary to serialize calls to this |
| 4090 | function. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4091 | \end{cfuncdesc} |
| 4092 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4093 | \begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp} |
| 4094 | Reset all information in an interpreter state object. The interpreter |
| 4095 | lock must be held. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4096 | \end{cfuncdesc} |
| 4097 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4098 | \begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp} |
| 4099 | Destroy an interpreter state object. The interpreter lock need not be |
| 4100 | held. The interpreter state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4101 | call to \cfunction{PyInterpreterState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4102 | \end{cfuncdesc} |
| 4103 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4104 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4105 | Create a new thread state object belonging to the given interpreter |
Guido van Rossum | ed9dcc1 | 1998-08-07 18:28:03 +0000 | [diff] [blame] | 4106 | object. The interpreter lock need not be held, but may be held if it |
| 4107 | is necessary to serialize calls to this function. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4108 | \end{cfuncdesc} |
| 4109 | |
| 4110 | \begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate} |
| 4111 | Reset all information in a thread state object. The interpreter lock |
| 4112 | must be held. |
| 4113 | \end{cfuncdesc} |
| 4114 | |
| 4115 | \begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate} |
| 4116 | Destroy a thread state object. The interpreter lock need not be |
| 4117 | held. The thread state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4118 | call to \cfunction{PyThreadState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4119 | \end{cfuncdesc} |
| 4120 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4121 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4122 | Return the current thread state. The interpreter lock must be held. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 4123 | When the current thread state is \NULL{}, this issues a fatal |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 4124 | error (so that the caller needn't check for \NULL{}). |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4125 | \end{cfuncdesc} |
| 4126 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4127 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4128 | 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] | 4129 | argument \var{tstate}, which may be \NULL{}. The interpreter lock |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 4130 | must be held. |
| 4131 | \end{cfuncdesc} |
| 4132 | |
| 4133 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4134 | \chapter{Memory Management \label{memory}} |
| 4135 | \sectionauthor{Vladimir Marangozov}{Vladimir.Marangozov@inrialpes.fr} |
| 4136 | |
| 4137 | |
| 4138 | \section{Overview \label{memoryOverview}} |
| 4139 | |
| 4140 | Memory management in Python involves a private heap containing all |
| 4141 | Python objects and data structures. The management of this private |
| 4142 | heap is ensured internally by the \emph{Python memory manager}. The |
| 4143 | Python memory manager has different components which deal with various |
| 4144 | dynamic storage management aspects, like sharing, segmentation, |
| 4145 | preallocation or caching. |
| 4146 | |
| 4147 | At the lowest level, a raw memory allocator ensures that there is |
| 4148 | enough room in the private heap for storing all Python-related data |
| 4149 | by interacting with the memory manager of the operating system. On top |
| 4150 | of the raw memory allocator, several object-specific allocators |
| 4151 | operate on the same heap and implement distinct memory management |
| 4152 | policies adapted to the peculiarities of every object type. For |
| 4153 | example, integer objects are managed differently within the heap than |
| 4154 | strings, tuples or dictionaries because integers imply different |
| 4155 | storage requirements and speed/space tradeoffs. The Python memory |
| 4156 | manager thus delegates some of the work to the object-specific |
| 4157 | allocators, but ensures that the latter operate within the bounds of |
| 4158 | the private heap. |
| 4159 | |
| 4160 | It is important to understand that the management of the Python heap |
| 4161 | is performed by the interpreter itself and that the user has no |
| 4162 | control on it, even if she regularly manipulates object pointers to |
| 4163 | memory blocks inside that heap. The allocation of heap space for |
| 4164 | Python objects and other internal buffers is performed on demand by |
| 4165 | the Python memory manager through the Python/C API functions listed in |
| 4166 | this document. |
| 4167 | |
| 4168 | To avoid memory corruption, extension writers should never try to |
| 4169 | operate on Python objects with the functions exported by the C |
| 4170 | library: \cfunction{malloc()}\ttindex{malloc()}, |
| 4171 | \cfunction{calloc()}\ttindex{calloc()}, |
| 4172 | \cfunction{realloc()}\ttindex{realloc()} and |
| 4173 | \cfunction{free()}\ttindex{free()}. This will result in |
| 4174 | mixed calls between the C allocator and the Python memory manager |
| 4175 | with fatal consequences, because they implement different algorithms |
| 4176 | and operate on different heaps. However, one may safely allocate and |
| 4177 | release memory blocks with the C library allocator for individual |
| 4178 | purposes, as shown in the following example: |
| 4179 | |
| 4180 | \begin{verbatim} |
| 4181 | PyObject *res; |
| 4182 | char *buf = (char *) malloc(BUFSIZ); /* for I/O */ |
| 4183 | |
| 4184 | if (buf == NULL) |
| 4185 | return PyErr_NoMemory(); |
| 4186 | ...Do some I/O operation involving buf... |
| 4187 | res = PyString_FromString(buf); |
| 4188 | free(buf); /* malloc'ed */ |
| 4189 | return res; |
| 4190 | \end{verbatim} |
| 4191 | |
| 4192 | In this example, the memory request for the I/O buffer is handled by |
| 4193 | the C library allocator. The Python memory manager is involved only |
| 4194 | in the allocation of the string object returned as a result. |
| 4195 | |
| 4196 | In most situations, however, it is recommended to allocate memory from |
| 4197 | the Python heap specifically because the latter is under control of |
| 4198 | the Python memory manager. For example, this is required when the |
| 4199 | interpreter is extended with new object types written in C. Another |
| 4200 | reason for using the Python heap is the desire to \emph{inform} the |
| 4201 | Python memory manager about the memory needs of the extension module. |
| 4202 | Even when the requested memory is used exclusively for internal, |
| 4203 | highly-specific purposes, delegating all memory requests to the Python |
| 4204 | memory manager causes the interpreter to have a more accurate image of |
| 4205 | its memory footprint as a whole. Consequently, under certain |
| 4206 | circumstances, the Python memory manager may or may not trigger |
| 4207 | appropriate actions, like garbage collection, memory compaction or |
| 4208 | other preventive procedures. Note that by using the C library |
| 4209 | allocator as shown in the previous example, the allocated memory for |
| 4210 | the I/O buffer escapes completely the Python memory manager. |
| 4211 | |
| 4212 | |
| 4213 | \section{Memory Interface \label{memoryInterface}} |
| 4214 | |
| 4215 | The following function sets, modeled after the ANSI C standard, are |
| 4216 | available for allocating and releasing memory from the Python heap: |
| 4217 | |
| 4218 | |
| 4219 | \begin{ctypedesc}{ANY*} |
| 4220 | The type used to represent arbitrary blocks of memory. Values of this |
| 4221 | type should be cast to the specific type that is needed. |
| 4222 | \end{ctypedesc} |
| 4223 | |
| 4224 | \begin{cfuncdesc}{ANY*}{PyMem_Malloc}{size_t n} |
| 4225 | Allocates \var{n} bytes and returns a pointer of type \ctype{ANY*} to |
| 4226 | the allocated memory, or \NULL{} if the request fails. Requesting zero |
| 4227 | bytes returns a non-\NULL{} pointer. |
| 4228 | \end{cfuncdesc} |
| 4229 | |
| 4230 | \begin{cfuncdesc}{ANY*}{PyMem_Realloc}{ANY *p, size_t n} |
| 4231 | Resizes the memory block pointed to by \var{p} to \var{n} bytes. The |
| 4232 | contents will be unchanged to the minimum of the old and the new |
| 4233 | sizes. If \var{p} is \NULL{}, the call is equivalent to |
| 4234 | \cfunction{PyMem_Malloc(\var{n})}; if \var{n} is equal to zero, the memory block |
| 4235 | is resized but is not freed, and the returned pointer is non-\NULL{}. |
| 4236 | Unless \var{p} is \NULL{}, it must have been returned by a previous |
| 4237 | call to \cfunction{PyMem_Malloc()} or \cfunction{PyMem_Realloc()}. |
| 4238 | \end{cfuncdesc} |
| 4239 | |
| 4240 | \begin{cfuncdesc}{void}{PyMem_Free}{ANY *p} |
| 4241 | Frees the memory block pointed to by \var{p}, which must have been |
| 4242 | returned by a previous call to \cfunction{PyMem_Malloc()} or |
| 4243 | \cfunction{PyMem_Realloc()}. Otherwise, or if |
| 4244 | \cfunction{PyMem_Free(p)} has been called before, undefined behaviour |
| 4245 | occurs. If \var{p} is \NULL{}, no operation is performed. |
| 4246 | \end{cfuncdesc} |
| 4247 | |
| 4248 | \begin{cfuncdesc}{ANY*}{Py_Malloc}{size_t n} |
| 4249 | Same as \cfunction{PyMem_Malloc()}, but calls |
| 4250 | \cfunction{PyErr_NoMemory()} on failure. |
| 4251 | \end{cfuncdesc} |
| 4252 | |
| 4253 | \begin{cfuncdesc}{ANY*}{Py_Realloc}{ANY *p, size_t n} |
| 4254 | Same as \cfunction{PyMem_Realloc()}, but calls |
| 4255 | \cfunction{PyErr_NoMemory()} on failure. |
| 4256 | \end{cfuncdesc} |
| 4257 | |
| 4258 | \begin{cfuncdesc}{void}{Py_Free}{ANY *p} |
| 4259 | Same as \cfunction{PyMem_Free()}. |
| 4260 | \end{cfuncdesc} |
| 4261 | |
| 4262 | The following type-oriented macros are provided for convenience. Note |
| 4263 | that \var{TYPE} refers to any C type. |
| 4264 | |
| 4265 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_NEW}{TYPE, size_t n} |
| 4266 | Same as \cfunction{PyMem_Malloc()}, but allocates \code{(\var{n} * |
| 4267 | sizeof(\var{TYPE}))} bytes of memory. Returns a pointer cast to |
| 4268 | \ctype{\var{TYPE}*}. |
| 4269 | \end{cfuncdesc} |
| 4270 | |
| 4271 | \begin{cfuncdesc}{\var{TYPE}*}{PyMem_RESIZE}{ANY *p, TYPE, size_t n} |
| 4272 | Same as \cfunction{PyMem_Realloc()}, but the memory block is resized |
| 4273 | to \code{(\var{n} * sizeof(\var{TYPE}))} bytes. Returns a pointer |
| 4274 | cast to \ctype{\var{TYPE}*}. |
| 4275 | \end{cfuncdesc} |
| 4276 | |
| 4277 | \begin{cfuncdesc}{void}{PyMem_DEL}{ANY *p} |
| 4278 | Same as \cfunction{PyMem_Free()}. |
| 4279 | \end{cfuncdesc} |
| 4280 | |
| 4281 | |
| 4282 | \section{Examples \label{memoryExamples}} |
| 4283 | |
| 4284 | Here is the example from section \ref{memoryOverview}, rewritten so |
| 4285 | that the I/O buffer is allocated from the Python heap by using the |
| 4286 | first function set: |
| 4287 | |
| 4288 | \begin{verbatim} |
| 4289 | PyObject *res; |
| 4290 | char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */ |
| 4291 | |
| 4292 | if (buf == NULL) |
| 4293 | return PyErr_NoMemory(); |
| 4294 | /* ...Do some I/O operation involving buf... */ |
| 4295 | res = PyString_FromString(buf); |
| 4296 | PyMem_Free(buf); /* allocated with PyMem_Malloc */ |
| 4297 | return res; |
| 4298 | \end{verbatim} |
| 4299 | |
| 4300 | With the second function set, the need to call |
| 4301 | \cfunction{PyErr_NoMemory()} is obviated: |
| 4302 | |
| 4303 | \begin{verbatim} |
| 4304 | PyObject *res; |
| 4305 | char *buf = (char *) Py_Malloc(BUFSIZ); /* for I/O */ |
| 4306 | |
| 4307 | if (buf == NULL) |
| 4308 | return NULL; |
| 4309 | /* ...Do some I/O operation involving buf... */ |
| 4310 | res = PyString_FromString(buf); |
| 4311 | Py_Free(buf); /* allocated with Py_Malloc */ |
| 4312 | return res; |
| 4313 | \end{verbatim} |
| 4314 | |
| 4315 | The same code using the macro set: |
| 4316 | |
| 4317 | \begin{verbatim} |
| 4318 | PyObject *res; |
| 4319 | char *buf = PyMem_NEW(char, BUFSIZ); /* for I/O */ |
| 4320 | |
| 4321 | if (buf == NULL) |
| 4322 | return PyErr_NoMemory(); |
| 4323 | /* ...Do some I/O operation involving buf... */ |
| 4324 | res = PyString_FromString(buf); |
| 4325 | PyMem_DEL(buf); /* allocated with PyMem_NEW */ |
| 4326 | return res; |
| 4327 | \end{verbatim} |
| 4328 | |
| 4329 | Note that in the three examples above, the buffer is always |
| 4330 | manipulated via functions/macros belonging to the same set. Indeed, it |
| 4331 | is required to use the same memory API family for a given |
| 4332 | memory block, so that the risk of mixing different allocators is |
| 4333 | reduced to a minimum. The following code sequence contains two errors, |
| 4334 | one of which is labeled as \emph{fatal} because it mixes two different |
| 4335 | allocators operating on different heaps. |
| 4336 | |
| 4337 | \begin{verbatim} |
| 4338 | char *buf1 = PyMem_NEW(char, BUFSIZ); |
| 4339 | char *buf2 = (char *) malloc(BUFSIZ); |
| 4340 | char *buf3 = (char *) PyMem_Malloc(BUFSIZ); |
| 4341 | ... |
| 4342 | PyMem_DEL(buf3); /* Wrong -- should be PyMem_Free() */ |
| 4343 | free(buf2); /* Right -- allocated via malloc() */ |
| 4344 | free(buf1); /* Fatal -- should be PyMem_DEL() */ |
| 4345 | \end{verbatim} |
| 4346 | |
| 4347 | In addition to the functions aimed at handling raw memory blocks from |
| 4348 | the Python heap, objects in Python are allocated and released with |
| 4349 | \cfunction{_PyObject_New()}\ttindex{_PyObject_New()} and |
| 4350 | \cfunction{_PyObject_NewVar()}\ttindex{_PyObject_NewVar()}, or with |
| 4351 | their corresponding macros |
| 4352 | \cfunction{PyObject_NEW()}\ttindex{PyObject_NEW()} and |
| 4353 | \cfunction{PyObject_NEW_VAR()}\ttindex{PyObject_NEW_VAR()}. |
| 4354 | |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 4355 | \cfunction{_PyObject_New()}, \cfunction{_PyObject_NewVar()}, |
| 4356 | \cfunction{_PyObject_Del()}, or with their corresponding macros |
| 4357 | \cfunction{PyObject_NEW()}, \cfunction{PyObject_NEW_VAR()}, |
| 4358 | \cfunction{PyObject_DEL()}. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4359 | |
Fred Drake | e06f0f9 | 2000-06-30 15:52:39 +0000 | [diff] [blame] | 4360 | These will be explained in the next chapter on defining and |
| 4361 | implementing new object types in C. |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4362 | |
| 4363 | |
Fred Drake | efd146c | 1999-02-15 15:30:45 +0000 | [diff] [blame] | 4364 | \chapter{Defining New Object Types \label{newTypes}} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 4365 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4366 | \begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4367 | \end{cfuncdesc} |
| 4368 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 4369 | \begin{cfuncdesc}{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4370 | \end{cfuncdesc} |
| 4371 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4372 | \begin{cfuncdesc}{\var{TYPE}}{_PyObject_NEW}{TYPE, PyTypeObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4373 | \end{cfuncdesc} |
| 4374 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4375 | \begin{cfuncdesc}{\var{TYPE}}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *type, |
| 4376 | int size} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 4377 | \end{cfuncdesc} |
| 4378 | |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 4379 | Py_InitModule (!!!) |
| 4380 | |
| 4381 | PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse |
| 4382 | |
| 4383 | Py_BuildValue |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4384 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4385 | DL_IMPORT |
| 4386 | |
| 4387 | Py*_Check |
| 4388 | |
| 4389 | _Py_NoneStruct |
| 4390 | |
| 4391 | |
| 4392 | \section{Common Object Structures \label{common-structs}} |
| 4393 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4394 | PyObject, PyVarObject |
| 4395 | |
| 4396 | PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD |
| 4397 | |
| 4398 | Typedefs: |
| 4399 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 4400 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4401 | destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 4402 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 4403 | |
Fred Drake | a8455ab | 2000-06-16 19:58:42 +0000 | [diff] [blame] | 4404 | \begin{ctypedesc}{PyCFunction} |
| 4405 | Type of the functions used to implement most Python callables in C. |
| 4406 | \end{ctypedesc} |
| 4407 | |
| 4408 | \begin{ctypedesc}{PyMethodDef} |
| 4409 | Structure used to describe a method of an extension type. This |
| 4410 | structure has four fields: |
| 4411 | |
| 4412 | \begin{tableiii}{l|l|l}{member}{Field}{C Type}{Meaning} |
| 4413 | \lineiii{ml_name}{char *}{name of the method} |
| 4414 | \lineiii{ml_meth}{PyCFunction}{pointer to the C implementation} |
| 4415 | \lineiii{ml_flags}{int}{flag bits indicating how the call should be |
| 4416 | constructed} |
| 4417 | \lineiii{ml_doc}{char *}{points to the contents of the docstring} |
| 4418 | \end{tableiii} |
| 4419 | \end{ctypedesc} |
| 4420 | |
| 4421 | \begin{cfuncdesc}{PyObject*}{Py_FindMethod}{PyMethodDef[] table, |
| 4422 | PyObject *ob, char *name} |
| 4423 | Return a bound method object for an extension type implemented in C. |
| 4424 | This function also handles the special attribute \member{__methods__}, |
| 4425 | returning a list of all the method names defined in \var{table}. |
| 4426 | \end{cfuncdesc} |
| 4427 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4428 | |
| 4429 | \section{Mapping Object Structures \label{mapping-structs}} |
| 4430 | |
| 4431 | \begin{ctypedesc}{PyMappingMethods} |
| 4432 | Structure used to hold pointers to the functions used to implement the |
| 4433 | mapping protocol for an extension type. |
| 4434 | \end{ctypedesc} |
| 4435 | |
| 4436 | |
| 4437 | \section{Number Object Structures \label{number-structs}} |
| 4438 | |
| 4439 | \begin{ctypedesc}{PyNumberMethods} |
| 4440 | Structure used to hold pointers to the functions an extension type |
| 4441 | uses to implement the number protocol. |
| 4442 | \end{ctypedesc} |
| 4443 | |
| 4444 | |
| 4445 | \section{Sequence Object Structures \label{sequence-structs}} |
| 4446 | |
| 4447 | \begin{ctypedesc}{PySequenceMethods} |
| 4448 | Structure used to hold pointers to the functions which an object uses |
| 4449 | to implement the sequence protocol. |
| 4450 | \end{ctypedesc} |
| 4451 | |
| 4452 | |
| 4453 | \section{Buffer Object Structures \label{buffer-structs}} |
| 4454 | \sectionauthor{Greg J. Stein}{greg@lyra.org} |
| 4455 | |
| 4456 | The buffer interface exports a model where an object can expose its |
| 4457 | internal data as a set of chunks of data, where each chunk is |
| 4458 | specified as a pointer/length pair. These chunks are called |
| 4459 | \dfn{segments} and are presumed to be non-contiguous in memory. |
| 4460 | |
| 4461 | If an object does not export the buffer interface, then its |
| 4462 | \member{tp_as_buffer} member in the \ctype{PyTypeObject} structure |
| 4463 | should be \NULL{}. Otherwise, the \member{tp_as_buffer} will point to |
| 4464 | a \ctype{PyBufferProcs} structure. |
| 4465 | |
| 4466 | \strong{Note:} It is very important that your |
| 4467 | \ctype{PyTypeObject} structure uses \code{Py_TPFLAGS_DEFAULT} for the |
| 4468 | value of the \member{tp_flags} member rather than \code{0}. This |
| 4469 | tells the Python runtime that your \ctype{PyBufferProcs} structure |
| 4470 | contains the \member{bf_getcharbuffer} slot. Older versions of Python |
| 4471 | did not have this member, so a new Python interpreter using an old |
| 4472 | extension needs to be able to test for its presence before using it. |
| 4473 | |
| 4474 | \begin{ctypedesc}{PyBufferProcs} |
| 4475 | Structure used to hold the function pointers which define an |
| 4476 | implementation of the buffer protocol. |
| 4477 | |
| 4478 | The first slot is \member{bf_getreadbuffer}, of type |
| 4479 | \ctype{getreadbufferproc}. If this slot is \NULL{}, then the object |
| 4480 | does not support reading from the internal data. This is |
| 4481 | non-sensical, so implementors should fill this in, but callers should |
| 4482 | test that the slot contains a non-\NULL{} value. |
| 4483 | |
| 4484 | The next slot is \member{bf_getwritebuffer} having type |
| 4485 | \ctype{getwritebufferproc}. This slot may be \NULL{} if the object |
| 4486 | does not allow writing into its returned buffers. |
| 4487 | |
| 4488 | The third slot is \member{bf_getsegcount}, with type |
| 4489 | \ctype{getsegcountproc}. This slot must not be \NULL{} and is used to |
| 4490 | inform the caller how many segments the object contains. Simple |
| 4491 | objects such as \ctype{PyString_Type} and |
| 4492 | \ctype{PyBuffer_Type} objects contain a single segment. |
| 4493 | |
| 4494 | The last slot is \member{bf_getcharbuffer}, of type |
| 4495 | \ctype{getcharbufferproc}. This slot will only be present if the |
| 4496 | \code{Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the |
| 4497 | \member{tp_flags} field of the object's \ctype{PyTypeObject}. Before using |
| 4498 | this slot, the caller should test whether it is present by using the |
| 4499 | \cfunction{PyType_HasFeature()}\ttindex{PyType_HasFeature()} function. |
| 4500 | If present, it may be \NULL, indicating that the object's contents |
| 4501 | cannot be used as \emph{8-bit characters}. |
| 4502 | The slot function may also raise an error if the object's contents |
| 4503 | cannot be interpreted as 8-bit characters. For example, if the object |
| 4504 | is an array which is configured to hold floating point values, an |
| 4505 | exception may be raised if a caller attempts to use |
| 4506 | \member{bf_getcharbuffer} to fetch a sequence of 8-bit characters. |
| 4507 | This notion of exporting the internal buffers as ``text'' is used to |
| 4508 | distinguish between objects that are binary in nature, and those which |
| 4509 | have character-based content. |
| 4510 | |
| 4511 | \strong{Note:} The current policy seems to state that these characters |
| 4512 | may be multi-byte characters. This implies that a buffer size of |
| 4513 | \var{N} does not mean there are \var{N} characters present. |
| 4514 | \end{ctypedesc} |
| 4515 | |
| 4516 | \begin{datadesc}{Py_TPFLAGS_HAVE_GETCHARBUFFER} |
| 4517 | Flag bit set in the type structure to indicate that the |
| 4518 | \member{bf_getcharbuffer} slot is known. This being set does not |
| 4519 | indicate that the object supports the buffer interface or that the |
| 4520 | \member{bf_getcharbuffer} slot is non-\NULL. |
| 4521 | \end{datadesc} |
| 4522 | |
| 4523 | \begin{ctypedesc}[getreadbufferproc]{int (*getreadbufferproc) |
| 4524 | (PyObject *self, int segment, void **ptrptr)} |
| 4525 | Return a pointer to a readable segment of the buffer. This function |
| 4526 | is allowed to raise an exception, in which case it must return |
| 4527 | \code{-1}. The \var{segment} which is passed must be zero or |
| 4528 | positive, and strictly less than the number of segments returned by |
| 4529 | the \member{bf_getsegcount} slot function. On success, returns |
| 4530 | \code{0} and sets \code{*\var{ptrptr}} to a pointer to the buffer |
| 4531 | memory. |
| 4532 | \end{ctypedesc} |
| 4533 | |
| 4534 | \begin{ctypedesc}[getwritebufferproc]{int (*getwritebufferproc) |
| 4535 | (PyObject *self, int segment, void **ptrptr)} |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 4536 | Return a pointer to a writable memory buffer in \code{*\var{ptrptr}}; |
| 4537 | the memory buffer must correspond to buffer segment \var{segment}. |
| 4538 | Must return \code{-1} and set an exception on error. |
| 4539 | \exception{TypeError} should be raised if the object only supports |
| 4540 | read-only buffers, and \exception{SystemError} should be raised when |
| 4541 | \var{segment} specifies a segment that doesn't exist. |
| 4542 | % Why doesn't it raise ValueError for this one? |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4543 | % GJS: because you shouldn't be calling it with an invalid |
| 4544 | % segment. That indicates a blatant programming error in the C |
| 4545 | % code. |
Fred Drake | 58c5a2a | 1999-08-04 13:13:24 +0000 | [diff] [blame] | 4546 | \end{ctypedesc} |
| 4547 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4548 | \begin{ctypedesc}[getsegcountproc]{int (*getsegcountproc) |
| 4549 | (PyObject *self, int *lenp)} |
| 4550 | Return the number of memory segments which comprise the buffer. If |
| 4551 | \var{lenp} is not \NULL, the implementation must report the sum of the |
| 4552 | sizes (in bytes) of all segments in \code{*\var{lenp}}. |
| 4553 | The function cannot fail. |
| 4554 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4555 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4556 | \begin{ctypedesc}[getcharbufferproc]{int (*getcharbufferproc) |
| 4557 | (PyObject *self, int segment, const char **ptrptr)} |
| 4558 | \end{ctypedesc} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4559 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 4560 | |
Fred Drake | 659ebfa | 2000-04-03 15:42:13 +0000 | [diff] [blame] | 4561 | % \chapter{Debugging \label{debugging}} |
| 4562 | % |
| 4563 | % XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 4564 | |
| 4565 | |
Fred Drake | f3aa0e0 | 1998-03-17 06:23:13 +0000 | [diff] [blame] | 4566 | \input{api.ind} % Index -- must be last |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 4567 | |
| 4568 | \end{document} |