(root)/
Python-3.12.0/
Include/
cpython/
pystate.h
       1  #ifndef Py_CPYTHON_PYSTATE_H
       2  #  error "this header file must not be included directly"
       3  #endif
       4  
       5  
       6  /*
       7  Runtime Feature Flags
       8  
       9  Each flag indicate whether or not a specific runtime feature
      10  is available in a given context.  For example, forking the process
      11  might not be allowed in the current interpreter (i.e. os.fork() would fail).
      12  */
      13  
      14  /* Set if the interpreter share obmalloc runtime state
      15     with the main interpreter. */
      16  #define Py_RTFLAGS_USE_MAIN_OBMALLOC (1UL << 5)
      17  
      18  /* Set if import should check a module for subinterpreter support. */
      19  #define Py_RTFLAGS_MULTI_INTERP_EXTENSIONS (1UL << 8)
      20  
      21  /* Set if threads are allowed. */
      22  #define Py_RTFLAGS_THREADS (1UL << 10)
      23  
      24  /* Set if daemon threads are allowed. */
      25  #define Py_RTFLAGS_DAEMON_THREADS (1UL << 11)
      26  
      27  /* Set if os.fork() is allowed. */
      28  #define Py_RTFLAGS_FORK (1UL << 15)
      29  
      30  /* Set if os.exec*() is allowed. */
      31  #define Py_RTFLAGS_EXEC (1UL << 16)
      32  
      33  
      34  PyAPI_FUNC(int) _PyInterpreterState_HasFeature(PyInterpreterState *interp,
      35                                                 unsigned long feature);
      36  
      37  
      38  /* private interpreter helpers */
      39  
      40  PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
      41  PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
      42  
      43  PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
      44  
      45  
      46  /* State unique per thread */
      47  
      48  /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
      49  typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
      50  
      51  /* The following values are used for 'what' for tracefunc functions
      52   *
      53   * To add a new kind of trace event, also update "trace_init" in
      54   * Python/sysmodule.c to define the Python level event name
      55   */
      56  #define PyTrace_CALL 0
      57  #define PyTrace_EXCEPTION 1
      58  #define PyTrace_LINE 2
      59  #define PyTrace_RETURN 3
      60  #define PyTrace_C_CALL 4
      61  #define PyTrace_C_EXCEPTION 5
      62  #define PyTrace_C_RETURN 6
      63  #define PyTrace_OPCODE 7
      64  
      65  // Internal structure: you should not use it directly, but use public functions
      66  // like PyThreadState_EnterTracing() and PyThreadState_LeaveTracing().
      67  typedef struct _PyCFrame {
      68      /* This struct will be threaded through the C stack
      69       * allowing fast access to per-thread state that needs
      70       * to be accessed quickly by the interpreter, but can
      71       * be modified outside of the interpreter.
      72       *
      73       * WARNING: This makes data on the C stack accessible from
      74       * heap objects. Care must be taken to maintain stack
      75       * discipline and make sure that instances of this struct cannot
      76       * accessed outside of their lifetime.
      77       */
      78      /* Pointer to the currently executing frame (it can be NULL) */
      79      struct _PyInterpreterFrame *current_frame;
      80      struct _PyCFrame *previous;
      81  } _PyCFrame;
      82  
      83  typedef struct _err_stackitem {
      84      /* This struct represents a single execution context where we might
      85       * be currently handling an exception.  It is a per-coroutine state
      86       * (coroutine in the computer science sense, including the thread
      87       * and generators).
      88       *
      89       * This is used as an entry on the exception stack, where each
      90       * entry indicates if it is currently handling an exception.
      91       * This ensures that the exception state is not impacted
      92       * by "yields" from an except handler.  The thread
      93       * always has an entry (the bottom-most one).
      94       */
      95  
      96      /* The exception currently being handled in this context, if any. */
      97      PyObject *exc_value;
      98  
      99      struct _err_stackitem *previous_item;
     100  
     101  } _PyErr_StackItem;
     102  
     103  typedef struct _stack_chunk {
     104      struct _stack_chunk *previous;
     105      size_t size;
     106      size_t top;
     107      PyObject * data[1]; /* Variable sized */
     108  } _PyStackChunk;
     109  
     110  struct _py_trashcan {
     111      int delete_nesting;
     112      PyObject *delete_later;
     113  };
     114  
     115  struct _ts {
     116      /* See Python/ceval.c for comments explaining most fields */
     117  
     118      PyThreadState *prev;
     119      PyThreadState *next;
     120      PyInterpreterState *interp;
     121  
     122      struct {
     123          /* Has been initialized to a safe state.
     124  
     125             In order to be effective, this must be set to 0 during or right
     126             after allocation. */
     127          unsigned int initialized:1;
     128  
     129          /* Has been bound to an OS thread. */
     130          unsigned int bound:1;
     131          /* Has been unbound from its OS thread. */
     132          unsigned int unbound:1;
     133          /* Has been bound aa current for the GILState API. */
     134          unsigned int bound_gilstate:1;
     135          /* Currently in use (maybe holds the GIL). */
     136          unsigned int active:1;
     137  
     138          /* various stages of finalization */
     139          unsigned int finalizing:1;
     140          unsigned int cleared:1;
     141          unsigned int finalized:1;
     142  
     143          /* padding to align to 4 bytes */
     144          unsigned int :24;
     145      } _status;
     146  
     147      int py_recursion_remaining;
     148      int py_recursion_limit;
     149  
     150      int c_recursion_remaining;
     151      int recursion_headroom; /* Allow 50 more calls to handle any errors. */
     152  
     153      /* 'tracing' keeps track of the execution depth when tracing/profiling.
     154         This is to prevent the actual trace/profile code from being recorded in
     155         the trace/profile. */
     156      int tracing;
     157      int what_event; /* The event currently being monitored, if any. */
     158  
     159      /* Pointer to current _PyCFrame in the C stack frame of the currently,
     160       * or most recently, executing _PyEval_EvalFrameDefault. */
     161      _PyCFrame *cframe;
     162  
     163      Py_tracefunc c_profilefunc;
     164      Py_tracefunc c_tracefunc;
     165      PyObject *c_profileobj;
     166      PyObject *c_traceobj;
     167  
     168      /* The exception currently being raised */
     169      PyObject *current_exception;
     170  
     171      /* Pointer to the top of the exception stack for the exceptions
     172       * we may be currently handling.  (See _PyErr_StackItem above.)
     173       * This is never NULL. */
     174      _PyErr_StackItem *exc_info;
     175  
     176      PyObject *dict;  /* Stores per-thread state */
     177  
     178      int gilstate_counter;
     179  
     180      PyObject *async_exc; /* Asynchronous exception to raise */
     181      unsigned long thread_id; /* Thread id where this tstate was created */
     182  
     183      /* Native thread id where this tstate was created. This will be 0 except on
     184       * those platforms that have the notion of native thread id, for which the
     185       * macro PY_HAVE_THREAD_NATIVE_ID is then defined.
     186       */
     187      unsigned long native_thread_id;
     188  
     189      struct _py_trashcan trash;
     190  
     191      /* Called when a thread state is deleted normally, but not when it
     192       * is destroyed after fork().
     193       * Pain:  to prevent rare but fatal shutdown errors (issue 18808),
     194       * Thread.join() must wait for the join'ed thread's tstate to be unlinked
     195       * from the tstate chain.  That happens at the end of a thread's life,
     196       * in pystate.c.
     197       * The obvious way doesn't quite work:  create a lock which the tstate
     198       * unlinking code releases, and have Thread.join() wait to acquire that
     199       * lock.  The problem is that we _are_ at the end of the thread's life:
     200       * if the thread holds the last reference to the lock, decref'ing the
     201       * lock will delete the lock, and that may trigger arbitrary Python code
     202       * if there's a weakref, with a callback, to the lock.  But by this time
     203       * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
     204       * of C code can be allowed to run (in particular it must not be possible to
     205       * release the GIL).
     206       * So instead of holding the lock directly, the tstate holds a weakref to
     207       * the lock:  that's the value of on_delete_data below.  Decref'ing a
     208       * weakref is harmless.
     209       * on_delete points to _threadmodule.c's static release_sentinel() function.
     210       * After the tstate is unlinked, release_sentinel is called with the
     211       * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
     212       * the indirectly held lock.
     213       */
     214      void (*on_delete)(void *);
     215      void *on_delete_data;
     216  
     217      int coroutine_origin_tracking_depth;
     218  
     219      PyObject *async_gen_firstiter;
     220      PyObject *async_gen_finalizer;
     221  
     222      PyObject *context;
     223      uint64_t context_ver;
     224  
     225      /* Unique thread state id. */
     226      uint64_t id;
     227  
     228      _PyStackChunk *datastack_chunk;
     229      PyObject **datastack_top;
     230      PyObject **datastack_limit;
     231      /* XXX signal handlers should also be here */
     232  
     233      /* The following fields are here to avoid allocation during init.
     234         The data is exposed through PyThreadState pointer fields.
     235         These fields should not be accessed directly outside of init.
     236         This is indicated by an underscore prefix on the field names.
     237  
     238         All other PyInterpreterState pointer fields are populated when
     239         needed and default to NULL.
     240         */
     241         // Note some fields do not have a leading underscore for backward
     242         // compatibility.  See https://bugs.python.org/issue45953#msg412046.
     243  
     244      /* The thread's exception stack entry.  (Always the last entry.) */
     245      _PyErr_StackItem exc_state;
     246  
     247      /* The bottom-most frame on the stack. */
     248      _PyCFrame root_cframe;
     249  };
     250  
     251  /* WASI has limited call stack. Python's recursion limit depends on code
     252     layout, optimization, and WASI runtime. Wasmtime can handle about 700
     253     recursions, sometimes less. 500 is a more conservative limit. */
     254  #ifndef C_RECURSION_LIMIT
     255  #  ifdef __wasi__
     256  #    define C_RECURSION_LIMIT 500
     257  #  else
     258      // This value is duplicated in Lib/test/support/__init__.py
     259  #    define C_RECURSION_LIMIT 1500
     260  #  endif
     261  #endif
     262  
     263  /* other API */
     264  
     265  // Alias for backward compatibility with Python 3.8
     266  #define _PyInterpreterState_Get PyInterpreterState_Get
     267  
     268  /* An alias for the internal _PyThreadState_New(),
     269     kept for stable ABI compatibility. */
     270  PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
     271  
     272  /* Similar to PyThreadState_Get(), but don't issue a fatal error
     273   * if it is NULL. */
     274  PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
     275  
     276  PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
     277  
     278  // Disable tracing and profiling.
     279  PyAPI_FUNC(void) PyThreadState_EnterTracing(PyThreadState *tstate);
     280  
     281  // Reset tracing and profiling: enable them if a trace function or a profile
     282  // function is set, otherwise disable them.
     283  PyAPI_FUNC(void) PyThreadState_LeaveTracing(PyThreadState *tstate);
     284  
     285  /* PyGILState */
     286  
     287  /* Helper/diagnostic function - return 1 if the current thread
     288     currently holds the GIL, 0 otherwise.
     289  
     290     The function returns 1 if _PyGILState_check_enabled is non-zero. */
     291  PyAPI_FUNC(int) PyGILState_Check(void);
     292  
     293  /* Get the single PyInterpreterState used by this process' GILState
     294     implementation.
     295  
     296     This function doesn't check for error. Return NULL before _PyGILState_Init()
     297     is called and after _PyGILState_Fini() is called.
     298  
     299     See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
     300  PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
     301  
     302  /* The implementation of sys._current_frames()  Returns a dict mapping
     303     thread id to that thread's current frame.
     304  */
     305  PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
     306  
     307  /* The implementation of sys._current_exceptions()  Returns a dict mapping
     308     thread id to that thread's current exception.
     309  */
     310  PyAPI_FUNC(PyObject *) _PyThread_CurrentExceptions(void);
     311  
     312  /* Routines for advanced debuggers, requested by David Beazley.
     313     Don't use unless you know what you are doing! */
     314  PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
     315  PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
     316  PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
     317  PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
     318  PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
     319  PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
     320  
     321  /* Frame evaluation API */
     322  
     323  typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, struct _PyInterpreterFrame *, int);
     324  
     325  PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
     326      PyInterpreterState *interp);
     327  PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
     328      PyInterpreterState *interp,
     329      _PyFrameEvalFunction eval_frame);
     330  
     331  PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
     332  
     333  /* Get a copy of the current interpreter configuration.
     334  
     335     Return 0 on success. Raise an exception and return -1 on error.
     336  
     337     The caller must initialize 'config', using PyConfig_InitPythonConfig()
     338     for example.
     339  
     340     Python must be preinitialized to call this method.
     341     The caller must hold the GIL.
     342  
     343     Once done with the configuration, PyConfig_Clear() must be called to clear
     344     it. */
     345  PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
     346      struct PyConfig *config);
     347  
     348  /* Set the configuration of the current interpreter.
     349  
     350     This function should be called during or just after the Python
     351     initialization.
     352  
     353     Update the sys module with the new configuration. If the sys module was
     354     modified directly after the Python initialization, these changes are lost.
     355  
     356     Some configuration like faulthandler or warnoptions can be updated in the
     357     configuration, but don't reconfigure Python (don't enable/disable
     358     faulthandler and don't reconfigure warnings filters).
     359  
     360     Return 0 on success. Raise an exception and return -1 on error.
     361  
     362     The configuration should come from _PyInterpreterState_GetConfigCopy(). */
     363  PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
     364      const struct PyConfig *config);
     365  
     366  // Get the configuration of the current interpreter.
     367  // The caller must hold the GIL.
     368  PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
     369  
     370  
     371  /* cross-interpreter data */
     372  
     373  // _PyCrossInterpreterData is similar to Py_buffer as an effectively
     374  // opaque struct that holds data outside the object machinery.  This
     375  // is necessary to pass safely between interpreters in the same process.
     376  typedef struct _xid _PyCrossInterpreterData;
     377  
     378  typedef PyObject *(*xid_newobjectfunc)(_PyCrossInterpreterData *);
     379  typedef void (*xid_freefunc)(void *);
     380  
     381  struct _xid {
     382      // data is the cross-interpreter-safe derivation of a Python object
     383      // (see _PyObject_GetCrossInterpreterData).  It will be NULL if the
     384      // new_object func (below) encodes the data.
     385      void *data;
     386      // obj is the Python object from which the data was derived.  This
     387      // is non-NULL only if the data remains bound to the object in some
     388      // way, such that the object must be "released" (via a decref) when
     389      // the data is released.  In that case the code that sets the field,
     390      // likely a registered "crossinterpdatafunc", is responsible for
     391      // ensuring it owns the reference (i.e. incref).
     392      PyObject *obj;
     393      // interp is the ID of the owning interpreter of the original
     394      // object.  It corresponds to the active interpreter when
     395      // _PyObject_GetCrossInterpreterData() was called.  This should only
     396      // be set by the cross-interpreter machinery.
     397      //
     398      // We use the ID rather than the PyInterpreterState to avoid issues
     399      // with deleted interpreters.  Note that IDs are never re-used, so
     400      // each one will always correspond to a specific interpreter
     401      // (whether still alive or not).
     402      int64_t interp;
     403      // new_object is a function that returns a new object in the current
     404      // interpreter given the data.  The resulting object (a new
     405      // reference) will be equivalent to the original object.  This field
     406      // is required.
     407      xid_newobjectfunc new_object;
     408      // free is called when the data is released.  If it is NULL then
     409      // nothing will be done to free the data.  For some types this is
     410      // okay (e.g. bytes) and for those types this field should be set
     411      // to NULL.  However, for most the data was allocated just for
     412      // cross-interpreter use, so it must be freed when
     413      // _PyCrossInterpreterData_Release is called or the memory will
     414      // leak.  In that case, at the very least this field should be set
     415      // to PyMem_RawFree (the default if not explicitly set to NULL).
     416      // The call will happen with the original interpreter activated.
     417      xid_freefunc free;
     418  };
     419  
     420  PyAPI_FUNC(void) _PyCrossInterpreterData_Init(
     421          _PyCrossInterpreterData *data,
     422          PyInterpreterState *interp, void *shared, PyObject *obj,
     423          xid_newobjectfunc new_object);
     424  PyAPI_FUNC(int) _PyCrossInterpreterData_InitWithSize(
     425          _PyCrossInterpreterData *,
     426          PyInterpreterState *interp, const size_t, PyObject *,
     427          xid_newobjectfunc);
     428  PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
     429          PyInterpreterState *, _PyCrossInterpreterData *);
     430  
     431  PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
     432  PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
     433  PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
     434  
     435  PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
     436  
     437  /* cross-interpreter data registry */
     438  
     439  typedef int (*crossinterpdatafunc)(PyThreadState *tstate, PyObject *,
     440                                     _PyCrossInterpreterData *);
     441  
     442  PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
     443  PyAPI_FUNC(int) _PyCrossInterpreterData_UnregisterClass(PyTypeObject *);
     444  PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);