diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2022-01-14 00:09:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-14 00:09:24 (GMT) |
commit | 324908ba936d5d262026deebb81f050803848c41 (patch) | |
tree | a45363816b664cbe18eb899e33688d7f3bc09e59 /Include | |
parent | d4e64cd4b0ea431d4e371f9b0a25f6b75a069dc1 (diff) | |
download | cpython-324908ba936d5d262026deebb81f050803848c41.zip cpython-324908ba936d5d262026deebb81f050803848c41.tar.gz cpython-324908ba936d5d262026deebb81f050803848c41.tar.bz2 |
bpo-45953: Statically initialize all the PyThreadState fields we can. (gh-30590)
https://bugs.python.org/issue45953
Diffstat (limited to 'Include')
-rw-r--r-- | Include/cpython/pystate.h | 42 | ||||
-rw-r--r-- | Include/internal/pycore_ceval.h | 5 | ||||
-rw-r--r-- | Include/internal/pycore_runtime_init.h | 2 |
3 files changed, 36 insertions, 13 deletions
diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index bcb1bb2..a35e5b8 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -53,12 +53,19 @@ typedef struct _cframe { } CFrame; typedef struct _err_stackitem { - /* This struct represents an entry on the exception stack, which is a - * per-coroutine state. (Coroutine in the computer science sense, - * including the thread and generators). - * This ensures that the exception state is not impacted by "yields" - * from an except handler. + /* This struct represents a single execution context where we might + * be currently handling an exception. It is a per-coroutine state + * (coroutine in the computer science sense, including the thread + * and generators). + * + * This is used as an entry on the exception stack, where each + * entry indicates if it is currently handling an exception. + * This ensures that the exception state is not impacted + * by "yields" from an except handler. The thread + * always has an entry (the bottom-most one). */ + + /* The exception currently being handled in this context, if any. */ PyObject *exc_value; struct _err_stackitem *previous_item; @@ -112,13 +119,9 @@ struct _ts { PyObject *curexc_value; PyObject *curexc_traceback; - /* The exception currently being handled, if no coroutines/generators - * are present. Always last element on the stack referred to be exc_info. - */ - _PyErr_StackItem exc_state; - - /* Pointer to the top of the stack of the exceptions currently - * being handled */ + /* Pointer to the top of the exception stack for the exceptions + * we may be currently handling. (See _PyErr_StackItem above.) + * This is never NULL. */ _PyErr_StackItem *exc_info; PyObject *dict; /* Stores per-thread state */ @@ -174,13 +177,26 @@ struct _ts { /* Unique thread state id. */ uint64_t id; - CFrame root_cframe; PyTraceInfo trace_info; _PyStackChunk *datastack_chunk; PyObject **datastack_top; PyObject **datastack_limit; /* XXX signal handlers should also be here */ + + /* The following fields are here to avoid allocation during init. + The data is exposed through PyThreadState pointer fields. + These fields should not be accessed directly outside of init. + + All other PyInterpreterState pointer fields are populated when + needed and default to NULL. + */ + + /* The thread's exception stack entry. (Always the last entry.) */ + _PyErr_StackItem _exc_state; + + /* The bottom-most frame on the stack. */ + CFrame _root_cframe; }; diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 20508d4..53d0b5c 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -12,9 +12,14 @@ extern "C" { struct pyruntimestate; struct _ceval_runtime_state; +#ifndef Py_DEFAULT_RECURSION_LIMIT +# define Py_DEFAULT_RECURSION_LIMIT 1000 +#endif + #include "pycore_interp.h" // PyInterpreterState.eval_frame #include "pycore_pystate.h" // _PyThreadState_GET() + extern void _Py_FinishPendingCalls(PyThreadState *tstate); extern void _PyEval_InitRuntimeState(struct _ceval_runtime_state *); extern void _PyEval_InitState(struct _ceval_state *, PyThread_type_lock); diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index fc768ff..aa66120 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -41,6 +41,8 @@ extern "C" { #define _PyThreadState_INIT \ { \ ._static = 1, \ + .recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \ + .context_ver = 1, \ } |