1use crate::{vectorcallfunc, PyObject, Py_TYPE, Py_ssize_t};
2#[cfg(all(any(not(PyPy), not(Py_3_11)), not(Py_3_12)))]
3use std::ffi::c_char;
4#[cfg(not(Py_3_11))]
5use std::ffi::c_int;
6
7#[cfg(not(any(PyPy, GraalPy)))]
8use crate::{
9 PyListObject, PyList_Check, PyList_GET_ITEM, PyList_GET_SIZE, PyTupleObject, PyTuple_GET_ITEM,
10 PyTuple_GET_SIZE,
11};
12
13#[cfg(not(Py_3_11))]
14use crate::Py_buffer;
15
16#[cfg(not(any(PyPy, Py_3_11)))]
17use crate::{PyCallable_Check, PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL};
18#[cfg(not(any(Py_3_12, PyPy)))]
19use crate::{PyThreadState, PyThreadState_GET, PyTuple_Check};
20use libc::size_t;
21
22#[cfg(not(Py_3_12))]
26extern_libpython! {
27 #[cfg(not(PyPy))]
28 fn _Py_CheckFunctionResult(
29 tstate: *mut PyThreadState,
30 callable: *mut PyObject,
31 result: *mut PyObject,
32 where_: *const c_char,
33 ) -> *mut PyObject;
34
35 #[cfg(not(PyPy))]
36 fn _PyObject_MakeTpCall(
37 tstate: *mut PyThreadState,
38 callable: *mut PyObject,
39 args: *const *mut PyObject,
40 nargs: Py_ssize_t,
41 keywords: *mut PyObject,
42 ) -> *mut PyObject;
43}
44
45#[cfg(not(Py_3_12))]
46const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = (1 as size_t)
47 .checked_shl((8 * std::mem::size_of::<size_t>() - 1) as u32)
48 .expect("size_t should fit the flag bits");
49
50#[cfg(Py_3_12)] use crate::PY_VECTORCALL_ARGUMENTS_OFFSET;
52
53#[inline(always)]
54pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t {
55 let n = n & !PY_VECTORCALL_ARGUMENTS_OFFSET;
56 n.try_into().expect("cannot fail due to mask")
57}
58
59#[cfg(any(PyPy, Py_3_11))]
60extern_libpython! {
61 #[cfg_attr(PyPy, link_name = "PyPyVectorcall_Function")]
62 pub fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc>;
63}
64
65#[cfg(not(any(PyPy, Py_3_11)))]
66#[inline(always)]
67pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc> {
68 assert!(!callable.is_null());
69 let tp = crate::Py_TYPE(callable);
70 if PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL) == 0 {
71 return None;
72 }
73 assert!(PyCallable_Check(callable) > 0);
74 let offset = (*tp).tp_vectorcall_offset;
75 assert!(offset > 0);
76 let ptr = callable.cast::<c_char>().offset(offset).cast();
77 *ptr
78}
79
80#[cfg(not(Py_3_12))]
81#[cfg(not(PyPy))]
82#[inline(always)]
83unsafe fn _PyObject_VectorcallTstate(
84 tstate: *mut PyThreadState,
85 callable: *mut PyObject,
86 args: *const *mut PyObject,
87 nargsf: size_t,
88 kwnames: *mut PyObject,
89) -> *mut PyObject {
90 assert!(kwnames.is_null() || PyTuple_Check(kwnames) > 0);
91 assert!(!args.is_null() || PyVectorcall_NARGS(nargsf) == 0);
92
93 match PyVectorcall_Function(callable) {
94 None => {
95 let nargs = PyVectorcall_NARGS(nargsf);
96 _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames)
97 }
98 Some(func) => {
99 let res = func(callable, args, nargsf, kwnames);
100 _Py_CheckFunctionResult(tstate, callable, res, std::ptr::null_mut())
101 }
102 }
103}
104
105#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] #[inline(always)]
107pub unsafe fn PyObject_Vectorcall(
108 callable: *mut PyObject,
109 args: *const *mut PyObject,
110 nargsf: size_t,
111 kwnames: *mut PyObject,
112) -> *mut PyObject {
113 _PyObject_VectorcallTstate(PyThreadState_GET(), callable, args, nargsf, kwnames)
114}
115
116extern_libpython! {
117 #[cfg_attr(
118 all(not(any(PyPy, GraalPy)), not(Py_3_9)),
119 link_name = "_PyObject_VectorcallDict"
120 )]
121 #[cfg_attr(all(PyPy, not(Py_3_9)), link_name = "_PyPyObject_VectorcallDict")]
122 #[cfg_attr(all(PyPy, Py_3_9), link_name = "PyPyObject_VectorcallDict")]
123 pub fn PyObject_VectorcallDict(
124 callable: *mut PyObject,
125 args: *const *mut PyObject,
126 nargsf: size_t,
127 kwdict: *mut PyObject,
128 ) -> *mut PyObject;
129}
130
131#[cfg(not(any(Py_3_12, PyPy)))]
132#[inline(always)]
133pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
134 assert!(!arg.is_null());
135 let args_array = [std::ptr::null_mut(), arg];
136 let args = args_array.as_ptr().offset(1); let tstate = PyThreadState_GET();
138 let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
139 _PyObject_VectorcallTstate(tstate, func, args, nargsf, std::ptr::null_mut())
140}
141
142extern_libpython! {
143 #[cfg(any(Py_3_12, PyPy))]
144 #[cfg_attr(PyPy, link_name = "PyPyObject_CallOneArg")]
145 pub fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject;
146}
147
148#[cfg(all(Py_3_9, not(PyPy)))]
149#[inline(always)]
150pub unsafe fn PyObject_CallMethodNoArgs(
151 self_: *mut PyObject,
152 name: *mut PyObject,
153) -> *mut PyObject {
154 crate::PyObject_VectorcallMethod(
155 name,
156 &self_,
157 1 | PY_VECTORCALL_ARGUMENTS_OFFSET,
158 std::ptr::null_mut(),
159 )
160}
161
162#[cfg(all(Py_3_9, not(PyPy)))]
163#[inline(always)]
164pub unsafe fn PyObject_CallMethodOneArg(
165 self_: *mut PyObject,
166 name: *mut PyObject,
167 arg: *mut PyObject,
168) -> *mut PyObject {
169 let args = [self_, arg];
170 assert!(!arg.is_null());
171 crate::PyObject_VectorcallMethod(
172 name,
173 args.as_ptr(),
174 2 | PY_VECTORCALL_ARGUMENTS_OFFSET,
175 std::ptr::null_mut(),
176 )
177}
178
179extern_libpython! {
180 #[cfg_attr(PyPy, link_name = "PyPyObject_LengthHint")]
181 pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t;
182
183 #[cfg(not(Py_3_11))] #[cfg(all(Py_3_9, not(PyPy)))]
185 pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int;
186}
187
188#[cfg(not(any(Py_3_9, PyPy)))]
189#[inline]
190pub unsafe fn PyObject_CheckBuffer(o: *mut PyObject) -> c_int {
191 let tp_as_buffer = (*crate::Py_TYPE(o)).tp_as_buffer;
192 (!tp_as_buffer.is_null() && (*tp_as_buffer).bf_getbuffer.is_some()) as c_int
193}
194
195#[cfg(not(Py_3_11))] extern_libpython! {
197 #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")]
198 pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int;
199 #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")]
200 pub fn PyBuffer_GetPointer(
201 view: *mut Py_buffer,
202 indices: *mut Py_ssize_t,
203 ) -> *mut std::ffi::c_void;
204 #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")]
205 pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t;
206 #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")]
207 pub fn PyBuffer_ToContiguous(
208 buf: *mut std::ffi::c_void,
209 view: *mut Py_buffer,
210 len: Py_ssize_t,
211 order: c_char,
212 ) -> c_int;
213 #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")]
214 pub fn PyBuffer_FromContiguous(
215 view: *mut Py_buffer,
216 buf: *mut std::ffi::c_void,
217 len: Py_ssize_t,
218 order: c_char,
219 ) -> c_int;
220 pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int;
221 #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")]
222 pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int;
223 pub fn PyBuffer_FillContiguousStrides(
224 ndims: c_int,
225 shape: *mut Py_ssize_t,
226 strides: *mut Py_ssize_t,
227 itemsize: c_int,
228 fort: c_char,
229 );
230 #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")]
231 pub fn PyBuffer_FillInfo(
232 view: *mut Py_buffer,
233 o: *mut PyObject,
234 buf: *mut std::ffi::c_void,
235 len: Py_ssize_t,
236 readonly: c_int,
237 flags: c_int,
238 ) -> c_int;
239 #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")]
240 pub fn PyBuffer_Release(view: *mut Py_buffer);
241}
242
243#[inline(always)]
244pub unsafe fn PySequence_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
245 (*(*Py_TYPE(seq)).tp_as_sequence).sq_item.unwrap_unchecked()(seq, i)
246}
247
248#[inline(always)]
249#[cfg(not(any(PyPy, GraalPy)))]
250pub unsafe fn PySequence_Fast_GET_SIZE(seq: *mut PyObject) -> Py_ssize_t {
251 if PyList_Check(seq) == 1 {
252 PyList_GET_SIZE(seq)
253 } else {
254 PyTuple_GET_SIZE(seq)
255 }
256}
257
258#[inline(always)]
259#[cfg(not(any(PyPy, GraalPy)))]
260pub unsafe fn PySequence_Fast_GET_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
261 if PyList_Check(seq) == 1 {
262 PyList_GET_ITEM(seq, i)
263 } else {
264 PyTuple_GET_ITEM(seq, i)
265 }
266}
267
268#[inline(always)]
269#[cfg(not(any(PyPy, GraalPy)))]
270pub unsafe fn PySequence_Fast_ITEMS(seq: *mut PyObject) -> *mut *mut PyObject {
271 if PyList_Check(seq) == 1 {
272 (*seq.cast::<PyListObject>()).ob_item
273 } else {
274 (*seq.cast::<PyTupleObject>()).ob_item.as_mut_ptr()
275 }
276}