2017-09-08 02:51:28 -03:00
|
|
|
#ifndef Py_INTERNAL_CEVAL_H
|
|
|
|
#define Py_INTERNAL_CEVAL_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-04-17 18:02:26 -03:00
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
2018-11-09 08:03:37 -04:00
|
|
|
#endif
|
|
|
|
|
2023-10-17 12:33:50 -03:00
|
|
|
#include "dynamic_annotations.h" // _Py_ANNOTATE_RWLOCK_CREATE
|
|
|
|
|
2023-08-21 15:05:59 -03:00
|
|
|
#include "pycore_interp.h" // PyInterpreterState.eval_frame
|
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
|
|
|
|
2019-10-02 18:51:20 -03:00
|
|
|
/* Forward declarations */
|
2019-10-03 21:21:05 -03:00
|
|
|
struct pyruntimestate;
|
2019-10-02 18:51:20 -03:00
|
|
|
struct _ceval_runtime_state;
|
2019-11-14 07:20:46 -04:00
|
|
|
|
2023-08-24 15:25:22 -03:00
|
|
|
// Export for '_lsprof' shared extension
|
|
|
|
PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
|
|
|
|
|
|
|
|
extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
|
|
|
|
|
2023-11-03 13:39:50 -03:00
|
|
|
extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable);
|
|
|
|
|
2023-08-24 15:25:22 -03:00
|
|
|
// Helper to look up a builtin object
|
|
|
|
// Export for 'array' shared extension
|
|
|
|
PyAPI_FUNC(PyObject*) _PyEval_GetBuiltin(PyObject *);
|
|
|
|
|
|
|
|
extern PyObject* _PyEval_GetBuiltinId(_Py_Identifier *);
|
|
|
|
|
|
|
|
extern void _PyEval_SetSwitchInterval(unsigned long microseconds);
|
|
|
|
extern unsigned long _PyEval_GetSwitchInterval(void);
|
|
|
|
|
|
|
|
// Export for '_queue' shared extension
|
|
|
|
PyAPI_FUNC(int) _PyEval_MakePendingCalls(PyThreadState *);
|
|
|
|
|
2022-01-13 20:09:24 -04:00
|
|
|
#ifndef Py_DEFAULT_RECURSION_LIMIT
|
2022-10-04 21:34:03 -03:00
|
|
|
# define Py_DEFAULT_RECURSION_LIMIT 1000
|
2022-01-13 20:09:24 -04:00
|
|
|
#endif
|
|
|
|
|
2020-03-17 14:56:44 -03:00
|
|
|
extern void _Py_FinishPendingCalls(PyThreadState *tstate);
|
2023-12-07 15:33:40 -04:00
|
|
|
extern void _PyEval_InitState(PyInterpreterState *);
|
2024-02-20 10:57:48 -04:00
|
|
|
extern void _PyEval_SignalReceived(void);
|
2023-08-24 13:06:53 -03:00
|
|
|
|
2023-10-09 10:39:51 -03:00
|
|
|
// bitwise flags:
|
|
|
|
#define _Py_PENDING_MAINTHREADONLY 1
|
|
|
|
#define _Py_PENDING_RAWFREE 2
|
|
|
|
|
2024-04-25 22:05:51 -03:00
|
|
|
typedef int _Py_add_pending_call_result;
|
|
|
|
#define _Py_ADD_PENDING_SUCCESS 0
|
|
|
|
#define _Py_ADD_PENDING_FULL -1
|
|
|
|
|
2023-07-25 00:16:28 -03:00
|
|
|
// Export for '_testinternalcapi' shared extension
|
2024-04-25 22:05:51 -03:00
|
|
|
PyAPI_FUNC(_Py_add_pending_call_result) _PyEval_AddPendingCall(
|
2020-04-08 18:35:05 -03:00
|
|
|
PyInterpreterState *interp,
|
2023-09-19 18:01:34 -03:00
|
|
|
_Py_pending_call_func func,
|
2023-06-13 18:02:19 -03:00
|
|
|
void *arg,
|
2023-10-09 10:39:51 -03:00
|
|
|
int flags);
|
|
|
|
|
2020-04-14 13:16:24 -03:00
|
|
|
#ifdef HAVE_FORK
|
2020-06-02 13:44:54 -03:00
|
|
|
extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
|
2020-04-14 13:16:24 -03:00
|
|
|
#endif
|
2017-09-08 02:51:28 -03:00
|
|
|
|
2022-03-20 23:03:22 -03:00
|
|
|
// Used by sys.call_tracing()
|
|
|
|
extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
|
|
|
|
|
2022-03-20 21:15:32 -03:00
|
|
|
// Used by sys.get_asyncgen_hooks()
|
|
|
|
extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
|
|
|
|
extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
|
|
|
|
|
|
|
|
// Used by sys.set_asyncgen_hooks()
|
|
|
|
extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
|
|
|
|
extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
|
|
|
|
|
2022-03-20 22:24:00 -03:00
|
|
|
// Used by sys.get_coroutine_origin_tracking_depth()
|
|
|
|
// and sys.set_coroutine_origin_tracking_depth()
|
|
|
|
extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
|
|
|
|
extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
|
|
|
|
|
|
|
|
extern void _PyEval_Fini(void);
|
2019-06-03 09:30:58 -03:00
|
|
|
|
2021-02-18 14:20:16 -04:00
|
|
|
|
2021-02-20 10:17:18 -04:00
|
|
|
extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
|
2022-03-20 22:24:00 -03:00
|
|
|
extern PyObject* _PyEval_BuiltinsFromGlobals(
|
2021-02-20 10:17:18 -04:00
|
|
|
PyThreadState *tstate,
|
|
|
|
PyObject *globals);
|
2021-02-18 14:20:16 -04:00
|
|
|
|
2022-08-30 14:11:18 -03:00
|
|
|
// Trampoline API
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// Callback to initialize the trampoline state
|
|
|
|
void* (*init_state)(void);
|
|
|
|
// Callback to register every trampoline being created
|
|
|
|
void (*write_state)(void* state, const void *code_addr,
|
|
|
|
unsigned int code_size, PyCodeObject* code);
|
|
|
|
// Callback to free the trampoline state
|
|
|
|
int (*free_state)(void* state);
|
|
|
|
} _PyPerf_Callbacks;
|
|
|
|
|
|
|
|
extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
|
|
|
|
extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
|
|
|
|
extern int _PyPerfTrampoline_Init(int activate);
|
|
|
|
extern int _PyPerfTrampoline_Fini(void);
|
2023-12-01 09:20:51 -04:00
|
|
|
extern void _PyPerfTrampoline_FreeArenas(void);
|
2022-08-30 14:11:18 -03:00
|
|
|
extern int _PyIsPerfTrampolineActive(void);
|
|
|
|
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
|
|
|
|
#ifdef PY_HAVE_PERF_TRAMPOLINE
|
|
|
|
extern _PyPerf_Callbacks _Py_perfmap_callbacks;
|
2024-05-04 22:07:29 -03:00
|
|
|
extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
|
2022-08-30 14:11:18 -03:00
|
|
|
#endif
|
2021-02-18 14:20:16 -04:00
|
|
|
|
2019-11-14 07:20:46 -04:00
|
|
|
static inline PyObject*
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
|
2019-11-14 07:20:46 -04:00
|
|
|
{
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
|
2021-10-11 07:34:02 -03:00
|
|
|
if (tstate->interp->eval_frame == NULL) {
|
|
|
|
return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
|
|
|
|
}
|
2021-07-26 07:22:16 -03:00
|
|
|
return tstate->interp->eval_frame(tstate, frame, throwflag);
|
2019-11-14 07:20:46 -04:00
|
|
|
}
|
|
|
|
|
2022-03-20 22:24:00 -03:00
|
|
|
extern PyObject*
|
2021-02-01 06:42:03 -04:00
|
|
|
_PyEval_Vector(PyThreadState *tstate,
|
2021-11-23 05:53:24 -04:00
|
|
|
PyFunctionObject *func, PyObject *locals,
|
2021-02-01 06:42:03 -04:00
|
|
|
PyObject* const* args, size_t argcount,
|
|
|
|
PyObject *kwnames);
|
2019-11-15 20:03:22 -04:00
|
|
|
|
2023-05-05 16:23:00 -03:00
|
|
|
extern int _PyEval_ThreadsInitialized(void);
|
2023-12-12 20:20:21 -04:00
|
|
|
extern void _PyEval_InitGIL(PyThreadState *tstate, int own_gil);
|
2021-02-19 10:10:45 -04:00
|
|
|
extern void _PyEval_FiniGIL(PyInterpreterState *interp);
|
2020-03-09 17:24:14 -03:00
|
|
|
|
2024-05-23 18:27:38 -03:00
|
|
|
extern void _PyEval_AcquireLock(PyThreadState *tstate);
|
2024-05-07 00:07:23 -03:00
|
|
|
|
2024-05-23 18:27:38 -03:00
|
|
|
extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *,
|
|
|
|
int final_release);
|
2020-03-17 22:26:04 -03:00
|
|
|
|
2024-05-07 00:07:23 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
// Returns 0 or 1 if the GIL for the given thread's interpreter is disabled or
|
|
|
|
// enabled, respectively.
|
|
|
|
//
|
|
|
|
// The enabled state of the GIL will not change while one or more threads are
|
|
|
|
// attached.
|
|
|
|
static inline int
|
|
|
|
_PyEval_IsGILEnabled(PyThreadState *tstate)
|
|
|
|
{
|
2024-06-02 11:42:46 -03:00
|
|
|
struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
|
|
|
|
return _Py_atomic_load_int_relaxed(&gil->enabled) != 0;
|
2024-05-07 00:07:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable or disable the GIL used by the interpreter that owns tstate, which
|
|
|
|
// must be the current thread. This may affect other interpreters, if the GIL
|
|
|
|
// is shared. All three functions will be no-ops (and return 0) if the
|
|
|
|
// interpreter's `enable_gil' config is not _PyConfig_GIL_DEFAULT.
|
|
|
|
//
|
|
|
|
// Every call to _PyEval_EnableGILTransient() must be paired with exactly one
|
|
|
|
// call to either _PyEval_EnableGILPermanent() or
|
|
|
|
// _PyEval_DisableGIL(). _PyEval_EnableGILPermanent() and _PyEval_DisableGIL()
|
|
|
|
// must only be called while the GIL is enabled from a call to
|
|
|
|
// _PyEval_EnableGILTransient().
|
|
|
|
//
|
|
|
|
// _PyEval_EnableGILTransient() returns 1 if it enabled the GIL, or 0 if the
|
|
|
|
// GIL was already enabled, whether transiently or permanently. The caller will
|
|
|
|
// hold the GIL upon return.
|
|
|
|
//
|
|
|
|
// _PyEval_EnableGILPermanent() returns 1 if it permanently enabled the GIL
|
|
|
|
// (which must already be enabled), or 0 if it was already permanently
|
|
|
|
// enabled. Once _PyEval_EnableGILPermanent() has been called once, all
|
|
|
|
// subsequent calls to any of the three functions will be no-ops.
|
|
|
|
//
|
|
|
|
// _PyEval_DisableGIL() returns 1 if it disabled the GIL, or 0 if the GIL was
|
|
|
|
// kept enabled because of another request, whether transient or permanent.
|
|
|
|
//
|
|
|
|
// All three functions must be called by an attached thread (this implies that
|
|
|
|
// if the GIL is enabled, the current thread must hold it).
|
|
|
|
extern int _PyEval_EnableGILTransient(PyThreadState *tstate);
|
|
|
|
extern int _PyEval_EnableGILPermanent(PyThreadState *tstate);
|
|
|
|
extern int _PyEval_DisableGIL(PyThreadState *state);
|
|
|
|
#endif
|
|
|
|
|
2021-03-08 17:56:37 -04:00
|
|
|
extern void _PyEval_DeactivateOpCache(void);
|
|
|
|
|
2020-03-13 06:19:38 -03:00
|
|
|
|
|
|
|
/* --- _Py_EnterRecursiveCall() ----------------------------------------- */
|
|
|
|
|
|
|
|
#ifdef USE_STACKCHECK
|
|
|
|
/* With USE_STACKCHECK macro defined, trigger stack checks in
|
2022-05-04 08:30:23 -03:00
|
|
|
_Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
|
2020-03-13 06:19:38 -03:00
|
|
|
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
|
2024-03-15 07:48:00 -03:00
|
|
|
return (tstate->c_recursion_remaining-- < 0
|
2022-10-04 21:34:03 -03:00
|
|
|
|| (tstate->c_recursion_remaining & 63) == 0);
|
2020-03-13 06:19:38 -03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
|
2024-03-15 07:48:00 -03:00
|
|
|
return tstate->c_recursion_remaining-- < 0;
|
2020-03-13 06:19:38 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-24 13:06:53 -03:00
|
|
|
// Export for '_json' shared extension, used via _Py_EnterRecursiveCall()
|
|
|
|
// static inline function.
|
2020-03-13 06:19:38 -03:00
|
|
|
PyAPI_FUNC(int) _Py_CheckRecursiveCall(
|
|
|
|
PyThreadState *tstate,
|
|
|
|
const char *where);
|
|
|
|
|
2022-10-04 21:34:03 -03:00
|
|
|
int _Py_CheckRecursiveCallPy(
|
|
|
|
PyThreadState *tstate);
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
|
|
|
|
const char *where) {
|
2020-03-13 06:19:38 -03:00
|
|
|
return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
|
|
|
|
}
|
|
|
|
|
2024-03-15 07:48:00 -03:00
|
|
|
static inline void _Py_EnterRecursiveCallTstateUnchecked(PyThreadState *tstate) {
|
|
|
|
assert(tstate->c_recursion_remaining > 0);
|
|
|
|
tstate->c_recursion_remaining--;
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
static inline int _Py_EnterRecursiveCall(const char *where) {
|
2021-10-13 09:09:13 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2022-05-04 08:30:23 -03:00
|
|
|
return _Py_EnterRecursiveCallTstate(tstate, where);
|
2020-03-13 06:19:38 -03:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
|
2022-10-04 21:34:03 -03:00
|
|
|
tstate->c_recursion_remaining++;
|
2020-03-13 06:19:38 -03:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
static inline void _Py_LeaveRecursiveCall(void) {
|
2021-10-13 09:09:13 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2022-05-04 08:30:23 -03:00
|
|
|
_Py_LeaveRecursiveCallTstate(tstate);
|
2020-03-13 06:19:38 -03:00
|
|
|
}
|
|
|
|
|
2022-03-20 22:24:00 -03:00
|
|
|
extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
|
2021-07-26 07:22:16 -03:00
|
|
|
|
2024-04-25 07:32:47 -03:00
|
|
|
PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
|
2020-03-13 06:19:38 -03:00
|
|
|
|
2023-07-03 17:28:27 -03:00
|
|
|
/* Handle signals, pending calls, GIL drop request
|
|
|
|
and asynchronous exception */
|
2024-02-29 12:11:28 -04:00
|
|
|
PyAPI_FUNC(int) _Py_HandlePending(PyThreadState *tstate);
|
2022-08-24 10:21:01 -03:00
|
|
|
|
2023-07-05 20:05:02 -03:00
|
|
|
extern PyObject * _PyEval_GetFrameLocals(void);
|
2022-08-24 10:21:01 -03:00
|
|
|
|
2024-02-29 12:11:28 -04:00
|
|
|
typedef PyObject *(*conversion_func)(PyObject *);
|
|
|
|
|
|
|
|
PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[];
|
|
|
|
PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];
|
|
|
|
|
|
|
|
PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
|
|
|
|
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
|
|
|
|
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
|
|
|
|
PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg);
|
|
|
|
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj);
|
|
|
|
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
|
|
|
|
PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
|
|
|
|
PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
|
|
|
|
PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
|
|
|
|
PyAPI_FUNC(int) _PyEval_UnpackIterable(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, PyObject **sp);
|
2024-07-29 18:51:19 -03:00
|
|
|
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
|
2024-02-29 12:11:28 -04:00
|
|
|
PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
|
2023-07-20 17:37:19 -03:00
|
|
|
|
2022-10-04 21:34:03 -03:00
|
|
|
|
2024-02-20 10:57:48 -04:00
|
|
|
/* Bits that can be set in PyThreadState.eval_breaker */
|
|
|
|
#define _PY_GIL_DROP_REQUEST_BIT (1U << 0)
|
|
|
|
#define _PY_SIGNALS_PENDING_BIT (1U << 1)
|
|
|
|
#define _PY_CALLS_TO_DO_BIT (1U << 2)
|
|
|
|
#define _PY_ASYNC_EXCEPTION_BIT (1U << 3)
|
|
|
|
#define _PY_GC_SCHEDULED_BIT (1U << 4)
|
|
|
|
#define _PY_EVAL_PLEASE_STOP_BIT (1U << 5)
|
|
|
|
#define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6)
|
2023-10-04 12:09:48 -03:00
|
|
|
|
|
|
|
/* Reserve a few bits for future use */
|
|
|
|
#define _PY_EVAL_EVENTS_BITS 8
|
|
|
|
#define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1)
|
|
|
|
|
|
|
|
static inline void
|
2024-02-20 10:57:48 -04:00
|
|
|
_Py_set_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
|
2023-10-04 12:09:48 -03:00
|
|
|
{
|
2024-02-20 10:57:48 -04:00
|
|
|
_Py_atomic_or_uintptr(&tstate->eval_breaker, bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
_Py_unset_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit)
|
|
|
|
{
|
|
|
|
_Py_atomic_and_uintptr(&tstate->eval_breaker, ~bit);
|
2023-10-04 12:09:48 -03:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:57:48 -04:00
|
|
|
static inline int
|
|
|
|
_Py_eval_breaker_bit_is_set(PyThreadState *tstate, uintptr_t bit)
|
2023-10-04 12:09:48 -03:00
|
|
|
{
|
2024-02-20 10:57:48 -04:00
|
|
|
uintptr_t b = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
|
|
|
|
return (b & bit) != 0;
|
2023-10-04 12:09:48 -03:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:57:48 -04:00
|
|
|
// Free-threaded builds use these functions to set or unset a bit on all
|
|
|
|
// threads in the given interpreter.
|
|
|
|
void _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
|
|
|
|
void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
|
|
|
|
|
2023-10-04 12:09:48 -03:00
|
|
|
|
2017-09-08 02:51:28 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_INTERNAL_CEVAL_H */
|