1
from cpython.object cimport PyObject
2
from cpython.ref cimport Py_XDECREF
5
# Defining PyContextVar_Get() below to always return the default value for Py<3.7 and PyPy<7.3.6
6
# to make the inline functions sort-of work.
8
#if (PY_VERSION_HEX < 0x030700b1 || (CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM < 0x07030600)) && !defined(PyContextVar_Get)
9
#define PyContextVar_Get(var, d, v) \
11
((void)(var), Py_INCREF(d), (v)[0] = (d), 0) : \
17
cdef extern from "Python.h":
18
############################################################################
19
# Context Variables Objects
20
############################################################################
23
# The C structure used to represent a `contextvars.Context` object.
26
# The C structure used to represent a `contextvars.ContextVar` object.
29
# The C structure used to represent a `contextvars.Token` object.
31
# PyTypeObject PyContext_Type
32
# Type object representing the `contextvars.Context` type.
34
# PyTypeObject PyContextVar_Type
35
# Type object representing the `contextvars.ContextVar` type.
37
# PyTypeObject PyContextToken_Type
38
# Type object representing the `contextvars.Token` type.
40
bint PyContext_CheckExact(object obj)
41
# Return `true` if `obj` is of type `PyContext_Type`.
42
# `obj` must not be NULL. This function always succeeds.
44
bint PyContextVar_CheckExact(object obj)
45
# Return `true` if `obj` is of type `PyContextVar_Type`.
46
# `obj` must not be NULL. This function always succeeds.
48
bint PyContextToken_CheckExact(object obj)
49
# Return `true` if `obj` is of type `PyContextToken_Type`.
50
# `obj` must not be NULL. This function always succeeds.
52
object PyContext_New()
53
# Return value: New reference.
54
# Create a new empty context object.
55
# Returns NULL if an error has occurred.
57
object PyContext_Copy(object ctx)
58
# Return value: New reference.
59
# Create a shallow copy of the passed `ctx` context object.
60
# Returns NULL if an error has occurred.
62
object PyContext_CopyCurrent()
63
# Return value: New reference.
64
# Create a shallow copy of the current thread context.
65
# Returns NULL if an error has occurred.
67
int PyContext_Enter(object ctx) except -1
68
# Set `ctx` as the current context for the current thread.
69
# Returns 0 on success, and -1 on error.
71
int PyContext_Exit(object ctx) except -1
72
# Deactivate the `ctx` context and restore the previous context
73
# as the current context for the current thread.
74
# Returns 0 on success, and -1 on error.
76
object PyContextVar_New(const char* name, PyObject* default_value)
77
# Return value: New reference.
78
# Create a new ContextVar object. The `name` parameter is used
79
# for introspection and debug purposes. The `default_value` parameter
80
# may optionally specify the default value for the context variable.
81
# If an error has occurred, this function returns NULL.
83
object PyContextVar_New_with_default "PyContextVar_New" (const char* name, object default_value)
84
# A different declaration of PyContextVar_New that requires a default value
85
# to be passed on call.
87
int PyContextVar_Get(object var, PyObject* default_value, PyObject** value) except -1
88
# Get the value of a context variable.
89
# Returns -1 if an error has occurred during lookup, and 0 if no error
90
# occurred, whether or not a value was found.
92
# If the context variable was found, `value` will be a pointer to it.
93
# If the context variable was not found, `value` will point to:
95
# • `default_value`, if not NULL;
96
# • the default value of `var`, if not NULL;
98
int PyContextVar_Get_with_default "PyContextVar_Get" (object var, object default_value, PyObject** value) except -1
99
# A different declaration of PyContextVar_Get that requires a default value
100
# to be passed on call.
102
object PyContextVar_Set(object var, object value)
103
# Return value: New reference.
104
# Set the value of `var` to `value` in the current context.
105
# Returns a token object for this value change, or NULL if an error has occurred.
107
int PyContextVar_Reset(object var, object token) except -1
108
# Reset the state of the `var` context variable to that it was in
109
# before `PyContextVar_Set()` that returned `token` was called.
110
# This function returns 0 on success and -1 on error.
113
cdef inline object get_value(var, default_value=None):
114
"""Return a new reference to the value of the context variable,
115
or the default value of the context variable,
116
or None if no such value or default was found.
118
cdef PyObject *value = NULL
119
PyContextVar_Get(var, NULL, &value)
121
# context variable does not have a default
122
pyvalue = default_value
124
# value or default value of context variable
125
pyvalue = <object>value
126
Py_XDECREF(value) # PyContextVar_Get() returned an owned reference as 'PyObject*'
130
cdef inline object get_value_no_default(var, default_value=None):
131
"""Return a new reference to the value of the context variable,
132
or the provided default value if no such value was found.
134
Ignores the default value of the context variable, if any.
136
cdef PyObject *value = NULL
137
PyContextVar_Get(var, <PyObject*>default_value, &value)
138
# value of context variable or 'default_value'
139
pyvalue = <object>value
140
Py_XDECREF(value) # PyContextVar_Get() returned an owned reference as 'PyObject*'