Skip to main content

pyforge_ffi/
modsupport.rs

1use crate::methodobject::PyMethodDef;
2use crate::moduleobject::PyModuleDef;
3use crate::object::PyObject;
4use crate::pyport::Py_ssize_t;
5use std::ffi::{c_char, c_int, c_long};
6
7extern_libpython! {
8    pub fn PyArg_Parse(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int;
9    pub fn PyArg_ParseTuple(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int;
10    pub fn PyArg_ParseTupleAndKeywords(
11        arg1: *mut PyObject,
12        arg2: *mut PyObject,
13        arg3: *const c_char,
14        #[cfg(not(Py_3_13))] arg4: *mut *mut c_char,
15        #[cfg(Py_3_13)] arg4: *const *const c_char,
16        ...
17    ) -> c_int;
18
19    // skipped PyArg_VaParse
20    // skipped PyArg_VaParseTupleAndKeywords
21
22    pub fn PyArg_ValidateKeywordArguments(arg1: *mut PyObject) -> c_int;
23    pub fn PyArg_UnpackTuple(
24        arg1: *mut PyObject,
25        arg2: *const c_char,
26        arg3: Py_ssize_t,
27        arg4: Py_ssize_t,
28        ...
29    ) -> c_int;
30
31    pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject;
32    // skipped Py_VaBuildValue
33
34    #[cfg(Py_3_13)]
35    pub fn PyModule_Add(module: *mut PyObject, name: *const c_char, value: *mut PyObject) -> c_int;
36    #[cfg(Py_3_10)]
37    pub fn PyModule_AddObjectRef(
38        module: *mut PyObject,
39        name: *const c_char,
40        value: *mut PyObject,
41    ) -> c_int;
42    pub fn PyModule_AddObject(
43        module: *mut PyObject,
44        name: *const c_char,
45        value: *mut PyObject,
46    ) -> c_int;
47    pub fn PyModule_AddIntConstant(
48        module: *mut PyObject,
49        name: *const c_char,
50        value: c_long,
51    ) -> c_int;
52    pub fn PyModule_AddStringConstant(
53        module: *mut PyObject,
54        name: *const c_char,
55        value: *const c_char,
56    ) -> c_int;
57    #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
58    pub fn PyModule_AddType(
59        module: *mut PyObject,
60        type_: *mut crate::object::PyTypeObject,
61    ) -> c_int;
62    // skipped PyModule_AddIntMacro
63    // skipped PyModule_AddStringMacro
64    pub fn PyModule_SetDocString(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
65    pub fn PyModule_AddFunctions(arg1: *mut PyObject, arg2: *mut PyMethodDef) -> c_int;
66    pub fn PyModule_ExecDef(module: *mut PyObject, def: *mut PyModuleDef) -> c_int;
67}
68
69pub const Py_CLEANUP_SUPPORTED: i32 = 0x2_0000;
70
71pub const PYTHON_API_VERSION: i32 = 1013;
72pub const PYTHON_ABI_VERSION: i32 = 3;
73
74extern_libpython! {
75
76    pub fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject;
77}
78
79#[inline]
80pub unsafe fn PyModule_Create(module: *mut PyModuleDef) -> *mut PyObject {
81    PyModule_Create2(
82        module,
83        if cfg!(Py_LIMITED_API) {
84            PYTHON_ABI_VERSION
85        } else {
86            PYTHON_API_VERSION
87        },
88    )
89}
90
91extern_libpython! {
92
93    pub fn PyModule_FromDefAndSpec2(
94        def: *mut PyModuleDef,
95        spec: *mut PyObject,
96        module_api_version: c_int,
97    ) -> *mut PyObject;
98
99}
100
101#[inline]
102pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject) -> *mut PyObject {
103    PyModule_FromDefAndSpec2(
104        def,
105        spec,
106        if cfg!(Py_LIMITED_API) {
107            PYTHON_ABI_VERSION
108        } else {
109            PYTHON_API_VERSION
110        },
111    )
112}
113
114#[cfg(Py_3_15)]
115#[repr(C)]
116pub struct PyABIInfo {
117    pub abiinfo_major_version: u8,
118    pub abiinfo_minor_version: u8,
119    pub flags: u16,
120    pub build_version: u32,
121    pub abi_version: u32,
122}
123#[cfg(Py_3_15)]
124pub const PyABIInfo_STABLE: u16 = 0x0001;
125#[cfg(Py_3_15)]
126pub const PyABIInfo_GIL: u16 = 0x0002;
127#[cfg(Py_3_15)]
128pub const PyABIInfo_FREETHREADED: u16 = 0x0004;
129#[cfg(Py_3_15)]
130pub const PyABIInfo_INTERNAL: u16 = 0x0008;
131
132#[cfg(Py_3_15)]
133pub const PyABIInfo_FREETHREADING_AGNOSTIC: u16 = PyABIInfo_GIL | PyABIInfo_FREETHREADED;
134
135#[cfg(Py_3_15)]
136extern_libpython! {
137    pub fn PyABIInfo_Check(info: *mut PyABIInfo, module_name: *const c_char) -> c_int;
138}
139
140#[cfg(all(Py_LIMITED_API, Py_3_15))]
141const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = PyABIInfo_STABLE;
142#[cfg(all(Py_3_15, not(Py_LIMITED_API)))]
143const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = 0;
144
145// skipped PyABIInfo_DEFAULT_ABI_VERSION: depends on Py_VERSION_HEX
146
147#[cfg(all(Py_3_15, Py_GIL_DISABLED))]
148const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_FREETHREADED;
149#[cfg(all(Py_3_15, not(Py_GIL_DISABLED)))]
150const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_GIL;
151
152#[cfg(Py_3_15)]
153// has an alternate definition if Py_BUILD_CORE is set, ignore that
154const _PyABIInfo_DEFAULT_FLAG_INTERNAL: u16 = 0;
155
156#[cfg(Py_3_15)]
157pub const PyABIInfo_DEFAULT_FLAGS: u16 =
158    _PyABIInfo_DEFAULT_FLAG_STABLE | _PyABIInfo_DEFAULT_FLAG_FT | _PyABIInfo_DEFAULT_FLAG_INTERNAL;
159
160#[cfg(Py_3_15)]
161// must be pub because it is used by PyABIInfo_VAR
162pub const _PyABIInfo_DEFAULT: PyABIInfo = PyABIInfo {
163    abiinfo_major_version: 1,
164    abiinfo_minor_version: 0,
165    flags: PyABIInfo_DEFAULT_FLAGS,
166    build_version: 0,
167    abi_version: 0,
168};
169
170#[cfg(Py_3_15)]
171#[macro_export]
172macro_rules! PyABIInfo_VAR {
173    ($name:ident) => {
174        static mut $name: pyo3_ffi::PyABIInfo = pyo3_ffi::_PyABIInfo_DEFAULT;
175    };
176}