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(PyPy, link_name = "PyPyObject_VectorcallDict")]
118    pub fn PyObject_VectorcallDict(
119        callable: *mut PyObject,
120        args: *const *mut PyObject,
121        nargsf: size_t,
122        kwdict: *mut PyObject,
123    ) -> *mut PyObject;
124}
125
126#[cfg(not(any(Py_3_12, PyPy)))]
127#[inline(always)]
128pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
129    assert!(!arg.is_null());
130    let args_array = [core::ptr::null_mut(), arg];
131    let args = args_array.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET
132    let tstate = PyThreadState_GET();
133    let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
134    _PyObject_VectorcallTstate(tstate, func, args, nargsf, core::ptr::null_mut())
135}
136
137extern_libpython! {
138    #[cfg(any(Py_3_12, PyPy))]
139    #[cfg_attr(PyPy, link_name = "PyPyObject_CallOneArg")]
140    pub fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject;
141}
142
143#[cfg(not(PyPy))]
144#[inline(always)]
145pub unsafe fn PyObject_CallMethodNoArgs(
146    self_: *mut PyObject,
147    name: *mut PyObject,
148) -> *mut PyObject {
149    crate::PyObject_VectorcallMethod(
150        name,
151        &self_,
152        1 | PY_VECTORCALL_ARGUMENTS_OFFSET,
153        core::ptr::null_mut(),
154    )
155}
156
157#[cfg(not(PyPy))]
158#[inline(always)]
159pub unsafe fn PyObject_CallMethodOneArg(
160    self_: *mut PyObject,
161    name: *mut PyObject,
162    arg: *mut PyObject,
163) -> *mut PyObject {
164    let args = [self_, arg];
165    assert!(!arg.is_null());
166    crate::PyObject_VectorcallMethod(
167        name,
168        args.as_ptr(),
169        2 | PY_VECTORCALL_ARGUMENTS_OFFSET,
170        core::ptr::null_mut(),
171    )
172}
173
174extern_libpython! {
175    #[cfg_attr(PyPy, link_name = "PyPyObject_LengthHint")]
176    pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t;
177
178    #[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11
179    #[cfg(not(PyPy))]
180    pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int;
181}
182
183#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11
184extern_libpython! {
185    #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")]
186    pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int;
187    #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")]
188    pub fn PyBuffer_GetPointer(
189        view: *mut Py_buffer,
190        indices: *mut Py_ssize_t,
191    ) -> *mut core::ffi::c_void;
192    #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")]
193    pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t;
194    #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")]
195    pub fn PyBuffer_ToContiguous(
196        buf: *mut core::ffi::c_void,
197        view: *mut Py_buffer,
198        len: Py_ssize_t,
199        order: c_char,
200    ) -> c_int;
201    #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")]
202    pub fn PyBuffer_FromContiguous(
203        view: *mut Py_buffer,
204        buf: *mut core::ffi::c_void,
205        len: Py_ssize_t,
206        order: c_char,
207    ) -> c_int;
208    pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int;
209    #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")]
210    pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int;
211    pub fn PyBuffer_FillContiguousStrides(
212        ndims: c_int,
213        shape: *mut Py_ssize_t,
214        strides: *mut Py_ssize_t,
215        itemsize: c_int,
216        fort: c_char,
217    );
218    #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")]
219    pub fn PyBuffer_FillInfo(
220        view: *mut Py_buffer,
221        o: *mut PyObject,
222        buf: *mut core::ffi::c_void,
223        len: Py_ssize_t,
224        readonly: c_int,
225        flags: c_int,
226    ) -> c_int;
227    #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")]
228    pub fn PyBuffer_Release(view: *mut Py_buffer);
229}
230
231#[inline(always)]
232pub unsafe fn PySequence_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
233    (*(*Py_TYPE(seq)).tp_as_sequence).sq_item.unwrap_unchecked()(seq, i)
234}
235
236#[inline(always)]
237#[cfg(not(any(PyPy, GraalPy)))]
238pub unsafe fn PySequence_Fast_GET_SIZE(seq: *mut PyObject) -> Py_ssize_t {
239    if PyList_Check(seq) == 1 {
240        PyList_GET_SIZE(seq)
241    } else {
242        PyTuple_GET_SIZE(seq)
243    }
244}
245
246#[inline(always)]
247#[cfg(not(any(PyPy, GraalPy)))]
248pub unsafe fn PySequence_Fast_GET_ITEM(seq: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
249    if PyList_Check(seq) == 1 {
250        PyList_GET_ITEM(seq, i)
251    } else {
252        PyTuple_GET_ITEM(seq, i)
253    }
254}
255
256#[inline(always)]
257#[cfg(not(any(PyPy, GraalPy)))]
258pub unsafe fn PySequence_Fast_ITEMS(seq: *mut PyObject) -> *mut *mut PyObject {
259    if PyList_Check(seq) == 1 {
260        (*seq.cast::<PyListObject>()).ob_item
261    } else {
262        (*seq.cast::<PyTupleObject>()).ob_item.as_mut_ptr()
263    }
264}