Skip to main content

pyo3_ffi/cpython/
context.rs

1use crate::object::PyObject;
2use crate::object::PyTypeObject;
3use crate::Py_IS_TYPE;
4#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
5use core::ffi::c_uint;
6use core::ffi::{c_char, c_int};
7
8extern_libpython! {
9    pub static mut PyContext_Type: PyTypeObject;
10    // skipped non-limited opaque PyContext
11    pub static mut PyContextVar_Type: PyTypeObject;
12    // skipped non-limited opaque PyContextVar
13    pub static mut PyContextToken_Type: PyTypeObject;
14    // skipped non-limited opaque PyContextToken
15}
16
17#[inline]
18pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int {
19    Py_IS_TYPE(op, &raw mut PyContext_Type)
20}
21
22#[inline]
23pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int {
24    Py_IS_TYPE(op, &raw mut PyContextVar_Type)
25}
26
27#[inline]
28pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int {
29    Py_IS_TYPE(op, &raw mut PyContextToken_Type)
30}
31
32extern_libpython! {
33    pub fn PyContext_New() -> *mut PyObject;
34    pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject;
35    pub fn PyContext_CopyCurrent() -> *mut PyObject;
36
37    pub fn PyContext_Enter(ctx: *mut PyObject) -> c_int;
38    pub fn PyContext_Exit(ctx: *mut PyObject) -> c_int;
39}
40
41// Use the C enum's integer representation to permit future event values.
42#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
43pub type PyContextEvent = c_uint;
44
45#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
46pub const Py_CONTEXT_SWITCHED: PyContextEvent = 1;
47
48#[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
49pub type PyContext_WatchCallback =
50    unsafe extern "C" fn(event: PyContextEvent, obj: *mut PyObject) -> c_int;
51
52extern_libpython! {
53    #[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
54    pub fn PyContext_AddWatcher(callback: PyContext_WatchCallback) -> c_int;
55    #[cfg(all(Py_3_14, not(any(PyPy, GraalPy))))]
56    pub fn PyContext_ClearWatcher(watcher_id: c_int) -> c_int;
57
58    pub fn PyContextVar_New(name: *const c_char, def: *mut PyObject) -> *mut PyObject;
59    pub fn PyContextVar_Get(
60        var: *mut PyObject,
61        default_value: *mut PyObject,
62        value: *mut *mut PyObject,
63    ) -> c_int;
64    pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject;
65    pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int;
66    // skipped non-limited _PyContext_NewHamtForTests
67}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here