blob: 304cfbfc37dff0f92fbe46ca16b6a3ac64686681 [file] [log] [blame]
Victor Stinner6eb99662018-11-26 17:09:16 +01001#ifndef Py_CPYTHON_OBJECT_H
2# error "this header file must not be included directly"
3#endif
4
Victor Stinnerf58bd7c2020-02-05 13:12:19 +01005PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
6
7#ifdef Py_TRACE_REFS
8/* Py_TRACE_REFS is such major surgery that we call external routines. */
9PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
10#endif
11
12/* Update the Python traceback of an object. This function must be called
13 when a memory block is reused from a free list. */
14PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
15
16#ifdef Py_REF_DEBUG
17PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
18#endif
19
20
Victor Stinner6eb99662018-11-26 17:09:16 +010021/********************* String Literals ****************************************/
22/* This structure helps managing static strings. The basic usage goes like this:
23 Instead of doing
24
25 r = PyObject_CallMethod(o, "foo", "args", ...);
26
27 do
28
29 _Py_IDENTIFIER(foo);
30 ...
31 r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
32
33 PyId_foo is a static variable, either on block level or file level. On first
34 usage, the string "foo" is interned, and the structures are linked. On interpreter
Victor Stinnerd6fb53f2020-05-14 01:11:54 +020035 shutdown, all strings are released.
Victor Stinner6eb99662018-11-26 17:09:16 +010036
37 Alternatively, _Py_static_string allows choosing the variable name.
38 _PyUnicode_FromId returns a borrowed reference to the interned string.
39 _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
40*/
41typedef struct _Py_Identifier {
42 struct _Py_Identifier *next;
43 const char* string;
44 PyObject *object;
45} _Py_Identifier;
46
47#define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
48#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
49#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
50
51/* buffer interface */
52typedef struct bufferinfo {
53 void *buf;
54 PyObject *obj; /* owned reference */
55 Py_ssize_t len;
56 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
57 pointed to by strides in simple case.*/
58 int readonly;
59 int ndim;
60 char *format;
61 Py_ssize_t *shape;
62 Py_ssize_t *strides;
63 Py_ssize_t *suboffsets;
64 void *internal;
65} Py_buffer;
66
67typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
68typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
69
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020070typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
71 size_t nargsf, PyObject *kwnames);
72
Victor Stinner6eb99662018-11-26 17:09:16 +010073/* Maximum number of dimensions */
74#define PyBUF_MAX_NDIM 64
75
76/* Flags for getting buffers */
77#define PyBUF_SIMPLE 0
78#define PyBUF_WRITABLE 0x0001
79/* we used to include an E, backwards compatible alias */
80#define PyBUF_WRITEABLE PyBUF_WRITABLE
81#define PyBUF_FORMAT 0x0004
82#define PyBUF_ND 0x0008
83#define PyBUF_STRIDES (0x0010 | PyBUF_ND)
84#define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
85#define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
86#define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
87#define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
88
89#define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
90#define PyBUF_CONTIG_RO (PyBUF_ND)
91
92#define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
93#define PyBUF_STRIDED_RO (PyBUF_STRIDES)
94
95#define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
96#define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
97
98#define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
99#define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
100
101
102#define PyBUF_READ 0x100
103#define PyBUF_WRITE 0x200
104/* End buffer interface */
105
106
107typedef struct {
108 /* Number implementations must check *both*
109 arguments for proper type and implement the necessary conversions
110 in the slot functions themselves. */
111
112 binaryfunc nb_add;
113 binaryfunc nb_subtract;
114 binaryfunc nb_multiply;
115 binaryfunc nb_remainder;
116 binaryfunc nb_divmod;
117 ternaryfunc nb_power;
118 unaryfunc nb_negative;
119 unaryfunc nb_positive;
120 unaryfunc nb_absolute;
121 inquiry nb_bool;
122 unaryfunc nb_invert;
123 binaryfunc nb_lshift;
124 binaryfunc nb_rshift;
125 binaryfunc nb_and;
126 binaryfunc nb_xor;
127 binaryfunc nb_or;
128 unaryfunc nb_int;
129 void *nb_reserved; /* the slot formerly known as nb_long */
130 unaryfunc nb_float;
131
132 binaryfunc nb_inplace_add;
133 binaryfunc nb_inplace_subtract;
134 binaryfunc nb_inplace_multiply;
135 binaryfunc nb_inplace_remainder;
136 ternaryfunc nb_inplace_power;
137 binaryfunc nb_inplace_lshift;
138 binaryfunc nb_inplace_rshift;
139 binaryfunc nb_inplace_and;
140 binaryfunc nb_inplace_xor;
141 binaryfunc nb_inplace_or;
142
143 binaryfunc nb_floor_divide;
144 binaryfunc nb_true_divide;
145 binaryfunc nb_inplace_floor_divide;
146 binaryfunc nb_inplace_true_divide;
147
148 unaryfunc nb_index;
149
150 binaryfunc nb_matrix_multiply;
151 binaryfunc nb_inplace_matrix_multiply;
152} PyNumberMethods;
153
154typedef struct {
155 lenfunc sq_length;
156 binaryfunc sq_concat;
157 ssizeargfunc sq_repeat;
158 ssizeargfunc sq_item;
159 void *was_sq_slice;
160 ssizeobjargproc sq_ass_item;
161 void *was_sq_ass_slice;
162 objobjproc sq_contains;
163
164 binaryfunc sq_inplace_concat;
165 ssizeargfunc sq_inplace_repeat;
166} PySequenceMethods;
167
168typedef struct {
169 lenfunc mp_length;
170 binaryfunc mp_subscript;
171 objobjargproc mp_ass_subscript;
172} PyMappingMethods;
173
174typedef struct {
175 unaryfunc am_await;
176 unaryfunc am_aiter;
177 unaryfunc am_anext;
178} PyAsyncMethods;
179
180typedef struct {
181 getbufferproc bf_getbuffer;
182 releasebufferproc bf_releasebuffer;
183} PyBufferProcs;
184
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200185/* Allow printfunc in the tp_vectorcall_offset slot for
186 * backwards-compatibility */
187typedef Py_ssize_t printfunc;
Victor Stinner6eb99662018-11-26 17:09:16 +0100188
Victor Stinnerf95cd192020-02-07 01:43:25 +0100189struct _typeobject {
Victor Stinner6eb99662018-11-26 17:09:16 +0100190 PyObject_VAR_HEAD
191 const char *tp_name; /* For printing, in format "<module>.<name>" */
192 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
193
194 /* Methods to implement standard operations */
195
196 destructor tp_dealloc;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200197 Py_ssize_t tp_vectorcall_offset;
Victor Stinner6eb99662018-11-26 17:09:16 +0100198 getattrfunc tp_getattr;
199 setattrfunc tp_setattr;
200 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
201 or tp_reserved (Python 3) */
202 reprfunc tp_repr;
203
204 /* Method suites for standard classes */
205
206 PyNumberMethods *tp_as_number;
207 PySequenceMethods *tp_as_sequence;
208 PyMappingMethods *tp_as_mapping;
209
210 /* More standard operations (here for binary compatibility) */
211
212 hashfunc tp_hash;
213 ternaryfunc tp_call;
214 reprfunc tp_str;
215 getattrofunc tp_getattro;
216 setattrofunc tp_setattro;
217
218 /* Functions to access object as input/output buffer */
219 PyBufferProcs *tp_as_buffer;
220
221 /* Flags to define presence of optional/expanded features */
222 unsigned long tp_flags;
223
224 const char *tp_doc; /* Documentation string */
225
226 /* Assigned meaning in release 2.0 */
227 /* call function for all accessible objects */
228 traverseproc tp_traverse;
229
230 /* delete references to contained objects */
231 inquiry tp_clear;
232
233 /* Assigned meaning in release 2.1 */
234 /* rich comparisons */
235 richcmpfunc tp_richcompare;
236
237 /* weak reference enabler */
238 Py_ssize_t tp_weaklistoffset;
239
240 /* Iterators */
241 getiterfunc tp_iter;
242 iternextfunc tp_iternext;
243
244 /* Attribute descriptor and subclassing stuff */
245 struct PyMethodDef *tp_methods;
246 struct PyMemberDef *tp_members;
247 struct PyGetSetDef *tp_getset;
248 struct _typeobject *tp_base;
249 PyObject *tp_dict;
250 descrgetfunc tp_descr_get;
251 descrsetfunc tp_descr_set;
252 Py_ssize_t tp_dictoffset;
253 initproc tp_init;
254 allocfunc tp_alloc;
255 newfunc tp_new;
256 freefunc tp_free; /* Low-level free-memory routine */
257 inquiry tp_is_gc; /* For PyObject_IS_GC */
258 PyObject *tp_bases;
259 PyObject *tp_mro; /* method resolution order */
260 PyObject *tp_cache;
261 PyObject *tp_subclasses;
262 PyObject *tp_weaklist;
263 destructor tp_del;
264
265 /* Type attribute cache version tag. Added in version 2.6 */
266 unsigned int tp_version_tag;
267
268 destructor tp_finalize;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200269 vectorcallfunc tp_vectorcall;
Victor Stinnerf95cd192020-02-07 01:43:25 +0100270};
Victor Stinner6eb99662018-11-26 17:09:16 +0100271
272/* The *real* layout of a type object when allocated on the heap */
273typedef struct _heaptypeobject {
274 /* Note: there's a dependency on the order of these members
275 in slotptr() in typeobject.c . */
276 PyTypeObject ht_type;
277 PyAsyncMethods as_async;
278 PyNumberMethods as_number;
279 PyMappingMethods as_mapping;
280 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
281 so that the mapping wins when both
282 the mapping and the sequence define
283 a given operator (e.g. __getitem__).
284 see add_operators() in typeobject.c . */
285 PyBufferProcs as_buffer;
286 PyObject *ht_name, *ht_slots, *ht_qualname;
287 struct _dictkeysobject *ht_cached_keys;
Petr Viktorine1becf42020-05-07 15:39:59 +0200288 PyObject *ht_module;
Victor Stinner6eb99662018-11-26 17:09:16 +0100289 /* here are optional user slots, followed by the members. */
290} PyHeapTypeObject;
291
292/* access macro to the members which are floating "behind" the object */
293#define PyHeapType_GET_MEMBERS(etype) \
294 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
295
296PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
297PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
298PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
299PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
300PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
301PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
302PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
303
304struct _Py_Identifier;
305PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
306PyAPI_FUNC(void) _Py_BreakPoint(void);
307PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
308PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
309
310PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
311PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
312PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
313PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
314/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
315 don't raise AttributeError.
316
317 Return 1 and set *result != NULL if an attribute is found.
318 Return 0 and set *result == NULL if an attribute is not found;
319 an AttributeError is silenced.
320 Return -1 and set *result == NULL if an error other than AttributeError
321 is raised.
322*/
323PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
324PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
Jeroen Demeyerb2f94732019-06-14 12:37:15 +0200325
326PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
327
Victor Stinner6eb99662018-11-26 17:09:16 +0100328PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
329PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
330PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
331PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
332
333/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
334 dict as the last parameter. */
335PyAPI_FUNC(PyObject *)
336_PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
337PyAPI_FUNC(int)
338_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
339 PyObject *, PyObject *);
340
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100341PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
Victor Stinner6eb99662018-11-26 17:09:16 +0100342
343/* Safely decref `op` and set `op` to `op2`.
344 *
345 * As in case of Py_CLEAR "the obvious" code can be deadly:
346 *
347 * Py_DECREF(op);
348 * op = op2;
349 *
350 * The safe way is:
351 *
352 * Py_SETREF(op, op2);
353 *
354 * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
355 * triggered as a side-effect of `op` getting torn down no longer believes
356 * `op` points to a valid object.
357 *
358 * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
359 * Py_DECREF.
360 */
361
362#define Py_SETREF(op, op2) \
363 do { \
364 PyObject *_py_tmp = _PyObject_CAST(op); \
365 (op) = (op2); \
366 Py_DECREF(_py_tmp); \
367 } while (0)
368
369#define Py_XSETREF(op, op2) \
370 do { \
371 PyObject *_py_tmp = _PyObject_CAST(op); \
372 (op) = (op2); \
373 Py_XDECREF(_py_tmp); \
374 } while (0)
375
376
377PyAPI_DATA(PyTypeObject) _PyNone_Type;
378PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
379
380/* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
381 * Defined in object.c.
382 */
383PyAPI_DATA(int) _Py_SwappedOp[];
384
Victor Stinner6eb99662018-11-26 17:09:16 +0100385PyAPI_FUNC(void)
386_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
387 size_t sizeof_block);
388PyAPI_FUNC(void)
389_PyObject_DebugTypeStats(FILE *out);
390
391/* Define a pair of assertion macros:
392 _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
393
394 These work like the regular C assert(), in that they will abort the
395 process with a message on stderr if the given condition fails to hold,
396 but compile away to nothing if NDEBUG is defined.
397
398 However, before aborting, Python will also try to call _PyObject_Dump() on
399 the given object. This may be of use when investigating bugs in which a
400 particular object is corrupt (e.g. buggy a tp_visit method in an extension
401 module breaking the garbage collector), to help locate the broken objects.
402
403 The WITH_MSG variant allows you to supply an additional message that Python
404 will attempt to print to stderr, after the object dump. */
405#ifdef NDEBUG
406 /* No debugging: compile away the assertions: */
407# define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
408 ((void)0)
409#else
410 /* With debugging: generate checks: */
411# define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
412 ((expr) \
413 ? (void)(0) \
414 : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
415 (msg), (filename), (lineno), (func)))
416#endif
417
418#define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
419 _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
420#define _PyObject_ASSERT(obj, expr) \
421 _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
422
423#define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
424 _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
425
426/* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
427 to avoid causing compiler/linker errors when building extensions without
428 NDEBUG against a Python built with NDEBUG defined.
429
430 msg, expr and function can be NULL. */
Victor Stinner2a4903f2020-01-30 13:09:11 +0100431PyAPI_FUNC(void) _Py_NO_RETURN _PyObject_AssertFailed(
Victor Stinner6eb99662018-11-26 17:09:16 +0100432 PyObject *obj,
433 const char *expr,
434 const char *msg,
435 const char *file,
436 int line,
437 const char *function);
438
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200439/* Check if an object is consistent. For example, ensure that the reference
440 counter is greater than or equal to 1, and ensure that ob_type is not NULL.
441
442 Call _PyObject_AssertFailed() if the object is inconsistent.
443
444 If check_content is zero, only check header fields: reduce the overhead.
445
446 The function always return 1. The return value is just here to be able to
447 write:
448
449 assert(_PyObject_CheckConsistency(obj, 1)); */
450PyAPI_FUNC(int) _PyObject_CheckConsistency(
451 PyObject *op,
452 int check_content);
453
Victor Stinner0fa4f432020-02-05 12:23:27 +0100454
455/* Trashcan mechanism, thanks to Christian Tismer.
456
457When deallocating a container object, it's possible to trigger an unbounded
458chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
459next" object in the chain to 0. This can easily lead to stack overflows,
460especially in threads (which typically have less stack space to work with).
461
462A container object can avoid this by bracketing the body of its tp_dealloc
463function with a pair of macros:
464
465static void
466mytype_dealloc(mytype *p)
467{
468 ... declarations go here ...
469
470 PyObject_GC_UnTrack(p); // must untrack first
471 Py_TRASHCAN_BEGIN(p, mytype_dealloc)
472 ... The body of the deallocator goes here, including all calls ...
473 ... to Py_DECREF on contained objects. ...
474 Py_TRASHCAN_END // there should be no code after this
475}
476
477CAUTION: Never return from the middle of the body! If the body needs to
478"get out early", put a label immediately before the Py_TRASHCAN_END
479call, and goto it. Else the call-depth counter (see below) will stay
480above 0 forever, and the trashcan will never get emptied.
481
482How it works: The BEGIN macro increments a call-depth counter. So long
483as this counter is small, the body of the deallocator is run directly without
484further ado. But if the counter gets large, it instead adds p to a list of
485objects to be deallocated later, skips the body of the deallocator, and
486resumes execution after the END macro. The tp_dealloc routine then returns
487without deallocating anything (and so unbounded call-stack depth is avoided).
488
489When the call stack finishes unwinding again, code generated by the END macro
490notices this, and calls another routine to deallocate all the objects that
491may have been added to the list of deferred deallocations. In effect, a
492chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces,
493with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
494
495Since the tp_dealloc of a subclass typically calls the tp_dealloc of the base
496class, we need to ensure that the trashcan is only triggered on the tp_dealloc
497of the actual class being deallocated. Otherwise we might end up with a
498partially-deallocated object. To check this, the tp_dealloc function must be
499passed as second argument to Py_TRASHCAN_BEGIN().
500*/
501
Victor Stinner38965ec2020-03-13 16:51:52 +0100502/* This is the old private API, invoked by the macros before 3.2.4.
503 Kept for binary compatibility of extensions using the stable ABI. */
504PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
505PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
506
507/* This is the old private API, invoked by the macros before 3.9.
508 Kept for binary compatibility of extensions using the stable ABI. */
Victor Stinner0fa4f432020-02-05 12:23:27 +0100509PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
510PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
511
Victor Stinner38965ec2020-03-13 16:51:52 +0100512/* Forward declarations for PyThreadState */
513struct _ts;
514
515/* Python 3.9 private API, invoked by the macros below. */
516PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op);
517PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
518
Victor Stinner0fa4f432020-02-05 12:23:27 +0100519#define PyTrash_UNWIND_LEVEL 50
520
521#define Py_TRASHCAN_BEGIN_CONDITION(op, cond) \
522 do { \
523 PyThreadState *_tstate = NULL; \
524 /* If "cond" is false, then _tstate remains NULL and the deallocator \
525 * is run normally without involving the trashcan */ \
526 if (cond) { \
527 _tstate = PyThreadState_GET(); \
Victor Stinner38965ec2020-03-13 16:51:52 +0100528 if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \
Victor Stinner0fa4f432020-02-05 12:23:27 +0100529 break; \
530 } \
Victor Stinner0fa4f432020-02-05 12:23:27 +0100531 }
532 /* The body of the deallocator is here. */
533#define Py_TRASHCAN_END \
534 if (_tstate) { \
Victor Stinner38965ec2020-03-13 16:51:52 +0100535 _PyTrash_end(_tstate); \
Victor Stinner0fa4f432020-02-05 12:23:27 +0100536 } \
537 } while (0);
538
Victor Stinner38965ec2020-03-13 16:51:52 +0100539#define Py_TRASHCAN_BEGIN(op, dealloc) \
540 Py_TRASHCAN_BEGIN_CONDITION(op, \
Victor Stinner0fa4f432020-02-05 12:23:27 +0100541 Py_TYPE(op)->tp_dealloc == (destructor)(dealloc))
542
543/* For backwards compatibility, these macros enable the trashcan
544 * unconditionally */
545#define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1)
546#define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END