8000 gh-81057: Move contextvars-related Globals to _PyRuntimeState by ericsnowcurrently · Pull Request #99400 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81057: Move contextvars-related Globals to _PyRuntimeState #99400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move _token_missing to _PyRuntimeState.
  • Loading branch information
ericsnowcurrently committed Nov 11, 2022
commit 902615318c3678e753ee9e0f39801fd528df0eb0
4 changes: 4 additions & 0 deletions Include/internal/pycore_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void _PyContext_Fini(PyInterpreterState *);

/* other API */

typedef struct {
PyObject_HEAD
} _PyContextTokenMissing;

#ifndef WITH_FREELISTS
// without freelists
# define PyContext_MAXFREELIST 0
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "pycore_gc.h" // PyGC_Head
#include "pycore_global_strings.h" // struct _Py_global_strings
#include "pycore_hamt.h" // PyHamtNode_Bitmap
#include "pycore_context.h" // _PyContextTokenMissing


// These would be in pycore_long.h if it weren't for an include cycle.
Expand Down Expand Up @@ -48,6 +49,7 @@ struct _Py_global_objects {

_PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used;
PyHamtNode_Bitmap hamt_bitmap_node_empty;
_PyContextTokenMissing context_token_missing;
} singletons;

PyObject *interned;
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ extern "C" {
.hamt_bitmap_node_empty = { \
.ob_base = _PyVarObject_IMMORTAL_INIT(&_PyHamt_BitmapNode_Type, 0) \
}, \
.context_token_missing = { \
.ob_base = _PyObject_IMMORTAL_INIT(&_PyContextTokenMissing_Type), \
}, \
}, \
}, \
._main_interpreter = _PyInterpreterState_INIT, \
Expand Down
37 changes: 14 additions & 23 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,25 +1235,29 @@ token_new(PyContext *ctx, PyContextVar *var, PyObject *val)
/////////////////////////// Token.MISSING


static PyObject *_token_missing;


typedef struct {
PyObject_HEAD
} PyContextTokenMissing;


static PyObject *
context_token_missing_tp_repr(PyObject *self)
{
return PyUnicode_FromString("<Token.MISSING>");
}

static void
context_token_missing_tp_dealloc(_PyContextTokenMissing *Py_UNUSED(self))
{
#ifdef Py_DEBUG
/* The singleton is statically allocated. */
_Py_FatalRefcountError("deallocating the token missing singleton");
#else
return;
#endif
}


PyTypeObject _PyContextTokenMissing_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"Token.MISSING",
sizeof(PyContextTokenMissing),
sizeof(_PyContextTokenMissing),
.tp_dealloc = (destructor)context_token_missing_tp_dealloc,
.tp_getattro = PyObject_GenericGetAttr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_repr = context_token_missing_tp_repr,
Expand All @@ -1263,17 +1267,7 @@ PyTypeObject _PyContextTokenMissing_Type = {
static PyObject *
get_token_missing(void)
{
if (_token_missing != NULL) {
return Py_NewRef(_token_missing);
}

_token_missing = (PyObject *)PyObject_New(
PyContextTokenMissing, &_PyContextTokenMissing_Type);
if (_token_missing == NULL) {
return NULL;
}

return Py_NewRef(_token_missing);
return Py_NewRef(&_Py_SINGLETON(context_token_missing));
}


Expand All @@ -1298,9 +1292,6 @@ _PyContext_ClearFreeList(PyInterpreterState *interp)
void
_PyContext_Fini(PyInterpreterState *interp)
{
if (_Py_IsMainInterpreter(interp)) {
Py_CLEAR(_token_missing);
}
_PyContext_ClearFreeList(interp);
#if defined(Py_DEBUG) && PyContext_MAXFREELIST > 0
struct _Py_context_state *state = &interp->context;
Expand Down
1 change: 1 addition & 0 deletions Tools/build/generate_global_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
'(PyObject *)&_Py_SINGLETON(tuple_empty)',
'(PyObject *)&_Py_SINGLETON(hamt_bitmap_node_empty)',
'(PyObject *)&_Py_INTERP_SINGLETON(interp, hamt_empty)',
'(PyObject *)&_Py_SINGLETON(context_token_missing)',
]


Expand Down
3 changes: 0 additions & 3 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,6 @@ Objects/typeobject.c object___reduce_ex___impl objreduce -
#-----------------------
# other

# initialized once
Python/context.c - _token_missing -

# state
Objects/typeobject.c resolve_slotdups pname -

Expand Down
0