home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / OBJECT.H < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-08  |  25.0 KB  |  717 lines

  1. #ifndef Py_OBJECT_H
  2. #define Py_OBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7.  
  8. /* Object and type object interface */
  9.  
  10. /*
  11. Objects are structures allocated on the heap.  Special rules apply to
  12. the use of objects to ensure they are properly garbage-collected.
  13. Objects are never allocated statically or on the stack; they must be
  14. accessed through special macros and functions only.  (Type objects are
  15. exceptions to the first rule; the standard types are represented by
  16. statically initialized type objects.)
  17.  
  18. An object has a 'reference count' that is increased or decreased when a
  19. pointer to the object is copied or deleted; when the reference count
  20. reaches zero there are no references to the object left and it can be
  21. removed from the heap.
  22.  
  23. An object has a 'type' that determines what it represents and what kind
  24. of data it contains.  An object's type is fixed when it is created.
  25. Types themselves are represented as objects; an object contains a
  26. pointer to the corresponding type object.  The type itself has a type
  27. pointer pointing to the object representing the type 'type', which
  28. contains a pointer to itself!).
  29.  
  30. Objects do not float around in memory; once allocated an object keeps
  31. the same size and address.  Objects that must hold variable-size data
  32. can contain pointers to variable-size parts of the object.  Not all
  33. objects of the same type have the same size; but the size cannot change
  34. after allocation.  (These restrictions are made so a reference to an
  35. object can be simply a pointer -- moving an object would require
  36. updating all the pointers, and changing an object's size would require
  37. moving it if there was another object right next to it.)
  38.  
  39. Objects are always accessed through pointers of the type 'PyObject *'.
  40. The type 'PyObject' is a structure that only contains the reference count
  41. and the type pointer.  The actual memory allocated for an object
  42. contains other data that can only be accessed after casting the pointer
  43. to a pointer to a longer structure type.  This longer type must start
  44. with the reference count and type fields; the macro PyObject_HEAD should be
  45. used for this (to accommodate for future changes).  The implementation
  46. of a particular object type can cast the object pointer to the proper
  47. type and back.
  48.  
  49. A standard interface exists for objects that contain an array of items
  50. whose size is determined when the object is allocated.
  51. */
  52.  
  53. #ifdef Py_DEBUG
  54.  
  55. /* Turn on heavy reference debugging */
  56. #define Py_TRACE_REFS
  57.  
  58. /* Turn on reference counting */
  59. #define Py_REF_DEBUG
  60.  
  61. #endif /* Py_DEBUG */
  62.  
  63. #ifdef Py_TRACE_REFS
  64. #define PyObject_HEAD \
  65.     struct _object *_ob_next, *_ob_prev; \
  66.     int ob_refcnt; \
  67.     struct _typeobject *ob_type;
  68. #define PyObject_HEAD_INIT(type) 0, 0, 1, type,
  69. #else /* !Py_TRACE_REFS */
  70. #define PyObject_HEAD \
  71.     int ob_refcnt; \
  72.     struct _typeobject *ob_type;
  73. #define PyObject_HEAD_INIT(type) 1, type,
  74. #endif /* !Py_TRACE_REFS */
  75.  
  76. #define PyObject_VAR_HEAD \
  77.     PyObject_HEAD \
  78.     int ob_size; /* Number of items in variable part */
  79.  
  80. typedef struct _object {
  81.     PyObject_HEAD
  82. } PyObject;
  83.  
  84. typedef struct {
  85.     PyObject_VAR_HEAD
  86. } PyVarObject;
  87.  
  88.  
  89. /*
  90. Type objects contain a string containing the type name (to help somewhat
  91. in debugging), the allocation parameters (see newobj() and newvarobj()),
  92. and methods for accessing objects of the type.  Methods are optional,a
  93. nil pointer meaning that particular kind of access is not available for
  94. this type.  The Py_DECREF() macro uses the tp_dealloc method without
  95. checking for a nil pointer; it should always be implemented except if
  96. the implementation can guarantee that the reference count will never
  97. reach zero (e.g., for type objects).
  98.  
  99. NB: the methods for certain type groups are now contained in separate
  100. method blocks.
  101. */
  102.  
  103. typedef PyObject * (*unaryfunc)(PyObject *);
  104. typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
  105. typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
  106. typedef int (*inquiry)(PyObject *);
  107. typedef int (*coercion)(PyObject **, PyObject **);
  108. typedef PyObject *(*intargfunc)(PyObject *, int);
  109. typedef PyObject *(*intintargfunc)(PyObject *, int, int);
  110. typedef int(*intobjargproc)(PyObject *, int, PyObject *);
  111. typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
  112. typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
  113. typedef int (*getreadbufferproc)(PyObject *, int, void **);
  114. typedef int (*getwritebufferproc)(PyObject *, int, void **);
  115. typedef int (*getsegcountproc)(PyObject *, int *);
  116. typedef int (*getcharbufferproc)(PyObject *, int, const char **);
  117. typedef int (*objobjproc)(PyObject *, PyObject *);
  118. typedef int (*visitproc)(PyObject *, void *);
  119. typedef int (*traverseproc)(PyObject *, visitproc, void *);
  120.  
  121. typedef struct {
  122.     /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
  123.        arguments are guaranteed to be of the object's type (modulo
  124.        coercion hacks that is -- i.e. if the type's coercion function
  125.        returns other types, then these are allowed as well).  Numbers that
  126.        have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
  127.        arguments for proper type and implement the necessary conversions
  128.        in the slot functions themselves. */
  129.  
  130.     binaryfunc nb_add;
  131.     binaryfunc nb_subtract;
  132.     binaryfunc nb_multiply;
  133.     binaryfunc nb_divide;
  134.     binaryfunc nb_remainder;
  135.     binaryfunc nb_divmod;
  136.     ternaryfunc nb_power;
  137.     unaryfunc nb_negative;
  138.     unaryfunc nb_positive;
  139.     unaryfunc nb_absolute;
  140.     inquiry nb_nonzero;
  141.     unaryfunc nb_invert;
  142.     binaryfunc nb_lshift;
  143.     binaryfunc nb_rshift;
  144.     binaryfunc nb_and;
  145.     binaryfunc nb_xor;
  146.     binaryfunc nb_or;
  147.     coercion nb_coerce;
  148.     unaryfunc nb_int;
  149.     unaryfunc nb_long;
  150.     unaryfunc nb_float;
  151.     unaryfunc nb_oct;
  152.     unaryfunc nb_hex;
  153.     /* Added in release 2.0 */
  154.     binaryfunc nb_inplace_add;
  155.     binaryfunc nb_inplace_subtract;
  156.     binaryfunc nb_inplace_multiply;
  157.     binaryfunc nb_inplace_divide;
  158.     binaryfunc nb_inplace_remainder;
  159.     ternaryfunc nb_inplace_power;
  160.     binaryfunc nb_inplace_lshift;
  161.     binaryfunc nb_inplace_rshift;
  162.     binaryfunc nb_inplace_and;
  163.     binaryfunc nb_inplace_xor;
  164.     binaryfunc nb_inplace_or;
  165.  
  166.     /* Added in release 2.2 */
  167.     /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
  168.     binaryfunc nb_floor_divide;
  169.     binaryfunc nb_true_divide;
  170.     binaryfunc nb_inplace_floor_divide;
  171.     binaryfunc nb_inplace_true_divide;
  172. } PyNumberMethods;
  173.  
  174. typedef struct {
  175.     inquiry sq_length;
  176.     binaryfunc sq_concat;
  177.     intargfunc sq_repeat;
  178.     intargfunc sq_item;
  179.     intintargfunc sq_slice;
  180.     intobjargproc sq_ass_item;
  181.     intintobjargproc sq_ass_slice;
  182.     objobjproc sq_contains;
  183.     /* Added in release 2.0 */
  184.     binaryfunc sq_inplace_concat;
  185.     intargfunc sq_inplace_repeat;
  186. } PySequenceMethods;
  187.  
  188. typedef struct {
  189.     inquiry mp_length;
  190.     binaryfunc mp_subscript;
  191.     objobjargproc mp_ass_subscript;
  192. } PyMappingMethods;
  193.  
  194. typedef struct {
  195.     getreadbufferproc bf_getreadbuffer;
  196.     getwritebufferproc bf_getwritebuffer;
  197.     getsegcountproc bf_getsegcount;
  198.     getcharbufferproc bf_getcharbuffer;
  199. } PyBufferProcs;
  200.     
  201.  
  202. typedef void (*destructor)(PyObject *);
  203. typedef int (*printfunc)(PyObject *, FILE *, int);
  204. typedef PyObject *(*getattrfunc)(PyObject *, char *);
  205. typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
  206. typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
  207. typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
  208. typedef int (*cmpfunc)(PyObject *, PyObject *);
  209. typedef PyObject *(*reprfunc)(PyObject *);
  210. typedef long (*hashfunc)(PyObject *);
  211. typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
  212. typedef PyObject *(*getiterfunc) (PyObject *);
  213. typedef PyObject *(*iternextfunc) (PyObject *);
  214. typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
  215. typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
  216. typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
  217. typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
  218. typedef PyObject *(*allocfunc)(struct _typeobject *, int);
  219.  
  220. typedef struct _typeobject {
  221.     PyObject_VAR_HEAD
  222.     char *tp_name; /* For printing, in format "<module>.<name>" */
  223.     int tp_basicsize, tp_itemsize; /* For allocation */
  224.     
  225.     /* Methods to implement standard operations */
  226.     
  227.     destructor tp_dealloc;
  228.     printfunc tp_print;
  229.     getattrfunc tp_getattr;
  230.     setattrfunc tp_setattr;
  231.     cmpfunc tp_compare;
  232.     reprfunc tp_repr;
  233.     
  234.     /* Method suites for standard classes */
  235.     
  236.     PyNumberMethods *tp_as_number;
  237.     PySequenceMethods *tp_as_sequence;
  238.     PyMappingMethods *tp_as_mapping;
  239.  
  240.     /* More standard operations (here for binary compatibility) */
  241.  
  242.     hashfunc tp_hash;
  243.     ternaryfunc tp_call;
  244.     reprfunc tp_str;
  245.     getattrofunc tp_getattro;
  246.     setattrofunc tp_setattro;
  247.  
  248.     /* Functions to access object as input/output buffer */
  249.     PyBufferProcs *tp_as_buffer;
  250.     
  251.     /* Flags to define presence of optional/expanded features */
  252.     long tp_flags;
  253.  
  254.     char *tp_doc; /* Documentation string */
  255.  
  256.     /* Assigned meaning in release 2.0 */
  257.     /* call function for all accessible objects */
  258.     traverseproc tp_traverse;
  259.     
  260.     /* delete references to contained objects */
  261.     inquiry tp_clear;
  262.  
  263.     /* Assigned meaning in release 2.1 */
  264.     /* rich comparisons */
  265.     richcmpfunc tp_richcompare;
  266.  
  267.     /* weak reference enabler */
  268.     long tp_weaklistoffset;
  269.  
  270.     /* Added in release 2.2 */
  271.     /* Iterators */
  272.     getiterfunc tp_iter;
  273.     iternextfunc tp_iternext;
  274.  
  275.     /* Attribute descriptor and subclassing stuff */
  276.     struct PyMethodDef *tp_methods;
  277.     struct PyMemberDef *tp_members;
  278.     struct PyGetSetDef *tp_getset;
  279.     struct _typeobject *tp_base;
  280.     PyObject *tp_dict;
  281.     descrgetfunc tp_descr_get;
  282.     descrsetfunc tp_descr_set;
  283.     long tp_dictoffset;
  284.     initproc tp_init;
  285.     allocfunc tp_alloc;
  286.     newfunc tp_new;
  287.     destructor tp_free; /* Low-level free-memory routine */
  288.     inquiry tp_is_gc; /* For PyObject_IS_GC */
  289.     PyObject *tp_bases;
  290.     PyObject *tp_mro; /* method resolution order */
  291.     PyObject *tp_cache;
  292.     PyObject *tp_subclasses;
  293.     PyObject *tp_weaklist;
  294.  
  295. #ifdef COUNT_ALLOCS
  296.     /* these must be last and never explicitly initialized */
  297.     int tp_allocs;
  298.     int tp_frees;
  299.     int tp_maxalloc;
  300.     struct _typeobject *tp_next;
  301. #endif
  302. } PyTypeObject;
  303.  
  304.  
  305. /* Generic type check */
  306. extern DL_IMPORT(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
  307. #define PyObject_TypeCheck(ob, tp) \
  308.     ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
  309.  
  310. extern DL_IMPORT(PyTypeObject) PyType_Type; /* built-in 'type' */
  311. extern DL_IMPORT(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
  312. extern DL_IMPORT(PyTypeObject) PySuper_Type; /* built-in 'super' */
  313.  
  314. #define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type)
  315. #define PyType_CheckExact(op) ((op)->ob_type == &PyType_Type)
  316.  
  317. extern DL_IMPORT(int) PyType_Ready(PyTypeObject *);
  318. extern DL_IMPORT(PyObject *) PyType_GenericAlloc(PyTypeObject *, int);
  319. extern DL_IMPORT(PyObject *) PyType_GenericNew(PyTypeObject *,
  320.                            PyObject *, PyObject *);
  321. extern DL_IMPORT(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
  322.  
  323. /* Generic operations on objects */
  324. extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
  325. extern DL_IMPORT(void) _PyObject_Dump(PyObject *);
  326. extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
  327. extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
  328. #ifdef Py_USING_UNICODE
  329. extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *);
  330. #endif
  331. extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
  332. extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
  333. extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
  334. extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
  335. extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
  336. extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
  337. extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
  338. extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
  339. extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
  340. extern DL_IMPORT(PyObject **) _PyObject_GetDictPtr(PyObject *);
  341. extern DL_IMPORT(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
  342. extern DL_IMPORT(int) PyObject_GenericSetAttr(PyObject *,
  343.                           PyObject *, PyObject *);
  344. extern DL_IMPORT(long) PyObject_Hash(PyObject *);
  345. extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
  346. extern DL_IMPORT(int) PyObject_Not(PyObject *);
  347. extern DL_IMPORT(int) PyCallable_Check(PyObject *);
  348. extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
  349. extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
  350.  
  351. extern DL_IMPORT(void) PyObject_ClearWeakRefs(PyObject *);
  352.  
  353. /* A slot function whose address we need to compare */
  354. extern int _PyObject_SlotCompare(PyObject *, PyObject *);
  355.  
  356.  
  357. /* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
  358.    list of strings.  PyObject_Dir(NULL) is like __builtin__.dir(),
  359.    returning the names of the current locals.  In this case, if there are
  360.    no current locals, NULL is returned, and PyErr_Occurred() is false.
  361. */
  362. extern DL_IMPORT(PyObject *) PyObject_Dir(PyObject *);
  363.  
  364.  
  365. /* Helpers for printing recursive container types */
  366. extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
  367. extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
  368.  
  369. /* Helpers for hash functions */
  370. extern DL_IMPORT(long) _Py_HashDouble(double);
  371. extern DL_IMPORT(long) _Py_HashPointer(void*);
  372.  
  373. /* Helper for passing objects to printf and the like */
  374. #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
  375.  
  376. /* Flag bits for printing: */
  377. #define Py_PRINT_RAW    1    /* No string quotes etc. */
  378.  
  379. /*
  380.  
  381. Type flags (tp_flags)
  382.  
  383. These flags are used to extend the type structure in a backwards-compatible
  384. fashion. Extensions can use the flags to indicate (and test) when a given
  385. type structure contains a new feature. The Python core will use these when
  386. introducing new functionality between major revisions (to avoid mid-version
  387. changes in the PYTHON_API_VERSION).
  388.  
  389. Arbitration of the flag bit positions will need to be coordinated among
  390. all extension writers who publically release their extensions (this will
  391. be fewer than you might expect!)..
  392.  
  393. Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
  394.  
  395. Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
  396.  
  397. Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
  398. given type object has a specified feature.
  399.  
  400. */
  401.  
  402. /* PyBufferProcs contains bf_getcharbuffer */
  403. #define Py_TPFLAGS_HAVE_GETCHARBUFFER  (1L<<0)
  404.  
  405. /* PySequenceMethods contains sq_contains */
  406. #define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
  407.  
  408. /* This is here for backwards compatibility.  Extensions that use the old GC
  409.  * API will still compile but the objects will not be tracked by the GC. */
  410. #define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */
  411.  
  412. /* PySequenceMethods and PyNumberMethods contain in-place operators */
  413. #define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
  414.  
  415. /* PyNumberMethods do their own coercion */
  416. #define Py_TPFLAGS_CHECKTYPES (1L<<4)
  417.  
  418. /* tp_richcompare is defined */
  419. #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
  420.  
  421. /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
  422. #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
  423.  
  424. /* tp_iter is defined */
  425. #define Py_TPFLAGS_HAVE_ITER (1L<<7)
  426.  
  427. /* New members introduced by Python 2.2 exist */
  428. #define Py_TPFLAGS_HAVE_CLASS (1L<<8)
  429.  
  430. /* Set if the type object is dynamically allocated */
  431. #define Py_TPFLAGS_HEAPTYPE (1L<<9)
  432.  
  433. /* Set if the type allows subclassing */
  434. #define Py_TPFLAGS_BASETYPE (1L<<10)
  435.  
  436. /* Set if the type is 'ready' -- fully initialized */
  437. #define Py_TPFLAGS_READY (1L<<12)
  438.  
  439. /* Set while the type is being 'readied', to prevent recursive ready calls */
  440. #define Py_TPFLAGS_READYING (1L<<13)
  441.  
  442. /* Objects support garbage collection (see objimp.h) */
  443. #ifdef WITH_CYCLE_GC
  444. #define Py_TPFLAGS_HAVE_GC (1L<<14)
  445. #else
  446. #define Py_TPFLAGS_HAVE_GC 0
  447. #endif
  448.  
  449. #define Py_TPFLAGS_DEFAULT  ( \
  450.                              Py_TPFLAGS_HAVE_GETCHARBUFFER | \
  451.                              Py_TPFLAGS_HAVE_SEQUENCE_IN | \
  452.                              Py_TPFLAGS_HAVE_INPLACEOPS | \
  453.                              Py_TPFLAGS_HAVE_RICHCOMPARE | \
  454.                              Py_TPFLAGS_HAVE_WEAKREFS | \
  455.                              Py_TPFLAGS_HAVE_ITER | \
  456.                              Py_TPFLAGS_HAVE_CLASS | \
  457.                             0)
  458.  
  459. #define PyType_HasFeature(t,f)  (((t)->tp_flags & (f)) != 0)
  460.  
  461.  
  462. /*
  463. The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
  464. reference counts.  Py_DECREF calls the object's deallocator function; for
  465. objects that don't contain references to other objects or heap memory
  466. this can be the standard function free().  Both macros can be used
  467. wherever a void expression is allowed.  The argument shouldn't be a
  468. NIL pointer.  The macro _Py_NewReference(op) is used only to initialize
  469. reference counts to 1; it is defined here for convenience.
  470.  
  471. We assume that the reference count field can never overflow; this can
  472. be proven when the size of the field is the same as the pointer size
  473. but even with a 16-bit reference count field it is pretty unlikely so
  474. we ignore the possibility.  (If you are paranoid, make it a long.)
  475.  
  476. Type objects should never be deallocated; the type pointer in an object
  477. is not considered to be a reference to the type object, to save
  478. complications in the deallocation function.  (This is actually a
  479. decision that's up to the implementer of each new type so if you want,
  480. you can count such references to the type object.)
  481.  
  482. *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
  483. since it may evaluate its argument multiple times.  (The alternative
  484. would be to mace it a proper function or assign it to a global temporary
  485. variable first, both of which are slower; and in a multi-threaded
  486. environment the global variable trick is not safe.)
  487. */
  488.  
  489. #ifdef Py_TRACE_REFS
  490. #ifndef Py_REF_DEBUG
  491. #define Py_REF_DEBUG
  492. #endif
  493. #endif
  494.  
  495. #ifdef Py_TRACE_REFS
  496. extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
  497. extern DL_IMPORT(void) _Py_NewReference(PyObject *);
  498. extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
  499. extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
  500. extern DL_IMPORT(void) _Py_ResetReferences(void);
  501. #endif
  502.  
  503. #ifndef Py_TRACE_REFS
  504. #ifdef COUNT_ALLOCS
  505. #define _Py_Dealloc(op) ((op)->ob_type->tp_frees++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
  506. #define _Py_ForgetReference(op) ((op)->ob_type->tp_frees++)
  507. #else /* !COUNT_ALLOCS */
  508. #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
  509. #define _Py_ForgetReference(op) /*empty*/
  510. #endif /* !COUNT_ALLOCS */
  511. #endif /* !Py_TRACE_REFS */
  512.  
  513. #ifdef COUNT_ALLOCS
  514. extern DL_IMPORT(void) inc_count(PyTypeObject *);
  515. #endif
  516.  
  517. #ifdef Py_REF_DEBUG
  518.  
  519. extern DL_IMPORT(long) _Py_RefTotal;
  520.  
  521. #ifndef Py_TRACE_REFS
  522. #ifdef COUNT_ALLOCS
  523. #define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
  524. #else
  525. #define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
  526. #endif
  527. #endif /* !Py_TRACE_REFS */
  528.  
  529. #define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
  530.   /* under Py_REF_DEBUG: also log negative ref counts after Py_DECREF() !! */
  531. #define Py_DECREF(op)                            \
  532.        if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ;            \
  533.        else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op));    \
  534.        else (void)fprintf( stderr, "%s:%i negative ref count %i\n",    \
  535.                    __FILE__, __LINE__, (op)->ob_refcnt)
  536. #else /* !Py_REF_DEBUG */
  537.  
  538. #ifdef COUNT_ALLOCS
  539. #define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
  540. #else
  541. #define _Py_NewReference(op) ((op)->ob_refcnt = 1)
  542. #endif
  543.  
  544. #define Py_INCREF(op) ((op)->ob_refcnt++)
  545. #define Py_DECREF(op) \
  546.     if (--(op)->ob_refcnt != 0) \
  547.         ; \
  548.     else \
  549.         _Py_Dealloc((PyObject *)(op))
  550. #endif /* !Py_REF_DEBUG */
  551.  
  552. /* Macros to use in case the object pointer may be NULL: */
  553.  
  554. #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
  555. #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
  556.  
  557. /*
  558. _Py_NoneStruct is an object of undefined type which can be used in contexts
  559. where NULL (nil) is not suitable (since NULL often means 'error').
  560.  
  561. Don't forget to apply Py_INCREF() when returning this value!!!
  562. */
  563.  
  564. extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
  565.  
  566. #define Py_None (&_Py_NoneStruct)
  567.  
  568. /*
  569. Py_NotImplemented is a singleton used to signal that an operation is
  570. not implemented for a given type combination.
  571. */
  572.  
  573. extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
  574.  
  575. #define Py_NotImplemented (&_Py_NotImplementedStruct)
  576.  
  577. /* Rich comparison opcodes */
  578. #define Py_LT 0
  579. #define Py_LE 1
  580. #define Py_EQ 2
  581. #define Py_NE 3
  582. #define Py_GT 4
  583. #define Py_GE 5
  584.  
  585. /*
  586. A common programming style in Python requires the forward declaration
  587. of static, initialized structures, e.g. for a type object that is used
  588. by the functions whose address must be used in the initializer.
  589. Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
  590. well) botch this if you use the static keyword for both declarations
  591. (they allocate two objects, and use the first, uninitialized one until
  592. the second declaration is encountered).  Therefore, the forward
  593. declaration should use the 'forwardstatic' keyword.  This expands to
  594. static on most systems, but to extern on a few.  The actual storage
  595. and name will still be static because the second declaration is
  596. static, so no linker visible symbols will be generated.  (Standard C
  597. compilers take offense to the extern forward declaration of a static
  598. object, so I can't just put extern in all cases. :-( )
  599. */
  600.  
  601. #ifdef BAD_STATIC_FORWARD
  602. #define staticforward extern
  603. #define statichere static
  604. #else /* !BAD_STATIC_FORWARD */
  605. #define staticforward static
  606. #define statichere static
  607. #endif /* !BAD_STATIC_FORWARD */
  608.  
  609.  
  610. /*
  611. More conventions
  612. ================
  613.  
  614. Argument Checking
  615. -----------------
  616.  
  617. Functions that take objects as arguments normally don't check for nil
  618. arguments, but they do check the type of the argument, and return an
  619. error if the function doesn't apply to the type.
  620.  
  621. Failure Modes
  622. -------------
  623.  
  624. Functions may fail for a variety of reasons, including running out of
  625. memory.  This is communicated to the caller in two ways: an error string
  626. is set (see errors.h), and the function result differs: functions that
  627. normally return a pointer return NULL for failure, functions returning
  628. an integer return -1 (which could be a legal return value too!), and
  629. other functions return 0 for success and -1 for failure.
  630. Callers should always check for errors before using the result.
  631.  
  632. Reference Counts
  633. ----------------
  634.  
  635. It takes a while to get used to the proper usage of reference counts.
  636.  
  637. Functions that create an object set the reference count to 1; such new
  638. objects must be stored somewhere or destroyed again with Py_DECREF().
  639. Functions that 'store' objects such as PyTuple_SetItem() and
  640. PyDict_SetItemString()
  641. don't increment the reference count of the object, since the most
  642. frequent use is to store a fresh object.  Functions that 'retrieve'
  643. objects such as PyTuple_GetItem() and PyDict_GetItemString() also
  644. don't increment
  645. the reference count, since most frequently the object is only looked at
  646. quickly.  Thus, to retrieve an object and store it again, the caller
  647. must call Py_INCREF() explicitly.
  648.  
  649. NOTE: functions that 'consume' a reference count like
  650. PyList_SetItemString() even consume the reference if the object wasn't
  651. stored, to simplify error handling.
  652.  
  653. It seems attractive to make other functions that take an object as
  654. argument consume a reference count; however this may quickly get
  655. confusing (even the current practice is already confusing).  Consider
  656. it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
  657. times.
  658. */
  659.  
  660. /*
  661.   trashcan
  662.   CT 2k0130
  663.   non-recursively destroy nested objects
  664.  
  665.   CT 2k0223
  666.   redefinition for better locality and less overhead.
  667.  
  668.   Objects that want to be recursion safe need to use
  669.   the macro's 
  670.         Py_TRASHCAN_SAFE_BEGIN(name)
  671.   and
  672.         Py_TRASHCAN_SAFE_END(name)
  673.   surrounding their actual deallocation code.
  674.  
  675.   It would be nice to do this using the thread state.
  676.   Also, we could do an exact stack measure then.
  677.   Unfortunately, deallocations also take place when
  678.   the thread state is undefined.
  679.  
  680.   CT 2k0422 complete rewrite.
  681.   There is no need to allocate new objects.
  682.   Everything is done vialob_refcnt and ob_type now.
  683.   Adding support for free-threading should be easy, too.
  684. */
  685.  
  686. #define PyTrash_UNWIND_LEVEL 50
  687.  
  688. #define Py_TRASHCAN_SAFE_BEGIN(op) \
  689.     { \
  690.         ++_PyTrash_delete_nesting; \
  691.         if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
  692.  
  693. #define Py_TRASHCAN_SAFE_END(op) \
  694.         ;} \
  695.         else \
  696.             _PyTrash_deposit_object((PyObject*)op);\
  697.         --_PyTrash_delete_nesting; \
  698.         if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
  699.             _PyTrash_destroy_chain(); \
  700.     } \
  701.  
  702. extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
  703. extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
  704.  
  705. extern DL_IMPORT(int) _PyTrash_delete_nesting;
  706. extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
  707.  
  708. /* swap the "xx" to check the speed loss */
  709.  
  710. #define xxPy_TRASHCAN_SAFE_BEGIN(op) 
  711. #define xxPy_TRASHCAN_SAFE_END(op) ;
  712.  
  713. #ifdef __cplusplus
  714. }
  715. #endif
  716. #endif /* !Py_OBJECT_H */
  717.