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 | |
| 14 | \input{copyright} |
| 15 | |
| 16 | \begin{abstract} |
| 17 | |
| 18 | \noindent |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 19 | This manual documents the API used by \C{} (or \Cpp{}) programmers who |
| 20 | want to write extension modules or embed Python. It is a companion to |
| 21 | \emph{Extending and Embedding the Python Interpreter}, which describes |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 22 | the general principles of extension writing but does not document the |
| 23 | API functions in detail. |
| 24 | |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 25 | \strong{Warning:} The current version of this document is incomplete. |
| 26 | I hope that it is nevertheless useful. I will continue to work on it, |
| 27 | and release new versions from time to time, independent from Python |
| 28 | source code releases. |
| 29 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 30 | \end{abstract} |
| 31 | |
Fred Drake | 4d4f9e7 | 1998-01-13 22:25:02 +0000 | [diff] [blame] | 32 | \tableofcontents |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 34 | % XXX Consider moving all this back to ext.tex and giving api.tex |
| 35 | % XXX a *really* short intro only. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 36 | |
| 37 | \chapter{Introduction} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 38 | \label{intro} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 39 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 40 | The Application Programmer's Interface to Python gives \C{} and \Cpp{} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 41 | programmers access to the Python interpreter at a variety of levels. |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 42 | The API is equally usable from \Cpp{}, but for brevity it is generally |
| 43 | referred to as the Python/\C{} API. There are two fundamentally |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 44 | different reasons for using the Python/\C{} API. The first reason is |
| 45 | to write \emph{extension modules} for specific purposes; these are |
| 46 | \C{} modules that extend the Python interpreter. This is probably the |
| 47 | most common use. The second reason is to use Python as a component in |
| 48 | a larger application; this technique is generally referred to as |
| 49 | \dfn{embedding} Python in an application. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 51 | Writing an extension module is a relatively well-understood process, |
| 52 | where a ``cookbook'' approach works well. There are several tools |
| 53 | that automate the process to some extent. While people have embedded |
| 54 | Python in other applications since its early existence, the process of |
| 55 | embedding Python is less straightforward that writing an extension. |
| 56 | Python 1.5 introduces a number of new API functions as well as some |
| 57 | changes to the build process that make embedding much simpler. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 58 | This manual describes the \version\ state of affairs. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 59 | % XXX Eventually, take the historical notes out |
| 60 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 61 | Many API functions are useful independent of whether you're embedding |
| 62 | or extending Python; moreover, most applications that embed Python |
| 63 | will need to provide a custom extension as well, so it's probably a |
| 64 | good idea to become familiar with writing an extension before |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 65 | attempting to embed Python in a real application. |
| 66 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 67 | \section{Include Files} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 68 | \label{includes} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 69 | |
| 70 | All function, type and macro definitions needed to use the Python/C |
| 71 | API are included in your code by the following line: |
| 72 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 73 | \begin{verbatim} |
| 74 | #include "Python.h" |
| 75 | \end{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 76 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 77 | This implies inclusion of the following standard headers: |
| 78 | \code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and |
| 79 | \code{<stdlib.h>} (if available). |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 80 | |
| 81 | All user visible names defined by Python.h (except those defined by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 82 | the included standard headers) have one of the prefixes \samp{Py} or |
| 83 | \samp{_Py}. Names beginning with \samp{_Py} are for internal use |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 84 | only. Structure member names do not have a reserved prefix. |
| 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 | |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 92 | \section{Objects, Types and Reference Counts} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 93 | \label{objects} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 95 | Most Python/C API functions have one or more arguments as well as a |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 96 | return value of type \ctype{PyObject *}. This type is a pointer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 97 | to an opaque data type representing an arbitrary Python |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 98 | object. Since all Python object types are treated the same way by the |
| 99 | Python language in most situations (e.g., assignments, scope rules, |
| 100 | and argument passing), it is only fitting that they should be |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 101 | represented by a single \C{} type. All Python objects live on the heap: |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 102 | you never declare an automatic or static variable of type |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 103 | \ctype{PyObject}, only pointer variables of type \ctype{PyObject *} can |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 104 | be declared. |
| 105 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 106 | All Python objects (even Python integers) have a \dfn{type} and a |
| 107 | \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] | 108 | it is (e.g., an integer, a list, or a user-defined function; there are |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 109 | many more as explained in the \emph{Python Reference Manual}). For |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 110 | each of the well-known types there is a macro to check whether an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 111 | object is of that type; for instance, \samp{PyList_Check(\var{a})} is |
| 112 | true iff the object pointed to by \var{a} is a Python list. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 114 | \subsection{Reference Counts} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 115 | \label{refcounts} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 117 | The reference count is important because today's computers have a |
Fred Drake | 003d8da | 1998-04-13 00:53:42 +0000 | [diff] [blame] | 118 | finite (and often severely limited) memory size; it counts how many |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 119 | different places there are that have a reference to an object. Such a |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 120 | place could be another object, or a global (or static) \C{} variable, or |
| 121 | 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] | 122 | becomes zero, the object is deallocated. If it contains references to |
| 123 | other objects, their reference count is decremented. Those other |
| 124 | objects may be deallocated in turn, if this decrement makes their |
| 125 | reference count become zero, and so on. (There's an obvious problem |
| 126 | with objects that reference each other here; for now, the solution is |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 127 | ``don't do that''.) |
| 128 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 129 | Reference counts are always manipulated explicitly. The normal way is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 130 | to use the macro \cfunction{Py_INCREF()} to increment an object's |
| 131 | reference count by one, and \cfunction{Py_DECREF()} to decrement it by |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 132 | one. The decref macro is considerably more complex than the incref one, |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 133 | since it must check whether the reference count becomes zero and then |
| 134 | cause the object's deallocator, which is a function pointer contained |
| 135 | in the object's type structure. The type-specific deallocator takes |
| 136 | care of decrementing the reference counts for other objects contained |
| 137 | in the object, and so on, if this is a compound object type such as a |
| 138 | list. There's no chance that the reference count can overflow; at |
| 139 | least as many bits are used to hold the reference count as there are |
| 140 | distinct memory locations in virtual memory (assuming |
| 141 | \code{sizeof(long) >= sizeof(char *)}). Thus, the reference count |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 142 | increment is a simple operation. |
| 143 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 144 | It is not necessary to increment an object's reference count for every |
| 145 | local variable that contains a pointer to an object. In theory, the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 146 | 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] | 147 | point to it and it goes down by one when the variable goes out of |
| 148 | scope. However, these two cancel each other out, so at the end the |
| 149 | reference count hasn't changed. The only real reason to use the |
| 150 | reference count is to prevent the object from being deallocated as |
| 151 | long as our variable is pointing to it. If we know that there is at |
| 152 | least one other reference to the object that lives at least as long as |
| 153 | our variable, there is no need to increment the reference count |
| 154 | temporarily. An important situation where this arises is in objects |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 155 | 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] | 156 | 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] | 157 | reference to every argument for the duration of the call. |
| 158 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 159 | However, a common pitfall is to extract an object from a list and |
| 160 | hold on to it for a while without incrementing its reference count. |
| 161 | Some other operation might conceivably remove the object from the |
| 162 | list, decrementing its reference count and possible deallocating it. |
| 163 | The real danger is that innocent-looking operations may invoke |
| 164 | arbitrary Python code which could do this; there is a code path which |
| 165 | allows control to flow back to the user from a \cfunction{Py_DECREF()}, |
| 166 | so almost any operation is potentially dangerous. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 168 | A safe approach is to always use the generic operations (functions |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 169 | whose name begins with \samp{PyObject_}, \samp{PyNumber_}, |
| 170 | \samp{PySequence_} or \samp{PyMapping_}). These operations always |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 171 | increment the reference count of the object they return. This leaves |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 172 | the caller with the responsibility to call \cfunction{Py_DECREF()} |
| 173 | when they are done with the result; this soon becomes second nature. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 175 | \subsubsection{Reference Count Details} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 176 | \label{refcountDetails} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 177 | |
| 178 | The reference count behavior of functions in the Python/C API is best |
| 179 | expelained in terms of \emph{ownership of references}. Note that we |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 180 | talk of owning references, never of owning objects; objects are always |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 181 | 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] | 182 | properly --- either by passing ownership on (usually to its caller) or |
| 183 | by calling \cfunction{Py_DECREF()} or \cfunction{Py_XDECREF()}. When |
| 184 | a function passes ownership of a reference on to its caller, the |
| 185 | caller is said to receive a \emph{new} reference. When no ownership |
| 186 | is transferred, the caller is said to \emph{borrow} the reference. |
| 187 | Nothing needs to be done for a borrowed reference. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 189 | Conversely, when calling a function passes it a reference to an |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 190 | object, there are two possibilities: the function \emph{steals} a |
| 191 | reference to the object, or it does not. Few functions steal |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 192 | references; the two notable exceptions are |
| 193 | \cfunction{PyList_SetItem()} and \cfunction{PyTuple_SetItem()}, which |
| 194 | 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] | 195 | the item is put!). These functions were designed to steal a reference |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 196 | because of a common idiom for populating a tuple or list with newly |
| 197 | created objects; for example, the code to create the tuple \code{(1, |
| 198 | 2, "three")} could look like this (forgetting about error handling for |
| 199 | the moment; a better way to code this is shown below anyway): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 200 | |
| 201 | \begin{verbatim} |
| 202 | PyObject *t; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 204 | t = PyTuple_New(3); |
| 205 | PyTuple_SetItem(t, 0, PyInt_FromLong(1L)); |
| 206 | PyTuple_SetItem(t, 1, PyInt_FromLong(2L)); |
| 207 | PyTuple_SetItem(t, 2, PyString_FromString("three")); |
| 208 | \end{verbatim} |
| 209 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 210 | Incidentally, \cfunction{PyTuple_SetItem()} is the \emph{only} way to |
| 211 | set tuple items; \cfunction{PySequence_SetItem()} and |
| 212 | \cfunction{PyObject_SetItem()} refuse to do this since tuples are an |
| 213 | immutable data type. You should only use |
| 214 | \cfunction{PyTuple_SetItem()} for tuples that you are creating |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 215 | yourself. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 216 | |
| 217 | Equivalent code for populating a list can be written using |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 218 | \cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code |
| 219 | can also use \cfunction{PySequence_SetItem()}; this illustrates the |
| 220 | difference between the two (the extra \cfunction{Py_DECREF()} calls): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 221 | |
| 222 | \begin{verbatim} |
| 223 | PyObject *l, *x; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 225 | l = PyList_New(3); |
| 226 | x = PyInt_FromLong(1L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 227 | PySequence_SetItem(l, 0, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 228 | x = PyInt_FromLong(2L); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 229 | PySequence_SetItem(l, 1, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 230 | x = PyString_FromString("three"); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 231 | PySequence_SetItem(l, 2, x); Py_DECREF(x); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 232 | \end{verbatim} |
| 233 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 234 | You might find it strange that the ``recommended'' approach takes more |
| 235 | code. However, in practice, you will rarely use these ways of |
| 236 | creating and populating a tuple or list. There's a generic function, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 237 | \cfunction{Py_BuildValue()}, that can create most common objects from |
| 238 | \C{} values, directed by a \dfn{format string}. For example, the |
| 239 | above two blocks of code could be replaced by the following (which |
| 240 | also takes care of the error checking): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 241 | |
| 242 | \begin{verbatim} |
| 243 | PyObject *t, *l; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 245 | t = Py_BuildValue("(iis)", 1, 2, "three"); |
| 246 | l = Py_BuildValue("[iis]", 1, 2, "three"); |
| 247 | \end{verbatim} |
| 248 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 249 | It is much more common to use \cfunction{PyObject_SetItem()} and |
| 250 | friends with items whose references you are only borrowing, like |
| 251 | arguments that were passed in to the function you are writing. In |
| 252 | that case, their behaviour regarding reference counts is much saner, |
| 253 | since you don't have to increment a reference count so you can give a |
| 254 | reference away (``have it be stolen''). For example, this function |
| 255 | sets all items of a list (actually, any mutable sequence) to a given |
| 256 | item: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 257 | |
| 258 | \begin{verbatim} |
| 259 | int set_all(PyObject *target, PyObject *item) |
| 260 | { |
| 261 | int i, n; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 263 | n = PyObject_Length(target); |
| 264 | if (n < 0) |
| 265 | return -1; |
| 266 | for (i = 0; i < n; i++) { |
| 267 | if (PyObject_SetItem(target, i, item) < 0) |
| 268 | return -1; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | \end{verbatim} |
| 273 | |
| 274 | The situation is slightly different for function return values. |
| 275 | While passing a reference to most functions does not change your |
| 276 | ownership responsibilities for that reference, many functions that |
| 277 | return a referece to an object give you ownership of the reference. |
| 278 | The reason is simple: in many cases, the returned object is created |
| 279 | 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] | 280 | object. Therefore, the generic functions that return object |
| 281 | references, like \cfunction{PyObject_GetItem()} and |
| 282 | \cfunction{PySequence_GetItem()}, always return a new reference (i.e., |
| 283 | the caller becomes the owner of the reference). |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 284 | |
| 285 | It is important to realize that whether you own a reference returned |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 286 | by a function depends on which function you call only --- \emph{the |
| 287 | plumage} (i.e., the type of the type of the object passed as an |
| 288 | argument to the function) \emph{doesn't enter into it!} Thus, if you |
| 289 | extract an item from a list using \cfunction{PyList_GetItem()}, you |
| 290 | don't own the reference --- but if you obtain the same item from the |
| 291 | same list using \cfunction{PySequence_GetItem()} (which happens to |
| 292 | take exactly the same arguments), you do own a reference to the |
| 293 | returned object. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 294 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 295 | 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] | 296 | sum of the items in a list of integers; once using |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 297 | \cfunction{PyList_GetItem()}, once using |
| 298 | \cfunction{PySequence_GetItem()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 299 | |
| 300 | \begin{verbatim} |
| 301 | long sum_list(PyObject *list) |
| 302 | { |
| 303 | int i, n; |
| 304 | long total = 0; |
| 305 | PyObject *item; |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 306 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 307 | n = PyList_Size(list); |
| 308 | if (n < 0) |
| 309 | return -1; /* Not a list */ |
| 310 | for (i = 0; i < n; i++) { |
| 311 | item = PyList_GetItem(list, i); /* Can't fail */ |
| 312 | if (!PyInt_Check(item)) continue; /* Skip non-integers */ |
| 313 | total += PyInt_AsLong(item); |
| 314 | } |
| 315 | return total; |
| 316 | } |
| 317 | \end{verbatim} |
| 318 | |
| 319 | \begin{verbatim} |
| 320 | long sum_sequence(PyObject *sequence) |
| 321 | { |
| 322 | int i, n; |
| 323 | long total = 0; |
| 324 | PyObject *item; |
| 325 | n = PyObject_Size(list); |
| 326 | if (n < 0) |
| 327 | return -1; /* Has no length */ |
| 328 | for (i = 0; i < n; i++) { |
| 329 | item = PySequence_GetItem(list, i); |
| 330 | if (item == NULL) |
| 331 | return -1; /* Not a sequence, or other failure */ |
| 332 | if (PyInt_Check(item)) |
| 333 | total += PyInt_AsLong(item); |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 334 | Py_DECREF(item); /* Discard reference ownership */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 335 | } |
| 336 | return total; |
| 337 | } |
| 338 | \end{verbatim} |
| 339 | |
| 340 | \subsection{Types} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 341 | \label{types} |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 342 | |
| 343 | There are few other data types that play a significant role in |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 344 | the Python/C API; most are simple \C{} types such as \ctype{int}, |
| 345 | \ctype{long}, \ctype{double} and \ctype{char *}. A few structure types |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 346 | are used to describe static tables used to list the functions exported |
| 347 | by a module or the data attributes of a new object type. These will |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 348 | be discussed together with the functions that use them. |
| 349 | |
| 350 | \section{Exceptions} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 351 | \label{exceptions} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 353 | The Python programmer only needs to deal with exceptions if specific |
| 354 | error handling is required; unhandled exceptions are automatically |
| 355 | propagated to the caller, then to the caller's caller, and so on, till |
| 356 | 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] | 357 | user accompanied by a stack traceback. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 358 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 359 | For \C{} programmers, however, error checking always has to be explicit. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 360 | All functions in the Python/C API can raise exceptions, unless an |
| 361 | explicit claim is made otherwise in a function's documentation. In |
| 362 | general, when a function encounters an error, it sets an exception, |
| 363 | discards any object references that it owns, and returns an |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 364 | error indicator --- usually \NULL{} or \code{-1}. A few functions |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 365 | return a Boolean true/false result, with false indicating an error. |
| 366 | Very few functions return no explicit error indicator or have an |
| 367 | ambiguous return value, and require explicit testing for errors with |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 368 | \cfunction{PyErr_Occurred()}. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 369 | |
| 370 | Exception state is maintained in per-thread storage (this is |
| 371 | equivalent to using global storage in an unthreaded application). A |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 372 | 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] | 373 | The function \cfunction{PyErr_Occurred()} can be used to check for |
| 374 | this: it returns a borrowed reference to the exception type object |
| 375 | when an exception has occurred, and \NULL{} otherwise. There are a |
| 376 | number of functions to set the exception state: |
| 377 | \cfunction{PyErr_SetString()} is the most common (though not the most |
| 378 | general) function to set the exception state, and |
| 379 | \cfunction{PyErr_Clear()} clears the exception state. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 380 | |
| 381 | The full exception state consists of three objects (all of which can |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 382 | be \NULL{}): the exception type, the corresponding exception |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 383 | value, and the traceback. These have the same meanings as the Python |
| 384 | object \code{sys.exc_type}, \code{sys.exc_value}, |
| 385 | \code{sys.exc_traceback}; however, they are not the same: the Python |
| 386 | objects represent the last exception being handled by a Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 387 | \keyword{try} \ldots\ \keyword{except} statement, while the \C{} level |
| 388 | exception state only exists while an exception is being passed on |
| 389 | between \C{} functions until it reaches the Python interpreter, which |
| 390 | takes care of transferring it to \code{sys.exc_type} and friends. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 391 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 392 | Note that starting with Python 1.5, the preferred, thread-safe way to |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 393 | access the exception state from Python code is to call the function |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 394 | \function{sys.exc_info()}, which returns the per-thread exception state |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 395 | for Python code. Also, the semantics of both ways to access the |
| 396 | exception state have changed so that a function which catches an |
| 397 | exception will save and restore its thread's exception state so as to |
| 398 | preserve the exception state of its caller. This prevents common bugs |
| 399 | in exception handling code caused by an innocent-looking function |
| 400 | overwriting the exception being handled; it also reduces the often |
| 401 | unwanted lifetime extension for objects that are referenced by the |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 402 | stack frames in the traceback. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 403 | |
| 404 | As a general principle, a function that calls another function to |
| 405 | perform some task should check whether the called function raised an |
| 406 | exception, and if so, pass the exception state on to its caller. It |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 407 | should discard any object references that it owns, and returns an |
| 408 | error indicator, but it should \emph{not} set another exception --- |
| 409 | that would overwrite the exception that was just raised, and lose |
| 410 | important information about the exact cause of the error. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 411 | |
| 412 | A simple example of detecting exceptions and passing them on is shown |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 413 | in the \cfunction{sum_sequence()} example above. It so happens that |
| 414 | that example doesn't need to clean up any owned references when it |
| 415 | detects an error. The following example function shows some error |
| 416 | cleanup. First, to remind you why you like Python, we show the |
| 417 | equivalent Python code: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 418 | |
| 419 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 420 | def incr_item(dict, key): |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 421 | try: |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 422 | item = dict[key] |
| 423 | except KeyError: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 424 | item = 0 |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 425 | return item + 1 |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 426 | \end{verbatim} |
| 427 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 428 | Here is the corresponding \C{} code, in all its glory: |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 430 | \begin{verbatim} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 431 | int incr_item(PyObject *dict, PyObject *key) |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 432 | { |
| 433 | /* Objects all initialized to NULL for Py_XDECREF */ |
| 434 | PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL; |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 435 | int rv = -1; /* Return value initialized to -1 (failure) */ |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 437 | item = PyObject_GetItem(dict, key); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 438 | if (item == NULL) { |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 439 | /* Handle KeyError only: */ |
| 440 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 441 | |
| 442 | /* Clear the error and use zero: */ |
| 443 | PyErr_Clear(); |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 444 | item = PyInt_FromLong(0L); |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 445 | if (item == NULL) goto error; |
| 446 | } |
| 447 | |
| 448 | const_one = PyInt_FromLong(1L); |
| 449 | if (const_one == NULL) goto error; |
| 450 | |
| 451 | incremented_item = PyNumber_Add(item, const_one); |
| 452 | if (incremented_item == NULL) goto error; |
| 453 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 454 | if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error; |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 455 | rv = 0; /* Success */ |
| 456 | /* Continue with cleanup code */ |
| 457 | |
| 458 | error: |
| 459 | /* Cleanup code, shared by success and failure path */ |
| 460 | |
| 461 | /* Use Py_XDECREF() to ignore NULL references */ |
| 462 | Py_XDECREF(item); |
| 463 | Py_XDECREF(const_one); |
| 464 | Py_XDECREF(incremented_item); |
| 465 | |
| 466 | return rv; /* -1 for error, 0 for success */ |
| 467 | } |
| 468 | \end{verbatim} |
| 469 | |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 470 | This example represents an endorsed use of the \keyword{goto} statement |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 471 | in \C{}! It illustrates the use of |
| 472 | \cfunction{PyErr_ExceptionMatches()} and \cfunction{PyErr_Clear()} to |
| 473 | handle specific exceptions, and the use of \cfunction{Py_XDECREF()} to |
| 474 | dispose of owned references that may be \NULL{} (note the \samp{X} in |
| 475 | the name; \cfunction{Py_DECREF()} would crash when confronted with a |
| 476 | \NULL{} reference). It is important that the variables used to hold |
| 477 | owned references are initialized to \NULL{} for this to work; |
| 478 | likewise, the proposed return value is initialized to \code{-1} |
| 479 | (failure) and only set to success after the final call made is |
| 480 | successful. |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 481 | |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 482 | |
| 483 | \section{Embedding Python} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 484 | \label{embedding} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 486 | The one important task that only embedders (as opposed to extension |
| 487 | writers) of the Python interpreter have to worry about is the |
| 488 | initialization, and possibly the finalization, of the Python |
| 489 | interpreter. Most functionality of the interpreter can only be used |
| 490 | after the interpreter has been initialized. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 491 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 492 | The basic initialization function is \cfunction{Py_Initialize()}. |
| 493 | This initializes the table of loaded modules, and creates the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 494 | fundamental modules \module{__builtin__}\refbimodindex{__builtin__}, |
| 495 | \module{__main__}\refbimodindex{__main__} and |
| 496 | \module{sys}\refbimodindex{sys}. It also initializes the module |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 497 | search path (\code{sys.path}).% |
| 498 | \indexiii{module}{search}{path} |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 499 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 500 | \cfunction{Py_Initialize()} does not set the ``script argument list'' |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 501 | (\code{sys.argv}). If this variable is needed by Python code that |
| 502 | will be executed later, it must be set explicitly with a call to |
| 503 | \code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 504 | to \cfunction{Py_Initialize()}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 505 | |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 506 | On most systems (in particular, on \UNIX{} and Windows, although the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 507 | details are slightly different), \cfunction{Py_Initialize()} |
| 508 | calculates the module search path based upon its best guess for the |
| 509 | location of the standard Python interpreter executable, assuming that |
| 510 | the Python library is found in a fixed location relative to the Python |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 511 | interpreter executable. In particular, it looks for a directory named |
Fred Drake | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 512 | \file{lib/python1.5} (replacing \file{1.5} with the current |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 513 | interpreter version) relative to the parent directory where the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 514 | executable named \file{python} is found on the shell command search |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 515 | path (the environment variable \envvar{PATH}). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 516 | |
| 517 | For instance, if the Python executable is found in |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 518 | \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] | 519 | \file{/usr/local/lib/python1.5}. (In fact, this particular path |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 520 | is also the ``fallback'' location, used when no executable file named |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 521 | \file{python} is found along \envvar{PATH}.) The user can override |
| 522 | this behavior by setting the environment variable \envvar{PYTHONHOME}, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 523 | or insert additional directories in front of the standard path by |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 524 | setting \envvar{PYTHONPATH}. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 526 | The embedding application can steer the search by calling |
| 527 | \code{Py_SetProgramName(\var{file})} \emph{before} calling |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 528 | \cfunction{Py_Initialize()}. Note that \envvar{PYTHONHOME} still |
| 529 | overrides this and \envvar{PYTHONPATH} is still inserted in front of |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 530 | the standard path. An application that requires total control has to |
| 531 | provide its own implementation of \cfunction{Py_GetPath()}, |
| 532 | \cfunction{Py_GetPrefix()}, \cfunction{Py_GetExecPrefix()}, |
| 533 | \cfunction{Py_GetProgramFullPath()} (all defined in |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 534 | \file{Modules/getpath.c}). |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 536 | Sometimes, it is desirable to ``uninitialize'' Python. For instance, |
| 537 | the application may want to start over (make another call to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 538 | \cfunction{Py_Initialize()}) or the application is simply done with its |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 539 | 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] | 540 | can be accomplished by calling \cfunction{Py_Finalize()}. The function |
| 541 | \cfunction{Py_IsInitialized()} returns true iff Python is currently in the |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 542 | initialized state. More information about these functions is given in |
| 543 | a later chapter. |
Guido van Rossum | 59a6135 | 1997-08-14 20:34:33 +0000 | [diff] [blame] | 544 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 545 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 546 | \chapter{The Very High Level Layer} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 547 | \label{veryhigh} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 548 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 549 | The functions in this chapter will let you execute Python source code |
| 550 | given in a file or a buffer, but they will not let you interact in a |
| 551 | more detailed way with the interpreter. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 552 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 553 | \begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 554 | \end{cfuncdesc} |
| 555 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 556 | \begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 557 | \end{cfuncdesc} |
| 558 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 559 | \begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 560 | \end{cfuncdesc} |
| 561 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 562 | \begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 563 | \end{cfuncdesc} |
| 564 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 565 | \begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 566 | \end{cfuncdesc} |
| 567 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 568 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str, |
| 569 | int start} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 570 | \end{cfuncdesc} |
| 571 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 572 | \begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp, |
| 573 | char *filename, int start} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 574 | \end{cfuncdesc} |
| 575 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 576 | \begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start, |
| 577 | PyObject *globals, |
| 578 | PyObject *locals} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 579 | \end{cfuncdesc} |
| 580 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 581 | \begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename, |
| 582 | int start, PyObject *globals, |
| 583 | PyObject *locals} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 584 | \end{cfuncdesc} |
| 585 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 586 | \begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename, |
| 587 | int start} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 588 | \end{cfuncdesc} |
| 589 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 590 | |
| 591 | \chapter{Reference Counting} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 592 | \label{countingRefs} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 594 | The macros in this section are used for managing reference counts |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 595 | of Python objects. |
| 596 | |
| 597 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 598 | Increment the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 599 | 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] | 600 | \cfunction{Py_XINCREF()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 601 | \end{cfuncdesc} |
| 602 | |
| 603 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 604 | 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] | 605 | \NULL{}, in which case the macro has no effect. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 606 | \end{cfuncdesc} |
| 607 | |
| 608 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 609 | Decrement the reference count for object \var{o}. The object must |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 610 | 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] | 611 | \cfunction{Py_XDECREF()}. If the reference count reaches zero, the |
| 612 | object's type's deallocation function (which must not be \NULL{}) is |
| 613 | invoked. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 614 | |
| 615 | \strong{Warning:} The deallocation function can cause arbitrary Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 616 | code to be invoked (e.g. when a class instance with a \method{__del__()} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 617 | method is deallocated). While exceptions in such code are not |
| 618 | propagated, the executed code has free access to all Python global |
| 619 | variables. This means that any object that is reachable from a global |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 620 | variable should be in a consistent state before \cfunction{Py_DECREF()} is |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 621 | invoked. For example, code to delete an object from a list should |
| 622 | copy a reference to the deleted object in a temporary variable, update |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 623 | the list data structure, and then call \cfunction{Py_DECREF()} for the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 624 | temporary variable. |
| 625 | \end{cfuncdesc} |
| 626 | |
| 627 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 628 | Decrement the reference count for object \var{o}. The object may be |
| 629 | \NULL{}, in which case the macro has no effect; otherwise the effect |
| 630 | 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] | 631 | applies. |
| 632 | \end{cfuncdesc} |
| 633 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 634 | The following functions or macros are only for internal use: |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 635 | \cfunction{_Py_Dealloc()}, \cfunction{_Py_ForgetReference()}, |
| 636 | \cfunction{_Py_NewReference()}, as well as the global variable |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 637 | \cdata{_Py_RefTotal}. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 639 | XXX Should mention Py_Malloc(), Py_Realloc(), Py_Free(), |
| 640 | PyMem_Malloc(), PyMem_Realloc(), PyMem_Free(), PyMem_NEW(), |
| 641 | PyMem_RESIZE(), PyMem_DEL(), PyMem_XDEL(). |
| 642 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 643 | |
| 644 | \chapter{Exception Handling} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 645 | \label{exceptionHandling} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 646 | |
| 647 | The functions in this chapter will let you handle and raise Python |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 648 | exceptions. It is important to understand some of the basics of |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 649 | Python exception handling. It works somewhat like the \UNIX{} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 650 | \cdata{errno} variable: there is a global indicator (per thread) of the |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 651 | last error that occurred. Most functions don't clear this on success, |
| 652 | but will set it to indicate the cause of the error on failure. Most |
| 653 | functions also return an error indicator, usually \NULL{} if they are |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 654 | supposed to return a pointer, or \code{-1} if they return an integer |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 655 | (exception: the \cfunction{PyArg_Parse*()} functions return \code{1} for |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 656 | success and \code{0} for failure). When a function must fail because |
| 657 | some function it called failed, it generally doesn't set the error |
| 658 | indicator; the function it called already set it. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 659 | |
| 660 | The error indicator consists of three Python objects corresponding to |
| 661 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and |
| 662 | \code{sys.exc_traceback}. API functions exist to interact with the |
| 663 | error indicator in various ways. There is a separate error indicator |
| 664 | for each thread. |
| 665 | |
| 666 | % XXX Order of these should be more thoughtful. |
| 667 | % Either alphabetical or some kind of structure. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 668 | |
| 669 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 670 | Print a standard traceback to \code{sys.stderr} and clear the error |
| 671 | indicator. Call this function only when the error indicator is set. |
| 672 | (Otherwise it will cause a fatal error!) |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 673 | \end{cfuncdesc} |
| 674 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 675 | \begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 676 | Test whether the error indicator is set. If set, return the exception |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 677 | \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] | 678 | \cfunction{PyErr_Set*()} functions or to \cfunction{PyErr_Restore()}). If |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 679 | not set, return \NULL{}. You do not own a reference to the return |
| 680 | value, so you do not need to \cfunction{Py_DECREF()} it. |
| 681 | \strong{Note:} do not compare the return value to a specific |
| 682 | exception; use \cfunction{PyErr_ExceptionMatches()} instead, shown |
| 683 | below. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 684 | \end{cfuncdesc} |
| 685 | |
| 686 | \begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 687 | Equivalent to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 688 | \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(), \var{exc})}. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 689 | This should only be called when an exception is actually set. |
| 690 | \end{cfuncdesc} |
| 691 | |
| 692 | \begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 693 | Return true if the \var{given} exception matches the exception in |
| 694 | \var{exc}. If \var{exc} is a class object, this also returns true |
| 695 | when \var{given} is a subclass. If \var{exc} is a tuple, all |
| 696 | exceptions in the tuple (and recursively in subtuples) are searched |
| 697 | for a match. This should only be called when an exception is actually |
| 698 | set. |
| 699 | \end{cfuncdesc} |
| 700 | |
| 701 | \begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 702 | Under certain circumstances, the values returned by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 703 | \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning that |
| 704 | \code{*\var{exc}} is a class object but \code{*\var{val}} is not an |
| 705 | instance of the same class. This function can be used to instantiate |
| 706 | the class in that case. If the values are already normalized, nothing |
| 707 | happens. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 708 | \end{cfuncdesc} |
| 709 | |
| 710 | \begin{cfuncdesc}{void}{PyErr_Clear}{} |
| 711 | Clear the error indicator. If the error indicator is not set, there |
| 712 | is no effect. |
| 713 | \end{cfuncdesc} |
| 714 | |
| 715 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback} |
| 716 | Retrieve the error indicator into three variables whose addresses are |
| 717 | passed. If the error indicator is not set, set all three variables to |
| 718 | \NULL{}. If it is set, it will be cleared and you own a reference to |
| 719 | each object retrieved. The value and traceback object may be \NULL{} |
| 720 | even when the type object is not. \strong{Note:} this function is |
| 721 | normally only used by code that needs to handle exceptions or by code |
| 722 | that needs to save and restore the error indicator temporarily. |
| 723 | \end{cfuncdesc} |
| 724 | |
| 725 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback} |
| 726 | Set the error indicator from the three objects. If the error |
| 727 | indicator is already set, it is cleared first. If the objects are |
| 728 | \NULL{}, the error indicator is cleared. Do not pass a \NULL{} type |
| 729 | and non-\NULL{} value or traceback. The exception type should be a |
| 730 | string or class; if it is a class, the value should be an instance of |
| 731 | that class. Do not pass an invalid exception type or value. |
| 732 | (Violating these rules will cause subtle problems later.) This call |
| 733 | takes away a reference to each object, i.e. you must own a reference |
| 734 | to each object before the call and after the call you no longer own |
| 735 | these references. (If you don't understand this, don't use this |
| 736 | function. I warned you.) \strong{Note:} this function is normally |
| 737 | only used by code that needs to save and restore the error indicator |
| 738 | temporarily. |
| 739 | \end{cfuncdesc} |
| 740 | |
| 741 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message} |
| 742 | This is the most common way to set the error indicator. The first |
| 743 | argument specifies the exception type; it is normally one of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 744 | standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 745 | increment its reference count. The second argument is an error |
| 746 | message; it is converted to a string object. |
| 747 | \end{cfuncdesc} |
| 748 | |
| 749 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 750 | This function is similar to \cfunction{PyErr_SetString()} but lets you |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 751 | specify an arbitrary Python object for the ``value'' of the exception. |
| 752 | You need not increment its reference count. |
| 753 | \end{cfuncdesc} |
| 754 | |
| 755 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 756 | 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] | 757 | \end{cfuncdesc} |
| 758 | |
| 759 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 760 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 761 | \var{message})}, where \var{message} indicates that a built-in operation |
| 762 | was invoked with an illegal argument. It is mostly for internal use. |
| 763 | \end{cfuncdesc} |
| 764 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 765 | \begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 766 | This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 767 | returns \NULL{} so an object allocation function can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 768 | \samp{return PyErr_NoMemory();} when it runs out of memory. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 769 | \end{cfuncdesc} |
| 770 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 771 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type} |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 772 | This is a convenience function to raise an exception when a \C{} library |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 773 | 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] | 774 | It constructs a tuple object whose first item is the integer |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 775 | \cdata{errno} value and whose second item is the corresponding error |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 776 | message (gotten from \cfunction{strerror()}), and then calls |
| 777 | \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 778 | the \cdata{errno} value is \constant{EINTR}, indicating an interrupted |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 779 | system call, this calls \cfunction{PyErr_CheckSignals()}, and if that set |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 780 | the error indicator, leaves it set to that. The function always |
| 781 | returns \NULL{}, so a wrapper function around a system call can write |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 782 | \samp{return PyErr_SetFromErrno();} when the system call returns an |
| 783 | error. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 784 | \end{cfuncdesc} |
| 785 | |
| 786 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 787 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 788 | \var{message})}, where \var{message} indicates that an internal |
Guido van Rossum | 5060b3b | 1997-08-17 18:02:23 +0000 | [diff] [blame] | 789 | 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] | 790 | argument. It is mostly for internal use. |
| 791 | \end{cfuncdesc} |
| 792 | |
| 793 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{} |
| 794 | This function interacts with Python's signal handling. It checks |
| 795 | 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] | 796 | corresponding signal handler. If the |
| 797 | \module{signal}\refbimodindex{signal} module is supported, this can |
| 798 | invoke a signal handler written in Python. In all cases, the default |
| 799 | effect for \constant{SIGINT} is to raise the |
| 800 | \exception{KeyboadInterrupt} exception. If an exception is raised the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 801 | error indicator is set and the function returns \code{1}; otherwise |
| 802 | the function returns \code{0}. The error indicator may or may not be |
| 803 | cleared if it was previously set. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 804 | \end{cfuncdesc} |
| 805 | |
| 806 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{} |
| 807 | This function is obsolete (XXX or platform dependent?). It simulates |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 808 | the effect of a \constant{SIGINT} signal arriving --- the next time |
| 809 | \cfunction{PyErr_CheckSignals()} is called, |
| 810 | \exception{KeyboadInterrupt} will be raised. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 811 | \end{cfuncdesc} |
| 812 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 813 | \begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name, |
| 814 | PyObject *base, |
| 815 | PyObject *dict} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 816 | This utility function creates and returns a new exception object. The |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 817 | \var{name} argument must be the name of the new exception, a \C{} string |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 818 | of the form \code{module.class}. The \var{base} and \var{dict} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 819 | arguments are normally \NULL{}. Normally, this creates a class |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 820 | object derived from the root for all exceptions, the built-in name |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 821 | \exception{Exception} (accessible in \C{} as \cdata{PyExc_Exception}). |
| 822 | In this case the \member{__module__} attribute of the new class is set to the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 823 | first part (up to the last dot) of the \var{name} argument, and the |
| 824 | class name is set to the last part (after the last dot). When the |
| 825 | user has specified the \code{-X} command line option to use string |
| 826 | exceptions, for backward compatibility, or when the \var{base} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 827 | argument is not a class object (and not \NULL{}), a string object |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 828 | created from the entire \var{name} argument is returned. The |
| 829 | \var{base} argument can be used to specify an alternate base class. |
| 830 | The \var{dict} argument can be used to specify a dictionary of class |
| 831 | variables and methods. |
| 832 | \end{cfuncdesc} |
| 833 | |
| 834 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 835 | \section{Standard Exceptions} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 836 | \label{standardExceptions} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 837 | |
| 838 | All standard Python exceptions are available as global variables whose |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 839 | names are \samp{PyExc_} followed by the Python exception name. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 840 | These have the type \ctype{PyObject *}; they are all either class |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 841 | objects or string objects, depending on the use of the \code{-X} |
| 842 | option to the interpreter. For completeness, here are all the |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 843 | variables: |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 844 | \cdata{PyExc_Exception}, |
| 845 | \cdata{PyExc_StandardError}, |
| 846 | \cdata{PyExc_ArithmeticError}, |
| 847 | \cdata{PyExc_LookupError}, |
| 848 | \cdata{PyExc_AssertionError}, |
| 849 | \cdata{PyExc_AttributeError}, |
| 850 | \cdata{PyExc_EOFError}, |
| 851 | \cdata{PyExc_FloatingPointError}, |
| 852 | \cdata{PyExc_IOError}, |
| 853 | \cdata{PyExc_ImportError}, |
| 854 | \cdata{PyExc_IndexError}, |
| 855 | \cdata{PyExc_KeyError}, |
| 856 | \cdata{PyExc_KeyboardInterrupt}, |
| 857 | \cdata{PyExc_MemoryError}, |
| 858 | \cdata{PyExc_NameError}, |
| 859 | \cdata{PyExc_OverflowError}, |
| 860 | \cdata{PyExc_RuntimeError}, |
| 861 | \cdata{PyExc_SyntaxError}, |
| 862 | \cdata{PyExc_SystemError}, |
| 863 | \cdata{PyExc_SystemExit}, |
| 864 | \cdata{PyExc_TypeError}, |
| 865 | \cdata{PyExc_ValueError}, |
| 866 | \cdata{PyExc_ZeroDivisionError}. |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 867 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 868 | |
| 869 | \chapter{Utilities} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 870 | \label{utilities} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 871 | |
| 872 | The functions in this chapter perform various utility tasks, such as |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 873 | parsing function arguments and constructing Python values from \C{} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 874 | values. |
| 875 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 876 | \section{OS Utilities} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 877 | \label{os} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 878 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 879 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 880 | Return true (nonzero) if the standard I/O file \var{fp} with name |
| 881 | \var{filename} is deemed interactive. This is the case for files for |
| 882 | which \samp{isatty(fileno(\var{fp}))} is true. If the global flag |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 883 | \cdata{Py_InteractiveFlag} is true, this function also returns true if |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 884 | the \var{name} pointer is \NULL{} or if the name is equal to one of |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 885 | the strings \code{"<stdin>"} or \code{"???"}. |
| 886 | \end{cfuncdesc} |
| 887 | |
| 888 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 889 | Return the time of last modification of the file \var{filename}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 890 | The result is encoded in the same way as the timestamp returned by |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 891 | the standard \C{} library function \cfunction{time()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 892 | \end{cfuncdesc} |
| 893 | |
| 894 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 895 | \section{Process Control} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 896 | \label{processControl} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 897 | |
| 898 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 899 | Print a fatal error message and kill the process. No cleanup is |
| 900 | performed. This function should only be invoked when a condition is |
| 901 | detected that would make it dangerous to continue using the Python |
| 902 | interpreter; e.g., when the object administration appears to be |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 903 | corrupted. On \UNIX{}, the standard \C{} library function |
| 904 | \cfunction{abort()} is called which will attempt to produce a |
| 905 | \file{core} file. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 906 | \end{cfuncdesc} |
| 907 | |
| 908 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 909 | Exit the current process. This calls \cfunction{Py_Finalize()} and |
| 910 | then calls the standard \C{} library function |
| 911 | \code{exit(\var{status})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 912 | \end{cfuncdesc} |
| 913 | |
| 914 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
| 915 | Register a cleanup function to be called by \cfunction{Py_Finalize()}. |
| 916 | The cleanup function will be called with no arguments and should |
| 917 | return no value. At most 32 cleanup functions can be registered. |
| 918 | When the registration is successful, \cfunction{Py_AtExit()} returns |
| 919 | \code{0}; on failure, it returns \code{-1}. The cleanup function |
| 920 | registered last is called first. Each cleanup function will be called |
| 921 | at most once. Since Python's internal finallization will have |
| 922 | completed before the cleanup function, no Python APIs should be called |
| 923 | by \var{func}. |
| 924 | \end{cfuncdesc} |
| 925 | |
| 926 | |
| 927 | \section{Importing Modules} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 928 | \label{importing} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 929 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 930 | \begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 931 | This is a simplified interface to \cfunction{PyImport_ImportModuleEx()} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 932 | below, leaving the \var{globals} and \var{locals} arguments set to |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 933 | \NULL{}. When the \var{name} argument contains a dot (i.e., when |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 934 | it specifies a submodule of a package), the \var{fromlist} argument is |
| 935 | set to the list \code{['*']} so that the return value is the named |
| 936 | module rather than the top-level package containing it as would |
| 937 | otherwise be the case. (Unfortunately, this has an additional side |
| 938 | effect when \var{name} in fact specifies a subpackage instead of a |
| 939 | submodule: the submodules specified in the package's \code{__all__} |
| 940 | variable are loaded.) Return a new reference to the imported module, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 941 | or \NULL{} with an exception set on failure (the module may still |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 942 | be created in this case --- examine \code{sys.modules} to find out). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 943 | \end{cfuncdesc} |
| 944 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 945 | \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] | 946 | 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] | 947 | Python function \function{__import__()}\bifuncindex{__import__}, as |
| 948 | the standard \function{__import__()} function calls this function |
| 949 | directly. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 950 | |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 951 | 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] | 952 | top-level package, or \NULL{} with an exception set on failure |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 953 | (the module may still be created in this case). Like for |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 954 | \function{__import__()}, the return value when a submodule of a |
| 955 | package was requested is normally the top-level package, unless a |
| 956 | non-empty \var{fromlist} was given. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 957 | \end{cfuncdesc} |
| 958 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 959 | \begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 960 | This is a higher-level interface that calls the current ``import hook |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 961 | function''. It invokes the \function{__import__()} function from the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 962 | \code{__builtins__} of the current globals. This means that the |
| 963 | import is done using whatever import hooks are installed in the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 964 | current environment, e.g. by \module{rexec}\refstmodindex{rexec} or |
| 965 | \module{ihooks}\refstmodindex{ihooks}. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 966 | \end{cfuncdesc} |
| 967 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 968 | \begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 969 | 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] | 970 | Python function \function{reload()}\bifuncindex{reload}, as the standard |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 971 | \function{reload()} function calls this function directly. Return a |
| 972 | new reference to the reloaded module, or \NULL{} with an exception set |
| 973 | on failure (the module still exists in this case). |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 974 | \end{cfuncdesc} |
| 975 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 976 | \begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 977 | Return the module object corresponding to a module name. The |
| 978 | \var{name} argument may be of the form \code{package.module}). First |
| 979 | check the modules dictionary if there's one there, and if not, create |
| 980 | a new one and insert in in the modules dictionary. Because the former |
| 981 | action is most common, this does not return a new reference, and you |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 982 | do not own the returned reference. Return \NULL{} with an |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 983 | exception set on failure. \strong{Note:} this function returns |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 984 | a ``borrowed'' reference. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 985 | \end{cfuncdesc} |
| 986 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 987 | \begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 988 | Given a module name (possibly of the form \code{package.module}) and a |
| 989 | code object read from a Python bytecode file or obtained from the |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 990 | built-in function \function{compile()}\bifuncindex{compile}, load the |
| 991 | module. Return a new reference to the module object, or \NULL{} with |
| 992 | an exception set if an error occurred (the module may still be created |
| 993 | in this case). (This function would reload the module if it was |
| 994 | already imported.) |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 995 | \end{cfuncdesc} |
| 996 | |
| 997 | \begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 998 | Return the magic number for Python bytecode files (a.k.a. \file{.pyc} |
| 999 | and \file{.pyo} files). The magic number should be present in the |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1000 | first four bytes of the bytecode file, in little-endian byte order. |
| 1001 | \end{cfuncdesc} |
| 1002 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1003 | \begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1004 | Return the dictionary used for the module administration |
| 1005 | (a.k.a. \code{sys.modules}). Note that this is a per-interpreter |
| 1006 | variable. |
| 1007 | \end{cfuncdesc} |
| 1008 | |
| 1009 | \begin{cfuncdesc}{void}{_PyImport_Init}{} |
| 1010 | Initialize the import mechanism. For internal use only. |
| 1011 | \end{cfuncdesc} |
| 1012 | |
| 1013 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 1014 | Empty the module table. For internal use only. |
| 1015 | \end{cfuncdesc} |
| 1016 | |
| 1017 | \begin{cfuncdesc}{void}{_PyImport_Fini}{} |
| 1018 | Finalize the import mechanism. For internal use only. |
| 1019 | \end{cfuncdesc} |
| 1020 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1021 | \begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1022 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1023 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1024 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1025 | \begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1026 | For internal use only. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 1027 | \end{cfuncdesc} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | \begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *} |
| 1030 | Load a frozen module. Return \code{1} for success, \code{0} if the |
| 1031 | module is not found, and \code{-1} with an exception set if the |
| 1032 | initialization failed. To access the imported module on a successful |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1033 | load, use \cfunction{PyImport_ImportModule()}. |
| 1034 | (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] | 1035 | already imported.) |
| 1036 | \end{cfuncdesc} |
| 1037 | |
| 1038 | \begin{ctypedesc}{struct _frozen} |
| 1039 | This is the structure type definition for frozen module descriptors, |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1040 | as generated by the \program{freeze}\index{freeze utility} utility |
| 1041 | (see \file{Tools/freeze/} in the Python source distribution). Its |
| 1042 | definition is: |
| 1043 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1044 | \begin{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1045 | struct _frozen { |
Fred Drake | 36fbe76 | 1997-10-13 18:18:33 +0000 | [diff] [blame] | 1046 | char *name; |
| 1047 | unsigned char *code; |
| 1048 | int size; |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1049 | }; |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 1050 | \end{verbatim} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1051 | \end{ctypedesc} |
| 1052 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1053 | \begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1054 | This pointer is initialized to point to an array of \ctype{struct |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 1055 | _frozen} records, terminated by one whose members are all \NULL{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 1056 | or zero. When a frozen module is imported, it is searched in this |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1057 | 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] | 1058 | dynamically created collection of frozen modules. |
| 1059 | \end{cvardesc} |
| 1060 | |
| 1061 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1062 | \chapter{Abstract Objects Layer} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1063 | \label{abstract} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1064 | |
| 1065 | The functions in this chapter interact with Python objects regardless |
| 1066 | of their type, or with wide classes of object types (e.g. all |
| 1067 | numerical types, or all sequence types). When used on object types |
| 1068 | for which they do not apply, they will flag a Python exception. |
| 1069 | |
| 1070 | \section{Object Protocol} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1071 | \label{object} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1072 | |
| 1073 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1074 | Print an object \var{o}, on file \var{fp}. Returns \code{-1} on error |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1075 | The flags argument is used to enable certain printing |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1076 | options. The only option currently supported is |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1077 | \constant{Py_PRINT_RAW}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1078 | \end{cfuncdesc} |
| 1079 | |
| 1080 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1081 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1082 | \code{0} otherwise. This is equivalent to the Python expression |
| 1083 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1084 | This function always succeeds. |
| 1085 | \end{cfuncdesc} |
| 1086 | |
| 1087 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1088 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1089 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1090 | This is the equivalent of the Python expression |
| 1091 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1092 | \end{cfuncdesc} |
| 1093 | |
| 1094 | |
| 1095 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1096 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 1097 | \code{0} otherwise. This is equivalent to the Python expression |
| 1098 | \samp{hasattr(\var{o}, \var{attr_name})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1099 | This function always succeeds. |
| 1100 | \end{cfuncdesc} |
| 1101 | |
| 1102 | |
| 1103 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1104 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1105 | Returns the attribute value on success, or \NULL{} on failure. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1106 | This is the equivalent of the Python expression |
| 1107 | \samp{\var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1108 | \end{cfuncdesc} |
| 1109 | |
| 1110 | |
| 1111 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1112 | Set the value of the attribute named \var{attr_name}, for object |
| 1113 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This is |
| 1114 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1115 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1116 | \end{cfuncdesc} |
| 1117 | |
| 1118 | |
| 1119 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1120 | Set the value of the attribute named \var{attr_name}, for |
| 1121 | object \var{o}, |
| 1122 | to the value \var{v}. Returns \code{-1} on failure. This is |
| 1123 | the equivalent of the Python statement \samp{\var{o}.\var{attr_name} = |
| 1124 | \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1125 | \end{cfuncdesc} |
| 1126 | |
| 1127 | |
| 1128 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1129 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1130 | \code{-1} on failure. This is the equivalent of the Python |
| 1131 | statement: \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1132 | \end{cfuncdesc} |
| 1133 | |
| 1134 | |
| 1135 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1136 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 1137 | \code{-1} on failure. This is the equivalent of the Python |
| 1138 | statement \samp{del \var{o}.\var{attr_name}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1139 | \end{cfuncdesc} |
| 1140 | |
| 1141 | |
| 1142 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1143 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1144 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1145 | \var{o2}. The result of the comparison is returned in \var{result}. |
| 1146 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1147 | statement \samp{\var{result} = cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1148 | \end{cfuncdesc} |
| 1149 | |
| 1150 | |
| 1151 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1152 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 1153 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 1154 | \var{o2}. Returns the result of the comparison on success. On error, |
| 1155 | the value returned is undefined; use \cfunction{PyErr_Occurred()} to |
| 1156 | detect an error. This is equivalent to the |
| 1157 | Python expression \samp{cmp(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1158 | \end{cfuncdesc} |
| 1159 | |
| 1160 | |
| 1161 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1162 | Compute the string representation of object, \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1163 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1164 | the equivalent of the Python expression \samp{repr(\var{o})}. |
| 1165 | Called by the \function{repr()}\bifuncindex{repr} built-in function |
| 1166 | and by reverse quotes. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1167 | \end{cfuncdesc} |
| 1168 | |
| 1169 | |
| 1170 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1171 | Compute the string representation of object \var{o}. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1172 | string representation on success, \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1173 | the equivalent of the Python expression \samp{str(\var{o})}. |
| 1174 | Called by the \function{str()}\bifuncindex{str} built-in function and |
| 1175 | by the \keyword{print} statement. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1176 | \end{cfuncdesc} |
| 1177 | |
| 1178 | |
| 1179 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1180 | Determine if the object \var{o}, is callable. Return \code{1} if the |
| 1181 | object is callable and \code{0} otherwise. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1182 | This function always succeeds. |
| 1183 | \end{cfuncdesc} |
| 1184 | |
| 1185 | |
| 1186 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1187 | Call a callable Python object \var{callable_object}, with |
| 1188 | arguments given by the tuple \var{args}. If no arguments are |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1189 | needed, then args may be \NULL{}. Returns the result of the |
| 1190 | call on success, or \NULL{} on failure. This is the equivalent |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1191 | of the Python expression \samp{apply(\var{o}, \var{args})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1192 | \end{cfuncdesc} |
| 1193 | |
| 1194 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1195 | Call a callable Python object \var{callable_object}, with a |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 1196 | variable number of \C{} arguments. The \C{} arguments are described |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1197 | using a \cfunction{Py_BuildValue()} style format string. The format may |
| 1198 | be \NULL{}, indicating that no arguments are provided. Returns the |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1199 | result of the call on success, or \NULL{} on failure. This is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1200 | the equivalent of the Python expression \samp{apply(\var{o}, |
| 1201 | \var{args})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1202 | \end{cfuncdesc} |
| 1203 | |
| 1204 | |
| 1205 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1206 | Call the method named \var{m} of object \var{o} with a variable number |
| 1207 | of C arguments. The \C{} arguments are described by a |
| 1208 | \cfunction{Py_BuildValue()} format string. The format may be \NULL{}, |
| 1209 | indicating that no arguments are provided. Returns the result of the |
| 1210 | call on success, or \NULL{} on failure. This is the equivalent of the |
| 1211 | Python expression \samp{\var{o}.\var{method}(\var{args})}. |
| 1212 | Note that Special method names, such as \method{__add__()}, |
| 1213 | \method{__getitem__()}, and so on are not supported. The specific |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1214 | abstract-object routines for these must be used. |
| 1215 | \end{cfuncdesc} |
| 1216 | |
| 1217 | |
| 1218 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1219 | Compute and return the hash value of an object \var{o}. On |
| 1220 | failure, return \code{-1}. This is the equivalent of the Python |
| 1221 | expression \samp{hash(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1222 | \end{cfuncdesc} |
| 1223 | |
| 1224 | |
| 1225 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1226 | Returns \code{1} if the object \var{o} is considered to be true, and |
| 1227 | \code{0} otherwise. This is equivalent to the Python expression |
| 1228 | \samp{not not \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1229 | This function always succeeds. |
| 1230 | \end{cfuncdesc} |
| 1231 | |
| 1232 | |
| 1233 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 1234 | On success, returns a type object corresponding to the object |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1235 | type of object \var{o}. On failure, returns \NULL{}. This is |
| 1236 | equivalent to the Python expression \samp{type(\var{o})}. |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1237 | \bifuncindex{type} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1238 | \end{cfuncdesc} |
| 1239 | |
| 1240 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1241 | 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] | 1242 | both sequence and mapping protocols, the sequence length is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1243 | returned. On error, \code{-1} is returned. This is the equivalent |
| 1244 | to the Python expression \samp{len(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1245 | \end{cfuncdesc} |
| 1246 | |
| 1247 | |
| 1248 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1249 | Return element of \var{o} corresponding to the object \var{key} or |
| 1250 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1251 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1252 | \end{cfuncdesc} |
| 1253 | |
| 1254 | |
| 1255 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1256 | Map the object \var{key} to the value \var{v}. |
| 1257 | Returns \code{-1} on failure. This is the equivalent |
| 1258 | of the Python statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1259 | \end{cfuncdesc} |
| 1260 | |
| 1261 | |
| 1262 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1263 | Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on |
| 1264 | failure. This is the equivalent of the Python statement \samp{del |
| 1265 | \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1266 | \end{cfuncdesc} |
| 1267 | |
| 1268 | |
| 1269 | \section{Number Protocol} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1270 | \label{number} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1271 | |
| 1272 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1273 | 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] | 1274 | false otherwise. |
| 1275 | This function always succeeds. |
| 1276 | \end{cfuncdesc} |
| 1277 | |
| 1278 | |
| 1279 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1280 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 1281 | failure. This is the equivalent of the Python expression |
| 1282 | \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1283 | \end{cfuncdesc} |
| 1284 | |
| 1285 | |
| 1286 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1287 | Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{} |
| 1288 | on failure. This is the equivalent of the Python expression |
| 1289 | \samp{\var{o1} - \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1290 | \end{cfuncdesc} |
| 1291 | |
| 1292 | |
| 1293 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1294 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} on |
| 1295 | failure. This is the equivalent of the Python expression |
| 1296 | \samp{\var{o1} * \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1297 | \end{cfuncdesc} |
| 1298 | |
| 1299 | |
| 1300 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1301 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1302 | failure. |
| 1303 | This is the equivalent of the Python expression \samp{\var{o1} / |
| 1304 | \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1305 | \end{cfuncdesc} |
| 1306 | |
| 1307 | |
| 1308 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1309 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 1310 | failure. This is the equivalent of the Python expression |
| 1311 | \samp{\var{o1} \% \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1312 | \end{cfuncdesc} |
| 1313 | |
| 1314 | |
| 1315 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1316 | See the built-in function \function{divmod()}\bifuncindex{divmod}. |
| 1317 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 1318 | expression \samp{divmod(\var{o1}, \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1319 | \end{cfuncdesc} |
| 1320 | |
| 1321 | |
| 1322 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3} |
Fred Drake | 53fb772 | 1998-02-16 06:23:20 +0000 | [diff] [blame] | 1323 | See the built-in function \function{pow()}\bifuncindex{pow}. Returns |
| 1324 | \NULL{} on failure. This is the equivalent of the Python expression |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1325 | \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} is optional. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1326 | If \var{o3} is to be ignored, pass \cdata{Py_None} in its place. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1327 | \end{cfuncdesc} |
| 1328 | |
| 1329 | |
| 1330 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1331 | Returns the negation of \var{o} on success, or \NULL{} on failure. |
| 1332 | This is the equivalent of the Python expression \samp{-\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1333 | \end{cfuncdesc} |
| 1334 | |
| 1335 | |
| 1336 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1337 | Returns \var{o} on success, or \NULL{} on failure. |
| 1338 | This is the equivalent of the Python expression \samp{+\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1339 | \end{cfuncdesc} |
| 1340 | |
| 1341 | |
| 1342 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1343 | Returns the absolute value of \var{o}, or \NULL{} on failure. This is |
| 1344 | the equivalent of the Python expression \samp{abs(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1345 | \end{cfuncdesc} |
| 1346 | |
| 1347 | |
| 1348 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1349 | Returns the bitwise negation of \var{o} on success, or \NULL{} on |
| 1350 | failure. This is the equivalent of the Python expression |
| 1351 | \samp{\~\var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1352 | \end{cfuncdesc} |
| 1353 | |
| 1354 | |
| 1355 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1356 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 1357 | or \NULL{} on failure. This is the equivalent of the Python |
| 1358 | expression \samp{\var{o1} << \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1359 | \end{cfuncdesc} |
| 1360 | |
| 1361 | |
| 1362 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1363 | Returns the result of right shifting \var{o1} by \var{o2} on success, |
| 1364 | or \NULL{} on failure. This is the equivalent of the Python |
| 1365 | expression \samp{\var{o1} >> \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1366 | \end{cfuncdesc} |
| 1367 | |
| 1368 | |
| 1369 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1370 | Returns the result of ``anding'' \var{o2} and \var{o2} on success and |
| 1371 | \NULL{} on failure. This is the equivalent of the Python |
| 1372 | expression \samp{\var{o1} and \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1373 | \end{cfuncdesc} |
| 1374 | |
| 1375 | |
| 1376 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1377 | Returns the bitwise exclusive or of \var{o1} by \var{o2} on success, |
| 1378 | or \NULL{} on failure. This is the equivalent of the Python |
| 1379 | expression \samp{\var{o1} \^{ }\var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1380 | \end{cfuncdesc} |
| 1381 | |
| 1382 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1383 | Returns the result of \var{o1} and \var{o2} on success, or \NULL{} on |
| 1384 | failure. This is the equivalent of the Python expression |
| 1385 | \samp{\var{o1} or \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1386 | \end{cfuncdesc} |
| 1387 | |
| 1388 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1389 | \begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject **p1, PyObject **p2} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1390 | This function takes the addresses of two variables of type |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1391 | \ctype{PyObject*}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1392 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1393 | If the objects pointed to by \code{*\var{p1}} and \code{*\var{p2}} |
| 1394 | have the same type, increment their reference count and return |
| 1395 | \code{0} (success). If the objects can be converted to a common |
| 1396 | numeric type, replace \code{*p1} and \code{*p2} by their converted |
| 1397 | value (with 'new' reference counts), and return \code{0}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1398 | If no conversion is possible, or if some other error occurs, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1399 | return \code{-1} (failure) and don't increment the reference counts. |
| 1400 | The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the |
| 1401 | Python statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, |
| 1402 | \var{o2})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1403 | \end{cfuncdesc} |
| 1404 | |
| 1405 | |
| 1406 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1407 | 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] | 1408 | \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1409 | expression \samp{int(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1410 | \end{cfuncdesc} |
| 1411 | |
| 1412 | |
| 1413 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1414 | 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] | 1415 | or \NULL{} on failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1416 | expression \samp{long(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1417 | \end{cfuncdesc} |
| 1418 | |
| 1419 | |
| 1420 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1421 | Returns the \var{o} converted to a float object on success, or \NULL{} |
| 1422 | on failure. This is the equivalent of the Python expression |
| 1423 | \samp{float(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1424 | \end{cfuncdesc} |
| 1425 | |
| 1426 | |
Fred Drake | f44617d | 1998-02-12 20:57:15 +0000 | [diff] [blame] | 1427 | \section{Sequence Protocol} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1428 | \label{sequence} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1429 | |
| 1430 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1431 | Return \code{1} if the object provides sequence protocol, and \code{0} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1432 | otherwise. |
| 1433 | This function always succeeds. |
| 1434 | \end{cfuncdesc} |
| 1435 | |
| 1436 | |
| 1437 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1438 | 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] | 1439 | failure. This is the equivalent of the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1440 | expression \samp{\var{o1} + \var{o2}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1441 | \end{cfuncdesc} |
| 1442 | |
| 1443 | |
| 1444 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1445 | Return the result of repeating sequence object \var{o} \var{count} |
| 1446 | times, or \NULL{} on failure. This is the equivalent of the Python |
| 1447 | expression \samp{\var{o} * \var{count}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1448 | \end{cfuncdesc} |
| 1449 | |
| 1450 | |
| 1451 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1452 | Return the \var{i}th element of \var{o}, or \NULL{} on failure. This |
| 1453 | 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] | 1454 | \end{cfuncdesc} |
| 1455 | |
| 1456 | |
| 1457 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1458 | Return the slice of sequence object \var{o} between \var{i1} and |
| 1459 | \var{i2}, or \NULL{} on failure. This is the equivalent of the Python |
| 1460 | expression \samp{\var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1461 | \end{cfuncdesc} |
| 1462 | |
| 1463 | |
| 1464 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1465 | Assign object \var{v} to the \var{i}th element of \var{o}. |
| 1466 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1467 | statement \samp{\var{o}[\var{i}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1468 | \end{cfuncdesc} |
| 1469 | |
| 1470 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1471 | Delete the \var{i}th element of object \var{v}. Returns |
| 1472 | \code{-1} on failure. This is the equivalent of the Python |
| 1473 | statement \samp{del \var{o}[\var{i}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1474 | \end{cfuncdesc} |
| 1475 | |
| 1476 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1477 | Assign the sequence object \var{v} to the slice in sequence |
| 1478 | object \var{o} from \var{i1} to \var{i2}. This is the equivalent of |
| 1479 | 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] | 1480 | \end{cfuncdesc} |
| 1481 | |
| 1482 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1483 | Delete the slice in sequence object \var{o} from \var{i1} to \var{i2}. |
| 1484 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1485 | statement \samp{del \var{o}[\var{i1}:\var{i2}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1486 | \end{cfuncdesc} |
| 1487 | |
| 1488 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1489 | Returns the \var{o} as a tuple on success, and \NULL{} on failure. |
| 1490 | This is equivalent to the Python expression \code{tuple(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1491 | \end{cfuncdesc} |
| 1492 | |
| 1493 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1494 | Return the number of occurrences of \var{value} in \var{o}, that is, |
| 1495 | return the number of keys for which \code{\var{o}[\var{key}] == |
| 1496 | \var{value}}. On failure, return \code{-1}. This is equivalent to |
| 1497 | the Python expression \samp{\var{o}.count(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1498 | \end{cfuncdesc} |
| 1499 | |
| 1500 | \begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1501 | Determine if \var{o} contains \var{value}. If an item in \var{o} is |
| 1502 | equal to \var{value}, return \code{1}, otherwise return \code{0}. On |
| 1503 | error, return \code{-1}. This is equivalent to the Python expression |
| 1504 | \samp{\var{value} in \var{o}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1505 | \end{cfuncdesc} |
| 1506 | |
| 1507 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1508 | Return the first index \var{i} for which \code{\var{o}[\var{i}] == |
| 1509 | \var{value}}. On error, return \code{-1}. This is equivalent to |
| 1510 | the Python expression \samp{\var{o}.index(\var{value})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1511 | \end{cfuncdesc} |
| 1512 | |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1513 | |
Fred Drake | f44617d | 1998-02-12 20:57:15 +0000 | [diff] [blame] | 1514 | \section{Mapping Protocol} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1515 | \label{mapping} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1516 | |
| 1517 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1518 | Return \code{1} if the object provides mapping protocol, and \code{0} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1519 | otherwise. |
| 1520 | This function always succeeds. |
| 1521 | \end{cfuncdesc} |
| 1522 | |
| 1523 | |
| 1524 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1525 | Returns the number of keys in object \var{o} on success, and \code{-1} |
| 1526 | on failure. For objects that do not provide sequence protocol, |
| 1527 | this is equivalent to the Python expression \samp{len(\var{o})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1528 | \end{cfuncdesc} |
| 1529 | |
| 1530 | |
| 1531 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1532 | Remove the mapping for object \var{key} from the object \var{o}. |
| 1533 | Return \code{-1} on failure. This is equivalent to |
| 1534 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1535 | \end{cfuncdesc} |
| 1536 | |
| 1537 | |
| 1538 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1539 | Remove the mapping for object \var{key} from the object \var{o}. |
| 1540 | Return \code{-1} on failure. This is equivalent to |
| 1541 | the Python statement \samp{del \var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1542 | \end{cfuncdesc} |
| 1543 | |
| 1544 | |
| 1545 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1546 | On success, return \code{1} if the mapping object has the key \var{key} |
| 1547 | and \code{0} otherwise. This is equivalent to the Python expression |
| 1548 | \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1549 | This function always succeeds. |
| 1550 | \end{cfuncdesc} |
| 1551 | |
| 1552 | |
| 1553 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1554 | Return \code{1} if the mapping object has the key \var{key} and |
| 1555 | \code{0} otherwise. This is equivalent to the Python expression |
| 1556 | \samp{\var{o}.has_key(\var{key})}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1557 | This function always succeeds. |
| 1558 | \end{cfuncdesc} |
| 1559 | |
| 1560 | |
| 1561 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1562 | 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] | 1563 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1564 | expression \samp{\var{o}.keys()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1565 | \end{cfuncdesc} |
| 1566 | |
| 1567 | |
| 1568 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1569 | 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] | 1570 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1571 | expression \samp{\var{o}.values()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1572 | \end{cfuncdesc} |
| 1573 | |
| 1574 | |
| 1575 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1576 | 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] | 1577 | each item is a tuple containing a key-value pair. On |
| 1578 | failure, return \NULL{}. This is equivalent to the Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1579 | expression \samp{\var{o}.items()}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1580 | \end{cfuncdesc} |
| 1581 | |
| 1582 | \begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1583 | Make object \var{o} empty. Returns \code{1} on success and \code{0} |
| 1584 | on failure. This is equivalent to the Python statement |
| 1585 | \samp{for key in \var{o}.keys(): del \var{o}[key]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1586 | \end{cfuncdesc} |
| 1587 | |
| 1588 | |
| 1589 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1590 | Return element of \var{o} corresponding to the object \var{key} or |
| 1591 | \NULL{} on failure. This is the equivalent of the Python expression |
| 1592 | \samp{\var{o}[\var{key}]}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1593 | \end{cfuncdesc} |
| 1594 | |
| 1595 | \begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1596 | Map the object \var{key} to the value \var{v} in object \var{o}. |
| 1597 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 1598 | statement \samp{\var{o}[\var{key}] = \var{v}}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1599 | \end{cfuncdesc} |
| 1600 | |
| 1601 | |
| 1602 | \section{Constructors} |
| 1603 | |
| 1604 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode} |
| 1605 | On success, returns a new file object that is opened on the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1606 | file given by \var{file_name}, with a file mode given by \var{mode}, |
| 1607 | where \var{mode} has the same semantics as the standard \C{} routine |
| 1608 | \cfunction{fopen()}. On failure, return \code{-1}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1609 | \end{cfuncdesc} |
| 1610 | |
| 1611 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1612 | Return a new file object for an already opened standard \C{} file |
| 1613 | pointer, \var{fp}. A file name, \var{file_name}, and open mode, |
| 1614 | \var{mode}, must be provided as well as a flag, \var{close_on_del}, |
| 1615 | that indicates whether the file is to be closed when the file object |
| 1616 | is destroyed. On failure, return \code{-1}. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1617 | \end{cfuncdesc} |
| 1618 | |
| 1619 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1620 | Returns a new float object with the value \var{v} on success, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1621 | \NULL{} on failure. |
| 1622 | \end{cfuncdesc} |
| 1623 | |
| 1624 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1625 | Returns a new int object with the value \var{v} on success, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1626 | \NULL{} on failure. |
| 1627 | \end{cfuncdesc} |
| 1628 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1629 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int len} |
| 1630 | Returns a new list of length \var{len} on success, and \NULL{} on |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1631 | failure. |
| 1632 | \end{cfuncdesc} |
| 1633 | |
| 1634 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1635 | Returns a new long object with the value \var{v} on success, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1636 | \NULL{} on failure. |
| 1637 | \end{cfuncdesc} |
| 1638 | |
| 1639 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1640 | Returns a new long object with the value \var{v} on success, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1641 | \NULL{} on failure. |
| 1642 | \end{cfuncdesc} |
| 1643 | |
| 1644 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
| 1645 | Returns a new empty dictionary on success, and \NULL{} on |
| 1646 | failure. |
| 1647 | \end{cfuncdesc} |
| 1648 | |
| 1649 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1650 | Returns a new string object with the value \var{v} on success, and |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1651 | \NULL{} on failure. |
| 1652 | \end{cfuncdesc} |
| 1653 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1654 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int len} |
| 1655 | Returns a new string object with the value \var{v} and length |
| 1656 | \var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{}, |
| 1657 | the contents of the string are uninitialized. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1658 | \end{cfuncdesc} |
| 1659 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1660 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len} |
| 1661 | Returns a new tuple of length \var{len} on success, and \NULL{} on |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1662 | failure. |
| 1663 | \end{cfuncdesc} |
| 1664 | |
| 1665 | |
| 1666 | \chapter{Concrete Objects Layer} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1667 | \label{concrete} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1668 | |
| 1669 | The functions in this chapter are specific to certain Python object |
| 1670 | types. Passing them an object of the wrong type is not a good idea; |
| 1671 | if you receive an object from a Python program and you are not sure |
| 1672 | that it has the right type, you must perform a type check first; |
| 1673 | e.g. to check that an object is a dictionary, use |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1674 | \cfunction{PyDict_Check()}. The chapter is structured like the |
| 1675 | ``family tree'' of Python object types. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1676 | |
| 1677 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1678 | \section{Fundamental Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1679 | \label{fundamental} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1680 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1681 | This section describes Python type objects and the singleton object |
| 1682 | \code{None}. |
| 1683 | |
| 1684 | |
| 1685 | \subsection{Type Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1686 | \label{typeObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1687 | |
| 1688 | \begin{ctypedesc}{PyTypeObject} |
| 1689 | |
| 1690 | \end{ctypedesc} |
| 1691 | |
| 1692 | \begin{cvardesc}{PyObject *}{PyType_Type} |
| 1693 | |
| 1694 | \end{cvardesc} |
| 1695 | |
| 1696 | |
| 1697 | \subsection{The None Object} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1698 | \label{noneObject} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1699 | |
| 1700 | \begin{cvardesc}{PyObject *}{Py_None} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1701 | The Python \code{None} object, denoting lack of value. This object has |
| 1702 | no methods. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1703 | \end{cvardesc} |
| 1704 | |
| 1705 | |
| 1706 | \section{Sequence Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1707 | \label{sequenceObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1708 | |
| 1709 | Generic operations on sequence objects were discussed in the previous |
| 1710 | chapter; this section deals with the specific kinds of sequence |
| 1711 | objects that are intrinsic to the Python language. |
| 1712 | |
| 1713 | |
| 1714 | \subsection{String Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1715 | \label{stringObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1716 | |
| 1717 | \begin{ctypedesc}{PyStringObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1718 | This subtype of \ctype{PyObject} represents a Python string object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1719 | \end{ctypedesc} |
| 1720 | |
| 1721 | \begin{cvardesc}{PyTypeObject}{PyString_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1722 | This instance of \ctype{PyTypeObject} represents the Python string type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1723 | \end{cvardesc} |
| 1724 | |
| 1725 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1726 | Returns true if the object \var{o} is a string object. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1727 | \end{cfuncdesc} |
| 1728 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1729 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v, |
| 1730 | int len} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1731 | Returns a new string object with the value \var{v} and length |
| 1732 | \var{len} on success, and \NULL{} on failure. If \var{v} is \NULL{}, |
| 1733 | the contents of the string are uninitialized. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1734 | \end{cfuncdesc} |
| 1735 | |
| 1736 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1737 | Returns a new string object with the value \var{v} on success, and |
| 1738 | \NULL{} on failure. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1739 | \end{cfuncdesc} |
| 1740 | |
| 1741 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1742 | Returns the length of the string in string object \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1743 | \end{cfuncdesc} |
| 1744 | |
| 1745 | \begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1746 | Resturns a \NULL{} terminated representation of the contents of \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1747 | \end{cfuncdesc} |
| 1748 | |
| 1749 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string, |
| 1750 | PyObject *newpart} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1751 | Creates a new string object in \var{*string} containing the contents |
| 1752 | of \var{newpart} appended to \var{string}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1753 | \end{cfuncdesc} |
| 1754 | |
| 1755 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string, |
| 1756 | PyObject *newpart} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1757 | Creates a new string object in \var{*string} containing the contents |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1758 | of \var{newpart} appended to \var{string}. This version decrements |
| 1759 | the reference count of \var{newpart}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1760 | \end{cfuncdesc} |
| 1761 | |
| 1762 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1763 | A way to resize a string object even though it is ``immutable''. |
| 1764 | Only use this to build up a brand new string object; don't use this if |
| 1765 | the string may already be known in other parts of the code. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1766 | \end{cfuncdesc} |
| 1767 | |
| 1768 | \begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format, |
| 1769 | PyObject *args} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1770 | Returns a new string object from \var{format} and \var{args}. Analogous |
| 1771 | to \code{\var{format} \% \var{args}}. The \var{args} argument must be |
| 1772 | a tuple. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1773 | \end{cfuncdesc} |
| 1774 | |
| 1775 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1776 | Intern the argument \var{*string} in place. The argument must be the |
| 1777 | address of a pointer variable pointing to a Python string object. |
| 1778 | If there is an existing interned string that is the same as |
| 1779 | \var{*string}, it sets \var{*string} to it (decrementing the reference |
| 1780 | count of the old string object and incrementing the reference count of |
| 1781 | the interned string object), otherwise it leaves \var{*string} alone |
| 1782 | and interns it (incrementing its reference count). (Clarification: |
| 1783 | 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] | 1784 | this function as reference-count-neutral; you own the object after |
| 1785 | the call if and only if you owned it before the call.) |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1786 | \end{cfuncdesc} |
| 1787 | |
| 1788 | \begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1789 | A combination of \cfunction{PyString_FromString()} and |
| 1790 | \cfunction{PyString_InternInPlace()}, returning either a new string object |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1791 | that has been interned, or a new (``owned'') reference to an earlier |
| 1792 | interned string object with the same value. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1793 | \end{cfuncdesc} |
| 1794 | |
| 1795 | \begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1796 | Macro form of \cfunction{PyString_AsString()} but without error checking. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1797 | \end{cfuncdesc} |
| 1798 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1799 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1800 | Macro form of \cfunction{PyString_GetSize()} but without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1801 | \end{cfuncdesc} |
| 1802 | |
| 1803 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1804 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1805 | \subsection{Tuple Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1806 | \label{tupleObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1807 | |
| 1808 | \begin{ctypedesc}{PyTupleObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1809 | This subtype of \ctype{PyObject} represents a Python tuple object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1810 | \end{ctypedesc} |
| 1811 | |
| 1812 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1813 | This instance of \ctype{PyTypeObject} represents the Python tuple type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1814 | \end{cvardesc} |
| 1815 | |
| 1816 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} |
| 1817 | Return true if the argument is a tuple object. |
| 1818 | \end{cfuncdesc} |
| 1819 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1820 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int s} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1821 | Return a new tuple object of size \var{s}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1822 | \end{cfuncdesc} |
| 1823 | |
| 1824 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1825 | Takes a pointer to a tuple object, and returns the size |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1826 | of that tuple. |
| 1827 | \end{cfuncdesc} |
| 1828 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1829 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyTupleObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1830 | Returns the object at position \var{pos} in the tuple pointed |
| 1831 | to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1832 | sets an \exception{IndexError} exception. \strong{Note:} this |
| 1833 | function returns a ``borrowed'' reference. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1834 | \end{cfuncdesc} |
| 1835 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1836 | \begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1837 | Does the same, but does no checking of its arguments. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1838 | \end{cfuncdesc} |
| 1839 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1840 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyTupleObject *p, |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1841 | int low, |
| 1842 | int high} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1843 | Takes a slice of the tuple pointed to by \var{p} from |
| 1844 | \var{low} to \var{high} and returns it as a new tuple. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1845 | \end{cfuncdesc} |
| 1846 | |
| 1847 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p, |
| 1848 | int pos, |
| 1849 | PyObject *o} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1850 | Inserts a reference to object \var{o} at position \var{pos} of |
| 1851 | the tuple pointed to by \var{p}. It returns \code{0} on success. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1852 | \end{cfuncdesc} |
| 1853 | |
| 1854 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p, |
| 1855 | int pos, |
| 1856 | PyObject *o} |
| 1857 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1858 | Does the same, but does no error checking, and |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1859 | should \emph{only} be used to fill in brand new tuples. |
| 1860 | \end{cfuncdesc} |
| 1861 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1862 | \begin{cfuncdesc}{int}{_PyTuple_Resize}{PyTupleObject *p, |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1863 | int new, |
| 1864 | int last_is_sticky} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1865 | Can be used to resize a tuple. Because tuples are |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1866 | \emph{supposed} to be immutable, this should only be used if there is only |
| 1867 | one module referencing the object. Do \emph{not} use this if the tuple may |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1868 | already be known to some other part of the code. \var{last_is_sticky} is |
| 1869 | a flag --- if set, the tuple will grow or shrink at the front, otherwise |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1870 | it will grow or shrink at the end. Think of this as destroying the old |
| 1871 | tuple and creating a new one, only more efficiently. |
| 1872 | \end{cfuncdesc} |
| 1873 | |
| 1874 | |
| 1875 | \subsection{List Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1876 | \label{listObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1877 | |
| 1878 | \begin{ctypedesc}{PyListObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1879 | This subtype of \ctype{PyObject} represents a Python list object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1880 | \end{ctypedesc} |
| 1881 | |
| 1882 | \begin{cvardesc}{PyTypeObject}{PyList_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1883 | This instance of \ctype{PyTypeObject} represents the Python list type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1884 | \end{cvardesc} |
| 1885 | |
| 1886 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1887 | Returns true if its argument is a \ctype{PyListObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1888 | \end{cfuncdesc} |
| 1889 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1890 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int size} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1891 | Returns a new list of length \var{len} on success, and \NULL{} on |
| 1892 | failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1893 | \end{cfuncdesc} |
| 1894 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1895 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *list} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1896 | Returns the length of the list object in \var{list}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1897 | \end{cfuncdesc} |
| 1898 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1899 | \begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1900 | Returns the object at position \var{pos} in the list pointed |
| 1901 | to by \var{p}. If \var{pos} is out of bounds, returns \NULL{} and |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1902 | sets an \exception{IndexError} exception. \strong{Note:} this |
| 1903 | function returns a ``borrowed'' reference. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1904 | \end{cfuncdesc} |
| 1905 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1906 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index, |
| 1907 | PyObject *item} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1908 | Sets the item at index \var{index} in list to \var{item}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1909 | \end{cfuncdesc} |
| 1910 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1911 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index, |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1912 | PyObject *item} |
| 1913 | Inserts the item \var{item} into list \var{list} in front of index |
| 1914 | \var{index}. Returns 0 if successful; returns -1 and sets an |
| 1915 | exception if unsuccessful. Analogous to \code{list.insert(index, item)}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1916 | \end{cfuncdesc} |
| 1917 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1918 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1919 | Appends the object \var{item} at the end of list \var{list}. Returns |
| 1920 | 0 if successful; returns -1 and sets an exception if unsuccessful. |
| 1921 | Analogous to \code{list.append(item)}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1922 | \end{cfuncdesc} |
| 1923 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1924 | \begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list, |
| 1925 | int low, int high} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1926 | 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] | 1927 | \emph{between} \var{low} and \var{high}. Returns NULL and sets an |
| 1928 | exception if unsuccessful. |
| 1929 | Analogous to \code{list[low:high]}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1930 | \end{cfuncdesc} |
| 1931 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1932 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list, |
| 1933 | int low, int high, |
| 1934 | PyObject *itemlist} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1935 | Sets the slice of \var{list} between \var{low} and \var{high} to the contents |
| 1936 | of \var{itemlist}. Analogous to \code{list[low:high]=itemlist}. Returns 0 |
| 1937 | on success, -1 on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1938 | \end{cfuncdesc} |
| 1939 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1940 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1941 | Sorts the items of \var{list} in place. Returns 0 on success, -1 on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1942 | \end{cfuncdesc} |
| 1943 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1944 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 1945 | Reverses the items of \var{list} in place. Returns 0 on success, -1 on failure. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1946 | \end{cfuncdesc} |
| 1947 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1948 | \begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list} |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 1949 | Returns a new tuple object containing the contents of \var{list}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1950 | \end{cfuncdesc} |
| 1951 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1952 | \begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1953 | Macro form of \cfunction{PyList_GetItem()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1954 | \end{cfuncdesc} |
| 1955 | |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 1956 | \begin{cfuncdesc}{PyObject*}{PyList_SET_ITEM}{PyObject *list, int i, |
| 1957 | PyObject *o} |
| 1958 | Macro form of \cfunction{PyList_SetItem()} without error checking. |
| 1959 | \end{cfuncdesc} |
| 1960 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1961 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1962 | Macro form of \cfunction{PyList_GetSize()} without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1963 | \end{cfuncdesc} |
| 1964 | |
| 1965 | |
| 1966 | \section{Mapping Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1967 | \label{mapObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1968 | |
| 1969 | \subsection{Dictionary Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 1970 | \label{dictObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1971 | |
| 1972 | \begin{ctypedesc}{PyDictObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1973 | This subtype of \ctype{PyObject} represents a Python dictionary object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1974 | \end{ctypedesc} |
| 1975 | |
| 1976 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1977 | This instance of \ctype{PyTypeObject} represents the Python dictionary type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1978 | \end{cvardesc} |
| 1979 | |
| 1980 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 1981 | Returns true if its argument is a \ctype{PyDictObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1982 | \end{cfuncdesc} |
| 1983 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 1984 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1985 | Returns a new empty dictionary. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1986 | \end{cfuncdesc} |
| 1987 | |
| 1988 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1989 | Empties an existing dictionary of all key/value pairs. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1990 | \end{cfuncdesc} |
| 1991 | |
| 1992 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p, |
| 1993 | PyObject *key, |
| 1994 | PyObject *val} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 1995 | Inserts \var{value} into the dictionary with a key of \var{key}. Both |
| 1996 | \var{key} and \var{value} should be PyObjects, and \var{key} should be |
| 1997 | hashable. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 1998 | \end{cfuncdesc} |
| 1999 | |
| 2000 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p, |
| 2001 | char *key, |
| 2002 | PyObject *val} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2003 | Inserts \var{value} into the dictionary using \var{key} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2004 | 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] | 2005 | created using \code{PyString_FromString(\var{key})}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2006 | \end{cfuncdesc} |
| 2007 | |
| 2008 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2009 | Removes the entry in dictionary \var{p} with key \var{key}. |
| 2010 | \var{key} is a PyObject. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2011 | \end{cfuncdesc} |
| 2012 | |
| 2013 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2014 | Removes the entry in dictionary \var{p} which has a key |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2015 | specified by the \ctype{char *}\var{key}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2016 | \end{cfuncdesc} |
| 2017 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2018 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyDictObject *p, PyObject *key} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2019 | Returns the object from dictionary \var{p} which has a key |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2020 | \var{key}. Returns \NULL{} if the key \var{key} is not present, but |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2021 | without (!) setting an exception. \strong{Note:} this function |
| 2022 | returns a ``borrowed'' reference. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2023 | \end{cfuncdesc} |
| 2024 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2025 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyDictObject *p, char *key} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2026 | This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is |
| 2027 | specified as a \ctype{char *}, rather than a \ctype{PyObject *}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2028 | \end{cfuncdesc} |
| 2029 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2030 | \begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyDictObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2031 | Returns a \ctype{PyListObject} containing all the items |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2032 | from the dictionary, as in the dictinoary method \method{items()} (see |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2033 | the \emph{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2034 | \end{cfuncdesc} |
| 2035 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2036 | \begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyDictObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2037 | Returns a \ctype{PyListObject} containing all the keys |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2038 | from the dictionary, as in the dictionary method \method{keys()} (see the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2039 | \emph{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2040 | \end{cfuncdesc} |
| 2041 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2042 | \begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyDictObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2043 | Returns a \ctype{PyListObject} containing all the values |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2044 | from the dictionary \var{p}, as in the dictionary method |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2045 | \method{values()} (see the \emph{Python Library Reference}). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2046 | \end{cfuncdesc} |
| 2047 | |
| 2048 | \begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2049 | Returns the number of items in the dictionary. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2050 | \end{cfuncdesc} |
| 2051 | |
| 2052 | \begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p, |
| 2053 | int ppos, |
| 2054 | PyObject **pkey, |
| 2055 | PyObject **pvalue} |
| 2056 | |
| 2057 | \end{cfuncdesc} |
| 2058 | |
| 2059 | |
| 2060 | \section{Numeric Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2061 | \label{numericObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2062 | |
| 2063 | \subsection{Plain Integer Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2064 | \label{intObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2065 | |
| 2066 | \begin{ctypedesc}{PyIntObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2067 | This subtype of \ctype{PyObject} represents a Python integer object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2068 | \end{ctypedesc} |
| 2069 | |
| 2070 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2071 | This instance of \ctype{PyTypeObject} represents the Python plain |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2072 | integer type. |
| 2073 | \end{cvardesc} |
| 2074 | |
| 2075 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject *} |
| 2076 | |
| 2077 | \end{cfuncdesc} |
| 2078 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2079 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2080 | Creates a new integer object with a value of \var{ival}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2081 | |
| 2082 | The current implementation keeps an array of integer objects for all |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2083 | integers between \code{-1} and \code{100}, when you create an int in |
| 2084 | that range you actually just get back a reference to the existing |
| 2085 | 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] | 2086 | suspect the behaviour of Python in this case is undefined. :-) |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2087 | \end{cfuncdesc} |
| 2088 | |
| 2089 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2090 | Returns the value of the object \var{io}. No error checking is |
| 2091 | performed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2092 | \end{cfuncdesc} |
| 2093 | |
| 2094 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2095 | Will first attempt to cast the object to a \ctype{PyIntObject}, if |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2096 | it is not already one, and then return its value. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2097 | \end{cfuncdesc} |
| 2098 | |
| 2099 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2100 | Returns the systems idea of the largest integer it can handle |
| 2101 | (\constant{LONG_MAX}, as defined in the system header files). |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2102 | \end{cfuncdesc} |
| 2103 | |
| 2104 | |
| 2105 | \subsection{Long Integer Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2106 | \label{longObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2107 | |
| 2108 | \begin{ctypedesc}{PyLongObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2109 | This subtype of \ctype{PyObject} represents a Python long integer |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2110 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2111 | \end{ctypedesc} |
| 2112 | |
| 2113 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2114 | This instance of \ctype{PyTypeObject} represents the Python long |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2115 | integer type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2116 | \end{cvardesc} |
| 2117 | |
| 2118 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2119 | Returns true if its argument is a \ctype{PyLongObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2120 | \end{cfuncdesc} |
| 2121 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2122 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2123 | Returns a new \ctype{PyLongObject} object from \var{v}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2124 | \end{cfuncdesc} |
| 2125 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2126 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2127 | Returns a new \ctype{PyLongObject} object from an unsigned \C{} long. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2128 | \end{cfuncdesc} |
| 2129 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2130 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2131 | Returns a new \ctype{PyLongObject} object from the integer part of \var{v}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2132 | \end{cfuncdesc} |
| 2133 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2134 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2135 | Returns a \C{} \ctype{long} representation of the contents of \var{pylong}. |
| 2136 | WHAT HAPPENS IF \var{pylong} is greater than \constant{LONG_MAX}? |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2137 | \end{cfuncdesc} |
| 2138 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2139 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2140 | Returns a \C{} \ctype{unsigned long} representation of the contents of |
| 2141 | \var{pylong}. WHAT HAPPENS IF \var{pylong} is greater than |
| 2142 | \constant{ULONG_MAX}? |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2143 | \end{cfuncdesc} |
| 2144 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2145 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2146 | Returns a \C{} \ctype{double} representation of the contents of \var{pylong}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2147 | \end{cfuncdesc} |
| 2148 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2149 | \begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend, |
| 2150 | int base} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2151 | \end{cfuncdesc} |
| 2152 | |
| 2153 | |
| 2154 | \subsection{Floating Point Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2155 | \label{floatObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2156 | |
| 2157 | \begin{ctypedesc}{PyFloatObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2158 | This subtype of \ctype{PyObject} represents a Python floating point |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2159 | object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2160 | \end{ctypedesc} |
| 2161 | |
| 2162 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2163 | This instance of \ctype{PyTypeObject} represents the Python floating |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2164 | point type. |
| 2165 | \end{cvardesc} |
| 2166 | |
| 2167 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2168 | Returns true if its argument is a \ctype{PyFloatObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2169 | \end{cfuncdesc} |
| 2170 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2171 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2172 | Creates a \ctype{PyFloatObject} object from \var{v}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2173 | \end{cfuncdesc} |
| 2174 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2175 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2176 | Returns a \C{} \ctype{double} representation of the contents of \var{pyfloat}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2177 | \end{cfuncdesc} |
| 2178 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2179 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2180 | Returns a \C{} \ctype{double} representation of the contents of |
| 2181 | \var{pyfloat}, but without error checking. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2182 | \end{cfuncdesc} |
| 2183 | |
| 2184 | |
| 2185 | \subsection{Complex Number Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2186 | \label{complexObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2187 | |
| 2188 | \begin{ctypedesc}{Py_complex} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2189 | The \C{} structure which corresponds to the value portion of a Python |
| 2190 | complex number object. Most of the functions for dealing with complex |
| 2191 | number objects use structures of this type as input or output values, |
| 2192 | as appropriate. It is defined as: |
| 2193 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2194 | \begin{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2195 | typedef struct { |
| 2196 | double real; |
| 2197 | double imag; |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2198 | } Py_complex; |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2199 | \end{verbatim} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2200 | \end{ctypedesc} |
| 2201 | |
| 2202 | \begin{ctypedesc}{PyComplexObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2203 | This subtype of \ctype{PyObject} represents a Python complex number object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2204 | \end{ctypedesc} |
| 2205 | |
| 2206 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2207 | This instance of \ctype{PyTypeObject} represents the Python complex |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2208 | number type. |
| 2209 | \end{cvardesc} |
| 2210 | |
| 2211 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2212 | Returns true if its argument is a \ctype{PyComplexObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2213 | \end{cfuncdesc} |
| 2214 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2215 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2216 | \end{cfuncdesc} |
| 2217 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2218 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2219 | \end{cfuncdesc} |
| 2220 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2221 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2222 | \end{cfuncdesc} |
| 2223 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2224 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2225 | \end{cfuncdesc} |
| 2226 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2227 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend, |
| 2228 | Py_complex divisor} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2229 | \end{cfuncdesc} |
| 2230 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2231 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2232 | \end{cfuncdesc} |
| 2233 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2234 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2235 | \end{cfuncdesc} |
| 2236 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2237 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2238 | Returns a new \ctype{PyComplexObject} object from \var{real} and \var{imag}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2239 | \end{cfuncdesc} |
| 2240 | |
| 2241 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2242 | Returns the real part of \var{op} as a \C{} \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2243 | \end{cfuncdesc} |
| 2244 | |
| 2245 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2246 | Returns the imaginary part of \var{op} as a \C{} \ctype{double}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2247 | \end{cfuncdesc} |
| 2248 | |
| 2249 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2250 | \end{cfuncdesc} |
| 2251 | |
| 2252 | |
| 2253 | |
| 2254 | \section{Other Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2255 | \label{otherObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2256 | |
| 2257 | \subsection{File Objects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2258 | \label{fileObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2259 | |
| 2260 | \begin{ctypedesc}{PyFileObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2261 | This subtype of \ctype{PyObject} represents a Python file object. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2262 | \end{ctypedesc} |
| 2263 | |
| 2264 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2265 | This instance of \ctype{PyTypeObject} represents the Python file type. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2266 | \end{cvardesc} |
| 2267 | |
| 2268 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2269 | Returns true if its argument is a \ctype{PyFileObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2270 | \end{cfuncdesc} |
| 2271 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2272 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *name, char *mode} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2273 | Creates a new \ctype{PyFileObject} pointing to the file |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2274 | specified in \var{name} with the mode specified in \var{mode}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2275 | \end{cfuncdesc} |
| 2276 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2277 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, |
Fred Drake | b92dce3 | 1998-02-25 15:40:22 +0000 | [diff] [blame] | 2278 | char *name, char *mode, int (*close)} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2279 | Creates a new \ctype{PyFileObject} from the already-open \var{fp}. |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2280 | The function \var{close} will be called when the file should be |
| 2281 | closed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2282 | \end{cfuncdesc} |
| 2283 | |
| 2284 | \begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2285 | Returns the file object associated with \var{p} as a \ctype{FILE *}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2286 | \end{cfuncdesc} |
| 2287 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2288 | \begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2289 | undocumented as yet |
| 2290 | \end{cfuncdesc} |
| 2291 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2292 | \begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2293 | Returns the name of the file specified by \var{p} as a |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2294 | \ctype{PyStringObject}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2295 | \end{cfuncdesc} |
| 2296 | |
| 2297 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2298 | Available on systems with \cfunction{setvbuf()} only. This should |
| 2299 | only be called immediately after file object creation. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2300 | \end{cfuncdesc} |
| 2301 | |
| 2302 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2303 | Sets the \member{softspace} attribute of \var{p} to \var{newflag}. |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2304 | Returns the previous value. This function clears any errors, and will |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2305 | return \code{0} as the previous value if the attribute either does not |
| 2306 | exist or if there were errors in retrieving it. There is no way to |
| 2307 | detect errors from this function, but doing so should not be needed. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2308 | \end{cfuncdesc} |
| 2309 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2310 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p, |
| 2311 | int flags} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2312 | Writes object \var{obj} to file object \var{p}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2313 | \end{cfuncdesc} |
| 2314 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2315 | \begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p, |
| 2316 | int flags} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2317 | Writes string \var{s} to file object \var{p}. |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2318 | \end{cfuncdesc} |
| 2319 | |
| 2320 | |
| 2321 | \subsection{CObjects} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2322 | \label{cObjects} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2323 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2324 | \begin{ctypedesc}{PyCObject} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2325 | This subtype of \ctype{PyObject} represents an opaque value, useful for |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2326 | \C{} extension modules who need to pass an opaque value (as a |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2327 | \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] | 2328 | often used to make a C function pointer defined in one module |
| 2329 | available to other modules, so the regular import mechanism can be |
| 2330 | used to access C APIs defined in dynamically loaded modules. |
| 2331 | \end{ctypedesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2332 | |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2333 | \begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtr}{void* cobj, |
| 2334 | void (*destr)(void *)} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2335 | Creates a \ctype{PyCObject} from the \code{void *} \var{cobj}. The |
| 2336 | \var{destr} function will be called when the object is reclaimed. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2337 | \end{cfuncdesc} |
| 2338 | |
| 2339 | \begin{cfuncdesc}{PyObject *}{PyCObject_FromVoidPtrAndDesc}{void* cobj, |
| 2340 | void* desc, void (*destr)(void *, void *) } |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2341 | Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The |
| 2342 | \var{destr} function will be called when the object is reclaimed. The |
| 2343 | \var{desc} argument can be used to pass extra callback data for the |
| 2344 | destructor function. |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2345 | \end{cfuncdesc} |
| 2346 | |
| 2347 | \begin{cfuncdesc}{void *}{PyCObject_AsVoidPtr}{PyObject* self} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2348 | Returns the object \ctype{void *} that the \ctype{PyCObject} \var{self} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2349 | was created with. |
| 2350 | \end{cfuncdesc} |
| 2351 | |
| 2352 | \begin{cfuncdesc}{void *}{PyCObject_GetDesc}{PyObject* self} |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2353 | Returns the description \ctype{void *} that the \ctype{PyCObject} |
Guido van Rossum | 4447513 | 1998-04-21 15:30:01 +0000 | [diff] [blame] | 2354 | \var{self} was created with. |
| 2355 | \end{cfuncdesc} |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 2356 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2357 | \chapter{Initialization, Finalization, and Threads} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2358 | \label{initialization} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2359 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2360 | \begin{cfuncdesc}{void}{Py_Initialize}{} |
| 2361 | Initialize the Python interpreter. In an application embedding |
| 2362 | Python, this should be called before using any other Python/C API |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2363 | functions; with the exception of \cfunction{Py_SetProgramName()}, |
| 2364 | \cfunction{PyEval_InitThreads()}, \cfunction{PyEval_ReleaseLock()}, |
| 2365 | and \cfunction{PyEval_AcquireLock()}. This initializes the table of |
| 2366 | loaded modules (\code{sys.modules}), and creates the fundamental |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2367 | modules \module{__builtin__}\refbimodindex{__builtin__}, |
| 2368 | \module{__main__}\refbimodindex{__main__} and |
| 2369 | \module{sys}\refbimodindex{sys}. It also initializes the module |
| 2370 | search path (\code{sys.path}).% |
| 2371 | \indexiii{module}{search}{path} |
| 2372 | It does not set \code{sys.argv}; use \cfunction{PySys_SetArgv()} for |
| 2373 | that. This is a no-op when called for a second time (without calling |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2374 | \cfunction{Py_Finalize()} first). There is no return value; it is a |
| 2375 | fatal error if the initialization fails. |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 2376 | \end{cfuncdesc} |
| 2377 | |
| 2378 | \begin{cfuncdesc}{int}{Py_IsInitialized}{} |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 2379 | Return true (nonzero) when the Python interpreter has been |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2380 | initialized, false (zero) if not. After \cfunction{Py_Finalize()} is |
| 2381 | called, this returns false until \cfunction{Py_Initialize()} is called |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 2382 | again. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2383 | \end{cfuncdesc} |
| 2384 | |
| 2385 | \begin{cfuncdesc}{void}{Py_Finalize}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2386 | Undo all initializations made by \cfunction{Py_Initialize()} and |
| 2387 | subsequent use of Python/C API functions, and destroy all |
| 2388 | sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that were |
| 2389 | created and not yet destroyed since the last call to |
| 2390 | \cfunction{Py_Initialize()}. Ideally, this frees all memory allocated |
| 2391 | by the Python interpreter. This is a no-op when called for a second |
| 2392 | time (without calling \cfunction{Py_Initialize()} again first). There |
| 2393 | is no return value; errors during finalization are ignored. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2394 | |
| 2395 | This function is provided for a number of reasons. An embedding |
| 2396 | application might want to restart Python without having to restart the |
| 2397 | application itself. An application that has loaded the Python |
| 2398 | interpreter from a dynamically loadable library (or DLL) might want to |
| 2399 | free all memory allocated by Python before unloading the DLL. During a |
| 2400 | hunt for memory leaks in an application a developer might want to free |
| 2401 | all memory allocated by Python before exiting from the application. |
| 2402 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2403 | \strong{Bugs and caveats:} The destruction of modules and objects in |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2404 | modules is done in random order; this may cause destructors |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2405 | (\method{__del__()} methods) to fail when they depend on other objects |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2406 | (even functions) or modules. Dynamically loaded extension modules |
| 2407 | loaded by Python are not unloaded. Small amounts of memory allocated |
| 2408 | by the Python interpreter may not be freed (if you find a leak, please |
| 2409 | report it). Memory tied up in circular references between objects is |
| 2410 | not freed. Some memory allocated by extension modules may not be |
| 2411 | freed. Some extension may not work properly if their initialization |
| 2412 | routine is called more than once; this can happen if an applcation |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2413 | calls \cfunction{Py_Initialize()} and \cfunction{Py_Finalize()} more |
| 2414 | than once. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2415 | \end{cfuncdesc} |
| 2416 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2417 | \begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2418 | Create a new sub-interpreter. This is an (almost) totally separate |
| 2419 | environment for the execution of Python code. In particular, the new |
| 2420 | interpreter has separate, independent versions of all imported |
| 2421 | modules, including the fundamental modules |
| 2422 | \module{__builtin__}\refbimodindex{__builtin__}, |
| 2423 | \module{__main__}\refbimodindex{__main__} and |
| 2424 | \module{sys}\refbimodindex{sys}. The table of loaded modules |
| 2425 | (\code{sys.modules}) and the module search path (\code{sys.path}) are |
| 2426 | also separate. The new environment has no \code{sys.argv} variable. |
| 2427 | It has new standard I/O stream file objects \code{sys.stdin}, |
| 2428 | \code{sys.stdout} and \code{sys.stderr} (however these refer to the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2429 | same underlying \ctype{FILE} structures in the \C{} library). |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2430 | |
| 2431 | The return value points to the first thread state created in the new |
| 2432 | sub-interpreter. This thread state is made the current thread state. |
| 2433 | Note that no actual thread is created; see the discussion of thread |
| 2434 | states below. If creation of the new interpreter is unsuccessful, |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2435 | \NULL{} is returned; no exception is set since the exception state |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2436 | is stored in the current thread state and there may not be a current |
| 2437 | thread state. (Like all other Python/C API functions, the global |
| 2438 | interpreter lock must be held before calling this function and is |
| 2439 | still held when it returns; however, unlike most other Python/C API |
| 2440 | functions, there needn't be a current thread state on entry.) |
| 2441 | |
| 2442 | Extension modules are shared between (sub-)interpreters as follows: |
| 2443 | the first time a particular extension is imported, it is initialized |
| 2444 | normally, and a (shallow) copy of its module's dictionary is |
| 2445 | squirreled away. When the same extension is imported by another |
| 2446 | (sub-)interpreter, a new module is initialized and filled with the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2447 | contents of this copy; the extension's \code{init} function is not |
| 2448 | called. Note that this is different from what happens when an |
| 2449 | extension is imported after the interpreter has been completely |
| 2450 | re-initialized by calling \cfunction{Py_Finalize()} and |
| 2451 | \cfunction{Py_Initialize()}; in that case, the extension's \code{init} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2452 | function \emph{is} called again. |
| 2453 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2454 | \strong{Bugs and caveats:} Because sub-interpreters (and the main |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2455 | interpreter) are part of the same process, the insulation between them |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2456 | isn't perfect --- for example, using low-level file operations like |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2457 | \function{os.close()} they can (accidentally or maliciously) affect each |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2458 | other's open files. Because of the way extensions are shared between |
| 2459 | (sub-)interpreters, some extensions may not work properly; this is |
| 2460 | especially likely when the extension makes use of (static) global |
| 2461 | variables, or when the extension manipulates its module's dictionary |
| 2462 | after its initialization. It is possible to insert objects created in |
| 2463 | one sub-interpreter into a namespace of another sub-interpreter; this |
| 2464 | should be done with great care to avoid sharing user-defined |
| 2465 | functions, methods, instances or classes between sub-interpreters, |
| 2466 | since import operations executed by such objects may affect the |
| 2467 | wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is |
| 2468 | a hard-to-fix bug that will be addressed in a future release.) |
| 2469 | \end{cfuncdesc} |
| 2470 | |
| 2471 | \begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate} |
| 2472 | Destroy the (sub-)interpreter represented by the given thread state. |
| 2473 | The given thread state must be the current thread state. See the |
| 2474 | discussion of thread states below. When the call returns, the current |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2475 | thread state is \NULL{}. All thread states associated with this |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2476 | interpreted are destroyed. (The global interpreter lock must be held |
| 2477 | before calling this function and is still held when it returns.) |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2478 | \cfunction{Py_Finalize()} will destroy all sub-interpreters that haven't |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2479 | been explicitly destroyed at that point. |
| 2480 | \end{cfuncdesc} |
| 2481 | |
| 2482 | \begin{cfuncdesc}{void}{Py_SetProgramName}{char *name} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2483 | This function should be called before \cfunction{Py_Initialize()} is called |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2484 | for the first time, if it is called at all. It tells the interpreter |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2485 | the value of the \code{argv[0]} argument to the \cfunction{main()} function |
| 2486 | of the program. This is used by \cfunction{Py_GetPath()} and some other |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2487 | functions below to find the Python run-time libraries relative to the |
| 2488 | interpreter executable. The default value is \code{"python"}. The |
| 2489 | argument should point to a zero-terminated character string in static |
| 2490 | storage whose contents will not change for the duration of the |
| 2491 | program's execution. No code in the Python interpreter will change |
| 2492 | the contents of this storage. |
| 2493 | \end{cfuncdesc} |
| 2494 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2495 | \begin{cfuncdesc}{char*}{Py_GetProgramName}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2496 | Return the program name set with \cfunction{Py_SetProgramName()}, or the |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2497 | default. The returned string points into static storage; the caller |
| 2498 | should not modify its value. |
| 2499 | \end{cfuncdesc} |
| 2500 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2501 | \begin{cfuncdesc}{char*}{Py_GetPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2502 | Return the \emph{prefix} for installed platform-independent files. This |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2503 | is derived through a number of complicated rules from the program name |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2504 | set with \cfunction{Py_SetProgramName()} and some environment variables; |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2505 | for example, if the program name is \code{"/usr/local/bin/python"}, |
| 2506 | the prefix is \code{"/usr/local"}. The returned string points into |
| 2507 | static storage; the caller should not modify its value. This |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 2508 | corresponds to the \makevar{prefix} variable in the top-level |
| 2509 | \file{Makefile} and the \code{-}\code{-prefix} argument to the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2510 | \program{configure} script at build time. The value is available to |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 2511 | 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] | 2512 | also the next function. |
| 2513 | \end{cfuncdesc} |
| 2514 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2515 | \begin{cfuncdesc}{char*}{Py_GetExecPrefix}{} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2516 | Return the \emph{exec-prefix} for installed platform-\emph{de}pendent |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2517 | files. This is derived through a number of complicated rules from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2518 | program name set with \cfunction{Py_SetProgramName()} and some environment |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2519 | variables; for example, if the program name is |
| 2520 | \code{"/usr/local/bin/python"}, the exec-prefix is |
| 2521 | \code{"/usr/local"}. The returned string points into static storage; |
| 2522 | the caller should not modify its value. This corresponds to the |
Fred Drake | c94d934 | 1998-04-12 02:39:13 +0000 | [diff] [blame] | 2523 | \makevar{exec_prefix} variable in the top-level \file{Makefile} and the |
| 2524 | \code{-}\code{-exec_prefix} argument to the \program{configure} script |
| 2525 | at build time. The value is available to Python code as |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 2526 | \code{sys.exec_prefix}. It is only useful on \UNIX{}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2527 | |
| 2528 | Background: The exec-prefix differs from the prefix when platform |
| 2529 | dependent files (such as executables and shared libraries) are |
| 2530 | installed in a different directory tree. In a typical installation, |
| 2531 | platform dependent files may be installed in the |
| 2532 | \code{"/usr/local/plat"} subtree while platform independent may be |
| 2533 | installed in \code{"/usr/local"}. |
| 2534 | |
| 2535 | Generally speaking, a platform is a combination of hardware and |
| 2536 | software families, e.g. Sparc machines running the Solaris 2.x |
| 2537 | operating system are considered the same platform, but Intel machines |
| 2538 | running Solaris 2.x are another platform, and Intel machines running |
| 2539 | Linux are yet another platform. Different major revisions of the same |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 2540 | operating system generally also form different platforms. Non-\UNIX{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2541 | operating systems are a different story; the installation strategies |
| 2542 | on those systems are so different that the prefix and exec-prefix are |
| 2543 | meaningless, and set to the empty string. Note that compiled Python |
| 2544 | bytecode files are platform independent (but not independent from the |
| 2545 | Python version by which they were compiled!). |
| 2546 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2547 | System administrators will know how to configure the \program{mount} or |
| 2548 | \program{automount} programs to share \code{"/usr/local"} between platforms |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2549 | while having \code{"/usr/local/plat"} be a different filesystem for each |
| 2550 | platform. |
| 2551 | \end{cfuncdesc} |
| 2552 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2553 | \begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2554 | Return the full program name of the Python executable; this is |
| 2555 | computed as a side-effect of deriving the default module search path |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2556 | from the program name (set by \cfunction{Py_SetProgramName()} above). The |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2557 | returned string points into static storage; the caller should not |
| 2558 | modify its value. The value is available to Python code as |
Guido van Rossum | 42cefd0 | 1997-10-05 15:27:29 +0000 | [diff] [blame] | 2559 | \code{sys.executable}. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2560 | \end{cfuncdesc} |
| 2561 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2562 | \begin{cfuncdesc}{char*}{Py_GetPath}{} |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2563 | \indexiii{module}{search}{path} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2564 | Return the default module search path; this is computed from the |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2565 | program name (set by \cfunction{Py_SetProgramName()} above) and some |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2566 | environment variables. The returned string consists of a series of |
| 2567 | directory names separated by a platform dependent delimiter character. |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2568 | The delimiter character is \character{:} on \UNIX{}, \character{;} on |
| 2569 | DOS/Windows, and \character{\\n} (the \ASCII{} newline character) on |
Fred Drake | e5bc497 | 1998-02-12 23:36:49 +0000 | [diff] [blame] | 2570 | Macintosh. The returned string points into static storage; the caller |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2571 | should not modify its value. The value is available to Python code |
| 2572 | as the list \code{sys.path}, which may be modified to change the |
| 2573 | future search path for loaded modules. |
| 2574 | |
| 2575 | % XXX should give the exact rules |
| 2576 | \end{cfuncdesc} |
| 2577 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2578 | \begin{cfuncdesc}{const char*}{Py_GetVersion}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2579 | Return the version of this Python interpreter. This is a string that |
| 2580 | looks something like |
| 2581 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 2582 | \begin{verbatim} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2583 | "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] | 2584 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2585 | |
| 2586 | The first word (up to the first space character) is the current Python |
| 2587 | version; the first three characters are the major and minor version |
| 2588 | separated by a period. The returned string points into static storage; |
| 2589 | the caller should not modify its value. The value is available to |
| 2590 | Python code as the list \code{sys.version}. |
| 2591 | \end{cfuncdesc} |
| 2592 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2593 | \begin{cfuncdesc}{const char*}{Py_GetPlatform}{} |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 2594 | Return the platform identifier for the current platform. On \UNIX{}, |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2595 | this is formed from the ``official'' name of the operating system, |
| 2596 | converted to lower case, followed by the major revision number; e.g., |
| 2597 | for Solaris 2.x, which is also known as SunOS 5.x, the value is |
| 2598 | \code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it |
| 2599 | is \code{"win"}. The returned string points into static storage; |
| 2600 | the caller should not modify its value. The value is available to |
| 2601 | Python code as \code{sys.platform}. |
| 2602 | \end{cfuncdesc} |
| 2603 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2604 | \begin{cfuncdesc}{const char*}{Py_GetCopyright}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2605 | Return the official copyright string for the current Python version, |
| 2606 | for example |
| 2607 | |
| 2608 | \code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"} |
| 2609 | |
| 2610 | The returned string points into static storage; the caller should not |
| 2611 | modify its value. The value is available to Python code as the list |
| 2612 | \code{sys.copyright}. |
| 2613 | \end{cfuncdesc} |
| 2614 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2615 | \begin{cfuncdesc}{const char*}{Py_GetCompiler}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2616 | Return an indication of the compiler used to build the current Python |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2617 | version, in square brackets, for example: |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2618 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2619 | \begin{verbatim} |
| 2620 | "[GCC 2.7.2.2]" |
| 2621 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2622 | |
| 2623 | The returned string points into static storage; the caller should not |
| 2624 | modify its value. The value is available to Python code as part of |
| 2625 | the variable \code{sys.version}. |
| 2626 | \end{cfuncdesc} |
| 2627 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2628 | \begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2629 | Return information about the sequence number and build date and time |
| 2630 | of the current Python interpreter instance, for example |
| 2631 | |
Guido van Rossum | 09270b5 | 1997-08-15 18:57:32 +0000 | [diff] [blame] | 2632 | \begin{verbatim} |
| 2633 | "#67, Aug 1 1997, 22:34:28" |
| 2634 | \end{verbatim} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2635 | |
| 2636 | The returned string points into static storage; the caller should not |
| 2637 | modify its value. The value is available to Python code as part of |
| 2638 | the variable \code{sys.version}. |
| 2639 | \end{cfuncdesc} |
| 2640 | |
| 2641 | \begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv} |
| 2642 | % XXX |
| 2643 | \end{cfuncdesc} |
| 2644 | |
| 2645 | % XXX Other PySys thingies (doesn't really belong in this chapter) |
| 2646 | |
| 2647 | \section{Thread State and the Global Interpreter Lock} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2648 | \label{threads} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2649 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2650 | The Python interpreter is not fully thread safe. In order to support |
| 2651 | multi-threaded Python programs, there's a global lock that must be |
| 2652 | held by the current thread before it can safely access Python objects. |
| 2653 | Without the lock, even the simplest operations could cause problems in |
Fred Drake | 7baf3d4 | 1998-02-20 00:45:52 +0000 | [diff] [blame] | 2654 | a multi-threaded program: for example, when two threads simultaneously |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2655 | increment the reference count of the same object, the reference count |
| 2656 | could end up being incremented only once instead of twice. |
| 2657 | |
| 2658 | Therefore, the rule exists that only the thread that has acquired the |
| 2659 | global interpreter lock may operate on Python objects or call Python/C |
| 2660 | API functions. In order to support multi-threaded Python programs, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2661 | the interpreter regularly release and reacquires the lock --- by |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2662 | default, every ten bytecode instructions (this can be changed with |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2663 | \function{sys.setcheckinterval()}). The lock is also released and |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2664 | reacquired around potentially blocking I/O operations like reading or |
| 2665 | writing a file, so that other threads can run while the thread that |
| 2666 | requests the I/O is waiting for the I/O operation to complete. |
| 2667 | |
| 2668 | The Python interpreter needs to keep some bookkeeping information |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2669 | separate per thread --- for this it uses a data structure called |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2670 | \ctype{PyThreadState}. This is new in Python 1.5; in earlier versions, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2671 | such state was stored in global variables, and switching threads could |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2672 | cause problems. In particular, exception handling is now thread safe, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2673 | when the application uses \function{sys.exc_info()} to access the |
| 2674 | exception last raised in the current thread. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2675 | |
| 2676 | There's one global variable left, however: the pointer to the current |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2677 | \ctype{PyThreadState} structure. While most thread packages have a way |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 2678 | to store ``per-thread global data,'' Python's internal platform |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2679 | independent thread abstraction doesn't support this yet. Therefore, |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2680 | the current thread state must be manipulated explicitly. |
| 2681 | |
| 2682 | This is easy enough in most cases. Most code manipulating the global |
| 2683 | interpreter lock has the following simple structure: |
| 2684 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2685 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2686 | Save the thread state in a local variable. |
| 2687 | Release the interpreter lock. |
| 2688 | ...Do some blocking I/O operation... |
| 2689 | Reacquire the interpreter lock. |
| 2690 | Restore the thread state from the local variable. |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2691 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2692 | |
| 2693 | This is so common that a pair of macros exists to simplify it: |
| 2694 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2695 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2696 | Py_BEGIN_ALLOW_THREADS |
| 2697 | ...Do some blocking I/O operation... |
| 2698 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2699 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2700 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2701 | The \code{Py_BEGIN_ALLOW_THREADS} macro opens a new block and declares |
| 2702 | a hidden local variable; the \code{Py_END_ALLOW_THREADS} macro closes |
| 2703 | the block. Another advantage of using these two macros is that when |
| 2704 | Python is compiled without thread support, they are defined empty, |
| 2705 | thus saving the thread state and lock manipulations. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2706 | |
| 2707 | When thread support is enabled, the block above expands to the |
| 2708 | following code: |
| 2709 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2710 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2711 | { |
| 2712 | PyThreadState *_save; |
| 2713 | _save = PyEval_SaveThread(); |
| 2714 | ...Do some blocking I/O operation... |
| 2715 | PyEval_RestoreThread(_save); |
| 2716 | } |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2717 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2718 | |
| 2719 | Using even lower level primitives, we can get roughly the same effect |
| 2720 | as follows: |
| 2721 | |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2722 | \begin{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2723 | { |
| 2724 | PyThreadState *_save; |
| 2725 | _save = PyThreadState_Swap(NULL); |
| 2726 | PyEval_ReleaseLock(); |
| 2727 | ...Do some blocking I/O operation... |
| 2728 | PyEval_AcquireLock(); |
| 2729 | PyThreadState_Swap(_save); |
| 2730 | } |
Guido van Rossum | 9faf4c5 | 1997-10-07 14:38:54 +0000 | [diff] [blame] | 2731 | \end{verbatim} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2732 | |
| 2733 | There are some subtle differences; in particular, |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2734 | \cfunction{PyEval_RestoreThread()} saves and restores the value of the |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2735 | global variable \cdata{errno}, since the lock manipulation does not |
| 2736 | guarantee that \cdata{errno} is left alone. Also, when thread support |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2737 | is disabled, \cfunction{PyEval_SaveThread()} and |
| 2738 | \cfunction{PyEval_RestoreThread()} don't manipulate the lock; in this |
| 2739 | case, \cfunction{PyEval_ReleaseLock()} and |
| 2740 | \cfunction{PyEval_AcquireLock()} are not available. This is done so |
| 2741 | that dynamically loaded extensions compiled with thread support |
| 2742 | enabled can be loaded by an interpreter that was compiled with |
| 2743 | disabled thread support. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2744 | |
| 2745 | The global interpreter lock is used to protect the pointer to the |
| 2746 | current thread state. When releasing the lock and saving the thread |
| 2747 | state, the current thread state pointer must be retrieved before the |
| 2748 | lock is released (since another thread could immediately acquire the |
| 2749 | lock and store its own thread state in the global variable). |
| 2750 | Reversely, when acquiring the lock and restoring the thread state, the |
| 2751 | lock must be acquired before storing the thread state pointer. |
| 2752 | |
| 2753 | Why am I going on with so much detail about this? Because when |
Fred Drake | b0a7873 | 1998-01-13 18:51:10 +0000 | [diff] [blame] | 2754 | 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] | 2755 | lock, nor is there a thread state data structure for them. Such |
| 2756 | threads must bootstrap themselves into existence, by first creating a |
| 2757 | thread state data structure, then acquiring the lock, and finally |
| 2758 | storing their thread state pointer, before they can start using the |
| 2759 | Python/C API. When they are done, they should reset the thread state |
| 2760 | pointer, release the lock, and finally free their thread state data |
| 2761 | structure. |
| 2762 | |
| 2763 | When creating a thread data structure, you need to provide an |
| 2764 | interpreter state data structure. The interpreter state data |
| 2765 | structure hold global data that is shared by all threads in an |
| 2766 | interpreter, for example the module administration |
| 2767 | (\code{sys.modules}). Depending on your needs, you can either create |
| 2768 | a new interpreter state data structure, or share the interpreter state |
| 2769 | data structure used by the Python main thread (to access the latter, |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2770 | 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] | 2771 | this must be done by a thread that is created by Python or by the main |
| 2772 | thread after Python is initialized). |
| 2773 | |
| 2774 | XXX More? |
| 2775 | |
| 2776 | \begin{ctypedesc}{PyInterpreterState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2777 | This data structure represents the state shared by a number of |
| 2778 | cooperating threads. Threads belonging to the same interpreter |
| 2779 | share their module administration and a few other internal items. |
| 2780 | There are no public members in this structure. |
| 2781 | |
| 2782 | Threads belonging to different interpreters initially share nothing, |
| 2783 | except process state like available memory, open file descriptors and |
| 2784 | such. The global interpreter lock is also shared by all threads, |
| 2785 | regardless of to which interpreter they belong. |
| 2786 | \end{ctypedesc} |
| 2787 | |
| 2788 | \begin{ctypedesc}{PyThreadState} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2789 | This data structure represents the state of a single thread. The only |
Fred Drake | f8830d1 | 1998-04-23 14:06:01 +0000 | [diff] [blame] | 2790 | public data member is \ctype{PyInterpreterState *}\member{interp}, |
| 2791 | which points to this thread's interpreter state. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2792 | \end{ctypedesc} |
| 2793 | |
| 2794 | \begin{cfuncdesc}{void}{PyEval_InitThreads}{} |
| 2795 | Initialize and acquire the global interpreter lock. It should be |
| 2796 | called in the main thread before creating a second thread or engaging |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2797 | in any other thread operations such as |
| 2798 | \cfunction{PyEval_ReleaseLock()} or |
| 2799 | \code{PyEval_ReleaseThread(\var{tstate})}. It is not needed before |
| 2800 | calling \cfunction{PyEval_SaveThread()} or |
| 2801 | \cfunction{PyEval_RestoreThread()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2802 | |
| 2803 | This is a no-op when called for a second time. It is safe to call |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2804 | this function before calling \cfunction{Py_Initialize()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2805 | |
| 2806 | When only the main thread exists, no lock operations are needed. This |
| 2807 | is a common situation (most Python programs do not use threads), and |
| 2808 | the lock operations slow the interpreter down a bit. Therefore, the |
| 2809 | lock is not created initially. This situation is equivalent to having |
| 2810 | acquired the lock: when there is only a single thread, all object |
| 2811 | accesses are safe. Therefore, when this function initializes the |
Fred Drake | 4de05a9 | 1998-02-16 14:25:26 +0000 | [diff] [blame] | 2812 | lock, it also acquires it. Before the Python |
| 2813 | \module{thread}\refbimodindex{thread} module creates a new thread, |
| 2814 | knowing that either it has the lock or the lock hasn't been created |
| 2815 | yet, it calls \cfunction{PyEval_InitThreads()}. When this call |
| 2816 | returns, it is guaranteed that the lock has been created and that it |
| 2817 | has acquired it. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2818 | |
| 2819 | It is \strong{not} safe to call this function when it is unknown which |
| 2820 | thread (if any) currently has the global interpreter lock. |
| 2821 | |
| 2822 | This function is not available when thread support is disabled at |
| 2823 | compile time. |
| 2824 | \end{cfuncdesc} |
| 2825 | |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2826 | \begin{cfuncdesc}{void}{PyEval_AcquireLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2827 | Acquire the global interpreter lock. The lock must have been created |
| 2828 | earlier. If this thread already has the lock, a deadlock ensues. |
| 2829 | This function is not available when thread support is disabled at |
| 2830 | compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2831 | \end{cfuncdesc} |
| 2832 | |
| 2833 | \begin{cfuncdesc}{void}{PyEval_ReleaseLock}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2834 | Release the global interpreter lock. The lock must have been created |
| 2835 | earlier. This function is not available when thread support is |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2836 | disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2837 | \end{cfuncdesc} |
| 2838 | |
| 2839 | \begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2840 | Acquire the global interpreter lock and then set the current thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2841 | 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] | 2842 | have been created earlier. If this thread already has the lock, |
| 2843 | deadlock ensues. This function is not available when thread support |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2844 | is disabled at compile time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2845 | \end{cfuncdesc} |
| 2846 | |
| 2847 | \begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate} |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2848 | Reset the current thread state to \NULL{} and release the global |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2849 | interpreter lock. The lock must have been created earlier and must be |
| 2850 | 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] | 2851 | be \NULL{}, is only used to check that it represents the current |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2852 | thread state --- if it isn't, a fatal error is reported. This |
| 2853 | function is not available when thread support is disabled at compile |
| 2854 | time. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2855 | \end{cfuncdesc} |
| 2856 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2857 | \begin{cfuncdesc}{PyThreadState*}{PyEval_SaveThread}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2858 | Release the interpreter lock (if it has been created and thread |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2859 | support is enabled) and reset the thread state to \NULL{}, |
| 2860 | returning the previous thread state (which is not \NULL{}). If |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2861 | the lock has been created, the current thread must have acquired it. |
| 2862 | (This function is available even when thread support is disabled at |
| 2863 | compile time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2864 | \end{cfuncdesc} |
| 2865 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2866 | \begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2867 | Acquire the interpreter lock (if it has been created and thread |
| 2868 | 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] | 2869 | 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] | 2870 | thread must not have acquired it, otherwise deadlock ensues. (This |
| 2871 | function is available even when thread support is disabled at compile |
| 2872 | time.) |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2873 | \end{cfuncdesc} |
| 2874 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2875 | % XXX These aren't really C types, but the ctypedesc macro is the simplest! |
| 2876 | \begin{ctypedesc}{Py_BEGIN_ALLOW_THREADS} |
| 2877 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2878 | \samp{\{ PyThreadState *_save; _save = PyEval_SaveThread();}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2879 | Note that it contains an opening brace; it must be matched with a |
| 2880 | following \code{Py_END_ALLOW_THREADS} macro. See above for further |
| 2881 | discussion of this macro. It is a no-op when thread support is |
| 2882 | disabled at compile time. |
| 2883 | \end{ctypedesc} |
| 2884 | |
| 2885 | \begin{ctypedesc}{Py_END_ALLOW_THREADS} |
| 2886 | This macro expands to |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2887 | \samp{PyEval_RestoreThread(_save); \}}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2888 | Note that it contains a closing brace; it must be matched with an |
| 2889 | earlier \code{Py_BEGIN_ALLOW_THREADS} macro. See above for further |
| 2890 | discussion of this macro. It is a no-op when thread support is |
| 2891 | disabled at compile time. |
| 2892 | \end{ctypedesc} |
| 2893 | |
| 2894 | \begin{ctypedesc}{Py_BEGIN_BLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2895 | This macro expands to \samp{PyEval_RestoreThread(_save);} i.e. it |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2896 | is equivalent to \code{Py_END_ALLOW_THREADS} without the closing |
| 2897 | brace. It is a no-op when thread support is disabled at compile |
| 2898 | time. |
| 2899 | \end{ctypedesc} |
| 2900 | |
| 2901 | \begin{ctypedesc}{Py_BEGIN_UNBLOCK_THREADS} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2902 | 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] | 2903 | equivalent to \code{Py_BEGIN_ALLOW_THREADS} without the opening brace |
| 2904 | and variable declaration. It is a no-op when thread support is |
| 2905 | disabled at compile time. |
| 2906 | \end{ctypedesc} |
| 2907 | |
| 2908 | All of the following functions are only available when thread support |
| 2909 | is enabled at compile time, and must be called only when the |
Fred Drake | 9d20ac3 | 1998-02-16 15:27:08 +0000 | [diff] [blame] | 2910 | interpreter lock has been created. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2911 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2912 | \begin{cfuncdesc}{PyInterpreterState*}{PyInterpreterState_New}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2913 | Create a new interpreter state object. The interpreter lock must be |
| 2914 | held. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2915 | \end{cfuncdesc} |
| 2916 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2917 | \begin{cfuncdesc}{void}{PyInterpreterState_Clear}{PyInterpreterState *interp} |
| 2918 | Reset all information in an interpreter state object. The interpreter |
| 2919 | lock must be held. |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2920 | \end{cfuncdesc} |
| 2921 | |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2922 | \begin{cfuncdesc}{void}{PyInterpreterState_Delete}{PyInterpreterState *interp} |
| 2923 | Destroy an interpreter state object. The interpreter lock need not be |
| 2924 | held. The interpreter state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2925 | call to \cfunction{PyInterpreterState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2926 | \end{cfuncdesc} |
| 2927 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2928 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_New}{PyInterpreterState *interp} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2929 | Create a new thread state object belonging to the given interpreter |
| 2930 | object. The interpreter lock must be held. |
| 2931 | \end{cfuncdesc} |
| 2932 | |
| 2933 | \begin{cfuncdesc}{void}{PyThreadState_Clear}{PyThreadState *tstate} |
| 2934 | Reset all information in a thread state object. The interpreter lock |
| 2935 | must be held. |
| 2936 | \end{cfuncdesc} |
| 2937 | |
| 2938 | \begin{cfuncdesc}{void}{PyThreadState_Delete}{PyThreadState *tstate} |
| 2939 | Destroy a thread state object. The interpreter lock need not be |
| 2940 | held. The thread state must have been reset with a previous |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2941 | call to \cfunction{PyThreadState_Clear()}. |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2942 | \end{cfuncdesc} |
| 2943 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2944 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Get}{} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2945 | Return the current thread state. The interpreter lock must be held. |
Guido van Rossum | 580aa8d | 1997-11-25 15:34:51 +0000 | [diff] [blame] | 2946 | When the current thread state is \NULL{}, this issues a fatal |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 2947 | error (so that the caller needn't check for \NULL{}). |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2948 | \end{cfuncdesc} |
| 2949 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2950 | \begin{cfuncdesc}{PyThreadState*}{PyThreadState_Swap}{PyThreadState *tstate} |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2951 | 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] | 2952 | argument \var{tstate}, which may be \NULL{}. The interpreter lock |
Guido van Rossum | c44d3d6 | 1997-10-06 05:10:47 +0000 | [diff] [blame] | 2953 | must be held. |
| 2954 | \end{cfuncdesc} |
| 2955 | |
| 2956 | |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2957 | \chapter{Defining New Object Types} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 2958 | \label{newTypes} |
Guido van Rossum | 4a944d7 | 1997-08-14 20:35:38 +0000 | [diff] [blame] | 2959 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2960 | \begin{cfuncdesc}{PyObject*}{_PyObject_New}{PyTypeObject *type} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2961 | \end{cfuncdesc} |
| 2962 | |
Fred Drake | c6fa34e | 1998-04-02 06:47:24 +0000 | [diff] [blame] | 2963 | \begin{cfuncdesc}{PyObject*}{_PyObject_NewVar}{PyTypeObject *type, int size} |
Fred Drake | e058b4f | 1998-02-16 06:15:35 +0000 | [diff] [blame] | 2964 | \end{cfuncdesc} |
| 2965 | |
| 2966 | \begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *} |
| 2967 | \end{cfuncdesc} |
| 2968 | |
| 2969 | \begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size} |
| 2970 | \end{cfuncdesc} |
| 2971 | |
Guido van Rossum | 3c4378b | 1998-04-14 20:21:10 +0000 | [diff] [blame] | 2972 | Py_InitModule (!!!) |
| 2973 | |
| 2974 | PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse |
| 2975 | |
| 2976 | Py_BuildValue |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 2977 | |
| 2978 | PyObject, PyVarObject |
| 2979 | |
| 2980 | PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD |
| 2981 | |
| 2982 | Typedefs: |
| 2983 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 2984 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
| 2985 | getreadbufferproc, getwritebufferproc, getsegcountproc, |
| 2986 | destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 2987 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 2988 | |
| 2989 | PyNumberMethods |
| 2990 | |
| 2991 | PySequenceMethods |
| 2992 | |
| 2993 | PyMappingMethods |
| 2994 | |
| 2995 | PyBufferProcs |
| 2996 | |
| 2997 | PyTypeObject |
| 2998 | |
| 2999 | DL_IMPORT |
| 3000 | |
| 3001 | PyType_Type |
| 3002 | |
| 3003 | Py*_Check |
| 3004 | |
| 3005 | Py_None, _Py_NoneStruct |
| 3006 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 3007 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3008 | \chapter{Debugging} |
Fred Drake | f39ed67 | 1998-02-26 22:01:23 +0000 | [diff] [blame] | 3009 | \label{debugging} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 3010 | |
Fred Drake | e5bf8b2 | 1998-02-12 21:22:28 +0000 | [diff] [blame] | 3011 | XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
Guido van Rossum | 5b8a523 | 1997-12-30 04:38:44 +0000 | [diff] [blame] | 3012 | |
| 3013 | |
Fred Drake | f3aa0e0 | 1998-03-17 06:23:13 +0000 | [diff] [blame] | 3014 | \input{api.ind} % Index -- must be last |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 3015 | |
| 3016 | \end{document} |