Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_OBJECT_H |
| 2 | #define Py_OBJECT_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | /*********************************************************** |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
| 11 | All Rights Reserved |
| 12 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 15 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 16 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 17 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 18 | Centrum or CWI or Corporation for National Research Initiatives or |
| 19 | CNRI not be used in advertising or publicity pertaining to |
| 20 | distribution of the software without specific, written prior |
| 21 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 23 | While CWI is the initial source for this software, a modified version |
| 24 | is made available by the Corporation for National Research Initiatives |
| 25 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 26 | |
| 27 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 28 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 29 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 30 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 31 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 32 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 33 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 34 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 35 | |
| 36 | ******************************************************************/ |
| 37 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | /* Object and type object interface */ |
| 39 | |
| 40 | /* |
| 41 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 42 | |
| 43 | Objects are structures allocated on the heap. Special rules apply to |
| 44 | the use of objects to ensure they are properly garbage-collected. |
| 45 | Objects are never allocated statically or on the stack; they must be |
| 46 | accessed through special macros and functions only. (Type objects are |
| 47 | exceptions to the first rule; the standard types are represented by |
| 48 | statically initialized type objects.) |
| 49 | |
| 50 | An object has a 'reference count' that is increased or decreased when a |
| 51 | pointer to the object is copied or deleted; when the reference count |
| 52 | reaches zero there are no references to the object left and it can be |
| 53 | removed from the heap. |
| 54 | |
| 55 | An object has a 'type' that determines what it represents and what kind |
| 56 | of data it contains. An object's type is fixed when it is created. |
| 57 | Types themselves are represented as objects; an object contains a |
| 58 | pointer to the corresponding type object. The type itself has a type |
| 59 | pointer pointing to the object representing the type 'type', which |
| 60 | contains a pointer to itself!). |
| 61 | |
| 62 | Objects do not float around in memory; once allocated an object keeps |
| 63 | the same size and address. Objects that must hold variable-size data |
| 64 | can contain pointers to variable-size parts of the object. Not all |
| 65 | objects of the same type have the same size; but the size cannot change |
| 66 | after allocation. (These restrictions are made so a reference to an |
| 67 | object can be simply a pointer -- moving an object would require |
| 68 | updating all the pointers, and changing an object's size would require |
| 69 | moving it if there was another object right next to it.) |
| 70 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 71 | Objects are always accessed through pointers of the type 'PyObject *'. |
| 72 | The type 'PyObject' is a structure that only contains the reference count |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | and the type pointer. The actual memory allocated for an object |
| 74 | contains other data that can only be accessed after casting the pointer |
| 75 | to a pointer to a longer structure type. This longer type must start |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 76 | with the reference count and type fields; the macro PyObject_HEAD should be |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | used for this (to accomodate for future changes). The implementation |
| 78 | of a particular object type can cast the object pointer to the proper |
| 79 | type and back. |
| 80 | |
| 81 | A standard interface exists for objects that contain an array of items |
| 82 | whose size is determined when the object is allocated. |
| 83 | |
| 84 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 85 | */ |
| 86 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 87 | #ifdef Py_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 88 | |
| 89 | /* Turn on heavy reference debugging */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 90 | #define Py_TRACE_REFS |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | |
| 92 | /* Turn on reference counting */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 93 | #define Py_REF_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 95 | #endif /* Py_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 97 | #ifdef Py_TRACE_REFS |
| 98 | #define PyObject_HEAD \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | struct _object *_ob_next, *_ob_prev; \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 100 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 102 | #define PyObject_HEAD_INIT(type) 0, 0, 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 103 | #else /* !Py_TRACE_REFS */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 104 | #define PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 105 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 107 | #define PyObject_HEAD_INIT(type) 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 108 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 110 | #define PyObject_VAR_HEAD \ |
| 111 | PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 112 | int ob_size; /* Number of items in variable part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | |
| 114 | typedef struct _object { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 115 | PyObject_HEAD |
| 116 | } PyObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | |
| 118 | typedef struct { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 119 | PyObject_VAR_HEAD |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 120 | } PyVarObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | /* |
| 124 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 125 | |
| 126 | Type objects contain a string containing the type name (to help somewhat |
| 127 | in debugging), the allocation parameters (see newobj() and newvarobj()), |
| 128 | and methods for accessing objects of the type. Methods are optional,a |
| 129 | nil pointer meaning that particular kind of access is not available for |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 130 | this type. The Py_DECREF() macro uses the tp_dealloc method without |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 131 | checking for a nil pointer; it should always be implemented except if |
| 132 | the implementation can guarantee that the reference count will never |
| 133 | reach zero (e.g., for type objects). |
| 134 | |
| 135 | NB: the methods for certain type groups are now contained in separate |
| 136 | method blocks. |
| 137 | */ |
| 138 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 139 | typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *)); |
| 140 | typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *)); |
| 141 | typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
| 142 | typedef int (*inquiry) Py_PROTO((PyObject *)); |
| 143 | typedef int (*coercion) Py_PROTO((PyObject **, PyObject **)); |
| 144 | typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int)); |
| 145 | typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int)); |
| 146 | typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *)); |
| 147 | typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *)); |
| 148 | typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 149 | typedef int (*getreadbufferproc) Py_PROTO((PyObject *, int, void **)); |
| 150 | typedef int (*getwritebufferproc) Py_PROTO((PyObject *, int, void **)); |
| 151 | typedef int (*getsegcountproc) Py_PROTO((PyObject *, int *)); |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 152 | typedef int (*getcharbufferproc) Py_PROTO((PyObject *, int, const char **)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | binaryfunc nb_add; |
| 156 | binaryfunc nb_subtract; |
| 157 | binaryfunc nb_multiply; |
| 158 | binaryfunc nb_divide; |
| 159 | binaryfunc nb_remainder; |
| 160 | binaryfunc nb_divmod; |
Guido van Rossum | 75abc63 | 1994-08-09 13:21:54 +0000 | [diff] [blame] | 161 | ternaryfunc nb_power; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | unaryfunc nb_negative; |
| 163 | unaryfunc nb_positive; |
| 164 | unaryfunc nb_absolute; |
| 165 | inquiry nb_nonzero; |
| 166 | unaryfunc nb_invert; |
| 167 | binaryfunc nb_lshift; |
| 168 | binaryfunc nb_rshift; |
| 169 | binaryfunc nb_and; |
| 170 | binaryfunc nb_xor; |
| 171 | binaryfunc nb_or; |
| 172 | coercion nb_coerce; |
| 173 | unaryfunc nb_int; |
| 174 | unaryfunc nb_long; |
| 175 | unaryfunc nb_float; |
| 176 | unaryfunc nb_oct; |
| 177 | unaryfunc nb_hex; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 178 | } PyNumberMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | |
| 180 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | inquiry sq_length; |
| 182 | binaryfunc sq_concat; |
| 183 | intargfunc sq_repeat; |
| 184 | intargfunc sq_item; |
| 185 | intintargfunc sq_slice; |
| 186 | intobjargproc sq_ass_item; |
| 187 | intintobjargproc sq_ass_slice; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 188 | } PySequenceMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | |
| 190 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | inquiry mp_length; |
| 192 | binaryfunc mp_subscript; |
| 193 | objobjargproc mp_ass_subscript; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 194 | } PyMappingMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 196 | typedef struct { |
| 197 | getreadbufferproc bf_getreadbuffer; |
| 198 | getwritebufferproc bf_getwritebuffer; |
| 199 | getsegcountproc bf_getsegcount; |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 200 | getcharbufferproc bf_getcharbuffer; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 201 | } PyBufferProcs; |
| 202 | |
| 203 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 204 | typedef void (*destructor) Py_PROTO((PyObject *)); |
| 205 | typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int)); |
| 206 | typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *)); |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame] | 207 | typedef PyObject *(*getattrofunc) Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 208 | typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *)); |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame] | 209 | typedef int (*setattrofunc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 210 | typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *)); |
| 211 | typedef PyObject *(*reprfunc) Py_PROTO((PyObject *)); |
| 212 | typedef long (*hashfunc) Py_PROTO((PyObject *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | typedef struct _typeobject { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 215 | PyObject_VAR_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | char *tp_name; /* For printing */ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 217 | int tp_basicsize, tp_itemsize; /* For allocation */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | |
| 219 | /* Methods to implement standard operations */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | destructor tp_dealloc; |
| 222 | printfunc tp_print; |
| 223 | getattrfunc tp_getattr; |
| 224 | setattrfunc tp_setattr; |
| 225 | cmpfunc tp_compare; |
| 226 | reprfunc tp_repr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 227 | |
| 228 | /* Method suites for standard classes */ |
| 229 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 230 | PyNumberMethods *tp_as_number; |
| 231 | PySequenceMethods *tp_as_sequence; |
| 232 | PyMappingMethods *tp_as_mapping; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 233 | |
| 234 | /* More standard operations (at end for binary compatibility) */ |
| 235 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | hashfunc tp_hash; |
Guido van Rossum | 884afd6 | 1995-07-18 14:21:06 +0000 | [diff] [blame] | 237 | ternaryfunc tp_call; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 238 | reprfunc tp_str; |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame] | 239 | getattrofunc tp_getattro; |
| 240 | setattrofunc tp_setattro; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 242 | /* Functions to access object as input/output buffer */ |
| 243 | PyBufferProcs *tp_as_buffer; |
| 244 | |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 245 | /* Flags to define presence of optional/expanded features */ |
| 246 | long tp_flags; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 247 | |
| 248 | char *tp_doc; /* Documentation string */ |
| 249 | |
Guido van Rossum | a9c2d7a | 1998-04-23 19:16:44 +0000 | [diff] [blame] | 250 | /* More spares */ |
| 251 | long tp_xxx5; |
| 252 | long tp_xxx6; |
| 253 | long tp_xxx7; |
| 254 | long tp_xxx8; |
| 255 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 256 | #ifdef COUNT_ALLOCS |
| 257 | /* these must be last */ |
| 258 | int tp_alloc; |
| 259 | int tp_free; |
| 260 | int tp_maxalloc; |
| 261 | struct _typeobject *tp_next; |
| 262 | #endif |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 263 | } PyTypeObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | |
Sjoerd Mullender | 107c747 | 1995-04-25 11:53:24 +0000 | [diff] [blame] | 265 | extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 267 | #define PyType_Check(op) ((op)->ob_type == &PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 269 | /* Generic operations on objects */ |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 270 | extern DL_IMPORT(int) PyObject_Print Py_PROTO((PyObject *, FILE *, int)); |
| 271 | extern DL_IMPORT(PyObject *) PyObject_Repr Py_PROTO((PyObject *)); |
| 272 | extern DL_IMPORT(PyObject *) PyObject_Str Py_PROTO((PyObject *)); |
| 273 | extern DL_IMPORT(int) PyObject_Compare Py_PROTO((PyObject *, PyObject *)); |
| 274 | extern DL_IMPORT(PyObject *) PyObject_GetAttrString Py_PROTO((PyObject *, char *)); |
| 275 | extern DL_IMPORT(int) PyObject_SetAttrString Py_PROTO((PyObject *, char *, PyObject *)); |
| 276 | extern DL_IMPORT(int) PyObject_HasAttrString Py_PROTO((PyObject *, char *)); |
| 277 | extern DL_IMPORT(PyObject *) PyObject_GetAttr Py_PROTO((PyObject *, PyObject *)); |
| 278 | extern DL_IMPORT(int) PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *)); |
| 279 | extern DL_IMPORT(int) PyObject_HasAttr Py_PROTO((PyObject *, PyObject *)); |
| 280 | extern DL_IMPORT(long) PyObject_Hash Py_PROTO((PyObject *)); |
| 281 | extern DL_IMPORT(int) PyObject_IsTrue Py_PROTO((PyObject *)); |
| 282 | extern DL_IMPORT(int) PyObject_Not Py_PROTO((PyObject *)); |
| 283 | extern DL_IMPORT(int) PyCallable_Check Py_PROTO((PyObject *)); |
| 284 | extern DL_IMPORT(int) PyNumber_Coerce Py_PROTO((PyObject **, PyObject **)); |
| 285 | extern DL_IMPORT(int) PyNumber_CoerceEx Py_PROTO((PyObject **, PyObject **)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 287 | /* Helpers for printing recursive container types */ |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 288 | extern DL_IMPORT(int) Py_ReprEnter Py_PROTO((PyObject *)); |
| 289 | extern DL_IMPORT(void) Py_ReprLeave Py_PROTO((PyObject *)); |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | /* Flag bits for printing: */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 292 | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | |
| 294 | /* |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 295 | |
| 296 | Type flags (tp_flags) |
| 297 | |
| 298 | These flags are used to extend the type structure in a backwards-compatible |
| 299 | fashion. Extensions can use the flags to indicate (and test) when a given |
| 300 | type structure contains a new feature. The Python core will use these when |
| 301 | introducing new functionality between major revisions (to avoid mid-version |
| 302 | changes in the PYTHON_API_VERSION). |
| 303 | |
| 304 | Arbitration of the flag bit positions will need to be coordinated among |
| 305 | all extension writers who publically release their extensions (this will |
| 306 | be fewer than you might expect!).. |
| 307 | |
| 308 | Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs. |
| 309 | |
| 310 | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. |
| 311 | |
| 312 | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the |
| 313 | given type object has a specified feature. |
| 314 | |
| 315 | */ |
| 316 | |
| 317 | /* PyBufferProcs contains bf_getcharbuffer */ |
| 318 | #define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0) |
| 319 | |
| 320 | #define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER) |
| 321 | |
| 322 | #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0) |
| 323 | |
| 324 | |
| 325 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 327 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 328 | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement |
| 329 | reference counts. Py_DECREF calls the object's deallocator function; for |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 330 | objects that don't contain references to other objects or heap memory |
| 331 | this can be the standard function free(). Both macros can be used |
| 332 | whereever a void expression is allowed. The argument shouldn't be a |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 333 | NIL pointer. The macro _Py_NewReference(op) is used only to initialize |
| 334 | reference counts to 1; it is defined here for convenience. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | |
| 336 | We assume that the reference count field can never overflow; this can |
| 337 | be proven when the size of the field is the same as the pointer size |
| 338 | but even with a 16-bit reference count field it is pretty unlikely so |
| 339 | we ignore the possibility. (If you are paranoid, make it a long.) |
| 340 | |
| 341 | Type objects should never be deallocated; the type pointer in an object |
| 342 | is not considered to be a reference to the type object, to save |
| 343 | complications in the deallocation function. (This is actually a |
| 344 | decision that's up to the implementer of each new type so if you want, |
| 345 | you can count such references to the type object.) |
| 346 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 347 | *** WARNING*** The Py_DECREF macro must have a side-effect-free argument |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | since it may evaluate its argument multiple times. (The alternative |
| 349 | would be to mace it a proper function or assign it to a global temporary |
| 350 | variable first, both of which are slower; and in a multi-threaded |
| 351 | environment the global variable trick is not safe.) |
| 352 | */ |
| 353 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 354 | #ifdef Py_TRACE_REFS |
| 355 | #ifndef Py_REF_DEBUG |
| 356 | #define Py_REF_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | #endif |
| 358 | #endif |
| 359 | |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 360 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 361 | extern DL_IMPORT(void) _Py_Dealloc Py_PROTO((PyObject *)); |
| 362 | extern DL_IMPORT(void) _Py_NewReference Py_PROTO((PyObject *)); |
| 363 | extern DL_IMPORT(void) _Py_ForgetReference Py_PROTO((PyObject *)); |
| 364 | extern DL_IMPORT(void) _Py_PrintReferences Py_PROTO((FILE *)); |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 367 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 368 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 369 | #define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op))) |
Sjoerd Mullender | 91e7a0b | 1995-04-06 13:47:48 +0000 | [diff] [blame] | 370 | #define _Py_ForgetReference(op) ((op)->ob_type->tp_free++) |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 371 | #else /* !COUNT_ALLOCS */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 372 | #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op)) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 373 | #define _Py_ForgetReference(op) /*empty*/ |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 374 | #endif /* !COUNT_ALLOCS */ |
| 375 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 377 | #ifdef COUNT_ALLOCS |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 378 | extern DL_IMPORT(void) inc_count Py_PROTO((PyTypeObject *)); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 379 | #endif |
| 380 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 381 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 382 | |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 383 | extern DL_IMPORT(long) _Py_RefTotal; |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 384 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 385 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 386 | #ifdef COUNT_ALLOCS |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 387 | #define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 388 | #else |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 389 | #define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 391 | #endif /* !Py_TRACE_REFS */ |
| 392 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 393 | #define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 394 | #define Py_DECREF(op) \ |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 395 | if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | ; \ |
| 397 | else \ |
Guido van Rossum | 1d529d1 | 1997-08-05 02:30:44 +0000 | [diff] [blame] | 398 | _Py_Dealloc((PyObject *)(op)) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 399 | #else /* !Py_REF_DEBUG */ |
| 400 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 401 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 402 | #define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 403 | #else |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 404 | #define _Py_NewReference(op) ((op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 405 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 407 | #define Py_INCREF(op) ((op)->ob_refcnt++) |
| 408 | #define Py_DECREF(op) \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 409 | if (--(op)->ob_refcnt != 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | ; \ |
| 411 | else \ |
Guido van Rossum | 1d529d1 | 1997-08-05 02:30:44 +0000 | [diff] [blame] | 412 | _Py_Dealloc((PyObject *)(op)) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 413 | #endif /* !Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 415 | /* Macros to use in case the object pointer may be NULL: */ |
| 416 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 417 | #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op) |
| 418 | #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | |
| 420 | /* Definition of NULL, so you don't have to include <stdio.h> */ |
| 421 | |
| 422 | #ifndef NULL |
| 423 | #define NULL 0 |
| 424 | #endif |
| 425 | |
| 426 | |
| 427 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 428 | _Py_NoneStruct is an object of undefined type which can be used in contexts |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 430 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 431 | Don't forget to apply Py_INCREF() when returning this value!!! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | */ |
| 433 | |
Sjoerd Mullender | 107c747 | 1995-04-25 11:53:24 +0000 | [diff] [blame] | 434 | extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 436 | #define Py_None (&_Py_NoneStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | |
| 438 | |
| 439 | /* |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 440 | A common programming style in Python requires the forward declaration |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 441 | of static, initialized structures, e.g. for a type object that is used |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 442 | by the functions whose address must be used in the initializer. |
| 443 | Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as |
| 444 | well) botch this if you use the static keyword for both declarations |
| 445 | (they allocate two objects, and use the first, uninitialized one until |
| 446 | the second declaration is encountered). Therefore, the forward |
| 447 | declaration should use the 'forwardstatic' keyword. This expands to |
| 448 | static on most systems, but to extern on a few. The actual storage |
| 449 | and name will still be static because the second declaration is |
| 450 | static, so no linker visible symbols will be generated. (Standard C |
| 451 | compilers take offense to the extern forward declaration of a static |
| 452 | object, so I can't just put extern in all cases. :-( ) |
| 453 | */ |
| 454 | |
| 455 | #ifdef BAD_STATIC_FORWARD |
| 456 | #define staticforward extern |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 457 | #ifdef __SC__ |
| 458 | #define statichere |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 459 | #else |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 460 | #define statichere static |
| 461 | #endif /* __SC__ */ |
| 462 | #else /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 463 | #define staticforward static |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 464 | #define statichere static |
| 465 | #endif /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 466 | |
| 467 | |
| 468 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 469 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 470 | |
| 471 | More conventions |
| 472 | ================ |
| 473 | |
| 474 | Argument Checking |
| 475 | ----------------- |
| 476 | |
| 477 | Functions that take objects as arguments normally don't check for nil |
| 478 | arguments, but they do check the type of the argument, and return an |
| 479 | error if the function doesn't apply to the type. |
| 480 | |
| 481 | Failure Modes |
| 482 | ------------- |
| 483 | |
| 484 | Functions may fail for a variety of reasons, including running out of |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 485 | memory. This is communicated to the caller in two ways: an error string |
| 486 | is set (see errors.h), and the function result differs: functions that |
| 487 | normally return a pointer return NULL for failure, functions returning |
| 488 | an integer return -1 (which could be a legal return value too!), and |
| 489 | other functions return 0 for success and -1 for failure. |
| 490 | Callers should always check for errors before using the result. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 491 | |
| 492 | Reference Counts |
| 493 | ---------------- |
| 494 | |
| 495 | It takes a while to get used to the proper usage of reference counts. |
| 496 | |
| 497 | Functions that create an object set the reference count to 1; such new |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 498 | objects must be stored somewhere or destroyed again with Py_DECREF(). |
| 499 | Functions that 'store' objects such as PyTuple_SetItem() and |
| 500 | PyDict_SetItemString() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | don't increment the reference count of the object, since the most |
| 502 | frequent use is to store a fresh object. Functions that 'retrieve' |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 503 | objects such as PyTuple_GetItem() and PyDict_GetItemString() also |
| 504 | don't increment |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 505 | the reference count, since most frequently the object is only looked at |
| 506 | quickly. Thus, to retrieve an object and store it again, the caller |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 507 | must call Py_INCREF() explicitly. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 509 | NOTE: functions that 'consume' a reference count like |
Fred Drake | 49bb0e3 | 1997-09-05 17:53:53 +0000 | [diff] [blame] | 510 | PyList_SetItemString() even consume the reference if the object wasn't |
| 511 | stored, to simplify error handling. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | |
| 513 | It seems attractive to make other functions that take an object as |
| 514 | argument consume a reference count; however this may quickly get |
| 515 | confusing (even the current practice is already confusing). Consider |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 516 | it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | times. |
| 518 | |
| 519 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 520 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 521 | |
| 522 | #ifdef __cplusplus |
| 523 | } |
| 524 | #endif |
| 525 | #endif /* !Py_OBJECT_H */ |