1use crate::object::*;
2use crate::pyport::Py_ssize_t;
3
4#[allow(unused_imports)]
5use std::os::raw::{c_char, c_int, c_short, c_uchar, c_void};
6#[cfg(not(PyPy))]
7use std::ptr::addr_of_mut;
8
9#[cfg(all(Py_3_8, not(PyPy), not(Py_3_11)))]
10opaque_struct!(_PyOpcache);
11
12#[cfg(Py_3_12)]
13pub const _PY_MONITORING_LOCAL_EVENTS: usize = 10;
14#[cfg(Py_3_12)]
15pub const _PY_MONITORING_UNGROUPED_EVENTS: usize = 15;
16#[cfg(Py_3_12)]
17pub const _PY_MONITORING_EVENTS: usize = 17;
18
19#[cfg(Py_3_12)]
20#[repr(C)]
21#[derive(Clone, Copy)]
22pub struct _Py_LocalMonitors {
23 pub tools: [u8; _PY_MONITORING_UNGROUPED_EVENTS],
24}
25
26#[cfg(Py_3_12)]
27#[repr(C)]
28#[derive(Clone, Copy)]
29pub struct _Py_GlobalMonitors {
30 pub tools: [u8; _PY_MONITORING_UNGROUPED_EVENTS],
31}
32
33// skipped _Py_CODEUNIT
34
35// skipped _Py_OPCODE
36// skipped _Py_OPARG
37
38// skipped _py_make_codeunit
39
40// skipped _py_set_opcode
41
42// skipped _Py_MAKE_CODEUNIT
43// skipped _Py_SET_OPCODE
44
45#[cfg(Py_3_12)]
46#[repr(C)]
47#[derive(Copy, Clone)]
48pub struct _PyCoCached {
49 pub _co_code: *mut PyObject,
50 pub _co_varnames: *mut PyObject,
51 pub _co_cellvars: *mut PyObject,
52 pub _co_freevars: *mut PyObject,
53}
54
55#[cfg(Py_3_12)]
56#[repr(C)]
57#[derive(Copy, Clone)]
58pub struct _PyCoLineInstrumentationData {
59 pub original_opcode: u8,
60 pub line_delta: i8,
61}
62
63#[cfg(Py_3_12)]
64#[repr(C)]
65#[derive(Copy, Clone)]
66pub struct _PyCoMonitoringData {
67 pub local_monitors: _Py_LocalMonitors,
68 pub active_monitors: _Py_LocalMonitors,
69 pub tools: *mut u8,
70 pub lines: *mut _PyCoLineInstrumentationData,
71 pub line_tools: *mut u8,
72 pub per_instruction_opcodes: *mut u8,
73 pub per_instruction_tools: *mut u8,
74}
75
76#[cfg(all(not(PyPy), not(Py_3_7)))]
77opaque_struct!(PyCodeObject);
78
79#[cfg(all(not(PyPy), Py_3_7, not(Py_3_8)))]
80#[repr(C)]
81#[derive(Copy, Clone)]
82pub struct PyCodeObject {
83 pub ob_base: PyObject,
84 pub co_argcount: c_int,
85 pub co_kwonlyargcount: c_int,
86 pub co_nlocals: c_int,
87 pub co_stacksize: c_int,
88 pub co_flags: c_int,
89 pub co_firstlineno: c_int,
90 pub co_code: *mut PyObject,
91 pub co_consts: *mut PyObject,
92 pub co_names: *mut PyObject,
93 pub co_varnames: *mut PyObject,
94 pub co_freevars: *mut PyObject,
95 pub co_cellvars: *mut PyObject,
96 pub co_cell2arg: *mut Py_ssize_t,
97 pub co_filename: *mut PyObject,
98 pub co_name: *mut PyObject,
99 pub co_lnotab: *mut PyObject,
100 pub co_zombieframe: *mut c_void,
101 pub co_weakreflist: *mut PyObject,
102 pub co_extra: *mut c_void,
103}
104
105#[cfg(all(not(PyPy), Py_3_8, not(Py_3_11)))]
106#[repr(C)]
107#[derive(Copy, Clone)]
108pub struct PyCodeObject {
109 pub ob_base: PyObject,
110 pub co_argcount: c_int,
111 pub co_posonlyargcount: c_int,
112 pub co_kwonlyargcount: c_int,
113 pub co_nlocals: c_int,
114 pub co_stacksize: c_int,
115 pub co_flags: c_int,
116 pub co_firstlineno: c_int,
117 pub co_code: *mut PyObject,
118 pub co_consts: *mut PyObject,
119 pub co_names: *mut PyObject,
120 pub co_varnames: *mut PyObject,
121 pub co_freevars: *mut PyObject,
122 pub co_cellvars: *mut PyObject,
123 pub co_cell2arg: *mut Py_ssize_t,
124 pub co_filename: *mut PyObject,
125 pub co_name: *mut PyObject,
126 #[cfg(not(Py_3_10))]
127 pub co_lnotab: *mut PyObject,
128 #[cfg(Py_3_10)]
129 pub co_linetable: *mut PyObject,
130 pub co_zombieframe: *mut c_void,
131 pub co_weakreflist: *mut PyObject,
132 pub co_extra: *mut c_void,
133 pub co_opcache_map: *mut c_uchar,
134 pub co_opcache: *mut _PyOpcache,
135 pub co_opcache_flag: c_int,
136 pub co_opcache_size: c_uchar,
137}
138
139#[cfg(all(not(PyPy), Py_3_11))]
140#[repr(C)]
141#[derive(Copy, Clone)]
142pub struct PyCodeObject {
143 pub ob_base: PyVarObject,
144 pub co_consts: *mut PyObject,
145 pub co_names: *mut PyObject,
146 pub co_exceptiontable: *mut PyObject,
147 pub co_flags: c_int,
148 #[cfg(not(Py_3_12))]
149 pub co_warmup: c_int,
150
151 pub co_argcount: c_int,
152 pub co_posonlyargcount: c_int,
153 pub co_kwonlyargcount: c_int,
154 pub co_stacksize: c_int,
155 pub co_firstlineno: c_int,
156
157 pub co_nlocalsplus: c_int,
158 #[cfg(Py_3_12)]
159 pub co_framesize: c_int,
160 pub co_nlocals: c_int,
161 #[cfg(not(Py_3_12))]
162 pub co_nplaincellvars: c_int,
163 pub co_ncellvars: c_int,
164 pub co_nfreevars: c_int,
165 #[cfg(Py_3_12)]
166 pub co_version: u32,
167
168 pub co_localsplusnames: *mut PyObject,
169 pub co_localspluskinds: *mut PyObject,
170 pub co_filename: *mut PyObject,
171 pub co_name: *mut PyObject,
172 pub co_qualname: *mut PyObject,
173 pub co_linetable: *mut PyObject,
174 pub co_weakreflist: *mut PyObject,
175 #[cfg(not(Py_3_12))]
176 pub _co_code: *mut PyObject,
177 #[cfg(not(Py_3_12))]
178 pub _co_linearray: *mut c_char,
179 #[cfg(Py_3_12)]
180 pub _co_cached: *mut _PyCoCached,
181 #[cfg(Py_3_12)]
182 pub _co_instrumentation_version: u64,
183 #[cfg(Py_3_12)]
184 pub _co_monitoring: *mut _PyCoMonitoringData,
185 pub _co_firsttraceable: c_int,
186 pub co_extra: *mut c_void,
187 pub co_code_adaptive: [c_char; 1],
188}
189
190#[cfg(PyPy)]
191#[repr(C)]
192#[derive(Copy, Clone)]
193pub struct PyCodeObject {
194 pub ob_base: PyObject,
195 pub co_name: *mut PyObject,
196 pub co_filename: *mut PyObject,
197 pub co_argcount: c_int,
198 pub co_flags: c_int,
199}
200
201/* Masks for co_flags */
202pub const CO_OPTIMIZED: c_int = 0x0001;
203pub const CO_NEWLOCALS: c_int = 0x0002;
204pub const CO_VARARGS: c_int = 0x0004;
205pub const CO_VARKEYWORDS: c_int = 0x0008;
206pub const CO_NESTED: c_int = 0x0010;
207pub const CO_GENERATOR: c_int = 0x0020;
208/* The CO_NOFREE flag is set if there are no free or cell variables.
209 This information is redundant, but it allows a single flag test
210 to determine whether there is any extra work to be done when the
211 call frame it setup.
212*/
213pub const CO_NOFREE: c_int = 0x0040;
214/* The CO_COROUTINE flag is set for coroutine functions (defined with
215``async def`` keywords) */
216pub const CO_COROUTINE: c_int = 0x0080;
217pub const CO_ITERABLE_COROUTINE: c_int = 0x0100;
218pub const CO_ASYNC_GENERATOR: c_int = 0x0200;
219
220pub const CO_FUTURE_DIVISION: c_int = 0x2000;
221pub const CO_FUTURE_ABSOLUTE_IMPORT: c_int = 0x4000; /* do absolute imports by default */
222pub const CO_FUTURE_WITH_STATEMENT: c_int = 0x8000;
223pub const CO_FUTURE_PRINT_FUNCTION: c_int = 0x1_0000;
224pub const CO_FUTURE_UNICODE_LITERALS: c_int = 0x2_0000;
225
226pub const CO_FUTURE_BARRY_AS_BDFL: c_int = 0x4_0000;
227pub const CO_FUTURE_GENERATOR_STOP: c_int = 0x8_0000;
228// skipped CO_FUTURE_ANNOTATIONS
229// skipped CO_CELL_NOT_AN_ARG
230
231pub const CO_MAXBLOCKS: usize = 20;
232
233#[cfg_attr(windows, link(name = "pythonXY"))]
234extern "C" {
235 pub static mut PyCode_Type: PyTypeObject;
236}
237
238#[inline]
239#[cfg(not(PyPy))]
240pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {
241 (Py_TYPE(ob:op) == addr_of_mut!(PyCode_Type)) as c_int
242}
243
244#[inline]
245#[cfg(all(not(PyPy), Py_3_10, not(Py_3_11)))]
246pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t {
247 crate::PyTuple_GET_SIZE((*op).co_freevars)
248}
249
250#[inline]
251#[cfg(all(not(Py_3_10), Py_3_11, not(PyPy)))]
252pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> c_int {
253 (*op).co_nfreevars
254}
255
256extern "C" {
257 #[cfg(PyPy)]
258 #[link_name = "PyPyCode_Check"]
259 pub fn PyCode_Check(op: *mut PyObject) -> c_int;
260
261 #[cfg(PyPy)]
262 #[link_name = "PyPyCode_GetNumFree"]
263 pub fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t;
264}
265
266extern "C" {
267 #[cfg_attr(PyPy, link_name = "PyPyCode_New")]
268 pub fn PyCode_New(
269 argcount: c_int,
270 kwonlyargcount: c_int,
271 nlocals: c_int,
272 stacksize: c_int,
273 flags: c_int,
274 code: *mut PyObject,
275 consts: *mut PyObject,
276 names: *mut PyObject,
277 varnames: *mut PyObject,
278 freevars: *mut PyObject,
279 cellvars: *mut PyObject,
280 filename: *mut PyObject,
281 name: *mut PyObject,
282 firstlineno: c_int,
283 lnotab: *mut PyObject,
284 ) -> *mut PyCodeObject;
285 #[cfg(Py_3_8)]
286 pub fn PyCode_NewWithPosOnlyArgs(
287 argcount: c_int,
288 posonlyargcount: c_int,
289 kwonlyargcount: c_int,
290 nlocals: c_int,
291 stacksize: c_int,
292 flags: c_int,
293 code: *mut PyObject,
294 consts: *mut PyObject,
295 names: *mut PyObject,
296 varnames: *mut PyObject,
297 freevars: *mut PyObject,
298 cellvars: *mut PyObject,
299 filename: *mut PyObject,
300 name: *mut PyObject,
301 firstlineno: c_int,
302 lnotab: *mut PyObject,
303 ) -> *mut PyCodeObject;
304 #[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")]
305 pub fn PyCode_NewEmpty(
306 filename: *const c_char,
307 funcname: *const c_char,
308 firstlineno: c_int,
309 ) -> *mut PyCodeObject;
310 pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int;
311 // skipped PyCodeAddressRange "for internal use only"
312 // skipped _PyCode_CheckLineNumber
313 // skipped _PyCode_ConstantKey
314 pub fn PyCode_Optimize(
315 code: *mut PyObject,
316 consts: *mut PyObject,
317 names: *mut PyObject,
318 lnotab: *mut PyObject,
319 ) -> *mut PyObject;
320 pub fn _PyCode_GetExtra(
321 code: *mut PyObject,
322 index: Py_ssize_t,
323 extra: *const *mut c_void,
324 ) -> c_int;
325 pub fn _PyCode_SetExtra(code: *mut PyObject, index: Py_ssize_t, extra: *mut c_void) -> c_int;
326}
327