(py-outdent-p): new function
[python/dscho.git] / Include / object.h
blob0b679d544d0c1d9402a5edf9df11b4ce9f2c0805
1 #ifndef Py_OBJECT_H
2 #define Py_OBJECT_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
7 /***********************************************************
8 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9 The Netherlands.
11 All Rights Reserved
13 Permission to use, copy, modify, and distribute this software and its
14 documentation for any purpose and without fee is hereby granted,
15 provided that the above copyright notice appear in all copies and that
16 both that copyright notice and this permission notice appear in
17 supporting documentation, and that the names of Stichting Mathematisch
18 Centrum or CWI not be used in advertising or publicity pertaining to
19 distribution of the software without specific, written prior permission.
21 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
22 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
24 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 ******************************************************************/
31 #ifndef DEBUG
32 #define NDEBUG
33 #endif
35 /* Object and type object interface */
38 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
40 Objects are structures allocated on the heap. Special rules apply to
41 the use of objects to ensure they are properly garbage-collected.
42 Objects are never allocated statically or on the stack; they must be
43 accessed through special macros and functions only. (Type objects are
44 exceptions to the first rule; the standard types are represented by
45 statically initialized type objects.)
47 An object has a 'reference count' that is increased or decreased when a
48 pointer to the object is copied or deleted; when the reference count
49 reaches zero there are no references to the object left and it can be
50 removed from the heap.
52 An object has a 'type' that determines what it represents and what kind
53 of data it contains. An object's type is fixed when it is created.
54 Types themselves are represented as objects; an object contains a
55 pointer to the corresponding type object. The type itself has a type
56 pointer pointing to the object representing the type 'type', which
57 contains a pointer to itself!).
59 Objects do not float around in memory; once allocated an object keeps
60 the same size and address. Objects that must hold variable-size data
61 can contain pointers to variable-size parts of the object. Not all
62 objects of the same type have the same size; but the size cannot change
63 after allocation. (These restrictions are made so a reference to an
64 object can be simply a pointer -- moving an object would require
65 updating all the pointers, and changing an object's size would require
66 moving it if there was another object right next to it.)
68 Objects are always accessed through pointers of the type 'PyObject *'.
69 The type 'PyObject' is a structure that only contains the reference count
70 and the type pointer. The actual memory allocated for an object
71 contains other data that can only be accessed after casting the pointer
72 to a pointer to a longer structure type. This longer type must start
73 with the reference count and type fields; the macro PyObject_HEAD should be
74 used for this (to accomodate for future changes). The implementation
75 of a particular object type can cast the object pointer to the proper
76 type and back.
78 A standard interface exists for objects that contain an array of items
79 whose size is determined when the object is allocated.
81 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
84 #ifndef NDEBUG
86 /* Turn on heavy reference debugging */
87 #define Py_TRACE_REFS
89 /* Turn on reference counting */
90 #define Py_REF_DEBUG
92 #endif /* NDEBUG */
94 #ifdef Py_TRACE_REFS
95 #define PyObject_HEAD \
96 struct _object *_ob_next, *_ob_prev; \
97 int ob_refcnt; \
98 struct _typeobject *ob_type;
99 #define PyObject_HEAD_INIT(type) 0, 0, 1, type,
100 #else
101 #define PyObject_HEAD \
102 int ob_refcnt; \
103 struct _typeobject *ob_type;
104 #define PyObject_HEAD_INIT(type) 1, type,
105 #endif
107 #define PyObject_VAR_HEAD \
108 PyObject_HEAD \
109 int ob_size; /* Number of items in variable part */
111 typedef struct _object {
112 PyObject_HEAD
113 } PyObject;
115 typedef struct {
116 PyObject_VAR_HEAD
117 } varobject;
121 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
123 Type objects contain a string containing the type name (to help somewhat
124 in debugging), the allocation parameters (see newobj() and newvarobj()),
125 and methods for accessing objects of the type. Methods are optional,a
126 nil pointer meaning that particular kind of access is not available for
127 this type. The Py_DECREF() macro uses the tp_dealloc method without
128 checking for a nil pointer; it should always be implemented except if
129 the implementation can guarantee that the reference count will never
130 reach zero (e.g., for type objects).
132 NB: the methods for certain type groups are now contained in separate
133 method blocks.
136 typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *));
137 typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *));
138 typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
139 typedef int (*inquiry) Py_PROTO((PyObject *));
140 typedef int (*coercion) Py_PROTO((PyObject **, PyObject **));
141 typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int));
142 typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int));
143 typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *));
144 typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *));
145 typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
147 typedef struct {
148 binaryfunc nb_add;
149 binaryfunc nb_subtract;
150 binaryfunc nb_multiply;
151 binaryfunc nb_divide;
152 binaryfunc nb_remainder;
153 binaryfunc nb_divmod;
154 ternaryfunc nb_power;
155 unaryfunc nb_negative;
156 unaryfunc nb_positive;
157 unaryfunc nb_absolute;
158 inquiry nb_nonzero;
159 unaryfunc nb_invert;
160 binaryfunc nb_lshift;
161 binaryfunc nb_rshift;
162 binaryfunc nb_and;
163 binaryfunc nb_xor;
164 binaryfunc nb_or;
165 coercion nb_coerce;
166 unaryfunc nb_int;
167 unaryfunc nb_long;
168 unaryfunc nb_float;
169 unaryfunc nb_oct;
170 unaryfunc nb_hex;
171 } PyNumberMethods;
173 typedef struct {
174 inquiry sq_length;
175 binaryfunc sq_concat;
176 intargfunc sq_repeat;
177 intargfunc sq_item;
178 intintargfunc sq_slice;
179 intobjargproc sq_ass_item;
180 intintobjargproc sq_ass_slice;
181 } PySequenceMethods;
183 typedef struct {
184 inquiry mp_length;
185 binaryfunc mp_subscript;
186 objobjargproc mp_ass_subscript;
187 } PyMappingMethods;
189 typedef void (*destructor) Py_PROTO((PyObject *));
190 typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int));
191 typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *));
192 typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *));
193 typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *));
194 typedef PyObject *(*reprfunc) Py_PROTO((PyObject *));
195 typedef long (*hashfunc) Py_PROTO((PyObject *));
197 typedef struct _typeobject {
198 PyObject_VAR_HEAD
199 char *tp_name; /* For printing */
200 int tp_basicsize, tp_itemsize; /* For allocation */
202 /* Methods to implement standard operations */
204 destructor tp_dealloc;
205 printfunc tp_print;
206 getattrfunc tp_getattr;
207 setattrfunc tp_setattr;
208 cmpfunc tp_compare;
209 reprfunc tp_repr;
211 /* Method suites for standard classes */
213 PyNumberMethods *tp_as_number;
214 PySequenceMethods *tp_as_sequence;
215 PyMappingMethods *tp_as_mapping;
217 /* More standard operations (at end for binary compatibility) */
219 hashfunc tp_hash;
220 binaryfunc tp_call;
221 reprfunc tp_str;
223 /* Space for future expansion */
224 long tp_xxx1;
225 long tp_xxx2;
226 long tp_xxx3;
227 long tp_xxx4;
229 char *tp_doc; /* Documentation string */
231 #ifdef COUNT_ALLOCS
232 /* these must be last */
233 int tp_alloc;
234 int tp_free;
235 int tp_maxalloc;
236 struct _typeobject *tp_next;
237 #endif
238 } PyTypeObject;
240 extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */
242 #define PyType_Check(op) ((op)->ob_type == &PyType_Type)
244 /* Generic operations on objects */
245 extern int PyObject_Print Py_PROTO((PyObject *, FILE *, int));
246 extern PyObject * PyObject_Repr Py_PROTO((PyObject *));
247 extern PyObject * PyObject_Str Py_PROTO((PyObject *));
248 extern int PyObject_Compare Py_PROTO((PyObject *, PyObject *));
249 extern PyObject *PyObject_GetAttrString Py_PROTO((PyObject *, char *));
250 extern int PyObject_HasAttrString Py_PROTO((PyObject *, char *));
251 extern PyObject *PyObject_GetAttr Py_PROTO((PyObject *, PyObject *));
252 extern int PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *));
253 extern long PyObject_Hash Py_PROTO((PyObject *));
254 extern int PyObject_IsTrue Py_PROTO((PyObject *));
255 extern int PyCallable_Check Py_PROTO((PyObject *));
257 /* Flag bits for printing: */
258 #define Py_PRINT_RAW 1 /* No string quotes etc. */
261 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
263 The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
264 reference counts. Py_DECREF calls the object's deallocator function; for
265 objects that don't contain references to other objects or heap memory
266 this can be the standard function free(). Both macros can be used
267 whereever a void expression is allowed. The argument shouldn't be a
268 NIL pointer. The macro _Py_NewReference(op) is used only to initialize
269 reference counts to 1; it is defined here for convenience.
271 We assume that the reference count field can never overflow; this can
272 be proven when the size of the field is the same as the pointer size
273 but even with a 16-bit reference count field it is pretty unlikely so
274 we ignore the possibility. (If you are paranoid, make it a long.)
276 Type objects should never be deallocated; the type pointer in an object
277 is not considered to be a reference to the type object, to save
278 complications in the deallocation function. (This is actually a
279 decision that's up to the implementer of each new type so if you want,
280 you can count such references to the type object.)
282 *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
283 since it may evaluate its argument multiple times. (The alternative
284 would be to mace it a proper function or assign it to a global temporary
285 variable first, both of which are slower; and in a multi-threaded
286 environment the global variable trick is not safe.)
289 #ifdef Py_TRACE_REFS
290 #ifndef Py_REF_DEBUG
291 #define Py_REF_DEBUG
292 #endif
293 #endif
295 #ifndef Py_TRACE_REFS
296 #ifdef COUNT_ALLOCS
297 #define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
298 #else
299 #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
300 #endif
301 #define _Py_ForgetReference(op) /*empty*/
302 #endif
304 #ifdef COUNT_ALLOCS
305 extern void inc_count Py_PROTO((PyTypeObject *));
306 #endif
308 #ifdef Py_REF_DEBUG
309 extern long ref_total;
310 #ifndef Py_TRACE_REFS
311 #ifdef COUNT_ALLOCS
312 #define _Py_NewReference(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1)
313 #else
314 #define _Py_NewReference(op) (ref_total++, (op)->ob_refcnt = 1)
315 #endif
316 #endif
317 #define Py_INCREF(op) (ref_total++, (op)->ob_refcnt++)
318 #define Py_DECREF(op) \
319 if (--ref_total, --(op)->ob_refcnt != 0) \
321 else \
322 _Py_Dealloc(op)
323 #else
324 #ifdef COUNT_ALLOCS
325 #define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
326 #else
327 #define _Py_NewReference(op) ((op)->ob_refcnt = 1)
328 #endif
329 #define Py_INCREF(op) ((op)->ob_refcnt++)
330 #define Py_DECREF(op) \
331 if (--(op)->ob_refcnt != 0) \
333 else \
334 _Py_Dealloc(op)
335 #endif
337 /* Macros to use in case the object pointer may be NULL: */
339 #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
340 #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
342 /* Definition of NULL, so you don't have to include <stdio.h> */
344 #ifndef NULL
345 #define NULL 0
346 #endif
350 _Py_NoneStruct is an object of undefined type which can be used in contexts
351 where NULL (nil) is not suitable (since NULL often means 'error').
353 Don't forget to apply Py_INCREF() when returning this value!!!
356 extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
358 #define Py_None (&_Py_NoneStruct)
362 A common programming style in Python requires the forward declaration
363 of static, initialized structures, e.g. for a type object that is used
364 by the functions whose address must be used in the initializer.
365 Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
366 well) botch this if you use the static keyword for both declarations
367 (they allocate two objects, and use the first, uninitialized one until
368 the second declaration is encountered). Therefore, the forward
369 declaration should use the 'forwardstatic' keyword. This expands to
370 static on most systems, but to extern on a few. The actual storage
371 and name will still be static because the second declaration is
372 static, so no linker visible symbols will be generated. (Standard C
373 compilers take offense to the extern forward declaration of a static
374 object, so I can't just put extern in all cases. :-( )
377 #ifdef BAD_STATIC_FORWARD
378 #define staticforward extern
379 #ifdef __SC__
380 #define statichere
381 #else
382 #define statichere static
383 #endif /* __SC__ */
384 #else /* !BAD_STATIC_FORWARD */
385 #define staticforward static
386 #define statichere static
387 #endif /* !BAD_STATIC_FORWARD */
391 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
393 More conventions
394 ================
396 Argument Checking
397 -----------------
399 Functions that take objects as arguments normally don't check for nil
400 arguments, but they do check the type of the argument, and return an
401 error if the function doesn't apply to the type.
403 Failure Modes
404 -------------
406 Functions may fail for a variety of reasons, including running out of
407 memory. This is communicated to the caller in two ways: an error string
408 is set (see errors.h), and the function result differs: functions that
409 normally return a pointer return NULL for failure, functions returning
410 an integer return -1 (which could be a legal return value too!), and
411 other functions return 0 for success and -1 for failure.
412 Callers should always check for errors before using the result.
414 Reference Counts
415 ----------------
417 It takes a while to get used to the proper usage of reference counts.
419 Functions that create an object set the reference count to 1; such new
420 objects must be stored somewhere or destroyed again with Py_DECREF().
421 Functions that 'store' objects such as PyTuple_SetItem() and
422 PyDict_SetItemString()
423 don't increment the reference count of the object, since the most
424 frequent use is to store a fresh object. Functions that 'retrieve'
425 objects such as PyTuple_GetItem() and PyDict_GetItemString() also
426 don't increment
427 the reference count, since most frequently the object is only looked at
428 quickly. Thus, to retrieve an object and store it again, the caller
429 must call Py_INCREF() explicitly.
431 NOTE: functions that 'consume' a reference count like
432 PyDict_SetItemString() even
433 consume the reference if the object wasn't stored, to simplify error
434 handling.
436 It seems attractive to make other functions that take an object as
437 argument consume a reference count; however this may quickly get
438 confusing (even the current practice is already confusing). Consider
439 it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
440 times.
442 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
445 #ifdef __cplusplus
447 #endif
448 #endif /* !Py_OBJECT_H */