cython

Форк
0
/
contextvars.pxd 
141 строка · 5.6 Кб
1
from cpython.object cimport PyObject
2
from cpython.ref cimport Py_XDECREF
3

4
cdef extern from *:
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.
7
    """
8
    #if (PY_VERSION_HEX < 0x030700b1 || (CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM < 0x07030600)) && !defined(PyContextVar_Get)
9
    #define PyContextVar_Get(var, d, v) \
10
        ((d) ? \
11
            ((void)(var), Py_INCREF(d), (v)[0] = (d), 0) : \
12
            ((v)[0] = NULL, 0) \
13
        )
14
    #endif
15
    """
16

17
cdef extern from "Python.h":
18
    ############################################################################
19
    # Context Variables Objects
20
    ############################################################################
21

22
    # PyContext
23
    # The C structure used to represent a `contextvars.Context` object.
24

25
    # PyContextVar
26
    # The C structure used to represent a `contextvars.ContextVar` object.
27

28
    # PyContextToken
29
    # The C structure used to represent a `contextvars.Token` object.
30

31
    # PyTypeObject PyContext_Type
32
    # Type object representing the `contextvars.Context` type.
33

34
    # PyTypeObject PyContextVar_Type
35
    # Type object representing the `contextvars.ContextVar` type.
36

37
    # PyTypeObject PyContextToken_Type
38
    # Type object representing the `contextvars.Token` type.
39

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.
43

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.
47

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.
51

52
    object PyContext_New()
53
    # Return value: New reference.
54
    # Create a new empty context object.
55
    # Returns NULL if an error has occurred.
56

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.
61

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.
66

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.
70

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.
75

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.
82

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.
86

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.
91
    #
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:
94
    #
95
    #   • `default_value`, if not NULL;
96
    #   • the default value of `var`, if not NULL;
97
    #   • 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.
101

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.
106

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.
111

112

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.
117
    """
118
    cdef PyObject *value = NULL
119
    PyContextVar_Get(var, NULL, &value)
120
    if value is NULL:
121
        # context variable does not have a default
122
        pyvalue = default_value
123
    else:
124
        # value or default value of context variable
125
        pyvalue = <object>value
126
        Py_XDECREF(value)  # PyContextVar_Get() returned an owned reference as 'PyObject*'
127
    return pyvalue
128

129

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.
133

134
    Ignores the default value of the context variable, if any.
135
    """
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*'
141
    return pyvalue
142

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.