Skip to main content

pyo3_ffi/cpython/
pystate.rs

1use crate::PyThreadState;
2use crate::{PyFrameObject, PyInterpreterState, PyObject};
3use std::ffi::c_int;
4
5// skipped private _PyInterpreterState_RequiresIDRef
6// skipped private _PyInterpreterState_RequireIDRef
7
8pub type Py_tracefunc = unsafe extern "C" fn(
9    obj: *mut PyObject,
10    frame: *mut PyFrameObject,
11    what: c_int,
12    arg: *mut PyObject,
13) -> c_int;
14
15pub const PyTrace_CALL: c_int = 0;
16pub const PyTrace_EXCEPTION: c_int = 1;
17pub const PyTrace_LINE: c_int = 2;
18pub const PyTrace_RETURN: c_int = 3;
19pub const PyTrace_C_CALL: c_int = 4;
20pub const PyTrace_C_EXCEPTION: c_int = 5;
21pub const PyTrace_C_RETURN: c_int = 6;
22pub const PyTrace_OPCODE: c_int = 7;
23
24// skipped private _Py_MAX_SCRIPT_PATH_SIZE
25// skipped private _PyRemoteDebuggerSupport
26
27/// Private structure used inline in `PyGenObject`
28///
29/// `PyGenObject` was made opaque in Python 3.14, so we don't bother defining this
30/// structure for that version and later.
31#[cfg(not(any(PyPy, Py_3_14)))]
32#[repr(C)]
33#[derive(Clone, Copy)]
34pub(crate) struct _PyErr_StackItem {
35    #[cfg(not(Py_3_11))]
36    exc_type: *mut PyObject,
37    exc_value: *mut PyObject,
38    #[cfg(not(Py_3_11))]
39    exc_traceback: *mut PyObject,
40    previous_item: *mut _PyErr_StackItem,
41}
42
43// skipped private _PyStackChunk
44
45// skipped private _PY_DATA_STACK_CHUNK_SIZE
46// skipped private _ts (aka PyThreadState)
47
48extern "C" {
49    #[cfg(Py_3_13)]
50    pub fn PyThreadState_GetUnchecked() -> *mut PyThreadState;
51
52    #[cfg(not(Py_3_13))]
53    pub(crate) fn _PyThreadState_UncheckedGet() -> *mut PyThreadState;
54
55    #[cfg(Py_3_11)]
56    pub fn PyThreadState_EnterTracing(state: *mut PyThreadState);
57    #[cfg(Py_3_11)]
58    pub fn PyThreadState_LeaveTracing(state: *mut PyThreadState);
59
60    #[cfg_attr(PyPy, link_name = "PyPyGILState_Check")]
61    pub fn PyGILState_Check() -> c_int;
62
63    // skipped private _PyThread_CurrentFrames
64
65    // skipped PyUnstable_ThreadState_SetStackProtection
66    // skipped PyUnstable_ThreadState_ResetStackProtection
67
68    #[cfg(not(PyPy))]
69    pub fn PyInterpreterState_Main() -> *mut PyInterpreterState;
70    #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Head")]
71    pub fn PyInterpreterState_Head() -> *mut PyInterpreterState;
72    #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Next")]
73    pub fn PyInterpreterState_Next(interp: *mut PyInterpreterState) -> *mut PyInterpreterState;
74    #[cfg(not(PyPy))]
75    pub fn PyInterpreterState_ThreadHead(interp: *mut PyInterpreterState) -> *mut PyThreadState;
76    #[cfg(not(PyPy))]
77    pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState;
78
79    #[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")]
80    pub fn PyThreadState_DeleteCurrent();
81}
82
83// skipped private _PyFrameEvalFunction
84// skipped private _PyInterpreterState_GetEvalFrameFunc
85// skipped private _PyInterpreterState_SetEvalFrameFunc