| 1 |
#ifndef Py_LIMITED_API |
| 2 |
#ifndef Py_CONTEXT_H |
| 3 |
#define Py_CONTEXT_H |
| 4 |
#ifdef __cplusplus |
| 5 |
extern "C" { |
| 6 |
#endif |
| 7 |
|
| 8 |
PyAPI_DATA(PyTypeObject) PyContext_Type; |
| 9 |
typedef struct _pycontextobject PyContext; |
| 10 |
|
| 11 |
PyAPI_DATA(PyTypeObject) PyContextVar_Type; |
| 12 |
typedef struct _pycontextvarobject PyContextVar; |
| 13 |
|
| 14 |
PyAPI_DATA(PyTypeObject) PyContextToken_Type; |
| 15 |
typedef struct _pycontexttokenobject PyContextToken; |
| 16 |
|
| 17 |
|
| 18 |
#define PyContext_CheckExact(o) Py_IS_TYPE((o), &PyContext_Type) |
| 19 |
#define PyContextVar_CheckExact(o) Py_IS_TYPE((o), &PyContextVar_Type) |
| 20 |
#define PyContextToken_CheckExact(o) Py_IS_TYPE((o), &PyContextToken_Type) |
| 21 |
|
| 22 |
|
| 23 |
PyAPI_FUNC(PyObject *) PyContext_New(void); |
| 24 |
PyAPI_FUNC(PyObject *) PyContext_Copy(PyObject *); |
| 25 |
PyAPI_FUNC(PyObject *) PyContext_CopyCurrent(void); |
| 26 |
|
| 27 |
PyAPI_FUNC(int) PyContext_Enter(PyObject *); |
| 28 |
PyAPI_FUNC(int) PyContext_Exit(PyObject *); |
| 29 |
|
| 30 |
typedef enum { |
| 31 |
/* |
| 32 |
* The current context has switched to a different context. The object |
| 33 |
* passed to the watch callback is the now-current contextvars.Context |
| 34 |
* object, or None if no context is current. |
| 35 |
*/ |
| 36 |
Py_CONTEXT_SWITCHED = 1, |
| 37 |
} PyContextEvent; |
| 38 |
|
| 39 |
/* |
| 40 |
* Context object watcher callback function. The object passed to the callback |
| 41 |
* is event-specific; see PyContextEvent for details. |
| 42 |
* |
| 43 |
* if the callback returns with an exception set, it must return -1. Otherwise |
| 44 |
* it should return 0 |
| 45 |
*/ |
| 46 |
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyObject *); |
| 47 |
|
| 48 |
/* |
| 49 |
* Register a per-interpreter callback that will be invoked for context object |
| 50 |
* enter/exit events. |
| 51 |
* |
| 52 |
* Returns a handle that may be passed to PyContext_ClearWatcher on success, |
| 53 |
* or -1 and sets and error if no more handles are available. |
| 54 |
*/ |
| 55 |
PyAPI_FUNC(int) PyContext_AddWatcher(PyContext_WatchCallback callback); |
| 56 |
|
| 57 |
/* |
| 58 |
* Clear the watcher associated with the watcher_id handle. |
| 59 |
* |
| 60 |
* Returns 0 on success or -1 if no watcher exists for the provided id. |
| 61 |
*/ |
| 62 |
PyAPI_FUNC(int) PyContext_ClearWatcher(int watcher_id); |
| 63 |
|
| 64 |
/* Create a new context variable. |
| 65 |
|
| 66 |
default_value can be NULL. |
| 67 |
*/ |
| 68 |
PyAPI_FUNC(PyObject *) PyContextVar_New( |
| 69 |
const char *name, PyObject *default_value); |
| 70 |
|
| 71 |
|
| 72 |
/* Get a value for the variable. |
| 73 |
|
| 74 |
Returns -1 if an error occurred during lookup. |
| 75 |
|
| 76 |
Returns 0 if value either was or was not found. |
| 77 |
|
| 78 |
If value was found, *value will point to it. |
| 79 |
If not, it will point to: |
| 80 |
|
| 81 |
- default_value, if not NULL; |
| 82 |
- the default value of "var", if not NULL; |
| 83 |
- NULL. |
| 84 |
|
| 85 |
'*value' will be a new ref, if not NULL. |
| 86 |
*/ |
| 87 |
PyAPI_FUNC(int) PyContextVar_Get( |
| 88 |
PyObject *var, PyObject *default_value, PyObject **value); |
| 89 |
|
| 90 |
|
| 91 |
/* Set a new value for the variable. |
| 92 |
Returns NULL if an error occurs. |
| 93 |
*/ |
| 94 |
PyAPI_FUNC(PyObject *) PyContextVar_Set(PyObject *var, PyObject *value); |
| 95 |
|
| 96 |
|
| 97 |
/* Reset a variable to its previous value. |
| 98 |
Returns 0 on success, -1 on error. |
| 99 |
*/ |
| 100 |
PyAPI_FUNC(int) PyContextVar_Reset(PyObject *var, PyObject *token); |
| 101 |
|
| 102 |
|
| 103 |
#ifdef __cplusplus |
| 104 |
} |
| 105 |
#endif |
| 106 |
#endif /* !Py_CONTEXT_H */ |
| 107 |
#endif /* !Py_LIMITED_API */ |