pystate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef Py_CPYTHON_PYSTATE_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
  5. PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
  6. PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
  7. /* State unique per thread */
  8. /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
  9. typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
  10. /* The following values are used for 'what' for tracefunc functions
  11. *
  12. * To add a new kind of trace event, also update "trace_init" in
  13. * Python/sysmodule.c to define the Python level event name
  14. */
  15. #define PyTrace_CALL 0
  16. #define PyTrace_EXCEPTION 1
  17. #define PyTrace_LINE 2
  18. #define PyTrace_RETURN 3
  19. #define PyTrace_C_CALL 4
  20. #define PyTrace_C_EXCEPTION 5
  21. #define PyTrace_C_RETURN 6
  22. #define PyTrace_OPCODE 7
  23. typedef struct _cframe {
  24. /* This struct will be threaded through the C stack
  25. * allowing fast access to per-thread state that needs
  26. * to be accessed quickly by the interpreter, but can
  27. * be modified outside of the interpreter.
  28. *
  29. * WARNING: This makes data on the C stack accessible from
  30. * heap objects. Care must be taken to maintain stack
  31. * discipline and make sure that instances of this struct cannot
  32. * accessed outside of their lifetime.
  33. */
  34. int use_tracing;
  35. struct _cframe *previous;
  36. } CFrame;
  37. typedef struct _err_stackitem {
  38. /* This struct represents an entry on the exception stack, which is a
  39. * per-coroutine state. (Coroutine in the computer science sense,
  40. * including the thread and generators).
  41. * This ensures that the exception state is not impacted by "yields"
  42. * from an except handler.
  43. */
  44. PyObject *exc_type, *exc_value, *exc_traceback;
  45. struct _err_stackitem *previous_item;
  46. } _PyErr_StackItem;
  47. // The PyThreadState typedef is in Include/pystate.h.
  48. struct _ts {
  49. /* See Python/ceval.c for comments explaining most fields */
  50. struct _ts *prev;
  51. struct _ts *next;
  52. PyInterpreterState *interp;
  53. /* Borrowed reference to the current frame (it can be NULL) */
  54. PyFrameObject *frame;
  55. int recursion_depth;
  56. int recursion_headroom; /* Allow 50 more calls to handle any errors. */
  57. int stackcheck_counter;
  58. /* 'tracing' keeps track of the execution depth when tracing/profiling.
  59. This is to prevent the actual trace/profile code from being recorded in
  60. the trace/profile. */
  61. int tracing;
  62. /* Pointer to current CFrame in the C stack frame of the currently,
  63. * or most recently, executing _PyEval_EvalFrameDefault. */
  64. CFrame *cframe;
  65. Py_tracefunc c_profilefunc;
  66. Py_tracefunc c_tracefunc;
  67. PyObject *c_profileobj;
  68. PyObject *c_traceobj;
  69. /* The exception currently being raised */
  70. PyObject *curexc_type;
  71. PyObject *curexc_value;
  72. PyObject *curexc_traceback;
  73. /* The exception currently being handled, if no coroutines/generators
  74. * are present. Always last element on the stack referred to be exc_info.
  75. */
  76. _PyErr_StackItem exc_state;
  77. /* Pointer to the top of the stack of the exceptions currently
  78. * being handled */
  79. _PyErr_StackItem *exc_info;
  80. PyObject *dict; /* Stores per-thread state */
  81. int gilstate_counter;
  82. PyObject *async_exc; /* Asynchronous exception to raise */
  83. unsigned long thread_id; /* Thread id where this tstate was created */
  84. int trash_delete_nesting;
  85. PyObject *trash_delete_later;
  86. /* Called when a thread state is deleted normally, but not when it
  87. * is destroyed after fork().
  88. * Pain: to prevent rare but fatal shutdown errors (issue 18808),
  89. * Thread.join() must wait for the join'ed thread's tstate to be unlinked
  90. * from the tstate chain. That happens at the end of a thread's life,
  91. * in pystate.c.
  92. * The obvious way doesn't quite work: create a lock which the tstate
  93. * unlinking code releases, and have Thread.join() wait to acquire that
  94. * lock. The problem is that we _are_ at the end of the thread's life:
  95. * if the thread holds the last reference to the lock, decref'ing the
  96. * lock will delete the lock, and that may trigger arbitrary Python code
  97. * if there's a weakref, with a callback, to the lock. But by this time
  98. * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
  99. * of C code can be allowed to run (in particular it must not be possible to
  100. * release the GIL).
  101. * So instead of holding the lock directly, the tstate holds a weakref to
  102. * the lock: that's the value of on_delete_data below. Decref'ing a
  103. * weakref is harmless.
  104. * on_delete points to _threadmodule.c's static release_sentinel() function.
  105. * After the tstate is unlinked, release_sentinel is called with the
  106. * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
  107. * the indirectly held lock.
  108. */
  109. void (*on_delete)(void *);
  110. void *on_delete_data;
  111. int coroutine_origin_tracking_depth;
  112. PyObject *async_gen_firstiter;
  113. PyObject *async_gen_finalizer;
  114. PyObject *context;
  115. uint64_t context_ver;
  116. /* Unique thread state id. */
  117. uint64_t id;
  118. CFrame root_cframe;
  119. /* XXX signal handlers should also be here */
  120. };
  121. // Alias for backward compatibility with Python 3.8
  122. #define _PyInterpreterState_Get PyInterpreterState_Get
  123. PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
  124. /* Similar to PyThreadState_Get(), but don't issue a fatal error
  125. * if it is NULL. */
  126. PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
  127. PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
  128. /* PyGILState */
  129. /* Helper/diagnostic function - return 1 if the current thread
  130. currently holds the GIL, 0 otherwise.
  131. The function returns 1 if _PyGILState_check_enabled is non-zero. */
  132. PyAPI_FUNC(int) PyGILState_Check(void);
  133. /* Get the single PyInterpreterState used by this process' GILState
  134. implementation.
  135. This function doesn't check for error. Return NULL before _PyGILState_Init()
  136. is called and after _PyGILState_Fini() is called.
  137. See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
  138. PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
  139. /* The implementation of sys._current_frames() Returns a dict mapping
  140. thread id to that thread's current frame.
  141. */
  142. PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
  143. /* The implementation of sys._current_exceptions() Returns a dict mapping
  144. thread id to that thread's current exception.
  145. */
  146. PyAPI_FUNC(PyObject *) _PyThread_CurrentExceptions(void);
  147. /* Routines for advanced debuggers, requested by David Beazley.
  148. Don't use unless you know what you are doing! */
  149. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
  150. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
  151. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
  152. PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
  153. PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
  154. PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
  155. /* Frame evaluation API */
  156. typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, PyFrameObject *, int);
  157. PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
  158. PyInterpreterState *interp);
  159. PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
  160. PyInterpreterState *interp,
  161. _PyFrameEvalFunction eval_frame);
  162. PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
  163. /* Get a copy of the current interpreter configuration.
  164. Return 0 on success. Raise an exception and return -1 on error.
  165. The caller must initialize 'config', using PyConfig_InitPythonConfig()
  166. for example.
  167. Python must be preinitialized to call this method.
  168. The caller must hold the GIL. */
  169. PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
  170. struct PyConfig *config);
  171. /* Set the configuration of the current interpreter.
  172. This function should be called during or just after the Python
  173. initialization.
  174. Update the sys module with the new configuration. If the sys module was
  175. modified directly after the Python initialization, these changes are lost.
  176. Some configuration like faulthandler or warnoptions can be updated in the
  177. configuration, but don't reconfigure Python (don't enable/disable
  178. faulthandler and don't reconfigure warnings filters).
  179. Return 0 on success. Raise an exception and return -1 on error.
  180. The configuration should come from _PyInterpreterState_GetConfigCopy(). */
  181. PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
  182. const struct PyConfig *config);
  183. // Get the configuration of the current interpreter.
  184. // The caller must hold the GIL.
  185. PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
  186. /* cross-interpreter data */
  187. struct _xid;
  188. // _PyCrossInterpreterData is similar to Py_buffer as an effectively
  189. // opaque struct that holds data outside the object machinery. This
  190. // is necessary to pass safely between interpreters in the same process.
  191. typedef struct _xid {
  192. // data is the cross-interpreter-safe derivation of a Python object
  193. // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
  194. // new_object func (below) encodes the data.
  195. void *data;
  196. // obj is the Python object from which the data was derived. This
  197. // is non-NULL only if the data remains bound to the object in some
  198. // way, such that the object must be "released" (via a decref) when
  199. // the data is released. In that case the code that sets the field,
  200. // likely a registered "crossinterpdatafunc", is responsible for
  201. // ensuring it owns the reference (i.e. incref).
  202. PyObject *obj;
  203. // interp is the ID of the owning interpreter of the original
  204. // object. It corresponds to the active interpreter when
  205. // _PyObject_GetCrossInterpreterData() was called. This should only
  206. // be set by the cross-interpreter machinery.
  207. //
  208. // We use the ID rather than the PyInterpreterState to avoid issues
  209. // with deleted interpreters. Note that IDs are never re-used, so
  210. // each one will always correspond to a specific interpreter
  211. // (whether still alive or not).
  212. int64_t interp;
  213. // new_object is a function that returns a new object in the current
  214. // interpreter given the data. The resulting object (a new
  215. // reference) will be equivalent to the original object. This field
  216. // is required.
  217. PyObject *(*new_object)(struct _xid *);
  218. // free is called when the data is released. If it is NULL then
  219. // nothing will be done to free the data. For some types this is
  220. // okay (e.g. bytes) and for those types this field should be set
  221. // to NULL. However, for most the data was allocated just for
  222. // cross-interpreter use, so it must be freed when
  223. // _PyCrossInterpreterData_Release is called or the memory will
  224. // leak. In that case, at the very least this field should be set
  225. // to PyMem_RawFree (the default if not explicitly set to NULL).
  226. // The call will happen with the original interpreter activated.
  227. void (*free)(void *);
  228. } _PyCrossInterpreterData;
  229. PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
  230. PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
  231. PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
  232. PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
  233. /* cross-interpreter data registry */
  234. typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
  235. PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
  236. PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);