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