object.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #ifndef Py_CPYTHON_OBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
  5. PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op);
  6. #ifdef Py_TRACE_REFS
  7. /* Py_TRACE_REFS is such major surgery that we call external routines. */
  8. PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
  9. #endif
  10. #ifdef Py_REF_DEBUG
  11. /* These are useful as debugging aids when chasing down refleaks. */
  12. PyAPI_FUNC(Py_ssize_t) _Py_GetGlobalRefTotal(void);
  13. # define _Py_GetRefTotal() _Py_GetGlobalRefTotal()
  14. PyAPI_FUNC(Py_ssize_t) _Py_GetLegacyRefTotal(void);
  15. PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_GetRefTotal(PyInterpreterState *);
  16. #endif
  17. /********************* String Literals ****************************************/
  18. /* This structure helps managing static strings. The basic usage goes like this:
  19. Instead of doing
  20. r = PyObject_CallMethod(o, "foo", "args", ...);
  21. do
  22. _Py_IDENTIFIER(foo);
  23. ...
  24. r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
  25. PyId_foo is a static variable, either on block level or file level. On first
  26. usage, the string "foo" is interned, and the structures are linked. On interpreter
  27. shutdown, all strings are released.
  28. Alternatively, _Py_static_string allows choosing the variable name.
  29. _PyUnicode_FromId returns a borrowed reference to the interned string.
  30. _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
  31. */
  32. typedef struct _Py_Identifier {
  33. const char* string;
  34. // Index in PyInterpreterState.unicode.ids.array. It is process-wide
  35. // unique and must be initialized to -1.
  36. Py_ssize_t index;
  37. } _Py_Identifier;
  38. #ifndef Py_BUILD_CORE
  39. // For now we are keeping _Py_IDENTIFIER for continued use
  40. // in non-builtin extensions (and naughty PyPI modules).
  41. #define _Py_static_string_init(value) { .string = (value), .index = -1 }
  42. #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
  43. #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
  44. #endif /* !Py_BUILD_CORE */
  45. typedef struct {
  46. /* Number implementations must check *both*
  47. arguments for proper type and implement the necessary conversions
  48. in the slot functions themselves. */
  49. binaryfunc nb_add;
  50. binaryfunc nb_subtract;
  51. binaryfunc nb_multiply;
  52. binaryfunc nb_remainder;
  53. binaryfunc nb_divmod;
  54. ternaryfunc nb_power;
  55. unaryfunc nb_negative;
  56. unaryfunc nb_positive;
  57. unaryfunc nb_absolute;
  58. inquiry nb_bool;
  59. unaryfunc nb_invert;
  60. binaryfunc nb_lshift;
  61. binaryfunc nb_rshift;
  62. binaryfunc nb_and;
  63. binaryfunc nb_xor;
  64. binaryfunc nb_or;
  65. unaryfunc nb_int;
  66. void *nb_reserved; /* the slot formerly known as nb_long */
  67. unaryfunc nb_float;
  68. binaryfunc nb_inplace_add;
  69. binaryfunc nb_inplace_subtract;
  70. binaryfunc nb_inplace_multiply;
  71. binaryfunc nb_inplace_remainder;
  72. ternaryfunc nb_inplace_power;
  73. binaryfunc nb_inplace_lshift;
  74. binaryfunc nb_inplace_rshift;
  75. binaryfunc nb_inplace_and;
  76. binaryfunc nb_inplace_xor;
  77. binaryfunc nb_inplace_or;
  78. binaryfunc nb_floor_divide;
  79. binaryfunc nb_true_divide;
  80. binaryfunc nb_inplace_floor_divide;
  81. binaryfunc nb_inplace_true_divide;
  82. unaryfunc nb_index;
  83. binaryfunc nb_matrix_multiply;
  84. binaryfunc nb_inplace_matrix_multiply;
  85. } PyNumberMethods;
  86. typedef struct {
  87. lenfunc sq_length;
  88. binaryfunc sq_concat;
  89. ssizeargfunc sq_repeat;
  90. ssizeargfunc sq_item;
  91. void *was_sq_slice;
  92. ssizeobjargproc sq_ass_item;
  93. void *was_sq_ass_slice;
  94. objobjproc sq_contains;
  95. binaryfunc sq_inplace_concat;
  96. ssizeargfunc sq_inplace_repeat;
  97. } PySequenceMethods;
  98. typedef struct {
  99. lenfunc mp_length;
  100. binaryfunc mp_subscript;
  101. objobjargproc mp_ass_subscript;
  102. } PyMappingMethods;
  103. typedef PySendResult (*sendfunc)(PyObject *iter, PyObject *value, PyObject **result);
  104. typedef struct {
  105. unaryfunc am_await;
  106. unaryfunc am_aiter;
  107. unaryfunc am_anext;
  108. sendfunc am_send;
  109. } PyAsyncMethods;
  110. typedef struct {
  111. getbufferproc bf_getbuffer;
  112. releasebufferproc bf_releasebuffer;
  113. } PyBufferProcs;
  114. /* Allow printfunc in the tp_vectorcall_offset slot for
  115. * backwards-compatibility */
  116. typedef Py_ssize_t printfunc;
  117. // If this structure is modified, Doc/includes/typestruct.h should be updated
  118. // as well.
  119. struct _typeobject {
  120. PyObject_VAR_HEAD
  121. const char *tp_name; /* For printing, in format "<module>.<name>" */
  122. Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
  123. /* Methods to implement standard operations */
  124. destructor tp_dealloc;
  125. Py_ssize_t tp_vectorcall_offset;
  126. getattrfunc tp_getattr;
  127. setattrfunc tp_setattr;
  128. PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
  129. or tp_reserved (Python 3) */
  130. reprfunc tp_repr;
  131. /* Method suites for standard classes */
  132. PyNumberMethods *tp_as_number;
  133. PySequenceMethods *tp_as_sequence;
  134. PyMappingMethods *tp_as_mapping;
  135. /* More standard operations (here for binary compatibility) */
  136. hashfunc tp_hash;
  137. ternaryfunc tp_call;
  138. reprfunc tp_str;
  139. getattrofunc tp_getattro;
  140. setattrofunc tp_setattro;
  141. /* Functions to access object as input/output buffer */
  142. PyBufferProcs *tp_as_buffer;
  143. /* Flags to define presence of optional/expanded features */
  144. unsigned long tp_flags;
  145. const char *tp_doc; /* Documentation string */
  146. /* Assigned meaning in release 2.0 */
  147. /* call function for all accessible objects */
  148. traverseproc tp_traverse;
  149. /* delete references to contained objects */
  150. inquiry tp_clear;
  151. /* Assigned meaning in release 2.1 */
  152. /* rich comparisons */
  153. richcmpfunc tp_richcompare;
  154. /* weak reference enabler */
  155. Py_ssize_t tp_weaklistoffset;
  156. /* Iterators */
  157. getiterfunc tp_iter;
  158. iternextfunc tp_iternext;
  159. /* Attribute descriptor and subclassing stuff */
  160. PyMethodDef *tp_methods;
  161. PyMemberDef *tp_members;
  162. PyGetSetDef *tp_getset;
  163. // Strong reference on a heap type, borrowed reference on a static type
  164. PyTypeObject *tp_base;
  165. PyObject *tp_dict;
  166. descrgetfunc tp_descr_get;
  167. descrsetfunc tp_descr_set;
  168. Py_ssize_t tp_dictoffset;
  169. initproc tp_init;
  170. allocfunc tp_alloc;
  171. newfunc tp_new;
  172. freefunc tp_free; /* Low-level free-memory routine */
  173. inquiry tp_is_gc; /* For PyObject_IS_GC */
  174. PyObject *tp_bases;
  175. PyObject *tp_mro; /* method resolution order */
  176. PyObject *tp_cache; /* no longer used */
  177. void *tp_subclasses; /* for static builtin types this is an index */
  178. PyObject *tp_weaklist; /* not used for static builtin types */
  179. destructor tp_del;
  180. /* Type attribute cache version tag. Added in version 2.6 */
  181. unsigned int tp_version_tag;
  182. destructor tp_finalize;
  183. vectorcallfunc tp_vectorcall;
  184. /* bitset of which type-watchers care about this type */
  185. unsigned char tp_watched;
  186. };
  187. /* This struct is used by the specializer
  188. * It should should be treated as an opaque blob
  189. * by code other than the specializer and interpreter. */
  190. struct _specialization_cache {
  191. // In order to avoid bloating the bytecode with lots of inline caches, the
  192. // members of this structure have a somewhat unique contract. They are set
  193. // by the specialization machinery, and are invalidated by PyType_Modified.
  194. // The rules for using them are as follows:
  195. // - If getitem is non-NULL, then it is the same Python function that
  196. // PyType_Lookup(cls, "__getitem__") would return.
  197. // - If getitem is NULL, then getitem_version is meaningless.
  198. // - If getitem->func_version == getitem_version, then getitem can be called
  199. // with two positional arguments and no keyword arguments, and has neither
  200. // *args nor **kwargs (as required by BINARY_SUBSCR_GETITEM):
  201. PyObject *getitem;
  202. uint32_t getitem_version;
  203. };
  204. /* The *real* layout of a type object when allocated on the heap */
  205. typedef struct _heaptypeobject {
  206. /* Note: there's a dependency on the order of these members
  207. in slotptr() in typeobject.c . */
  208. PyTypeObject ht_type;
  209. PyAsyncMethods as_async;
  210. PyNumberMethods as_number;
  211. PyMappingMethods as_mapping;
  212. PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
  213. so that the mapping wins when both
  214. the mapping and the sequence define
  215. a given operator (e.g. __getitem__).
  216. see add_operators() in typeobject.c . */
  217. PyBufferProcs as_buffer;
  218. PyObject *ht_name, *ht_slots, *ht_qualname;
  219. struct _dictkeysobject *ht_cached_keys;
  220. PyObject *ht_module;
  221. char *_ht_tpname; // Storage for "tp_name"; see PyType_FromModuleAndSpec
  222. struct _specialization_cache _spec_cache; // For use by the specializer.
  223. /* here are optional user slots, followed by the members. */
  224. } PyHeapTypeObject;
  225. PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
  226. PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
  227. PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
  228. PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
  229. #ifndef Py_BUILD_CORE
  230. // Backward compatibility for 3rd-party extensions
  231. // that may be using the old name.
  232. #define _PyObject_LookupSpecial _PyObject_LookupSpecialId
  233. #endif
  234. PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
  235. PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
  236. PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
  237. PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *);
  238. PyAPI_FUNC(PyObject *) PyType_GetDict(PyTypeObject *);
  239. PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
  240. PyAPI_FUNC(void) _Py_BreakPoint(void);
  241. PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
  242. PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
  243. PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
  244. PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
  245. PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
  246. /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
  247. don't raise AttributeError.
  248. Return 1 and set *result != NULL if an attribute is found.
  249. Return 0 and set *result == NULL if an attribute is not found;
  250. an AttributeError is silenced.
  251. Return -1 and set *result == NULL if an error other than AttributeError
  252. is raised.
  253. */
  254. PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
  255. PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, _Py_Identifier *, PyObject **);
  256. PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
  257. PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
  258. PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
  259. PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
  260. PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
  261. /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
  262. dict as the last parameter. */
  263. PyAPI_FUNC(PyObject *)
  264. _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
  265. PyAPI_FUNC(int)
  266. _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
  267. PyObject *, PyObject *);
  268. PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
  269. /* Safely decref `dst` and set `dst` to `src`.
  270. *
  271. * As in case of Py_CLEAR "the obvious" code can be deadly:
  272. *
  273. * Py_DECREF(dst);
  274. * dst = src;
  275. *
  276. * The safe way is:
  277. *
  278. * Py_SETREF(dst, src);
  279. *
  280. * That arranges to set `dst` to `src` _before_ decref'ing, so that any code
  281. * triggered as a side-effect of `dst` getting torn down no longer believes
  282. * `dst` points to a valid object.
  283. *
  284. * Temporary variables are used to only evalutate macro arguments once and so
  285. * avoid the duplication of side effects. _Py_TYPEOF() or memcpy() is used to
  286. * avoid a miscompilation caused by type punning. See Py_CLEAR() comment for
  287. * implementation details about type punning.
  288. *
  289. * The memcpy() implementation does not emit a compiler warning if 'src' has
  290. * not the same type than 'src': any pointer type is accepted for 'src'.
  291. */
  292. #ifdef _Py_TYPEOF
  293. #define Py_SETREF(dst, src) \
  294. do { \
  295. _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
  296. _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
  297. *_tmp_dst_ptr = (src); \
  298. Py_DECREF(_tmp_old_dst); \
  299. } while (0)
  300. #else
  301. #define Py_SETREF(dst, src) \
  302. do { \
  303. PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
  304. PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
  305. PyObject *_tmp_src = _PyObject_CAST(src); \
  306. memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
  307. Py_DECREF(_tmp_old_dst); \
  308. } while (0)
  309. #endif
  310. /* Py_XSETREF() is a variant of Py_SETREF() that uses Py_XDECREF() instead of
  311. * Py_DECREF().
  312. */
  313. #ifdef _Py_TYPEOF
  314. #define Py_XSETREF(dst, src) \
  315. do { \
  316. _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
  317. _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
  318. *_tmp_dst_ptr = (src); \
  319. Py_XDECREF(_tmp_old_dst); \
  320. } while (0)
  321. #else
  322. #define Py_XSETREF(dst, src) \
  323. do { \
  324. PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
  325. PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
  326. PyObject *_tmp_src = _PyObject_CAST(src); \
  327. memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
  328. Py_XDECREF(_tmp_old_dst); \
  329. } while (0)
  330. #endif
  331. PyAPI_DATA(PyTypeObject) _PyNone_Type;
  332. PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
  333. /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
  334. * Defined in object.c.
  335. */
  336. PyAPI_DATA(int) _Py_SwappedOp[];
  337. PyAPI_FUNC(void)
  338. _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
  339. size_t sizeof_block);
  340. PyAPI_FUNC(void)
  341. _PyObject_DebugTypeStats(FILE *out);
  342. /* Define a pair of assertion macros:
  343. _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
  344. These work like the regular C assert(), in that they will abort the
  345. process with a message on stderr if the given condition fails to hold,
  346. but compile away to nothing if NDEBUG is defined.
  347. However, before aborting, Python will also try to call _PyObject_Dump() on
  348. the given object. This may be of use when investigating bugs in which a
  349. particular object is corrupt (e.g. buggy a tp_visit method in an extension
  350. module breaking the garbage collector), to help locate the broken objects.
  351. The WITH_MSG variant allows you to supply an additional message that Python
  352. will attempt to print to stderr, after the object dump. */
  353. #ifdef NDEBUG
  354. /* No debugging: compile away the assertions: */
  355. # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
  356. ((void)0)
  357. #else
  358. /* With debugging: generate checks: */
  359. # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
  360. ((expr) \
  361. ? (void)(0) \
  362. : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
  363. (msg), (filename), (lineno), (func)))
  364. #endif
  365. #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
  366. _PyObject_ASSERT_FROM((obj), expr, (msg), __FILE__, __LINE__, __func__)
  367. #define _PyObject_ASSERT(obj, expr) \
  368. _PyObject_ASSERT_WITH_MSG((obj), expr, NULL)
  369. #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
  370. _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
  371. /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
  372. to avoid causing compiler/linker errors when building extensions without
  373. NDEBUG against a Python built with NDEBUG defined.
  374. msg, expr and function can be NULL. */
  375. PyAPI_FUNC(void) _Py_NO_RETURN _PyObject_AssertFailed(
  376. PyObject *obj,
  377. const char *expr,
  378. const char *msg,
  379. const char *file,
  380. int line,
  381. const char *function);
  382. /* Check if an object is consistent. For example, ensure that the reference
  383. counter is greater than or equal to 1, and ensure that ob_type is not NULL.
  384. Call _PyObject_AssertFailed() if the object is inconsistent.
  385. If check_content is zero, only check header fields: reduce the overhead.
  386. The function always return 1. The return value is just here to be able to
  387. write:
  388. assert(_PyObject_CheckConsistency(obj, 1)); */
  389. PyAPI_FUNC(int) _PyObject_CheckConsistency(
  390. PyObject *op,
  391. int check_content);
  392. /* Trashcan mechanism, thanks to Christian Tismer.
  393. When deallocating a container object, it's possible to trigger an unbounded
  394. chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
  395. next" object in the chain to 0. This can easily lead to stack overflows,
  396. especially in threads (which typically have less stack space to work with).
  397. A container object can avoid this by bracketing the body of its tp_dealloc
  398. function with a pair of macros:
  399. static void
  400. mytype_dealloc(mytype *p)
  401. {
  402. ... declarations go here ...
  403. PyObject_GC_UnTrack(p); // must untrack first
  404. Py_TRASHCAN_BEGIN(p, mytype_dealloc)
  405. ... The body of the deallocator goes here, including all calls ...
  406. ... to Py_DECREF on contained objects. ...
  407. Py_TRASHCAN_END // there should be no code after this
  408. }
  409. CAUTION: Never return from the middle of the body! If the body needs to
  410. "get out early", put a label immediately before the Py_TRASHCAN_END
  411. call, and goto it. Else the call-depth counter (see below) will stay
  412. above 0 forever, and the trashcan will never get emptied.
  413. How it works: The BEGIN macro increments a call-depth counter. So long
  414. as this counter is small, the body of the deallocator is run directly without
  415. further ado. But if the counter gets large, it instead adds p to a list of
  416. objects to be deallocated later, skips the body of the deallocator, and
  417. resumes execution after the END macro. The tp_dealloc routine then returns
  418. without deallocating anything (and so unbounded call-stack depth is avoided).
  419. When the call stack finishes unwinding again, code generated by the END macro
  420. notices this, and calls another routine to deallocate all the objects that
  421. may have been added to the list of deferred deallocations. In effect, a
  422. chain of N deallocations is broken into (N-1)/(_PyTrash_UNWIND_LEVEL-1) pieces,
  423. with the call stack never exceeding a depth of _PyTrash_UNWIND_LEVEL.
  424. Since the tp_dealloc of a subclass typically calls the tp_dealloc of the base
  425. class, we need to ensure that the trashcan is only triggered on the tp_dealloc
  426. of the actual class being deallocated. Otherwise we might end up with a
  427. partially-deallocated object. To check this, the tp_dealloc function must be
  428. passed as second argument to Py_TRASHCAN_BEGIN().
  429. */
  430. /* Python 3.9 private API, invoked by the macros below. */
  431. PyAPI_FUNC(int) _PyTrash_begin(PyThreadState *tstate, PyObject *op);
  432. PyAPI_FUNC(void) _PyTrash_end(PyThreadState *tstate);
  433. /* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */
  434. PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
  435. #define Py_TRASHCAN_BEGIN_CONDITION(op, cond) \
  436. do { \
  437. PyThreadState *_tstate = NULL; \
  438. /* If "cond" is false, then _tstate remains NULL and the deallocator \
  439. * is run normally without involving the trashcan */ \
  440. if (cond) { \
  441. _tstate = _PyThreadState_UncheckedGet(); \
  442. if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \
  443. break; \
  444. } \
  445. }
  446. /* The body of the deallocator is here. */
  447. #define Py_TRASHCAN_END \
  448. if (_tstate) { \
  449. _PyTrash_end(_tstate); \
  450. } \
  451. } while (0);
  452. #define Py_TRASHCAN_BEGIN(op, dealloc) \
  453. Py_TRASHCAN_BEGIN_CONDITION((op), \
  454. _PyTrash_cond(_PyObject_CAST(op), (destructor)(dealloc)))
  455. /* The following two macros, Py_TRASHCAN_SAFE_BEGIN and
  456. * Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and
  457. * will be removed in the future.
  458. * Use Py_TRASHCAN_BEGIN and Py_TRASHCAN_END instead.
  459. */
  460. Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro;
  461. #define Py_TRASHCAN_SAFE_BEGIN(op) \
  462. do { \
  463. UsingDeprecatedTrashcanMacro cond=1; \
  464. Py_TRASHCAN_BEGIN_CONDITION((op), cond);
  465. #define Py_TRASHCAN_SAFE_END(op) \
  466. Py_TRASHCAN_END; \
  467. } while(0);
  468. PyAPI_FUNC(void *) PyObject_GetItemData(PyObject *obj);
  469. PyAPI_FUNC(int) _PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg);
  470. PyAPI_FUNC(void) _PyObject_ClearManagedDict(PyObject *obj);
  471. #define TYPE_MAX_WATCHERS 8
  472. typedef int(*PyType_WatchCallback)(PyTypeObject *);
  473. PyAPI_FUNC(int) PyType_AddWatcher(PyType_WatchCallback callback);
  474. PyAPI_FUNC(int) PyType_ClearWatcher(int watcher_id);
  475. PyAPI_FUNC(int) PyType_Watch(int watcher_id, PyObject *type);
  476. PyAPI_FUNC(int) PyType_Unwatch(int watcher_id, PyObject *type);
  477. /* Attempt to assign a version tag to the given type.
  478. *
  479. * Returns 1 if the type already had a valid version tag or a new one was
  480. * assigned, or 0 if a new tag could not be assigned.
  481. */
  482. PyAPI_FUNC(int) PyUnstable_Type_AssignVersionTag(PyTypeObject *type);