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 #[cfg_attr(PyPy, link_name = "PyPyArg_Parse")]
9 pub fn PyArg_Parse(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int;
10 #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTuple")]
11 pub fn PyArg_ParseTuple(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int;
12 #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTupleAndKeywords")]
13 pub fn PyArg_ParseTupleAndKeywords(
14 arg1: *mut PyObject,
15 arg2: *mut PyObject,
16 arg3: *const c_char,
17 #[cfg(not(Py_3_13))] arg4: *mut *mut c_char,
18 #[cfg(Py_3_13)] arg4: *const *const c_char,
19 ...
20 ) -> c_int;
21
22 pub fn PyArg_ValidateKeywordArguments(arg1: *mut PyObject) -> c_int;
26 #[cfg_attr(PyPy, link_name = "PyPyArg_UnpackTuple")]
27 pub fn PyArg_UnpackTuple(
28 arg1: *mut PyObject,
29 arg2: *const c_char,
30 arg3: Py_ssize_t,
31 arg4: Py_ssize_t,
32 ...
33 ) -> c_int;
34
35 #[cfg_attr(PyPy, link_name = "PyPy_BuildValue")]
36 pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject;
37 #[cfg(Py_3_13)]
40 pub fn PyModule_Add(module: *mut PyObject, name: *const c_char, value: *mut PyObject) -> c_int;
41 #[cfg(Py_3_10)]
42 #[cfg_attr(PyPy, link_name = "PyPyModule_AddObjectRef")]
43 pub fn PyModule_AddObjectRef(
44 module: *mut PyObject,
45 name: *const c_char,
46 value: *mut PyObject,
47 ) -> c_int;
48 #[cfg_attr(PyPy, link_name = "PyPyModule_AddObject")]
49 pub fn PyModule_AddObject(
50 module: *mut PyObject,
51 name: *const c_char,
52 value: *mut PyObject,
53 ) -> c_int;
54 #[cfg_attr(PyPy, link_name = "PyPyModule_AddIntConstant")]
55 pub fn PyModule_AddIntConstant(
56 module: *mut PyObject,
57 name: *const c_char,
58 value: c_long,
59 ) -> c_int;
60 #[cfg_attr(PyPy, link_name = "PyPyModule_AddStringConstant")]
61 pub fn PyModule_AddStringConstant(
62 module: *mut PyObject,
63 name: *const c_char,
64 value: *const c_char,
65 ) -> c_int;
66 #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
67 #[cfg_attr(PyPy, link_name = "PyPyModule_AddType")]
68 pub fn PyModule_AddType(
69 module: *mut PyObject,
70 type_: *mut crate::object::PyTypeObject,
71 ) -> c_int;
72 pub fn PyModule_SetDocString(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
75 pub fn PyModule_AddFunctions(arg1: *mut PyObject, arg2: *mut PyMethodDef) -> c_int;
76 #[cfg_attr(PyPy, link_name = "PyPyModule_ExecDef")]
77 pub fn PyModule_ExecDef(module: *mut PyObject, def: *mut PyModuleDef) -> c_int;
78}
79
80pub const Py_CLEANUP_SUPPORTED: i32 = 0x2_0000;
81
82pub const PYTHON_API_VERSION: i32 = 1013;
83pub const PYTHON_ABI_VERSION: i32 = 3;
84
85extern_libpython! {
86
87 #[cfg_attr(PyPy, link_name = "PyPyModule_Create2")]
88 pub fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject;
89}
90
91#[inline]
92pub unsafe fn PyModule_Create(module: *mut PyModuleDef) -> *mut PyObject {
93 PyModule_Create2(
94 module,
95 if cfg!(Py_LIMITED_API) {
96 PYTHON_ABI_VERSION
97 } else {
98 PYTHON_API_VERSION
99 },
100 )
101}
102
103extern_libpython! {
104
105 #[cfg_attr(PyPy, link_name = "PyPyModule_FromDefAndSpec2")]
106 pub fn PyModule_FromDefAndSpec2(
107 def: *mut PyModuleDef,
108 spec: *mut PyObject,
109 module_api_version: c_int,
110 ) -> *mut PyObject;
111
112}
113
114#[inline]
115pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject) -> *mut PyObject {
116 PyModule_FromDefAndSpec2(
117 def,
118 spec,
119 if cfg!(Py_LIMITED_API) {
120 PYTHON_ABI_VERSION
121 } else {
122 PYTHON_API_VERSION
123 },
124 )
125}
126
127#[cfg(Py_3_15)]
128#[repr(C)]
129pub struct PyABIInfo {
130 pub abiinfo_major_version: u8,
131 pub abiinfo_minor_version: u8,
132 pub flags: u16,
133 pub build_version: u32,
134 pub abi_version: u32,
135}
136#[cfg(Py_3_15)]
137pub const PyABIInfo_STABLE: u16 = 0x0001;
138#[cfg(Py_3_15)]
139pub const PyABIInfo_GIL: u16 = 0x0002;
140#[cfg(Py_3_15)]
141pub const PyABIInfo_FREETHREADED: u16 = 0x0004;
142#[cfg(Py_3_15)]
143pub const PyABIInfo_INTERNAL: u16 = 0x0008;
144
145#[cfg(Py_3_15)]
146pub const PyABIInfo_FREETHREADING_AGNOSTIC: u16 = PyABIInfo_GIL | PyABIInfo_FREETHREADED;
147
148#[cfg(Py_3_15)]
149extern_libpython! {
150 pub fn PyABIInfo_Check(info: *mut PyABIInfo, module_name: *const c_char) -> c_int;
151}
152
153#[cfg(all(Py_LIMITED_API, Py_3_15))]
154const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = PyABIInfo_STABLE;
155#[cfg(all(Py_3_15, not(Py_LIMITED_API)))]
156const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = 0;
157
158#[cfg(all(Py_3_15, Py_GIL_DISABLED))]
161const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_FREETHREADED;
162#[cfg(all(Py_3_15, not(Py_GIL_DISABLED)))]
163const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_GIL;
164
165#[cfg(Py_3_15)]
166const _PyABIInfo_DEFAULT_FLAG_INTERNAL: u16 = 0;
168
169#[cfg(Py_3_15)]
170pub const PyABIInfo_DEFAULT_FLAGS: u16 =
171 _PyABIInfo_DEFAULT_FLAG_STABLE | _PyABIInfo_DEFAULT_FLAG_FT | _PyABIInfo_DEFAULT_FLAG_INTERNAL;
172
173#[cfg(Py_3_15)]
174pub const _PyABIInfo_DEFAULT: PyABIInfo = PyABIInfo {
176 abiinfo_major_version: 1,
177 abiinfo_minor_version: 0,
178 flags: PyABIInfo_DEFAULT_FLAGS,
179 build_version: 0,
180 abi_version: 0,
181};
182
183#[cfg(Py_3_15)]
184#[macro_export]
185macro_rules! PyABIInfo_VAR {
186 ($name:ident) => {
187 static mut $name: pyo3_ffi::PyABIInfo = pyo3_ffi::_PyABIInfo_DEFAULT;
188 };
189}