Skip to main content

pyo3_ffi/
context.rs

1use crate::object::PyObject;
2#[cfg(not(RustPython))]
3use crate::object::PyTypeObject;
4#[cfg(not(RustPython))]
5use crate::Py_IS_TYPE;
6use core::ffi::{c_char, c_int};
7
8#[cfg(not(RustPython))]
9extern_libpython! {
10    pub static mut PyContext_Type: PyTypeObject;
11    // skipped non-limited opaque PyContext
12    pub static mut PyContextVar_Type: PyTypeObject;
13    // skipped non-limited opaque PyContextVar
14    pub static mut PyContextToken_Type: PyTypeObject;
15    // skipped non-limited opaque PyContextToken
16}
17
18#[inline]
19#[cfg(not(RustPython))]
20pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int {
21    Py_IS_TYPE(op, &raw mut PyContext_Type)
22}
23
24#[inline]
25#[cfg(not(RustPython))]
26pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int {
27    Py_IS_TYPE(op, &raw mut PyContextVar_Type)
28}
29
30#[inline]
31#[cfg(not(RustPython))]
32pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int {
33    Py_IS_TYPE(op, &raw mut PyContextToken_Type)
34}
35
36extern_libpython! {
37    #[cfg(RustPython)]
38    pub fn PyContext_CheckExact(op: *mut PyObject) -> c_int;
39    #[cfg(RustPython)]
40    pub fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int;
41    #[cfg(RustPython)]
42    pub fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int;
43
44    pub fn PyContext_New() -> *mut PyObject;
45    pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject;
46    pub fn PyContext_CopyCurrent() -> *mut PyObject;
47
48    pub fn PyContext_Enter(ctx: *mut PyObject) -> c_int;
49    pub fn PyContext_Exit(ctx: *mut PyObject) -> c_int;
50
51    pub fn PyContextVar_New(name: *const c_char, def: *mut PyObject) -> *mut PyObject;
52    pub fn PyContextVar_Get(
53        var: *mut PyObject,
54        default_value: *mut PyObject,
55        value: *mut *mut PyObject,
56    ) -> c_int;
57    pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject;
58    pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int;
59    // skipped non-limited _PyContext_NewHamtForTests
60}