1 | use crate::object::*; |
2 | use crate::pyport::Py_ssize_t; |
3 | use std::os::raw::{c_char, c_int}; |
4 | use std::ptr; |
5 | |
6 | extern "C" { |
7 | #[cfg (PyPy)] |
8 | #[link_name = "PyPyObject_DelAttrString" ] |
9 | pub fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int; |
10 | } |
11 | |
12 | #[inline ] |
13 | #[cfg (not(PyPy))] |
14 | pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int { |
15 | PyObject_SetAttrString(arg1:o, arg2:attr_name, arg3:ptr::null_mut()) |
16 | } |
17 | |
18 | #[inline ] |
19 | pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int { |
20 | PyObject_SetAttr(arg1:o, arg2:attr_name, arg3:ptr::null_mut()) |
21 | } |
22 | |
23 | extern "C" { |
24 | #[cfg (all( |
25 | not(PyPy), |
26 | 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 |
27 | ))] |
28 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallNoArgs" )] |
29 | pub fn PyObject_CallNoArgs(func: *mut PyObject) -> *mut PyObject; |
30 | #[cfg_attr (PyPy, link_name = "PyPyObject_Call" )] |
31 | pub fn PyObject_Call( |
32 | callable_object: *mut PyObject, |
33 | args: *mut PyObject, |
34 | kw: *mut PyObject, |
35 | ) -> *mut PyObject; |
36 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallObject" )] |
37 | pub fn PyObject_CallObject( |
38 | callable_object: *mut PyObject, |
39 | args: *mut PyObject, |
40 | ) -> *mut PyObject; |
41 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallFunction" )] |
42 | pub fn PyObject_CallFunction( |
43 | callable_object: *mut PyObject, |
44 | format: *const c_char, |
45 | ... |
46 | ) -> *mut PyObject; |
47 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallMethod" )] |
48 | pub fn PyObject_CallMethod( |
49 | o: *mut PyObject, |
50 | method: *const c_char, |
51 | format: *const c_char, |
52 | ... |
53 | ) -> *mut PyObject; |
54 | |
55 | #[cfg (not(Py_3_13))] |
56 | #[cfg_attr (PyPy, link_name = "_PyPyObject_CallFunction_SizeT" )] |
57 | pub fn _PyObject_CallFunction_SizeT( |
58 | callable_object: *mut PyObject, |
59 | format: *const c_char, |
60 | ... |
61 | ) -> *mut PyObject; |
62 | #[cfg (not(Py_3_13))] |
63 | #[cfg_attr (PyPy, link_name = "_PyPyObject_CallMethod_SizeT" )] |
64 | pub fn _PyObject_CallMethod_SizeT( |
65 | o: *mut PyObject, |
66 | method: *const c_char, |
67 | format: *const c_char, |
68 | ... |
69 | ) -> *mut PyObject; |
70 | |
71 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallFunctionObjArgs" )] |
72 | pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject; |
73 | #[cfg_attr (PyPy, link_name = "PyPyObject_CallMethodObjArgs" )] |
74 | pub fn PyObject_CallMethodObjArgs( |
75 | o: *mut PyObject, |
76 | method: *mut PyObject, |
77 | ... |
78 | ) -> *mut PyObject; |
79 | #[cfg_attr (PyPy, link_name = "PyPyObject_Type" )] |
80 | pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject; |
81 | #[cfg_attr (PyPy, link_name = "PyPyObject_Size" )] |
82 | pub fn PyObject_Size(o: *mut PyObject) -> Py_ssize_t; |
83 | } |
84 | |
85 | #[inline ] |
86 | pub unsafe fn PyObject_Length(o: *mut PyObject) -> Py_ssize_t { |
87 | PyObject_Size(o) |
88 | } |
89 | |
90 | extern "C" { |
91 | #[cfg_attr (PyPy, link_name = "PyPyObject_GetItem" )] |
92 | pub fn PyObject_GetItem(o: *mut PyObject, key: *mut PyObject) -> *mut PyObject; |
93 | #[cfg_attr (PyPy, link_name = "PyPyObject_SetItem" )] |
94 | pub fn PyObject_SetItem(o: *mut PyObject, key: *mut PyObject, v: *mut PyObject) -> c_int; |
95 | #[cfg_attr (PyPy, link_name = "PyPyObject_DelItemString" )] |
96 | pub fn PyObject_DelItemString(o: *mut PyObject, key: *const c_char) -> c_int; |
97 | #[cfg_attr (PyPy, link_name = "PyPyObject_DelItem" )] |
98 | pub fn PyObject_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int; |
99 | } |
100 | |
101 | extern "C" { |
102 | #[cfg_attr (PyPy, link_name = "PyPyObject_Format" )] |
103 | pub fn PyObject_Format(obj: *mut PyObject, format_spec: *mut PyObject) -> *mut PyObject; |
104 | #[cfg_attr (PyPy, link_name = "PyPyObject_GetIter" )] |
105 | pub fn PyObject_GetIter(arg1: *mut PyObject) -> *mut PyObject; |
106 | } |
107 | |
108 | // Before 3.8 PyIter_Check was defined in CPython as a macro, |
109 | // but the implementation of that in PyO3 did not work, see |
110 | // https://github.com/PyO3/pyo3/pull/2914 |
111 | // |
112 | // This is a slow implementation which should function equivalently. |
113 | #[cfg (not(any(Py_3_8, PyPy)))] |
114 | #[inline ] |
115 | pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int { |
116 | crate::PyObject_HasAttrString( |
117 | crate::Py_TYPE(o).cast(), |
118 | "__next__ \0" .as_ptr() as *const c_char, |
119 | ) |
120 | } |
121 | |
122 | extern "C" { |
123 | #[cfg (any(Py_3_8, PyPy))] |
124 | #[cfg_attr (PyPy, link_name = "PyPyIter_Check" )] |
125 | pub fn PyIter_Check(obj: *mut PyObject) -> c_int; |
126 | |
127 | #[cfg_attr (PyPy, link_name = "PyPyIter_Next" )] |
128 | pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject; |
129 | #[cfg (all(not(PyPy), Py_3_10))] |
130 | #[cfg_attr (PyPy, link_name = "PyPyIter_Send" )] |
131 | pub fn PyIter_Send(iter: *mut PyObject, arg: *mut PyObject, presult: *mut *mut PyObject); |
132 | |
133 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Check" )] |
134 | pub fn PyNumber_Check(o: *mut PyObject) -> c_int; |
135 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Add" )] |
136 | pub fn PyNumber_Add(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
137 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Subtract" )] |
138 | pub fn PyNumber_Subtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
139 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Multiply" )] |
140 | pub fn PyNumber_Multiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
141 | #[cfg_attr (PyPy, link_name = "PyPyNumber_MatrixMultiply" )] |
142 | pub fn PyNumber_MatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
143 | #[cfg_attr (PyPy, link_name = "PyPyNumber_FloorDivide" )] |
144 | pub fn PyNumber_FloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
145 | #[cfg_attr (PyPy, link_name = "PyPyNumber_TrueDivide" )] |
146 | pub fn PyNumber_TrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
147 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Remainder" )] |
148 | pub fn PyNumber_Remainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
149 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Divmod" )] |
150 | pub fn PyNumber_Divmod(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
151 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Power" )] |
152 | pub fn PyNumber_Power(o1: *mut PyObject, o2: *mut PyObject, o3: *mut PyObject) |
153 | -> *mut PyObject; |
154 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Negative" )] |
155 | pub fn PyNumber_Negative(o: *mut PyObject) -> *mut PyObject; |
156 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Positive" )] |
157 | pub fn PyNumber_Positive(o: *mut PyObject) -> *mut PyObject; |
158 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Absolute" )] |
159 | pub fn PyNumber_Absolute(o: *mut PyObject) -> *mut PyObject; |
160 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Invert" )] |
161 | pub fn PyNumber_Invert(o: *mut PyObject) -> *mut PyObject; |
162 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Lshift" )] |
163 | pub fn PyNumber_Lshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
164 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Rshift" )] |
165 | pub fn PyNumber_Rshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
166 | #[cfg_attr (PyPy, link_name = "PyPyNumber_And" )] |
167 | pub fn PyNumber_And(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
168 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Xor" )] |
169 | pub fn PyNumber_Xor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
170 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Or" )] |
171 | pub fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
172 | } |
173 | |
174 | // Defined as this macro in Python limited API, but relies on |
175 | // non-limited PyTypeObject. Don't expose this since it cannot be used. |
176 | #[cfg (not(any(Py_LIMITED_API, PyPy)))] |
177 | #[inline ] |
178 | pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int { |
179 | let tp_as_number: *mut PyNumberMethods = (*Py_TYPE(ob:o)).tp_as_number; |
180 | (!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()) as c_int |
181 | } |
182 | |
183 | extern "C" { |
184 | #[cfg (any(all(Py_3_8, Py_LIMITED_API), PyPy))] |
185 | #[link_name = "PyPyIndex_Check" ] |
186 | pub fn PyIndex_Check(o: *mut PyObject) -> c_int; |
187 | |
188 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Index" )] |
189 | pub fn PyNumber_Index(o: *mut PyObject) -> *mut PyObject; |
190 | #[cfg_attr (PyPy, link_name = "PyPyNumber_AsSsize_t" )] |
191 | pub fn PyNumber_AsSsize_t(o: *mut PyObject, exc: *mut PyObject) -> Py_ssize_t; |
192 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Long" )] |
193 | pub fn PyNumber_Long(o: *mut PyObject) -> *mut PyObject; |
194 | #[cfg_attr (PyPy, link_name = "PyPyNumber_Float" )] |
195 | pub fn PyNumber_Float(o: *mut PyObject) -> *mut PyObject; |
196 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceAdd" )] |
197 | pub fn PyNumber_InPlaceAdd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
198 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceSubtract" )] |
199 | pub fn PyNumber_InPlaceSubtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
200 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceMultiply" )] |
201 | pub fn PyNumber_InPlaceMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
202 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceMatrixMultiply" )] |
203 | pub fn PyNumber_InPlaceMatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
204 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceFloorDivide" )] |
205 | pub fn PyNumber_InPlaceFloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
206 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceTrueDivide" )] |
207 | pub fn PyNumber_InPlaceTrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
208 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceRemainder" )] |
209 | pub fn PyNumber_InPlaceRemainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
210 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlacePower" )] |
211 | pub fn PyNumber_InPlacePower( |
212 | o1: *mut PyObject, |
213 | o2: *mut PyObject, |
214 | o3: *mut PyObject, |
215 | ) -> *mut PyObject; |
216 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceLshift" )] |
217 | pub fn PyNumber_InPlaceLshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
218 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceRshift" )] |
219 | pub fn PyNumber_InPlaceRshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
220 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceAnd" )] |
221 | pub fn PyNumber_InPlaceAnd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
222 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceXor" )] |
223 | pub fn PyNumber_InPlaceXor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
224 | #[cfg_attr (PyPy, link_name = "PyPyNumber_InPlaceOr" )] |
225 | pub fn PyNumber_InPlaceOr(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
226 | pub fn PyNumber_ToBase(n: *mut PyObject, base: c_int) -> *mut PyObject; |
227 | |
228 | #[cfg_attr (PyPy, link_name = "PyPySequence_Check" )] |
229 | pub fn PySequence_Check(o: *mut PyObject) -> c_int; |
230 | #[cfg_attr (PyPy, link_name = "PyPySequence_Size" )] |
231 | pub fn PySequence_Size(o: *mut PyObject) -> Py_ssize_t; |
232 | |
233 | #[cfg (PyPy)] |
234 | #[link_name = "PyPySequence_Length" ] |
235 | pub fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t; |
236 | } |
237 | |
238 | #[inline ] |
239 | #[cfg (not(PyPy))] |
240 | pub unsafe fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t { |
241 | PySequence_Size(o) |
242 | } |
243 | |
244 | extern "C" { |
245 | #[cfg_attr (PyPy, link_name = "PyPySequence_Concat" )] |
246 | pub fn PySequence_Concat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
247 | #[cfg_attr (PyPy, link_name = "PyPySequence_Repeat" )] |
248 | pub fn PySequence_Repeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; |
249 | #[cfg_attr (PyPy, link_name = "PyPySequence_GetItem" )] |
250 | pub fn PySequence_GetItem(o: *mut PyObject, i: Py_ssize_t) -> *mut PyObject; |
251 | #[cfg_attr (PyPy, link_name = "PyPySequence_GetSlice" )] |
252 | pub fn PySequence_GetSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> *mut PyObject; |
253 | #[cfg_attr (PyPy, link_name = "PyPySequence_SetItem" )] |
254 | pub fn PySequence_SetItem(o: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) -> c_int; |
255 | #[cfg_attr (PyPy, link_name = "PyPySequence_DelItem" )] |
256 | pub fn PySequence_DelItem(o: *mut PyObject, i: Py_ssize_t) -> c_int; |
257 | #[cfg_attr (PyPy, link_name = "PyPySequence_SetSlice" )] |
258 | pub fn PySequence_SetSlice( |
259 | o: *mut PyObject, |
260 | i1: Py_ssize_t, |
261 | i2: Py_ssize_t, |
262 | v: *mut PyObject, |
263 | ) -> c_int; |
264 | #[cfg_attr (PyPy, link_name = "PyPySequence_DelSlice" )] |
265 | pub fn PySequence_DelSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> c_int; |
266 | #[cfg_attr (PyPy, link_name = "PyPySequence_Tuple" )] |
267 | pub fn PySequence_Tuple(o: *mut PyObject) -> *mut PyObject; |
268 | #[cfg_attr (PyPy, link_name = "PyPySequence_List" )] |
269 | pub fn PySequence_List(o: *mut PyObject) -> *mut PyObject; |
270 | #[cfg_attr (PyPy, link_name = "PyPySequence_Fast" )] |
271 | pub fn PySequence_Fast(o: *mut PyObject, m: *const c_char) -> *mut PyObject; |
272 | // skipped PySequence_Fast_GET_SIZE |
273 | // skipped PySequence_Fast_GET_ITEM |
274 | // skipped PySequence_Fast_GET_ITEMS |
275 | pub fn PySequence_Count(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; |
276 | #[cfg_attr (PyPy, link_name = "PyPySequence_Contains" )] |
277 | pub fn PySequence_Contains(seq: *mut PyObject, ob: *mut PyObject) -> c_int; |
278 | } |
279 | |
280 | #[inline ] |
281 | pub unsafe fn PySequence_In(o: *mut PyObject, value: *mut PyObject) -> c_int { |
282 | PySequence_Contains(seq:o, ob:value) |
283 | } |
284 | |
285 | extern "C" { |
286 | #[cfg_attr (PyPy, link_name = "PyPySequence_Index" )] |
287 | pub fn PySequence_Index(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; |
288 | #[cfg_attr (PyPy, link_name = "PyPySequence_InPlaceConcat" )] |
289 | pub fn PySequence_InPlaceConcat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; |
290 | #[cfg_attr (PyPy, link_name = "PyPySequence_InPlaceRepeat" )] |
291 | pub fn PySequence_InPlaceRepeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; |
292 | #[cfg_attr (PyPy, link_name = "PyPyMapping_Check" )] |
293 | pub fn PyMapping_Check(o: *mut PyObject) -> c_int; |
294 | #[cfg_attr (PyPy, link_name = "PyPyMapping_Size" )] |
295 | pub fn PyMapping_Size(o: *mut PyObject) -> Py_ssize_t; |
296 | |
297 | #[cfg (PyPy)] |
298 | #[link_name = "PyPyMapping_Length" ] |
299 | pub fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t; |
300 | } |
301 | |
302 | #[inline ] |
303 | #[cfg (not(PyPy))] |
304 | pub unsafe fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t { |
305 | PyMapping_Size(o) |
306 | } |
307 | |
308 | #[inline ] |
309 | pub unsafe fn PyMapping_DelItemString(o: *mut PyObject, key: *mut c_char) -> c_int { |
310 | PyObject_DelItemString(o, key) |
311 | } |
312 | |
313 | #[inline ] |
314 | pub unsafe fn PyMapping_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int { |
315 | PyObject_DelItem(o, key) |
316 | } |
317 | |
318 | extern "C" { |
319 | #[cfg_attr (PyPy, link_name = "PyPyMapping_HasKeyString" )] |
320 | pub fn PyMapping_HasKeyString(o: *mut PyObject, key: *const c_char) -> c_int; |
321 | #[cfg_attr (PyPy, link_name = "PyPyMapping_HasKey" )] |
322 | pub fn PyMapping_HasKey(o: *mut PyObject, key: *mut PyObject) -> c_int; |
323 | #[cfg_attr (PyPy, link_name = "PyPyMapping_Keys" )] |
324 | pub fn PyMapping_Keys(o: *mut PyObject) -> *mut PyObject; |
325 | #[cfg_attr (PyPy, link_name = "PyPyMapping_Values" )] |
326 | pub fn PyMapping_Values(o: *mut PyObject) -> *mut PyObject; |
327 | #[cfg_attr (PyPy, link_name = "PyPyMapping_Items" )] |
328 | pub fn PyMapping_Items(o: *mut PyObject) -> *mut PyObject; |
329 | #[cfg_attr (PyPy, link_name = "PyPyMapping_GetItemString" )] |
330 | pub fn PyMapping_GetItemString(o: *mut PyObject, key: *const c_char) -> *mut PyObject; |
331 | #[cfg_attr (PyPy, link_name = "PyPyMapping_SetItemString" )] |
332 | pub fn PyMapping_SetItemString( |
333 | o: *mut PyObject, |
334 | key: *const c_char, |
335 | value: *mut PyObject, |
336 | ) -> c_int; |
337 | #[cfg_attr (PyPy, link_name = "PyPyObject_IsInstance" )] |
338 | pub fn PyObject_IsInstance(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; |
339 | #[cfg_attr (PyPy, link_name = "PyPyObject_IsSubclass" )] |
340 | pub fn PyObject_IsSubclass(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; |
341 | } |
342 | |