diff options
| author | shadchin <[email protected]> | 2022-04-18 12:39:32 +0300 |
|---|---|---|
| committer | shadchin <[email protected]> | 2022-04-18 12:39:32 +0300 |
| commit | d4be68e361f4258cf0848fc70018dfe37a2acc24 (patch) | |
| tree | 153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Python/context.c | |
| parent | 260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff) | |
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Python/context.c')
| -rw-r--r-- | contrib/tools/python3/src/Python/context.c | 63 |
1 files changed, 43 insertions, 20 deletions
diff --git a/contrib/tools/python3/src/Python/context.c b/contrib/tools/python3/src/Python/context.c index bacc7010c45..bf2ba93c14e 100644 --- a/contrib/tools/python3/src/Python/context.c +++ b/contrib/tools/python3/src/Python/context.c @@ -10,8 +10,6 @@ #define CONTEXT_FREELIST_MAXLEN 255 -static PyContext *ctx_freelist = NULL; -static int ctx_freelist_len = 0; #include "clinic/context.c.h" @@ -68,6 +66,14 @@ static int contextvar_del(PyContextVar *var); +static struct _Py_context_state * +get_context_state(void) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + return &interp->context; +} + + PyObject * _PyContext_NewHamtForTests(void) { @@ -334,11 +340,16 @@ class _contextvars.Context "PyContext *" "&PyContext_Type" static inline PyContext * _context_alloc(void) { + struct _Py_context_state *state = get_context_state(); PyContext *ctx; - if (ctx_freelist_len) { - ctx_freelist_len--; - ctx = ctx_freelist; - ctx_freelist = (PyContext *)ctx->ctx_weakreflist; +#ifdef Py_DEBUG + // _context_alloc() must not be called after _PyContext_Fini() + assert(state->numfree != -1); +#endif + if (state->numfree) { + state->numfree--; + ctx = state->freelist; + state->freelist = (PyContext *)ctx->ctx_weakreflist; ctx->ctx_weakreflist = NULL; _Py_NewReference((PyObject *)ctx); } @@ -458,10 +469,15 @@ context_tp_dealloc(PyContext *self) } (void)context_tp_clear(self); - if (ctx_freelist_len < CONTEXT_FREELIST_MAXLEN) { - ctx_freelist_len++; - self->ctx_weakreflist = (PyObject *)ctx_freelist; - ctx_freelist = self; + struct _Py_context_state *state = get_context_state(); +#ifdef Py_DEBUG + // _context_alloc() must not be called after _PyContext_Fini() + assert(state->numfree != -1); +#endif + if (state->numfree < CONTEXT_FREELIST_MAXLEN) { + state->numfree++; + self->ctx_weakreflist = (PyObject *)state->freelist; + state->freelist = self; } else { Py_TYPE(self)->tp_free(self); @@ -687,7 +703,7 @@ static PyMappingMethods PyContext_as_mapping = { PyTypeObject PyContext_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "Context", + "_contextvars.Context", sizeof(PyContext), .tp_methods = PyContext_methods, .tp_as_mapping = &PyContext_as_mapping, @@ -1040,7 +1056,7 @@ static PyMethodDef PyContextVar_methods[] = { PyTypeObject PyContextVar_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "ContextVar", + "_contextvars.ContextVar", sizeof(PyContextVar), .tp_methods = PyContextVar_methods, .tp_members = PyContextVar_members, @@ -1181,7 +1197,7 @@ static PyMethodDef PyContextTokenType_methods[] = { PyTypeObject PyContextToken_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "Token", + "_contextvars.Token", sizeof(PyContextToken), .tp_methods = PyContextTokenType_methods, .tp_getset = PyContextTokenType_getsetlist, @@ -1271,11 +1287,12 @@ get_token_missing(void) void -_PyContext_ClearFreeList(void) +_PyContext_ClearFreeList(PyInterpreterState *interp) { - for (; ctx_freelist_len; ctx_freelist_len--) { - PyContext *ctx = ctx_freelist; - ctx_freelist = (PyContext *)ctx->ctx_weakreflist; + struct _Py_context_state *state = &interp->context; + for (; state->numfree; state->numfree--) { + PyContext *ctx = state->freelist; + state->freelist = (PyContext *)ctx->ctx_weakreflist; ctx->ctx_weakreflist = NULL; PyObject_GC_Del(ctx); } @@ -1283,10 +1300,16 @@ _PyContext_ClearFreeList(void) void -_PyContext_Fini(void) +_PyContext_Fini(PyInterpreterState *interp) { - Py_CLEAR(_token_missing); - _PyContext_ClearFreeList(); + if (_Py_IsMainInterpreter(interp)) { + Py_CLEAR(_token_missing); + } + _PyContext_ClearFreeList(interp); +#ifdef Py_DEBUG + struct _Py_context_state *state = &interp->context; + state->numfree = -1; +#endif _PyHamt_Fini(); } |
