object.h 40 KB

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