pyo3_ffi/
abstract_.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use crate::object::*;
use crate::pyport::Py_ssize_t;
use std::os::raw::{c_char, c_int};

#[inline]
#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h
pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int {
    PyObject_SetAttrString(o, attr_name, std::ptr::null_mut())
}

#[inline]
#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h
pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int {
    PyObject_SetAttr(o, attr_name, std::ptr::null_mut())
}

extern "C" {
    #[cfg(all(
        not(PyPy),
        not(GraalPy),
        any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10
    ))]
    #[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")]
    pub fn PyObject_CallNoArgs(func: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_Call")]
    pub fn PyObject_Call(
        callable_object: *mut PyObject,
        args: *mut PyObject,
        kw: *mut PyObject,
    ) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_CallObject")]
    pub fn PyObject_CallObject(
        callable_object: *mut PyObject,
        args: *mut PyObject,
    ) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunction")]
    pub fn PyObject_CallFunction(
        callable_object: *mut PyObject,
        format: *const c_char,
        ...
    ) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethod")]
    pub fn PyObject_CallMethod(
        o: *mut PyObject,
        method: *const c_char,
        format: *const c_char,
        ...
    ) -> *mut PyObject;

    #[cfg(not(Py_3_13))]
    #[cfg_attr(PyPy, link_name = "_PyPyObject_CallFunction_SizeT")]
    pub fn _PyObject_CallFunction_SizeT(
        callable_object: *mut PyObject,
        format: *const c_char,
        ...
    ) -> *mut PyObject;
    #[cfg(not(Py_3_13))]
    #[cfg_attr(PyPy, link_name = "_PyPyObject_CallMethod_SizeT")]
    pub fn _PyObject_CallMethod_SizeT(
        o: *mut PyObject,
        method: *const c_char,
        format: *const c_char,
        ...
    ) -> *mut PyObject;

    #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunctionObjArgs")]
    pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethodObjArgs")]
    pub fn PyObject_CallMethodObjArgs(
        o: *mut PyObject,
        method: *mut PyObject,
        ...
    ) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_Type")]
    pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_Size")]
    pub fn PyObject_Size(o: *mut PyObject) -> Py_ssize_t;
}

#[inline]
pub unsafe fn PyObject_Length(o: *mut PyObject) -> Py_ssize_t {
    PyObject_Size(o)
}

extern "C" {
    #[cfg_attr(PyPy, link_name = "PyPyObject_GetItem")]
    pub fn PyObject_GetItem(o: *mut PyObject, key: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_SetItem")]
    pub fn PyObject_SetItem(o: *mut PyObject, key: *mut PyObject, v: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyObject_DelItemString")]
    pub fn PyObject_DelItemString(o: *mut PyObject, key: *const c_char) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyObject_DelItem")]
    pub fn PyObject_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int;
}

extern "C" {
    #[cfg_attr(PyPy, link_name = "PyPyObject_Format")]
    pub fn PyObject_Format(obj: *mut PyObject, format_spec: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyObject_GetIter")]
    pub fn PyObject_GetIter(arg1: *mut PyObject) -> *mut PyObject;
}

// Before 3.8 PyIter_Check was defined in CPython as a macro,
// but the implementation of that in PyO3 did not work, see
// https://github.com/PyO3/pyo3/pull/2914
//
// This is a slow implementation which should function equivalently.
#[cfg(not(any(Py_3_8, PyPy)))]
#[inline]
pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int {
    crate::PyObject_HasAttrString(crate::Py_TYPE(o).cast(), c_str!("__next__").as_ptr())
}

extern "C" {
    #[cfg(any(Py_3_8, PyPy))]
    #[cfg_attr(PyPy, link_name = "PyPyIter_Check")]
    pub fn PyIter_Check(obj: *mut PyObject) -> c_int;

    #[cfg_attr(PyPy, link_name = "PyPyIter_Next")]
    pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject;
    #[cfg(all(not(PyPy), Py_3_10))]
    #[cfg_attr(PyPy, link_name = "PyPyIter_Send")]
    pub fn PyIter_Send(iter: *mut PyObject, arg: *mut PyObject, presult: *mut *mut PyObject);

    #[cfg_attr(PyPy, link_name = "PyPyNumber_Check")]
    pub fn PyNumber_Check(o: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Add")]
    pub fn PyNumber_Add(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Subtract")]
    pub fn PyNumber_Subtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Multiply")]
    pub fn PyNumber_Multiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_MatrixMultiply")]
    pub fn PyNumber_MatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_FloorDivide")]
    pub fn PyNumber_FloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_TrueDivide")]
    pub fn PyNumber_TrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Remainder")]
    pub fn PyNumber_Remainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Divmod")]
    pub fn PyNumber_Divmod(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Power")]
    pub fn PyNumber_Power(o1: *mut PyObject, o2: *mut PyObject, o3: *mut PyObject)
        -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Negative")]
    pub fn PyNumber_Negative(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Positive")]
    pub fn PyNumber_Positive(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Absolute")]
    pub fn PyNumber_Absolute(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Invert")]
    pub fn PyNumber_Invert(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Lshift")]
    pub fn PyNumber_Lshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Rshift")]
    pub fn PyNumber_Rshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_And")]
    pub fn PyNumber_And(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Xor")]
    pub fn PyNumber_Xor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Or")]
    pub fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
}

// Defined as this macro in Python limited API, but relies on
// non-limited PyTypeObject. Don't expose this since it cannot be used.
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
#[inline]
pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int {
    let tp_as_number = (*Py_TYPE(o)).tp_as_number;
    (!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()) as c_int
}

extern "C" {
    #[cfg(any(all(Py_3_8, Py_LIMITED_API), PyPy))]
    #[link_name = "PyPyIndex_Check"]
    pub fn PyIndex_Check(o: *mut PyObject) -> c_int;

    #[cfg_attr(PyPy, link_name = "PyPyNumber_Index")]
    pub fn PyNumber_Index(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_AsSsize_t")]
    pub fn PyNumber_AsSsize_t(o: *mut PyObject, exc: *mut PyObject) -> Py_ssize_t;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Long")]
    pub fn PyNumber_Long(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_Float")]
    pub fn PyNumber_Float(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAdd")]
    pub fn PyNumber_InPlaceAdd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceSubtract")]
    pub fn PyNumber_InPlaceSubtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMultiply")]
    pub fn PyNumber_InPlaceMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMatrixMultiply")]
    pub fn PyNumber_InPlaceMatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceFloorDivide")]
    pub fn PyNumber_InPlaceFloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceTrueDivide")]
    pub fn PyNumber_InPlaceTrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRemainder")]
    pub fn PyNumber_InPlaceRemainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlacePower")]
    pub fn PyNumber_InPlacePower(
        o1: *mut PyObject,
        o2: *mut PyObject,
        o3: *mut PyObject,
    ) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceLshift")]
    pub fn PyNumber_InPlaceLshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRshift")]
    pub fn PyNumber_InPlaceRshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAnd")]
    pub fn PyNumber_InPlaceAnd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceXor")]
    pub fn PyNumber_InPlaceXor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceOr")]
    pub fn PyNumber_InPlaceOr(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    pub fn PyNumber_ToBase(n: *mut PyObject, base: c_int) -> *mut PyObject;

    #[cfg_attr(PyPy, link_name = "PyPySequence_Check")]
    pub fn PySequence_Check(o: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPySequence_Size")]
    pub fn PySequence_Size(o: *mut PyObject) -> Py_ssize_t;

    #[cfg(PyPy)]
    #[link_name = "PyPySequence_Length"]
    pub fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t;
}

#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t {
    PySequence_Size(o)
}

extern "C" {
    #[cfg_attr(PyPy, link_name = "PyPySequence_Concat")]
    pub fn PySequence_Concat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_Repeat")]
    pub fn PySequence_Repeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_GetItem")]
    pub fn PySequence_GetItem(o: *mut PyObject, i: Py_ssize_t) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_GetSlice")]
    pub fn PySequence_GetSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_SetItem")]
    pub fn PySequence_SetItem(o: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPySequence_DelItem")]
    pub fn PySequence_DelItem(o: *mut PyObject, i: Py_ssize_t) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPySequence_SetSlice")]
    pub fn PySequence_SetSlice(
        o: *mut PyObject,
        i1: Py_ssize_t,
        i2: Py_ssize_t,
        v: *mut PyObject,
    ) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPySequence_DelSlice")]
    pub fn PySequence_DelSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPySequence_Tuple")]
    pub fn PySequence_Tuple(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_List")]
    pub fn PySequence_List(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_Fast")]
    pub fn PySequence_Fast(o: *mut PyObject, m: *const c_char) -> *mut PyObject;
    // skipped PySequence_Fast_GET_SIZE
    // skipped PySequence_Fast_GET_ITEM
    // skipped PySequence_Fast_GET_ITEMS
    pub fn PySequence_Count(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t;
    #[cfg_attr(PyPy, link_name = "PyPySequence_Contains")]
    pub fn PySequence_Contains(seq: *mut PyObject, ob: *mut PyObject) -> c_int;
}

#[inline]
pub unsafe fn PySequence_In(o: *mut PyObject, value: *mut PyObject) -> c_int {
    PySequence_Contains(o, value)
}

extern "C" {
    #[cfg_attr(PyPy, link_name = "PyPySequence_Index")]
    pub fn PySequence_Index(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t;
    #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceConcat")]
    pub fn PySequence_InPlaceConcat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceRepeat")]
    pub fn PySequence_InPlaceRepeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_Check")]
    pub fn PyMapping_Check(o: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_Size")]
    pub fn PyMapping_Size(o: *mut PyObject) -> Py_ssize_t;

    #[cfg(PyPy)]
    #[link_name = "PyPyMapping_Length"]
    pub fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t;
}

#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t {
    PyMapping_Size(o)
}

#[inline]
pub unsafe fn PyMapping_DelItemString(o: *mut PyObject, key: *mut c_char) -> c_int {
    PyObject_DelItemString(o, key)
}

#[inline]
pub unsafe fn PyMapping_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int {
    PyObject_DelItem(o, key)
}

extern "C" {
    #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKeyString")]
    pub fn PyMapping_HasKeyString(o: *mut PyObject, key: *const c_char) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKey")]
    pub fn PyMapping_HasKey(o: *mut PyObject, key: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_Keys")]
    pub fn PyMapping_Keys(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_Values")]
    pub fn PyMapping_Values(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_Items")]
    pub fn PyMapping_Items(o: *mut PyObject) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_GetItemString")]
    pub fn PyMapping_GetItemString(o: *mut PyObject, key: *const c_char) -> *mut PyObject;
    #[cfg_attr(PyPy, link_name = "PyPyMapping_SetItemString")]
    pub fn PyMapping_SetItemString(
        o: *mut PyObject,
        key: *const c_char,
        value: *mut PyObject,
    ) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyObject_IsInstance")]
    pub fn PyObject_IsInstance(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int;
    #[cfg_attr(PyPy, link_name = "PyPyObject_IsSubclass")]
    pub fn PyObject_IsSubclass(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int;
}