Skip to main content

pyo3_ffi/cpython/
code.rs

1use crate::object::*;
2use crate::pyport::Py_ssize_t;
3
4#[cfg(not(GraalPy))]
5use crate::PyCodeObject;
6#[cfg(not(GraalPy))]
7use std::ffi::c_char;
8use std::ffi::{c_int, c_void};
9
10// skipped private _PY_MONITORING_LOCAL_EVENTS
11// skipped private _PY_MONITORING_UNGROUPED_EVENTS
12// skipped private _PY_MONITORING_EVENTS
13
14// skipped private _PyLocalMonitors
15// skipped private _Py_GlobalMonitors
16
17// skipped private _Py_CODEUNIT
18
19// skipped private _Py_OPCODE
20// skipped private _Py_OPARG
21
22// skipped private _py_make_codeunit
23
24// skipped private _py_set_opcode
25
26// skipped private _Py_MAKE_CODEUNIT
27// skipped private _Py_SET_OPCODE
28
29// skipped private _PyCoCached
30// skipped private _PyCoLineInstrumentationData
31// skipped private _PyCoMonitoringData
32
33// skipped private _PyExecutorArray
34
35/* Masks for co_flags */
36pub const CO_OPTIMIZED: c_int = 0x0001;
37pub const CO_NEWLOCALS: c_int = 0x0002;
38pub const CO_VARARGS: c_int = 0x0004;
39pub const CO_VARKEYWORDS: c_int = 0x0008;
40pub const CO_NESTED: c_int = 0x0010;
41pub const CO_GENERATOR: c_int = 0x0020;
42/* The CO_NOFREE flag is set if there are no free or cell variables.
43   This information is redundant, but it allows a single flag test
44   to determine whether there is any extra work to be done when the
45   call frame it setup.
46*/
47pub const CO_NOFREE: c_int = 0x0040;
48/* The CO_COROUTINE flag is set for coroutine functions (defined with
49``async def`` keywords) */
50pub const CO_COROUTINE: c_int = 0x0080;
51pub const CO_ITERABLE_COROUTINE: c_int = 0x0100;
52pub const CO_ASYNC_GENERATOR: c_int = 0x0200;
53
54pub const CO_FUTURE_DIVISION: c_int = 0x2000;
55pub const CO_FUTURE_ABSOLUTE_IMPORT: c_int = 0x4000; /* do absolute imports by default */
56pub const CO_FUTURE_WITH_STATEMENT: c_int = 0x8000;
57pub const CO_FUTURE_PRINT_FUNCTION: c_int = 0x1_0000;
58pub const CO_FUTURE_UNICODE_LITERALS: c_int = 0x2_0000;
59
60pub const CO_FUTURE_BARRY_AS_BDFL: c_int = 0x4_0000;
61pub const CO_FUTURE_GENERATOR_STOP: c_int = 0x8_0000;
62// skipped CO_FUTURE_ANNOTATIONS
63// skipped CO_CELL_NOT_AN_ARG
64
65pub const CO_MAXBLOCKS: usize = 20;
66
67#[cfg(not(PyPy))]
68extern_libpython! {
69    pub static mut PyCode_Type: PyTypeObject;
70}
71
72#[inline]
73#[cfg(not(PyPy))]
74pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {
75    (Py_TYPE(op) == &raw mut PyCode_Type) as c_int
76}
77
78extern_libpython! {
79    #[cfg(PyPy)]
80    #[link_name = "PyPyCode_Check"]
81    pub fn PyCode_Check(op: *mut PyObject) -> c_int;
82}
83
84// skipped PyCode_GetNumFree (requires knowledge of code object layout)
85
86extern_libpython! {
87    #[cfg(not(GraalPy))]
88    #[cfg_attr(PyPy, link_name = "PyPyCode_New")]
89    pub fn PyCode_New(
90        argcount: c_int,
91        kwonlyargcount: c_int,
92        nlocals: c_int,
93        stacksize: c_int,
94        flags: c_int,
95        code: *mut PyObject,
96        consts: *mut PyObject,
97        names: *mut PyObject,
98        varnames: *mut PyObject,
99        freevars: *mut PyObject,
100        cellvars: *mut PyObject,
101        filename: *mut PyObject,
102        name: *mut PyObject,
103        firstlineno: c_int,
104        lnotab: *mut PyObject,
105    ) -> *mut PyCodeObject;
106    #[cfg(not(GraalPy))]
107    pub fn PyCode_NewWithPosOnlyArgs(
108        argcount: c_int,
109        posonlyargcount: c_int,
110        kwonlyargcount: c_int,
111        nlocals: c_int,
112        stacksize: c_int,
113        flags: c_int,
114        code: *mut PyObject,
115        consts: *mut PyObject,
116        names: *mut PyObject,
117        varnames: *mut PyObject,
118        freevars: *mut PyObject,
119        cellvars: *mut PyObject,
120        filename: *mut PyObject,
121        name: *mut PyObject,
122        firstlineno: c_int,
123        lnotab: *mut PyObject,
124    ) -> *mut PyCodeObject;
125    #[cfg(not(GraalPy))]
126    #[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")]
127    pub fn PyCode_NewEmpty(
128        filename: *const c_char,
129        funcname: *const c_char,
130        firstlineno: c_int,
131    ) -> *mut PyCodeObject;
132    #[cfg(not(GraalPy))]
133    pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int;
134    // skipped PyCodeAddressRange "for internal use only"
135    // skipped _PyCode_CheckLineNumber
136    // skipped _PyCode_ConstantKey
137    pub fn PyCode_Optimize(
138        code: *mut PyObject,
139        consts: *mut PyObject,
140        names: *mut PyObject,
141        lnotab: *mut PyObject,
142    ) -> *mut PyObject;
143    pub fn _PyCode_GetExtra(
144        code: *mut PyObject,
145        index: Py_ssize_t,
146        extra: *const *mut c_void,
147    ) -> c_int;
148    pub fn _PyCode_SetExtra(code: *mut PyObject, index: Py_ssize_t, extra: *mut c_void) -> c_int;
149}