cpython/Python/context.c

#include "Python.h"
#include "pycore_call.h"          // _PyObject_VectorcallTstate()
#include "pycore_context.h"
#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
#include "pycore_gc.h"            // _PyObject_GC_MAY_BE_TRACKED()
#include "pycore_hamt.h"
#include "pycore_initconfig.h"    // _PyStatus_OK()
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"       // _PyThreadState_GET()



#include "clinic/context.c.h"
/*[clinic input]
module _contextvars
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a0955718c8b8cea6]*/


#define ENSURE_Context(o, err_ret)

#define ENSURE_ContextVar(o, err_ret)

#define ENSURE_ContextToken(o, err_ret)


/////////////////////////// Context API


static PyContext *
context_new_empty(void);

static PyContext *
context_new_from_vars(PyHamtObject *vars);

static inline PyContext *
context_get(void);

static PyContextToken *
token_new(PyContext *ctx, PyContextVar *var, PyObject *val);

static PyContextVar *
contextvar_new(PyObject *name, PyObject *def);

static int
contextvar_set(PyContextVar *var, PyObject *val);

static int
contextvar_del(PyContextVar *var);


PyObject *
_PyContext_NewHamtForTests(void)
{}


PyObject *
PyContext_New(void)
{}


PyObject *
PyContext_Copy(PyObject * octx)
{}


PyObject *
PyContext_CopyCurrent(void)
{}

static const char *
context_event_name(PyContextEvent event) {}

static void
notify_context_watchers(PyThreadState *ts, PyContextEvent event, PyObject *ctx)
{}


int
PyContext_AddWatcher(PyContext_WatchCallback callback)
{}


int
PyContext_ClearWatcher(int watcher_id)
{}


static inline void
context_switched(PyThreadState *ts)
{}


static int
_PyContext_Enter(PyThreadState *ts, PyObject *octx)
{}


int
PyContext_Enter(PyObject *octx)
{}


static int
_PyContext_Exit(PyThreadState *ts, PyObject *octx)
{}

int
PyContext_Exit(PyObject *octx)
{}


PyObject *
PyContextVar_New(const char *name, PyObject *def)
{}


int
PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
{}


PyObject *
PyContextVar_Set(PyObject *ovar, PyObject *val)
{}


int
PyContextVar_Reset(PyObject *ovar, PyObject *otok)
{}


/////////////////////////// PyContext

/*[clinic input]
class _contextvars.Context "PyContext *" "&PyContext_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdf87f8e0cb580e8]*/


static inline PyContext *
_context_alloc(void)
{}


static PyContext *
context_new_empty(void)
{}


static PyContext *
context_new_from_vars(PyHamtObject *vars)
{}


static inline PyContext *
context_get(void)
{}

static int
context_check_key_type(PyObject *key)
{}

static PyObject *
context_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{}

static int
context_tp_clear(PyContext *self)
{}

static int
context_tp_traverse(PyContext *self, visitproc visit, void *arg)
{}

static void
context_tp_dealloc(PyContext *self)
{}

static PyObject *
context_tp_iter(PyContext *self)
{}

static PyObject *
context_tp_richcompare(PyObject *v, PyObject *w, int op)
{}

static Py_ssize_t
context_tp_len(PyContext *self)
{}

static PyObject *
context_tp_subscript(PyContext *self, PyObject *key)
{}

static int
context_tp_contains(PyContext *self, PyObject *key)
{}


/*[clinic input]
_contextvars.Context.get
    key: object
    default: object = None
    /

Return the value for `key` if `key` has the value in the context object.

If `key` does not exist, return `default`. If `default` is not given,
return None.
[clinic start generated code]*/

static PyObject *
_contextvars_Context_get_impl(PyContext *self, PyObject *key,
                              PyObject *default_value)
/*[clinic end generated code: output=0c54aa7664268189 input=c8eeb81505023995]*/
{}


/*[clinic input]
_contextvars.Context.items

Return all variables and their values in the context object.

The result is returned as a list of 2-tuples (variable, value).
[clinic start generated code]*/

static PyObject *
_contextvars_Context_items_impl(PyContext *self)
/*[clinic end generated code: output=fa1655c8a08502af input=00db64ae379f9f42]*/
{}


/*[clinic input]
_contextvars.Context.keys

Return a list of all variables in the context object.
[clinic start generated code]*/

static PyObject *
_contextvars_Context_keys_impl(PyContext *self)
/*[clinic end generated code: output=177227c6b63ec0e2 input=114b53aebca3449c]*/
{}


/*[clinic input]
_contextvars.Context.values

Return a list of all variables' values in the context object.
[clinic start generated code]*/

static PyObject *
_contextvars_Context_values_impl(PyContext *self)
/*[clinic end generated code: output=d286dabfc8db6dde input=ce8075d04a6ea526]*/
{}


/*[clinic input]
_contextvars.Context.copy

Return a shallow copy of the context object.
[clinic start generated code]*/

static PyObject *
_contextvars_Context_copy_impl(PyContext *self)
/*[clinic end generated code: output=30ba8896c4707a15 input=ebafdbdd9c72d592]*/
{}


static PyObject *
context_run(PyContext *self, PyObject *const *args,
            Py_ssize_t nargs, PyObject *kwnames)
{}


static PyMethodDef PyContext_methods[] =;

static PySequenceMethods PyContext_as_sequence =;

static PyMappingMethods PyContext_as_mapping =;

PyTypeObject PyContext_Type =;


/////////////////////////// ContextVar


static int
contextvar_set(PyContextVar *var, PyObject *val)
{}

static int
contextvar_del(PyContextVar *var)
{}

static Py_hash_t
contextvar_generate_hash(void *addr, PyObject *name)
{}

static PyContextVar *
contextvar_new(PyObject *name, PyObject *def)
{}


/*[clinic input]
class _contextvars.ContextVar "PyContextVar *" "&PyContextVar_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=445da935fa8883c3]*/


static PyObject *
contextvar_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{}

static int
contextvar_tp_clear(PyContextVar *self)
{}

static int
contextvar_tp_traverse(PyContextVar *self, visitproc visit, void *arg)
{}

static void
contextvar_tp_dealloc(PyContextVar *self)
{}

static Py_hash_t
contextvar_tp_hash(PyContextVar *self)
{}

static PyObject *
contextvar_tp_repr(PyContextVar *self)
{}


/*[clinic input]
_contextvars.ContextVar.get
    default: object = NULL
    /

Return a value for the context variable for the current context.

If there is no value for the variable in the current context, the method will:
 * return the value of the default argument of the method, if provided; or
 * return the default value for the context variable, if it was created
   with one; or
 * raise a LookupError.
[clinic start generated code]*/

static PyObject *
_contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value)
/*[clinic end generated code: output=0746bd0aa2ced7bf input=30aa2ab9e433e401]*/
{}

/*[clinic input]
_contextvars.ContextVar.set
    value: object
    /

Call to set a new value for the context variable in the current context.

The required value argument is the new value for the context variable.

Returns a Token object that can be used to restore the variable to its previous
value via the `ContextVar.reset()` method.
[clinic start generated code]*/

static PyObject *
_contextvars_ContextVar_set(PyContextVar *self, PyObject *value)
/*[clinic end generated code: output=446ed5e820d6d60b input=c0a6887154227453]*/
{}

/*[clinic input]
_contextvars.ContextVar.reset
    token: object
    /

Reset the context variable.

The variable is reset to the value it had before the `ContextVar.set()` that
created the token was used.
[clinic start generated code]*/

static PyObject *
_contextvars_ContextVar_reset(PyContextVar *self, PyObject *token)
/*[clinic end generated code: output=d4ee34d0742d62ee input=ebe2881e5af4ffda]*/
{}


static PyMemberDef PyContextVar_members[] =;

static PyMethodDef PyContextVar_methods[] =;

PyTypeObject PyContextVar_Type =;


/////////////////////////// Token

static PyObject * get_token_missing(void);


/*[clinic input]
class _contextvars.Token "PyContextToken *" "&PyContextToken_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=338a5e2db13d3f5b]*/


static PyObject *
token_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{}

static int
token_tp_clear(PyContextToken *self)
{}

static int
token_tp_traverse(PyContextToken *self, visitproc visit, void *arg)
{}

static void
token_tp_dealloc(PyContextToken *self)
{}

static PyObject *
token_tp_repr(PyContextToken *self)
{}

static PyObject *
token_get_var(PyContextToken *self, void *Py_UNUSED(ignored))
{}

static PyObject *
token_get_old_value(PyContextToken *self, void *Py_UNUSED(ignored))
{}

static PyGetSetDef PyContextTokenType_getsetlist[] =;

static PyMethodDef PyContextTokenType_methods[] =;

PyTypeObject PyContextToken_Type =;

static PyContextToken *
token_new(PyContext *ctx, PyContextVar *var, PyObject *val)
{}


/////////////////////////// Token.MISSING


static PyObject *
context_token_missing_tp_repr(PyObject *self)
{}

static void
context_token_missing_tp_dealloc(_PyContextTokenMissing *Py_UNUSED(self))
{}


PyTypeObject _PyContextTokenMissing_Type =;


static PyObject *
get_token_missing(void)
{}


///////////////////////////


PyStatus
_PyContext_Init(PyInterpreterState *interp)
{}