| 1 | use crate::methodobject::PyMethodDef; |
| 2 | use crate::moduleobject::PyModuleDef; |
| 3 | use crate::object::PyObject; |
| 4 | use crate::pyport::Py_ssize_t; |
| 5 | use std::ffi::{c_char, c_int, c_long}; |
| 6 | |
| 7 | extern "C" { |
| 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 | // skipped PyArg_VaParse |
| 23 | // skipped PyArg_VaParseTupleAndKeywords |
| 24 | |
| 25 | 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 | // skipped Py_VaBuildValue |
| 38 | |
| 39 | #[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 | // skipped PyModule_AddIntMacro |
| 73 | // skipped PyModule_AddStringMacro |
| 74 | 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 | |
| 80 | pub const Py_CLEANUP_SUPPORTED: i32 = 0x2_0000; |
| 81 | |
| 82 | pub const PYTHON_API_VERSION: i32 = 1013; |
| 83 | pub const PYTHON_ABI_VERSION: i32 = 3; |
| 84 | |
| 85 | extern "C" { |
| 86 | #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] |
| 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] |
| 92 | pub 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 | |
| 103 | extern "C" { |
| 104 | #[cfg(py_sys_config = "Py_TRACE_REFS")] |
| 105 | fn PyModule_Create2TraceRefs(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject; |
| 106 | |
| 107 | #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] |
| 108 | #[cfg_attr(PyPy, link_name = "PyPyModule_FromDefAndSpec2")] |
| 109 | pub fn PyModule_FromDefAndSpec2( |
| 110 | def: *mut PyModuleDef, |
| 111 | spec: *mut PyObject, |
| 112 | module_api_version: c_int, |
| 113 | ) -> *mut PyObject; |
| 114 | |
| 115 | #[cfg(py_sys_config = "Py_TRACE_REFS")] |
| 116 | fn PyModule_FromDefAndSpec2TraceRefs( |
| 117 | def: *mut PyModuleDef, |
| 118 | spec: *mut PyObject, |
| 119 | module_api_version: c_int, |
| 120 | ) -> *mut PyObject; |
| 121 | } |
| 122 | |
| 123 | #[cfg(py_sys_config = "Py_TRACE_REFS")] |
| 124 | #[inline] |
| 125 | pub unsafe fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject { |
| 126 | PyModule_Create2TraceRefs(module, apiver) |
| 127 | } |
| 128 | |
| 129 | #[cfg(py_sys_config = "Py_TRACE_REFS")] |
| 130 | #[inline] |
| 131 | pub unsafe fn PyModule_FromDefAndSpec2( |
| 132 | def: *mut PyModuleDef, |
| 133 | spec: *mut PyObject, |
| 134 | module_api_version: c_int, |
| 135 | ) -> *mut PyObject { |
| 136 | PyModule_FromDefAndSpec2TraceRefs(def, spec, module_api_version) |
| 137 | } |
| 138 | |
| 139 | #[inline] |
| 140 | pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject) -> *mut PyObject { |
| 141 | PyModule_FromDefAndSpec2( |
| 142 | def, |
| 143 | spec, |
| 144 | if cfg!(Py_LIMITED_API) { |
| 145 | PYTHON_ABI_VERSION |
| 146 | } else { |
| 147 | PYTHON_API_VERSION |
| 148 | }, |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | #[cfg(Py_3_15)] |
| 153 | #[repr(C)] |
| 154 | pub struct PyABIInfo { |
| 155 | pub abiinfo_major_version: u8, |
| 156 | pub abiinfo_minor_version: u8, |
| 157 | pub flags: u16, |
| 158 | pub build_version: u32, |
| 159 | pub abi_version: u32, |
| 160 | } |
| 161 | #[cfg(Py_3_15)] |
| 162 | pub const PyABIInfo_STABLE: u16 = 0x0001; |
| 163 | #[cfg(Py_3_15)] |
| 164 | pub const PyABIInfo_GIL: u16 = 0x0002; |
| 165 | #[cfg(Py_3_15)] |
| 166 | pub const PyABIInfo_FREETHREADED: u16 = 0x0004; |
| 167 | #[cfg(Py_3_15)] |
| 168 | pub const PyABIInfo_INTERNAL: u16 = 0x0008; |
| 169 | |
| 170 | #[cfg(Py_3_15)] |
| 171 | pub const PyABIInfo_FREETHREADING_AGNOSTIC: u16 = PyABIInfo_GIL | PyABIInfo_FREETHREADED; |
| 172 | |
| 173 | #[cfg(Py_3_15)] |
| 174 | extern "C" { |
| 175 | pub fn PyABIInfo_Check(info: *mut PyABIInfo, module_name: *const c_char) -> c_int; |
| 176 | } |
| 177 | |
| 178 | #[cfg(all(Py_LIMITED_API, Py_3_15))] |
| 179 | const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = PyABIInfo_STABLE; |
| 180 | #[cfg(all(Py_3_15, not(Py_LIMITED_API)))] |
| 181 | const _PyABIInfo_DEFAULT_FLAG_STABLE: u16 = 0; |
| 182 | |
| 183 | // skipped PyABIInfo_DEFAULT_ABI_VERSION: depends on Py_VERSION_HEX |
| 184 | |
| 185 | #[cfg(all(Py_3_15, Py_GIL_DISABLED))] |
| 186 | const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_FREETHREADED; |
| 187 | #[cfg(all(Py_3_15, not(Py_GIL_DISABLED)))] |
| 188 | const _PyABIInfo_DEFAULT_FLAG_FT: u16 = PyABIInfo_GIL; |
| 189 | |
| 190 | #[cfg(Py_3_15)] |
| 191 | // has an alternate definition if Py_BUILD_CORE is set, ignore that |
| 192 | const _PyABIInfo_DEFAULT_FLAG_INTERNAL: u16 = 0; |
| 193 | |
| 194 | #[cfg(Py_3_15)] |
| 195 | pub const PyABIInfo_DEFAULT_FLAGS: u16 = |
| 196 | _PyABIInfo_DEFAULT_FLAG_STABLE | _PyABIInfo_DEFAULT_FLAG_FT | _PyABIInfo_DEFAULT_FLAG_INTERNAL; |
| 197 | |
| 198 | #[cfg(Py_3_15)] |
| 199 | // must be pub because it is used by PyABIInfo_VAR |
| 200 | pub const _PyABIInfo_DEFAULT: PyABIInfo = PyABIInfo { |
| 201 | abiinfo_major_version: 1, |
| 202 | abiinfo_minor_version: 0, |
| 203 | flags: PyABIInfo_DEFAULT_FLAGS, |
| 204 | build_version: 0, |
| 205 | abi_version: 0, |
| 206 | }; |
| 207 | |
| 208 | #[cfg(Py_3_15)] |
| 209 | #[macro_export] |
| 210 | macro_rules! PyABIInfo_VAR { |
| 211 | ($name:ident) => { |
| 212 | static mut $name: pyo3_ffi::PyABIInfo = pyo3_ffi::_PyABIInfo_DEFAULT; |
| 213 | }; |
| 214 | } |