Skip to main content

pyo3_ffi/cpython/
compile.rs

1#[cfg(not(any(PyPy, Py_3_10)))]
2use crate::object::PyObject;
3#[cfg(not(any(PyPy, Py_3_10)))]
4use crate::pyarena::*;
5#[cfg(not(any(PyPy, Py_3_10)))]
6use crate::pythonrun::*;
7#[cfg(not(any(PyPy, Py_3_10)))]
8use crate::PyCodeObject;
9use crate::INT_MAX;
10#[cfg(not(any(PyPy, Py_3_10)))]
11use std::ffi::c_char;
12use std::ffi::c_int;
13
14// skipped PyCF_MASK
15// skipped PyCF_MASK_OBSOLETE
16// skipped PyCF_SOURCE_IS_UTF8
17// skipped PyCF_DONT_IMPLY_DEDENT
18// skipped PyCF_ONLY_AST
19// skipped PyCF_IGNORE_COOKIE
20// skipped PyCF_TYPE_COMMENTS
21// skipped PyCF_ALLOW_TOP_LEVEL_AWAIT
22// skipped PyCF_OPTIMIZED_AST
23// skipped PyCF_COMPILE_MASK
24
25#[repr(C)]
26#[derive(Copy, Clone)]
27pub struct PyCompilerFlags {
28    pub cf_flags: c_int,
29    pub cf_feature_version: c_int,
30}
31
32// skipped _PyCompilerFlags_INIT
33
34// NB this type technically existed in the header until 3.13, when it was
35// moved to the internal CPython headers.
36//
37// We choose not to expose it in the public API past 3.10, as it is
38// not used in the public API past that point.
39#[cfg(not(any(PyPy, GraalPy, Py_3_10)))]
40#[repr(C)]
41#[derive(Copy, Clone)]
42pub struct PyFutureFeatures {
43    pub ff_features: c_int,
44    pub ff_lineno: c_int,
45}
46
47// FIXME: these constants should probably be &CStr, if they are used at all
48
49pub const FUTURE_NESTED_SCOPES: &str = "nested_scopes";
50pub const FUTURE_GENERATORS: &str = "generators";
51pub const FUTURE_DIVISION: &str = "division";
52pub const FUTURE_ABSOLUTE_IMPORT: &str = "absolute_import";
53pub const FUTURE_WITH_STATEMENT: &str = "with_statement";
54pub const FUTURE_PRINT_FUNCTION: &str = "print_function";
55pub const FUTURE_UNICODE_LITERALS: &str = "unicode_literals";
56pub const FUTURE_BARRY_AS_BDFL: &str = "barry_as_FLUFL";
57pub const FUTURE_GENERATOR_STOP: &str = "generator_stop";
58pub const FUTURE_ANNOTATIONS: &str = "annotations";
59
60#[cfg(not(any(PyPy, GraalPy, Py_3_10)))]
61extern_libpython! {
62    pub fn PyNode_Compile(arg1: *mut _node, arg2: *const c_char) -> *mut PyCodeObject;
63
64    pub fn PyAST_CompileEx(
65        _mod: *mut _mod,
66        filename: *const c_char,
67        flags: *mut PyCompilerFlags,
68        optimize: c_int,
69        arena: *mut PyArena,
70    ) -> *mut PyCodeObject;
71
72    pub fn PyAST_CompileObject(
73        _mod: *mut _mod,
74        filename: *mut PyObject,
75        flags: *mut PyCompilerFlags,
76        optimize: c_int,
77        arena: *mut PyArena,
78    ) -> *mut PyCodeObject;
79
80    pub fn PyFuture_FromAST(_mod: *mut _mod, filename: *const c_char) -> *mut PyFutureFeatures;
81
82    pub fn PyFuture_FromASTObject(
83        _mod: *mut _mod,
84        filename: *mut PyObject,
85    ) -> *mut PyFutureFeatures;
86}
87
88pub const PY_INVALID_STACK_EFFECT: c_int = INT_MAX;
89
90extern_libpython! {
91
92    pub fn PyCompile_OpcodeStackEffect(opcode: c_int, oparg: c_int) -> c_int;
93
94    pub fn PyCompile_OpcodeStackEffectWithJump(opcode: c_int, oparg: c_int, jump: c_int) -> c_int;
95}