Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

868 lines
32 KiB

  1. #ifndef Py_OBJECT_H
  2. #define Py_OBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* Object and type object interface */
  7. /*
  8. Objects are structures allocated on the heap. Special rules apply to
  9. the use of objects to ensure they are properly garbage-collected.
  10. Objects are never allocated statically or on the stack; they must be
  11. accessed through special macros and functions only. (Type objects are
  12. exceptions to the first rule; the standard types are represented by
  13. statically initialized type objects, although work on type/class unification
  14. for Python 2.2 made it possible to have heap-allocated type objects too).
  15. An object has a 'reference count' that is increased or decreased when a
  16. pointer to the object is copied or deleted; when the reference count
  17. reaches zero there are no references to the object left and it can be
  18. removed from the heap.
  19. An object has a 'type' that determines what it represents and what kind
  20. of data it contains. An object's type is fixed when it is created.
  21. Types themselves are represented as objects; an object contains a
  22. pointer to the corresponding type object. The type itself has a type
  23. pointer pointing to the object representing the type 'type', which
  24. contains a pointer to itself!).
  25. Objects do not float around in memory; once allocated an object keeps
  26. the same size and address. Objects that must hold variable-size data
  27. can contain pointers to variable-size parts of the object. Not all
  28. objects of the same type have the same size; but the size cannot change
  29. after allocation. (These restrictions are made so a reference to an
  30. object can be simply a pointer -- moving an object would require
  31. updating all the pointers, and changing an object's size would require
  32. moving it if there was another object right next to it.)
  33. Objects are always accessed through pointers of the type 'PyObject *'.
  34. The type 'PyObject' is a structure that only contains the reference count
  35. and the type pointer. The actual memory allocated for an object
  36. contains other data that can only be accessed after casting the pointer
  37. to a pointer to a longer structure type. This longer type must start
  38. with the reference count and type fields; the macro PyObject_HEAD should be
  39. used for this (to accommodate for future changes). The implementation
  40. of a particular object type can cast the object pointer to the proper
  41. type and back.
  42. A standard interface exists for objects that contain an array of items
  43. whose size is determined when the object is allocated.
  44. */
  45. /* Py_DEBUG implies Py_TRACE_REFS. */
  46. #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
  47. #define Py_TRACE_REFS
  48. #endif
  49. /* Py_TRACE_REFS implies Py_REF_DEBUG. */
  50. #if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
  51. #define Py_REF_DEBUG
  52. #endif
  53. #ifdef Py_TRACE_REFS
  54. /* Define pointers to support a doubly-linked list of all live heap objects. */
  55. #define _PyObject_HEAD_EXTRA \
  56. struct _object *_ob_next; \
  57. struct _object *_ob_prev;
  58. #define _PyObject_EXTRA_INIT 0, 0,
  59. #else
  60. #define _PyObject_HEAD_EXTRA
  61. #define _PyObject_EXTRA_INIT
  62. #endif
  63. /* PyObject_HEAD defines the initial segment of every PyObject. */
  64. #define PyObject_HEAD \
  65. _PyObject_HEAD_EXTRA \
  66. Py_ssize_t ob_refcnt; \
  67. struct _typeobject *ob_type;
  68. #define PyObject_HEAD_INIT(type) \
  69. _PyObject_EXTRA_INIT \
  70. 1, type,
  71. /* PyObject_VAR_HEAD defines the initial segment of all variable-size
  72. * container objects. These end with a declaration of an array with 1
  73. * element, but enough space is malloc'ed so that the array actually
  74. * has room for ob_size elements. Note that ob_size is an element count,
  75. * not necessarily a byte count.
  76. */
  77. #define PyObject_VAR_HEAD \
  78. PyObject_HEAD \
  79. Py_ssize_t ob_size; /* Number of items in variable part */
  80. #define Py_INVALID_SIZE (Py_ssize_t)-1
  81. /* Nothing is actually declared to be a PyObject, but every pointer to
  82. * a Python object can be cast to a PyObject*. This is inheritance built
  83. * by hand. Similarly every pointer to a variable-size Python object can,
  84. * in addition, be cast to PyVarObject*.
  85. */
  86. typedef struct _object {
  87. PyObject_HEAD
  88. } PyObject;
  89. typedef struct {
  90. PyObject_VAR_HEAD
  91. } PyVarObject;
  92. /*
  93. Type objects contain a string containing the type name (to help somewhat
  94. in debugging), the allocation parameters (see PyObject_New() and
  95. PyObject_NewVar()),
  96. and methods for accessing objects of the type. Methods are optional, a
  97. nil pointer meaning that particular kind of access is not available for
  98. this type. The Py_DECREF() macro uses the tp_dealloc method without
  99. checking for a nil pointer; it should always be implemented except if
  100. the implementation can guarantee that the reference count will never
  101. reach zero (e.g., for statically allocated type objects).
  102. NB: the methods for certain type groups are now contained in separate
  103. method blocks.
  104. */
  105. typedef PyObject * (*unaryfunc)(PyObject *);
  106. typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
  107. typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
  108. typedef int (*inquiry)(PyObject *);
  109. typedef Py_ssize_t (*lenfunc)(PyObject *);
  110. typedef int (*coercion)(PyObject **, PyObject **);
  111. typedef PyObject *(*intargfunc)(PyObject *, int) Py_DEPRECATED(2.5);
  112. typedef PyObject *(*intintargfunc)(PyObject *, int, int) Py_DEPRECATED(2.5);
  113. typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
  114. typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
  115. typedef int(*intobjargproc)(PyObject *, int, PyObject *);
  116. typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
  117. typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
  118. typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
  119. typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
  120. /* int-based buffer interface */
  121. typedef int (*getreadbufferproc)(PyObject *, int, void **);
  122. typedef int (*getwritebufferproc)(PyObject *, int, void **);
  123. typedef int (*getsegcountproc)(PyObject *, int *);
  124. typedef int (*getcharbufferproc)(PyObject *, int, char **);
  125. /* ssize_t-based buffer interface */
  126. typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
  127. typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
  128. typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
  129. typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
  130. typedef int (*objobjproc)(PyObject *, PyObject *);
  131. typedef int (*visitproc)(PyObject *, void *);
  132. typedef int (*traverseproc)(PyObject *, visitproc, void *);
  133. typedef struct {
  134. /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
  135. arguments are guaranteed to be of the object's type (modulo
  136. coercion hacks -- i.e. if the type's coercion function
  137. returns other types, then these are allowed as well). Numbers that
  138. have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
  139. arguments for proper type and implement the necessary conversions
  140. in the slot functions themselves. */
  141. binaryfunc nb_add;
  142. binaryfunc nb_subtract;
  143. binaryfunc nb_multiply;
  144. binaryfunc nb_divide;
  145. binaryfunc nb_remainder;
  146. binaryfunc nb_divmod;
  147. ternaryfunc nb_power;
  148. unaryfunc nb_negative;
  149. unaryfunc nb_positive;
  150. unaryfunc nb_absolute;
  151. inquiry nb_nonzero;
  152. unaryfunc nb_invert;
  153. binaryfunc nb_lshift;
  154. binaryfunc nb_rshift;
  155. binaryfunc nb_and;
  156. binaryfunc nb_xor;
  157. binaryfunc nb_or;
  158. coercion nb_coerce;
  159. unaryfunc nb_int;
  160. unaryfunc nb_long;
  161. unaryfunc nb_float;
  162. unaryfunc nb_oct;
  163. unaryfunc nb_hex;
  164. /* Added in release 2.0 */
  165. binaryfunc nb_inplace_add;
  166. binaryfunc nb_inplace_subtract;
  167. binaryfunc nb_inplace_multiply;
  168. binaryfunc nb_inplace_divide;
  169. binaryfunc nb_inplace_remainder;
  170. ternaryfunc nb_inplace_power;
  171. binaryfunc nb_inplace_lshift;
  172. binaryfunc nb_inplace_rshift;
  173. binaryfunc nb_inplace_and;
  174. binaryfunc nb_inplace_xor;
  175. binaryfunc nb_inplace_or;
  176. /* Added in release 2.2 */
  177. /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
  178. binaryfunc nb_floor_divide;
  179. binaryfunc nb_true_divide;
  180. binaryfunc nb_inplace_floor_divide;
  181. binaryfunc nb_inplace_true_divide;
  182. /* Added in release 2.5 */
  183. unaryfunc nb_index;
  184. } PyNumberMethods;
  185. typedef struct {
  186. lenfunc sq_length;
  187. binaryfunc sq_concat;
  188. ssizeargfunc sq_repeat;
  189. ssizeargfunc sq_item;
  190. ssizessizeargfunc sq_slice;
  191. ssizeobjargproc sq_ass_item;
  192. ssizessizeobjargproc sq_ass_slice;
  193. objobjproc sq_contains;
  194. /* Added in release 2.0 */
  195. binaryfunc sq_inplace_concat;
  196. ssizeargfunc sq_inplace_repeat;
  197. } PySequenceMethods;
  198. typedef struct {
  199. lenfunc mp_length;
  200. binaryfunc mp_subscript;
  201. objobjargproc mp_ass_subscript;
  202. } PyMappingMethods;
  203. typedef struct {
  204. readbufferproc bf_getreadbuffer;
  205. writebufferproc bf_getwritebuffer;
  206. segcountproc bf_getsegcount;
  207. charbufferproc bf_getcharbuffer;
  208. } PyBufferProcs;
  209. typedef void (*freefunc)(void *);
  210. typedef void (*destructor)(PyObject *);
  211. typedef int (*printfunc)(PyObject *, FILE *, int);
  212. typedef PyObject *(*getattrfunc)(PyObject *, char *);
  213. typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
  214. typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
  215. typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
  216. typedef int (*cmpfunc)(PyObject *, PyObject *);
  217. typedef PyObject *(*reprfunc)(PyObject *);
  218. typedef long (*hashfunc)(PyObject *);
  219. typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
  220. typedef PyObject *(*getiterfunc) (PyObject *);
  221. typedef PyObject *(*iternextfunc) (PyObject *);
  222. typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
  223. typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
  224. typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
  225. typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
  226. typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
  227. typedef struct _typeobject {
  228. PyObject_VAR_HEAD
  229. const char *tp_name; /* For printing, in format "<module>.<name>" */
  230. Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
  231. /* Methods to implement standard operations */
  232. destructor tp_dealloc;
  233. printfunc tp_print;
  234. getattrfunc tp_getattr;
  235. setattrfunc tp_setattr;
  236. cmpfunc tp_compare;
  237. reprfunc tp_repr;
  238. /* Method suites for standard classes */
  239. PyNumberMethods *tp_as_number;
  240. PySequenceMethods *tp_as_sequence;
  241. PyMappingMethods *tp_as_mapping;
  242. /* More standard operations (here for binary compatibility) */
  243. hashfunc tp_hash;
  244. ternaryfunc tp_call;
  245. reprfunc tp_str;
  246. getattrofunc tp_getattro;
  247. setattrofunc tp_setattro;
  248. /* Functions to access object as input/output buffer */
  249. PyBufferProcs *tp_as_buffer;
  250. /* Flags to define presence of optional/expanded features */
  251. long tp_flags;
  252. const char *tp_doc; /* Documentation string */
  253. /* Assigned meaning in release 2.0 */
  254. /* call function for all accessible objects */
  255. traverseproc tp_traverse;
  256. /* delete references to contained objects */
  257. inquiry tp_clear;
  258. /* Assigned meaning in release 2.1 */
  259. /* rich comparisons */
  260. richcmpfunc tp_richcompare;
  261. /* weak reference enabler */
  262. Py_ssize_t tp_weaklistoffset;
  263. /* Added in release 2.2 */
  264. /* Iterators */
  265. getiterfunc tp_iter;
  266. iternextfunc tp_iternext;
  267. /* Attribute descriptor and subclassing stuff */
  268. struct PyMethodDef *tp_methods;
  269. struct PyMemberDef *tp_members;
  270. struct PyGetSetDef *tp_getset;
  271. struct _typeobject *tp_base;
  272. PyObject *tp_dict;
  273. descrgetfunc tp_descr_get;
  274. descrsetfunc tp_descr_set;
  275. Py_ssize_t tp_dictoffset;
  276. initproc tp_init;
  277. allocfunc tp_alloc;
  278. newfunc tp_new;
  279. freefunc tp_free; /* Low-level free-memory routine */
  280. inquiry tp_is_gc; /* For PyObject_IS_GC */
  281. PyObject *tp_bases;
  282. PyObject *tp_mro; /* method resolution order */
  283. PyObject *tp_cache;
  284. PyObject *tp_subclasses;
  285. PyObject *tp_weaklist;
  286. destructor tp_del;
  287. #ifdef COUNT_ALLOCS
  288. /* these must be last and never explicitly initialized */
  289. Py_ssize_t tp_allocs;
  290. Py_ssize_t tp_frees;
  291. Py_ssize_t tp_maxalloc;
  292. struct _typeobject *tp_prev;
  293. struct _typeobject *tp_next;
  294. #endif
  295. } PyTypeObject;
  296. /* The *real* layout of a type object when allocated on the heap */
  297. typedef struct _heaptypeobject {
  298. /* Note: there's a dependency on the order of these members
  299. in slotptr() in typeobject.c . */
  300. PyTypeObject ht_type;
  301. PyNumberMethods as_number;
  302. PyMappingMethods as_mapping;
  303. PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
  304. so that the mapping wins when both
  305. the mapping and the sequence define
  306. a given operator (e.g. __getitem__).
  307. see add_operators() in typeobject.c . */
  308. PyBufferProcs as_buffer;
  309. PyObject *ht_name, *ht_slots;
  310. /* here are optional user slots, followed by the members. */
  311. } PyHeapTypeObject;
  312. /* access macro to the members which are floating "behind" the object */
  313. #define PyHeapType_GET_MEMBERS(etype) \
  314. ((PyMemberDef *)(((char *)etype) + (etype)->ht_type.ob_type->tp_basicsize))
  315. /* Generic type check */
  316. PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
  317. #define PyObject_TypeCheck(ob, tp) \
  318. ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
  319. PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
  320. PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
  321. PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
  322. #define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type)
  323. #define PyType_CheckExact(op) ((op)->ob_type == &PyType_Type)
  324. PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
  325. PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
  326. PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
  327. PyObject *, PyObject *);
  328. PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
  329. /* Generic operations on objects */
  330. PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
  331. PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
  332. PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
  333. PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *);
  334. PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
  335. #ifdef Py_USING_UNICODE
  336. PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
  337. #endif
  338. PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
  339. PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
  340. PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
  341. PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
  342. PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
  343. PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
  344. PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
  345. PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
  346. PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
  347. PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
  348. PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
  349. PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
  350. PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
  351. PyObject *, PyObject *);
  352. PyAPI_FUNC(long) PyObject_Hash(PyObject *);
  353. PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
  354. PyAPI_FUNC(int) PyObject_Not(PyObject *);
  355. PyAPI_FUNC(int) PyCallable_Check(PyObject *);
  356. PyAPI_FUNC(int) PyNumber_Coerce(PyObject **, PyObject **);
  357. PyAPI_FUNC(int) PyNumber_CoerceEx(PyObject **, PyObject **);
  358. PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
  359. /* A slot function whose address we need to compare */
  360. extern int _PyObject_SlotCompare(PyObject *, PyObject *);
  361. /* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
  362. list of strings. PyObject_Dir(NULL) is like __builtin__.dir(),
  363. returning the names of the current locals. In this case, if there are
  364. no current locals, NULL is returned, and PyErr_Occurred() is false.
  365. */
  366. PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
  367. /* Helpers for printing recursive container types */
  368. PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
  369. PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
  370. /* Helpers for hash functions */
  371. PyAPI_FUNC(long) _Py_HashDouble(double);
  372. PyAPI_FUNC(long) _Py_HashPointer(void*);
  373. /* Helper for passing objects to printf and the like */
  374. #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
  375. /* Flag bits for printing: */
  376. #define Py_PRINT_RAW 1 /* No string quotes etc. */
  377. /*
  378. `Type flags (tp_flags)
  379. These flags are used to extend the type structure in a backwards-compatible
  380. fashion. Extensions can use the flags to indicate (and test) when a given
  381. type structure contains a new feature. The Python core will use these when
  382. introducing new functionality between major revisions (to avoid mid-version
  383. changes in the PYTHON_API_VERSION).
  384. Arbitration of the flag bit positions will need to be coordinated among
  385. all extension writers who publically release their extensions (this will
  386. be fewer than you might expect!)..
  387. Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
  388. Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
  389. Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
  390. given type object has a specified feature.
  391. */
  392. /* PyBufferProcs contains bf_getcharbuffer */
  393. #define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
  394. /* PySequenceMethods contains sq_contains */
  395. #define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
  396. /* This is here for backwards compatibility. Extensions that use the old GC
  397. * API will still compile but the objects will not be tracked by the GC. */
  398. #define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */
  399. /* PySequenceMethods and PyNumberMethods contain in-place operators */
  400. #define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
  401. /* PyNumberMethods do their own coercion */
  402. #define Py_TPFLAGS_CHECKTYPES (1L<<4)
  403. /* tp_richcompare is defined */
  404. #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
  405. /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
  406. #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
  407. /* tp_iter is defined */
  408. #define Py_TPFLAGS_HAVE_ITER (1L<<7)
  409. /* New members introduced by Python 2.2 exist */
  410. #define Py_TPFLAGS_HAVE_CLASS (1L<<8)
  411. /* Set if the type object is dynamically allocated */
  412. #define Py_TPFLAGS_HEAPTYPE (1L<<9)
  413. /* Set if the type allows subclassing */
  414. #define Py_TPFLAGS_BASETYPE (1L<<10)
  415. /* Set if the type is 'ready' -- fully initialized */
  416. #define Py_TPFLAGS_READY (1L<<12)
  417. /* Set while the type is being 'readied', to prevent recursive ready calls */
  418. #define Py_TPFLAGS_READYING (1L<<13)
  419. /* Objects support garbage collection (see objimp.h) */
  420. #define Py_TPFLAGS_HAVE_GC (1L<<14)
  421. /* These two bits are preserved for Stackless Python, next after this is 17 */
  422. #ifdef STACKLESS
  423. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
  424. #else
  425. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
  426. #endif
  427. /* Objects support nb_index in PyNumberMethods */
  428. #define Py_TPFLAGS_HAVE_INDEX (1L<<17)
  429. #define Py_TPFLAGS_DEFAULT ( \
  430. Py_TPFLAGS_HAVE_GETCHARBUFFER | \
  431. Py_TPFLAGS_HAVE_SEQUENCE_IN | \
  432. Py_TPFLAGS_HAVE_INPLACEOPS | \
  433. Py_TPFLAGS_HAVE_RICHCOMPARE | \
  434. Py_TPFLAGS_HAVE_WEAKREFS | \
  435. Py_TPFLAGS_HAVE_ITER | \
  436. Py_TPFLAGS_HAVE_CLASS | \
  437. Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
  438. Py_TPFLAGS_HAVE_INDEX | \
  439. 0)
  440. #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
  441. /*
  442. The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
  443. reference counts. Py_DECREF calls the object's deallocator function when
  444. the refcount falls to 0; for
  445. objects that don't contain references to other objects or heap memory
  446. this can be the standard function free(). Both macros can be used
  447. wherever a void expression is allowed. The argument must not be a
  448. NIL pointer. If it may be NIL, use Py_XINCREF/Py_XDECREF instead.
  449. The macro _Py_NewReference(op) initialize reference counts to 1, and
  450. in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
  451. bookkeeping appropriate to the special build.
  452. We assume that the reference count field can never overflow; this can
  453. be proven when the size of the field is the same as the pointer size, so
  454. we ignore the possibility. Provided a C int is at least 32 bits (which
  455. is implicitly assumed in many parts of this code), that's enough for
  456. about 2**31 references to an object.
  457. XXX The following became out of date in Python 2.2, but I'm not sure
  458. XXX what the full truth is now. Certainly, heap-allocated type objects
  459. XXX can and should be deallocated.
  460. Type objects should never be deallocated; the type pointer in an object
  461. is not considered to be a reference to the type object, to save
  462. complications in the deallocation function. (This is actually a
  463. decision that's up to the implementer of each new type so if you want,
  464. you can count such references to the type object.)
  465. *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
  466. since it may evaluate its argument multiple times. (The alternative
  467. would be to mace it a proper function or assign it to a global temporary
  468. variable first, both of which are slower; and in a multi-threaded
  469. environment the global variable trick is not safe.)
  470. */
  471. /* First define a pile of simple helper macros, one set per special
  472. * build symbol. These either expand to the obvious things, or to
  473. * nothing at all when the special mode isn't in effect. The main
  474. * macros can later be defined just once then, yet expand to different
  475. * things depending on which special build options are and aren't in effect.
  476. * Trust me <wink>: while painful, this is 20x easier to understand than,
  477. * e.g, defining _Py_NewReference five different times in a maze of nested
  478. * #ifdefs (we used to do that -- it was impenetrable).
  479. */
  480. #ifdef Py_REF_DEBUG
  481. PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
  482. PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname,
  483. int lineno, PyObject *op);
  484. PyAPI_FUNC(PyObject *) _PyDict_Dummy(void);
  485. PyAPI_FUNC(PyObject *) _PySet_Dummy(void);
  486. PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
  487. #define _Py_INC_REFTOTAL _Py_RefTotal++
  488. #define _Py_DEC_REFTOTAL _Py_RefTotal--
  489. #define _Py_REF_DEBUG_COMMA ,
  490. #define _Py_CHECK_REFCNT(OP) \
  491. { if ((OP)->ob_refcnt < 0) \
  492. _Py_NegativeRefcount(__FILE__, __LINE__, \
  493. (PyObject *)(OP)); \
  494. }
  495. #else
  496. #define _Py_INC_REFTOTAL
  497. #define _Py_DEC_REFTOTAL
  498. #define _Py_REF_DEBUG_COMMA
  499. #define _Py_CHECK_REFCNT(OP) /* a semicolon */;
  500. #endif /* Py_REF_DEBUG */
  501. #ifdef COUNT_ALLOCS
  502. PyAPI_FUNC(void) inc_count(PyTypeObject *);
  503. PyAPI_FUNC(void) dec_count(PyTypeObject *);
  504. #define _Py_INC_TPALLOCS(OP) inc_count((OP)->ob_type)
  505. #define _Py_INC_TPFREES(OP) dec_count((OP)->ob_type)
  506. #define _Py_DEC_TPFREES(OP) (OP)->ob_type->tp_frees--
  507. #define _Py_COUNT_ALLOCS_COMMA ,
  508. #else
  509. #define _Py_INC_TPALLOCS(OP)
  510. #define _Py_INC_TPFREES(OP)
  511. #define _Py_DEC_TPFREES(OP)
  512. #define _Py_COUNT_ALLOCS_COMMA
  513. #endif /* COUNT_ALLOCS */
  514. #ifdef Py_TRACE_REFS
  515. /* Py_TRACE_REFS is such major surgery that we call external routines. */
  516. PyAPI_FUNC(void) _Py_NewReference(PyObject *);
  517. PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
  518. PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
  519. PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
  520. PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
  521. PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
  522. #else
  523. /* Without Py_TRACE_REFS, there's little enough to do that we expand code
  524. * inline.
  525. */
  526. #define _Py_NewReference(op) ( \
  527. _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
  528. _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
  529. (op)->ob_refcnt = 1)
  530. #define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
  531. #define _Py_Dealloc(op) ( \
  532. _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
  533. (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
  534. #endif /* !Py_TRACE_REFS */
  535. #define Py_INCREF(op) ( \
  536. _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
  537. (op)->ob_refcnt++)
  538. #define Py_DECREF(op) \
  539. if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
  540. --(op)->ob_refcnt != 0) \
  541. _Py_CHECK_REFCNT(op) \
  542. else \
  543. _Py_Dealloc((PyObject *)(op))
  544. /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
  545. * and tp_dealloc implementatons.
  546. *
  547. * Note that "the obvious" code can be deadly:
  548. *
  549. * Py_XDECREF(op);
  550. * op = NULL;
  551. *
  552. * Typically, `op` is something like self->containee, and `self` is done
  553. * using its `containee` member. In the code sequence above, suppose
  554. * `containee` is non-NULL with a refcount of 1. Its refcount falls to
  555. * 0 on the first line, which can trigger an arbitrary amount of code,
  556. * possibly including finalizers (like __del__ methods or weakref callbacks)
  557. * coded in Python, which in turn can release the GIL and allow other threads
  558. * to run, etc. Such code may even invoke methods of `self` again, or cause
  559. * cyclic gc to trigger, but-- oops! --self->containee still points to the
  560. * object being torn down, and it may be in an insane state while being torn
  561. * down. This has in fact been a rich historic source of miserable (rare &
  562. * hard-to-diagnose) segfaulting (and other) bugs.
  563. *
  564. * The safe way is:
  565. *
  566. * Py_CLEAR(op);
  567. *
  568. * That arranges to set `op` to NULL _before_ decref'ing, so that any code
  569. * triggered as a side-effect of `op` getting torn down no longer believes
  570. * `op` points to a valid object.
  571. *
  572. * There are cases where it's safe to use the naive code, but they're brittle.
  573. * For example, if `op` points to a Python integer, you know that destroying
  574. * one of those can't cause problems -- but in part that relies on that
  575. * Python integers aren't currently weakly referencable. Best practice is
  576. * to use Py_CLEAR() even if you can't think of a reason for why you need to.
  577. */
  578. #define Py_CLEAR(op) \
  579. do { \
  580. if (op) { \
  581. PyObject *tmp = (PyObject *)(op); \
  582. (op) = NULL; \
  583. Py_DECREF(tmp); \
  584. } \
  585. } while (0)
  586. /* Macros to use in case the object pointer may be NULL: */
  587. #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
  588. #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
  589. /*
  590. These are provided as conveniences to Python runtime embedders, so that
  591. they can have object code that is not dependent on Python compilation flags.
  592. */
  593. PyAPI_FUNC(void) Py_IncRef(PyObject *);
  594. PyAPI_FUNC(void) Py_DecRef(PyObject *);
  595. /*
  596. _Py_NoneStruct is an object of undefined type which can be used in contexts
  597. where NULL (nil) is not suitable (since NULL often means 'error').
  598. Don't forget to apply Py_INCREF() when returning this value!!!
  599. */
  600. PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
  601. #define Py_None (&_Py_NoneStruct)
  602. /* Macro for returning Py_None from a function */
  603. #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  604. /*
  605. Py_NotImplemented is a singleton used to signal that an operation is
  606. not implemented for a given type combination.
  607. */
  608. PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
  609. #define Py_NotImplemented (&_Py_NotImplementedStruct)
  610. /* Rich comparison opcodes */
  611. #define Py_LT 0
  612. #define Py_LE 1
  613. #define Py_EQ 2
  614. #define Py_NE 3
  615. #define Py_GT 4
  616. #define Py_GE 5
  617. /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
  618. * Defined in object.c.
  619. */
  620. PyAPI_DATA(int) _Py_SwappedOp[];
  621. /*
  622. Define staticforward and statichere for source compatibility with old
  623. C extensions.
  624. The staticforward define was needed to support certain broken C
  625. compilers (notably SCO ODT 3.0, perhaps early AIX as well) botched the
  626. static keyword when it was used with a forward declaration of a static
  627. initialized structure. Standard C allows the forward declaration with
  628. static, and we've decided to stop catering to broken C compilers.
  629. (In fact, we expect that the compilers are all fixed eight years later.)
  630. */
  631. #define staticforward static
  632. #define statichere static
  633. /*
  634. More conventions
  635. ================
  636. Argument Checking
  637. -----------------
  638. Functions that take objects as arguments normally don't check for nil
  639. arguments, but they do check the type of the argument, and return an
  640. error if the function doesn't apply to the type.
  641. Failure Modes
  642. -------------
  643. Functions may fail for a variety of reasons, including running out of
  644. memory. This is communicated to the caller in two ways: an error string
  645. is set (see errors.h), and the function result differs: functions that
  646. normally return a pointer return NULL for failure, functions returning
  647. an integer return -1 (which could be a legal return value too!), and
  648. other functions return 0 for success and -1 for failure.
  649. Callers should always check for errors before using the result. If
  650. an error was set, the caller must either explicitly clear it, or pass
  651. the error on to its caller.
  652. Reference Counts
  653. ----------------
  654. It takes a while to get used to the proper usage of reference counts.
  655. Functions that create an object set the reference count to 1; such new
  656. objects must be stored somewhere or destroyed again with Py_DECREF().
  657. Some functions that 'store' objects, such as PyTuple_SetItem() and
  658. PyList_SetItem(),
  659. don't increment the reference count of the object, since the most
  660. frequent use is to store a fresh object. Functions that 'retrieve'
  661. objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
  662. don't increment
  663. the reference count, since most frequently the object is only looked at
  664. quickly. Thus, to retrieve an object and store it again, the caller
  665. must call Py_INCREF() explicitly.
  666. NOTE: functions that 'consume' a reference count, like
  667. PyList_SetItem(), consume the reference even if the object wasn't
  668. successfully stored, to simplify error handling.
  669. It seems attractive to make other functions that take an object as
  670. argument consume a reference count; however, this may quickly get
  671. confusing (even the current practice is already confusing). Consider
  672. it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
  673. times.
  674. */
  675. /* Trashcan mechanism, thanks to Christian Tismer.
  676. When deallocating a container object, it's possible to trigger an unbounded
  677. chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
  678. next" object in the chain to 0. This can easily lead to stack faults, and
  679. especially in threads (which typically have less stack space to work with).
  680. A container object that participates in cyclic gc can avoid this by
  681. bracketing the body of its tp_dealloc function with a pair of macros:
  682. static void
  683. mytype_dealloc(mytype *p)
  684. {
  685. ... declarations go here ...
  686. PyObject_GC_UnTrack(p); // must untrack first
  687. Py_TRASHCAN_SAFE_BEGIN(p)
  688. ... The body of the deallocator goes here, including all calls ...
  689. ... to Py_DECREF on contained objects. ...
  690. Py_TRASHCAN_SAFE_END(p)
  691. }
  692. CAUTION: Never return from the middle of the body! If the body needs to
  693. "get out early", put a label immediately before the Py_TRASHCAN_SAFE_END
  694. call, and goto it. Else the call-depth counter (see below) will stay
  695. above 0 forever, and the trashcan will never get emptied.
  696. How it works: The BEGIN macro increments a call-depth counter. So long
  697. as this counter is small, the body of the deallocator is run directly without
  698. further ado. But if the counter gets large, it instead adds p to a list of
  699. objects to be deallocated later, skips the body of the deallocator, and
  700. resumes execution after the END macro. The tp_dealloc routine then returns
  701. without deallocating anything (and so unbounded call-stack depth is avoided).
  702. When the call stack finishes unwinding again, code generated by the END macro
  703. notices this, and calls another routine to deallocate all the objects that
  704. may have been added to the list of deferred deallocations. In effect, a
  705. chain of N deallocations is broken into N / PyTrash_UNWIND_LEVEL pieces,
  706. with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
  707. */
  708. PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
  709. PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
  710. PyAPI_DATA(int) _PyTrash_delete_nesting;
  711. PyAPI_DATA(PyObject *) _PyTrash_delete_later;
  712. #define PyTrash_UNWIND_LEVEL 50
  713. #define Py_TRASHCAN_SAFE_BEGIN(op) \
  714. if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
  715. ++_PyTrash_delete_nesting;
  716. /* The body of the deallocator is here. */
  717. #define Py_TRASHCAN_SAFE_END(op) \
  718. --_PyTrash_delete_nesting; \
  719. if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
  720. _PyTrash_destroy_chain(); \
  721. } \
  722. else \
  723. _PyTrash_deposit_object((PyObject*)op);
  724. #ifdef __cplusplus
  725. }
  726. #endif
  727. #endif /* !Py_OBJECT_H */