(root)/
Python-3.11.7/
Include/
internal/
pycore_object.h
       1  #ifndef Py_INTERNAL_OBJECT_H
       2  #define Py_INTERNAL_OBJECT_H
       3  #ifdef __cplusplus
       4  extern "C" {
       5  #endif
       6  
       7  #ifndef Py_BUILD_CORE
       8  #  error "this header requires Py_BUILD_CORE define"
       9  #endif
      10  
      11  #include <stdbool.h>
      12  #include "pycore_gc.h"            // _PyObject_GC_IS_TRACKED()
      13  #include "pycore_interp.h"        // PyInterpreterState.gc
      14  #include "pycore_pystate.h"       // _PyInterpreterState_GET()
      15  #include "pycore_runtime.h"       // _PyRuntime
      16  
      17  #define _PyObject_IMMORTAL_INIT(type) \
      18      { \
      19          .ob_refcnt = 999999999, \
      20          .ob_type = type, \
      21      }
      22  #define _PyVarObject_IMMORTAL_INIT(type, size) \
      23      { \
      24          .ob_base = _PyObject_IMMORTAL_INIT(type), \
      25          .ob_size = size, \
      26      }
      27  
      28  PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
      29      const char *func,
      30      const char *message);
      31  
      32  #define _Py_FatalRefcountError(message) _Py_FatalRefcountErrorFunc(__func__, message)
      33  
      34  static inline void
      35  _Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct)
      36  {
      37  #ifdef Py_REF_DEBUG
      38      _Py_RefTotal--;
      39  #endif
      40      if (--op->ob_refcnt != 0) {
      41          assert(op->ob_refcnt > 0);
      42      }
      43      else {
      44  #ifdef Py_TRACE_REFS
      45          _Py_ForgetReference(op);
      46  #endif
      47          destruct(op);
      48      }
      49  }
      50  
      51  static inline void
      52  _Py_DECREF_NO_DEALLOC(PyObject *op)
      53  {
      54  #ifdef Py_REF_DEBUG
      55      _Py_RefTotal--;
      56  #endif
      57      op->ob_refcnt--;
      58  #ifdef Py_DEBUG
      59      if (op->ob_refcnt <= 0) {
      60          _Py_FatalRefcountError("Expected a positive remaining refcount");
      61      }
      62  #endif
      63  }
      64  
      65  PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
      66  PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
      67  
      68  /* Update the Python traceback of an object. This function must be called
      69     when a memory block is reused from a free list.
      70  
      71     Internal function called by _Py_NewReference(). */
      72  extern int _PyTraceMalloc_NewReference(PyObject *op);
      73  
      74  // Fast inlined version of PyType_HasFeature()
      75  static inline int
      76  _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
      77      return ((type->tp_flags & feature) != 0);
      78  }
      79  
      80  extern void _PyType_InitCache(PyInterpreterState *interp);
      81  
      82  
      83  /* Inline functions trading binary compatibility for speed:
      84     _PyObject_Init() is the fast version of PyObject_Init(), and
      85     _PyObject_InitVar() is the fast version of PyObject_InitVar().
      86  
      87     These inline functions must not be called with op=NULL. */
      88  static inline void
      89  _PyObject_Init(PyObject *op, PyTypeObject *typeobj)
      90  {
      91      assert(op != NULL);
      92      Py_SET_TYPE(op, typeobj);
      93      if (_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE)) {
      94          Py_INCREF(typeobj);
      95      }
      96      _Py_NewReference(op);
      97  }
      98  
      99  static inline void
     100  _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
     101  {
     102      assert(op != NULL);
     103      Py_SET_SIZE(op, size);
     104      _PyObject_Init((PyObject *)op, typeobj);
     105  }
     106  
     107  
     108  /* Tell the GC to track this object.
     109   *
     110   * The object must not be tracked by the GC.
     111   *
     112   * NB: While the object is tracked by the collector, it must be safe to call the
     113   * ob_traverse method.
     114   *
     115   * Internal note: interp->gc.generation0->_gc_prev doesn't have any bit flags
     116   * because it's not object header.  So we don't use _PyGCHead_PREV() and
     117   * _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
     118   *
     119   * See also the public PyObject_GC_Track() function.
     120   */
     121  static inline void _PyObject_GC_TRACK(
     122  // The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
     123  #ifndef NDEBUG
     124      const char *filename, int lineno,
     125  #endif
     126      PyObject *op)
     127  {
     128      _PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op),
     129                            "object already tracked by the garbage collector",
     130                            filename, lineno, __func__);
     131  
     132      PyGC_Head *gc = _Py_AS_GC(op);
     133      _PyObject_ASSERT_FROM(op,
     134                            (gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0,
     135                            "object is in generation which is garbage collected",
     136                            filename, lineno, __func__);
     137  
     138      PyInterpreterState *interp = _PyInterpreterState_GET();
     139      PyGC_Head *generation0 = interp->gc.generation0;
     140      PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
     141      _PyGCHead_SET_NEXT(last, gc);
     142      _PyGCHead_SET_PREV(gc, last);
     143      _PyGCHead_SET_NEXT(gc, generation0);
     144      generation0->_gc_prev = (uintptr_t)gc;
     145  }
     146  
     147  /* Tell the GC to stop tracking this object.
     148   *
     149   * Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
     150   * must be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
     151   *
     152   * The object must be tracked by the GC.
     153   *
     154   * See also the public PyObject_GC_UnTrack() which accept an object which is
     155   * not tracked.
     156   */
     157  static inline void _PyObject_GC_UNTRACK(
     158  // The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
     159  #ifndef NDEBUG
     160      const char *filename, int lineno,
     161  #endif
     162      PyObject *op)
     163  {
     164      _PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op),
     165                            "object not tracked by the garbage collector",
     166                            filename, lineno, __func__);
     167  
     168      PyGC_Head *gc = _Py_AS_GC(op);
     169      PyGC_Head *prev = _PyGCHead_PREV(gc);
     170      PyGC_Head *next = _PyGCHead_NEXT(gc);
     171      _PyGCHead_SET_NEXT(prev, next);
     172      _PyGCHead_SET_PREV(next, prev);
     173      gc->_gc_next = 0;
     174      gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
     175  }
     176  
     177  // Macros to accept any type for the parameter, and to automatically pass
     178  // the filename and the filename (if NDEBUG is not defined) where the macro
     179  // is called.
     180  #ifdef NDEBUG
     181  #  define _PyObject_GC_TRACK(op) \
     182          _PyObject_GC_TRACK(_PyObject_CAST(op))
     183  #  define _PyObject_GC_UNTRACK(op) \
     184          _PyObject_GC_UNTRACK(_PyObject_CAST(op))
     185  #else
     186  #  define _PyObject_GC_TRACK(op) \
     187          _PyObject_GC_TRACK(__FILE__, __LINE__, _PyObject_CAST(op))
     188  #  define _PyObject_GC_UNTRACK(op) \
     189          _PyObject_GC_UNTRACK(__FILE__, __LINE__, _PyObject_CAST(op))
     190  #endif
     191  
     192  #ifdef Py_REF_DEBUG
     193  extern void _PyDebug_PrintTotalRefs(void);
     194  #endif
     195  
     196  #ifdef Py_TRACE_REFS
     197  extern void _Py_AddToAllObjects(PyObject *op, int force);
     198  extern void _Py_PrintReferences(FILE *);
     199  extern void _Py_PrintReferenceAddresses(FILE *);
     200  #endif
     201  
     202  static inline PyObject **
     203  _PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
     204  {
     205      Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset;
     206      return (PyObject **)((char *)op + offset);
     207  }
     208  
     209  // Fast inlined version of PyObject_IS_GC()
     210  static inline int
     211  _PyObject_IS_GC(PyObject *obj)
     212  {
     213      return (PyType_IS_GC(Py_TYPE(obj))
     214              && (Py_TYPE(obj)->tp_is_gc == NULL
     215                  || Py_TYPE(obj)->tp_is_gc(obj)));
     216  }
     217  
     218  // Fast inlined version of PyType_IS_GC()
     219  #define _PyType_IS_GC(t) _PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
     220  
     221  static inline size_t
     222  _PyType_PreHeaderSize(PyTypeObject *tp)
     223  {
     224      return _PyType_IS_GC(tp) * sizeof(PyGC_Head) +
     225          _PyType_HasFeature(tp, Py_TPFLAGS_MANAGED_DICT) * 2 * sizeof(PyObject *);
     226  }
     227  
     228  void _PyObject_GC_Link(PyObject *op);
     229  
     230  // Usage: assert(_Py_CheckSlotResult(obj, "__getitem__", result != NULL));
     231  extern int _Py_CheckSlotResult(
     232      PyObject *obj,
     233      const char *slot_name,
     234      int success);
     235  
     236  // PyType_Ready() must be called if _PyType_IsReady() is false.
     237  // See also the Py_TPFLAGS_READY flag.
     238  #define _PyType_IsReady(type) ((type)->tp_dict != NULL)
     239  
     240  // Test if a type supports weak references
     241  static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) {
     242      return (type->tp_weaklistoffset > 0);
     243  }
     244  
     245  extern PyObject* _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems);
     246  
     247  extern int _PyObject_InitializeDict(PyObject *obj);
     248  extern int _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values,
     249                                            PyObject *name, PyObject *value);
     250  PyObject * _PyObject_GetInstanceAttribute(PyObject *obj, PyDictValues *values,
     251                                          PyObject *name);
     252  
     253  static inline PyDictValues **_PyObject_ValuesPointer(PyObject *obj)
     254  {
     255      assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
     256      return ((PyDictValues **)obj)-4;
     257  }
     258  
     259  static inline PyObject **_PyObject_ManagedDictPointer(PyObject *obj)
     260  {
     261      assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
     262      return ((PyObject **)obj)-3;
     263  }
     264  
     265  #define MANAGED_DICT_OFFSET (((int)sizeof(PyObject *))*-3)
     266  
     267  extern PyObject ** _PyObject_DictPointer(PyObject *);
     268  extern int _PyObject_VisitInstanceAttributes(PyObject *self, visitproc visit, void *arg);
     269  extern void _PyObject_ClearInstanceAttributes(PyObject *self);
     270  extern void _PyObject_FreeInstanceAttributes(PyObject *self);
     271  extern int _PyObject_IsInstanceDictEmpty(PyObject *);
     272  extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
     273  
     274  // Access macro to the members which are floating "behind" the object
     275  #define _PyHeapType_GET_MEMBERS(etype) \
     276      ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
     277  
     278  PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, PyObject *);
     279  
     280  /* C function call trampolines to mitigate bad function pointer casts.
     281   *
     282   * Typical native ABIs ignore additional arguments or fill in missing
     283   * values with 0/NULL in function pointer cast. Compilers do not show
     284   * warnings when a function pointer is explicitly casted to an
     285   * incompatible type.
     286   *
     287   * Bad fpcasts are an issue in WebAssembly. WASM's indirect_call has strict
     288   * function signature checks. Argument count, types, and return type must
     289   * match.
     290   *
     291   * Third party code unintentionally rely on problematic fpcasts. The call
     292   * trampoline mitigates common occurences of bad fpcasts on Emscripten.
     293   */
     294  #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
     295  #define _PyCFunction_TrampolineCall(meth, self, args) \
     296      _PyCFunctionWithKeywords_TrampolineCall( \
     297          (*(PyCFunctionWithKeywords)(void(*)(void))meth), self, args, NULL)
     298  extern PyObject* _PyCFunctionWithKeywords_TrampolineCall(
     299      PyCFunctionWithKeywords meth, PyObject *, PyObject *, PyObject *);
     300  #else
     301  #define _PyCFunction_TrampolineCall(meth, self, args) \
     302      (meth)((self), (args))
     303  #define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \
     304      (meth)((self), (args), (kw))
     305  #endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE
     306  
     307  #ifdef __cplusplus
     308  }
     309  #endif
     310  #endif /* !Py_INTERNAL_OBJECT_H */