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