Skip to main content

pyo3_ffi/cpython/
abstract_.rs

1use crate::{vectorcallfunc, PyObject, Py_TYPE, Py_ssize_t};
2#[cfg(all(any(not(PyPy), not(Py_3_11)), not(Py_3_12)))]
3use core::ffi::c_char;
4#[cfg(not(Py_3_11))]
5use core::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// skipped private _PyObject_CallMethodId
23// skipped private _PyStack_AsDict
24
25#[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 * core::mem::size_of::<size_t>() - 1) as u32)
48    .expect("size_t should fit the flag bits");
49
50#[cfg(Py_3_12)] // public API from 3.12
51use 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, core::ptr::null_mut())
101        }
102    }
103}
104
105#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] // exported as a function from 3.11, see abstract.rs
106#[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 = [core::ptr::null_mut(), arg];
136    let args = args_array.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET
137    let tstate = PyThreadState_GET();
138    let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
139    _PyObject_VectorcallTstate(tstate, func, args, nargsf, core::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        core::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        core::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))] // moved to src/buffer.rs from 3.11
184    #[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))] // moved to src/buffer.rs from 3.11
196extern_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 core::ffi::c_void;
204    #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")]
205    #[cfg(not(Py_3_9))] // return value changed from c_int to Py_ssize_t in 3.9
206    pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> c_int;
207    #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")]
208    #[cfg(Py_3_9)]
209    pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t;
210    #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")]
211    pub fn PyBuffer_ToContiguous(
212        buf: *mut core::ffi::c_void,
213        view: *mut Py_buffer,
214        len: Py_ssize_t,
215        order: c_char,
216    ) -> c_int;
217    #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")]
218    pub fn PyBuffer_FromContiguous(
219        view: *mut Py_buffer,
220        buf: *mut core::ffi::c_void,
221        len: Py_ssize_t,
222        order: c_char,
223    ) -> c_int;
224    pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int;
225    #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")]
226    pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int;
227    pub fn PyBuffer_FillContiguousStrides(
228        ndims: c_int,
229        shape: *mut Py_ssize_t,
230        strides: *mut Py_ssize_t,
231        itemsize: c_int,
232        fort: c_char,
233    );
234    #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")]
235    pub fn PyBuffer_FillInfo(
236        view: *mut Py_buffer,
237        o: *mut PyObject,
238        buf: *mut core::ffi::c_void,
239        len: Py_ssize_t,
240        readonly: c_int,
241        flags: c_int,
242    ) -> c_int;
243    #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")]
244    pub fn PyBuffer_Release(view: *mut Py_buffer);
245}
246
247#[inline(always)]
248pub unsafe fn PySequence_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
249    (*(*Py_TYPE(seq)).tp_as_sequence).sq_item.unwrap_unchecked()(seq, i)
250}
251
252#[inline(always)]
253#[cfg(not(any(PyPy, GraalPy)))]
254pub unsafe fn PySequence_Fast_GET_SIZE(seq: *mut PyObject) -> Py_ssize_t {
255    if PyList_Check(seq) == 1 {
256        PyList_GET_SIZE(seq)
257    } else {
258        PyTuple_GET_SIZE(seq)
259    }
260}
261
262#[inline(always)]
263#[cfg(not(any(PyPy, GraalPy)))]
264pub unsafe fn PySequence_Fast_GET_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
265    if PyList_Check(seq) == 1 {
266        PyList_GET_ITEM(seq, i)
267    } else {
268        PyTuple_GET_ITEM(seq, i)
269    }
270}
271
272#[inline(always)]
273#[cfg(not(any(PyPy, GraalPy)))]
274pub unsafe fn PySequence_Fast_ITEMS(seq: *mut PyObject) -> *mut *mut PyObject {
275    if PyList_Check(seq) == 1 {
276        (*seq.cast::<PyListObject>()).ob_item
277    } else {
278        (*seq.cast::<PyTupleObject>()).ob_item.as_mut_ptr()
279    }
280}