python3_sys/
frameobject.rs

1use libc::{c_schar, c_int};
2#[cfg(not(Py_3_11))]
3use libc::c_char;
4
5use crate::code::{PyCodeObject};
6#[cfg(not(Py_3_11))]
7use crate::code::CO_MAXBLOCKS;
8use crate::object::*;
9use crate::pystate::PyThreadState;
10
11#[cfg(not(Py_LIMITED_API))]
12pub type PyFrameState = c_schar;
13
14#[cfg(not(Py_LIMITED_API))]
15#[repr(C)]
16#[derive(Copy, Clone)]
17pub struct PyTryBlock {
18    pub b_type: c_int,
19    pub b_handler: c_int,
20    pub b_level: c_int,
21}
22
23/// In Python > 3.11, frame object internals are always private
24///
25/// This improves performance by creating frame object lazily.
26/// There are now getter methods to get info from the frame.
27#[cfg(any(Py_LIMITED_API, Py_3_11))]
28#[repr(C)]
29pub struct PyFrameObject {
30    _private: [u8; 0],
31}
32
33#[cfg(all(not(Py_LIMITED_API), not(Py_3_11)))]
34#[repr(C)]
35#[derive(Copy, Clone)]
36pub struct PyFrameObject {
37    pub ob_base: PyVarObject,
38    pub f_back: *mut PyFrameObject,       /* previous frame, or NULL */
39    pub f_code: *mut PyCodeObject,        /* code segment */
40    pub f_builtins: *mut PyObject,        /* builtin symbol table (PyDictObject) */
41    pub f_globals: *mut PyObject,         /* global symbol table (PyDictObject) */
42    pub f_locals: *mut PyObject,          /* local symbol table (any mapping) */
43    pub f_valuestack: *mut *mut PyObject, /* points after the last local */
44    /* Next free slot in f_valuestack.  Frame creation sets to f_valuestack.
45    Frame evaluation usually NULLs it, but a frame that yields sets it
46    to the current stack top. */
47    #[cfg(not(Py_3_10))]
48    pub f_stacktop: *mut *mut PyObject,
49    pub f_trace: *mut PyObject, /* Trace function */
50    #[cfg(Py_3_10)]
51    pub f_stackdepth: c_int,
52
53    #[cfg(not(Py_3_7))]
54    pub f_exc_type: *mut PyObject,
55    #[cfg(not(Py_3_7))]
56    pub f_exc_value: *mut PyObject,
57    #[cfg(not(Py_3_7))]
58    pub f_exc_traceback: *mut PyObject,
59
60    #[cfg(Py_3_7)]
61    pub f_trace_lines: c_char,
62    #[cfg(Py_3_7)]
63    pub f_trace_opcodes: c_char,
64
65    #[cfg(not(Py_3_4))]
66    pub f_tstate: *mut PyThreadState,
67
68    #[cfg(Py_3_4)]
69    pub f_gen: *mut PyObject,
70
71    pub f_lasti: c_int, /* Last instruction if called */
72    /* Call PyFrame_GetLineNumber() instead of reading this field
73     directly.  As of 2.3 f_lineno is only valid when tracing is
74     active (i.e. when f_trace is set).  At other times we use
75     PyCode_Addr2Line to calculate the line from the current
76    bytecode index. */
77    pub f_lineno: c_int, /* Current line number. Only valid if non-zero */
78    pub f_iblock: c_int, /* index in f_blockstack */
79    #[cfg(all(Py_3_4, not(Py_3_10)))]
80    pub f_executing: c_char, /* whether the frame is still executing */
81    #[cfg(Py_3_10)]
82    pub f_state: PyFrameState,  /* What state the frame is in */
83    pub f_blockstack: [PyTryBlock; CO_MAXBLOCKS], /* for try and loop blocks */
84    pub f_localsplus: [*mut PyObject; 1], /* locals+stack, dynamically sized */
85}
86
87#[cfg(not(Py_LIMITED_API))]
88#[cfg_attr(windows, link(name = "pythonXY"))]
89extern "C" {
90    pub static mut PyFrame_Type: PyTypeObject;
91}
92
93#[cfg(not(Py_LIMITED_API))]
94#[inline]
95pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
96    (Py_TYPE(op) == &mut PyFrame_Type) as c_int
97}
98
99#[cfg(not(Py_LIMITED_API))]
100#[cfg_attr(windows, link(name = "pythonXY"))]
101extern "C" {
102    pub fn PyFrame_New(
103        tstate: *mut PyThreadState,
104        code: *mut PyCodeObject,
105        globals: *mut PyObject,
106        locals: *mut PyObject,
107    ) -> *mut PyFrameObject;
108
109    #[cfg(not(Py_3_11))]
110    pub fn PyFrame_BlockSetup(
111        f: *mut PyFrameObject,
112        _type: c_int,
113        handler: c_int,
114        level: c_int,
115    ) -> ();
116    #[cfg(not(Py_3_11))]
117    pub fn PyFrame_BlockPop(f: *mut PyFrameObject) -> *mut PyTryBlock;
118
119    pub fn PyFrame_LocalsToFast(f: *mut PyFrameObject, clear: c_int) -> ();
120    #[cfg(Py_3_4)]
121    pub fn PyFrame_FastToLocalsWithError(f: *mut PyFrameObject) -> c_int;
122    pub fn PyFrame_FastToLocals(f: *mut PyFrameObject) -> ();
123
124    #[cfg(not(Py_3_9))]
125    pub fn PyFrame_ClearFreeList() -> c_int;
126
127    #[cfg(Py_3_9)]
128    pub fn PyFrame_GetBack(frame: *mut PyFrameObject) -> *mut PyFrameObject;
129
130    #[cfg(Py_3_11)]
131    pub fn PyFrame_GetBuiltins(frame: *mut PyFrameObject) -> *mut PyObject;
132
133    #[cfg(Py_3_11)]
134    pub fn PyFrame_GetGenerator(frame: *mut PyFrameObject) -> *mut PyObject;
135
136
137    #[cfg(Py_3_11)]
138    pub fn PyFrame_GetGlobals(frame: *mut PyFrameObject) -> *mut PyObject;
139
140   #[cfg(Py_3_11)]
141    pub fn PyFrame_GetLasti(frame: *mut PyFrameObject) -> c_int;
142
143   #[cfg(Py_3_11)]
144    pub fn PyFrame_GetLocals(frame: *mut PyFrameObject) -> *mut PyObject;
145}   
146
147#[cfg_attr(windows, link(name = "pythonXY"))]
148extern "C" {
149    pub fn PyFrame_GetLineNumber(f: *mut PyFrameObject) -> c_int;
150
151    #[cfg(Py_3_9)]
152    pub fn PyFrame_GetCode(frame: *mut PyFrameObject) -> *mut PyCodeObject;
153}