1 /**
2   Mirror abstract.h
3 
4 See_Also:
5 <a href="http://docs.python.org/c-api/abstract.html"> Abstract Objects Layer</a>
6   */
7 module deimos.python.abstract_;
8 
9 import deimos.python.pyport;
10 import deimos.python.object;
11 
12 extern(C):
13 
14 // D translations of C macros:
15 /// _
16 int PyObject_DelAttrString()(PyObject* o, const(char) *a) {
17     return PyObject_SetAttrString(o, a, null);
18 }
19 /// _
20 int PyObject_DelAttr()(PyObject* o, PyObject* a) {
21     return PyObject_SetAttr(o, a, null);
22 }
23 
24 version(Python_3_0_Or_Later) {
25 }else{
26 /// _
27 int PyObject_Cmp(PyObject* o1, PyObject* o2, int *result);
28 }
29 
30 //-//////////////////////////////////////////////////////////////////////////
31 // CALLABLES
32 //-//////////////////////////////////////////////////////////////////////////
33 
34 /// _
35 PyObject* PyObject_Call(PyObject* callable_object, PyObject* args, PyObject* kw);
36 /// _
37 PyObject* PyObject_CallObject(PyObject* callable_object, PyObject* args);
38 /// _
39 PyObject* PyObject_CallFunction(PyObject* callable_object, char* format, ...);
40 /// _
41 PyObject* PyObject_CallMethod(PyObject* o, const(char)* m, const(char)* format, ...);
42 /// _
43 PyObject* PyObject_CallFunctionObjArgs(PyObject* callable, ...);
44 /// _
45 PyObject* PyObject_CallMethodObjArgs(PyObject* o,PyObject* m, ...);
46 
47 //-//////////////////////////////////////////////////////////////////////////
48 // GENERIC
49 //-//////////////////////////////////////////////////////////////////////////
50 /// _
51 PyObject* PyObject_Type(PyObject* o);
52 
53 //-//////////////////////////////////////////////////////////////////////////
54 // CONTAINERS
55 //-//////////////////////////////////////////////////////////////////////////
56 
57 /// _
58 Py_ssize_t PyObject_Length(PyObject* o);
59 /// _
60 alias PyObject_Length PyObject_Size;
61 
62 /** The length hint function returns a non-negative value from o.__len__()
63    or o.__length_hint__().  If those methods aren't found or return a negative
64    value, then the defaultvalue is returned.  If one of the calls fails,
65    this function returns -1.
66 */
67 version(Python_2_6_Or_Later){
68     Py_ssize_t _PyObject_LengthHint(PyObject*, Py_ssize_t);
69 }else version(Python_2_5_Or_Later){
70     Py_ssize_t _PyObject_LengthHint(PyObject*);
71 }
72 
73 /// _
74 PyObject* PyObject_GetItem(PyObject* o, PyObject* key);
75 /// _
76 int PyObject_SetItem(PyObject* o, PyObject* key, PyObject* v);
77 /// _
78 int PyObject_DelItemString(PyObject* o, char* key);
79 /// _
80 int PyObject_DelItem(PyObject* o, PyObject* key);
81 /// _
82 
83 int PyObject_AsCharBuffer(PyObject* obj, const(char)** buffer,
84         Py_ssize_t* buffer_len);
85 /// _
86 int PyObject_CheckReadBuffer(PyObject* obj);
87 /// _
88 int PyObject_AsReadBuffer(PyObject* obj, void** buffer, Py_ssize_t* buffer_len);
89 /// _
90 int PyObject_AsWriteBuffer(PyObject* obj, void** buffer, Py_ssize_t* buffer_len);
91 
92 version(Python_2_6_Or_Later){
93     /* new buffer API */
94 
95     /** Return 1 if the getbuffer function is available, otherwise
96        return 0 */
97     int PyObject_CheckBuffer()(PyObject* obj){
98         version(Python_3_0_Or_Later) {
99             return (obj.ob_type.tp_as_buffer !is null) &&
100                 (obj.ob_type.tp_as_buffer.bf_getbuffer !is null);
101         }else{
102             return (obj.ob_type.tp_as_buffer !is null) &&
103                 PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_NEWBUFFER) &&
104                 (obj.ob_type.tp_as_buffer.bf_getbuffer !is null);
105         }
106     }
107 
108     /** This is a C-API version of the getbuffer function call.  It checks
109        to make sure object has the required function pointer and issues the
110        call.  Returns -1 and raises an error on failure and returns 0 on
111        success
112      */
113     int PyObject_GetBuffer(PyObject* obj, Py_buffer* view,
114             int flags);
115 
116     /** Get the memory area pointed to by the indices for the buffer given.
117        Note that view->ndim is the assumed size of indices
118      */
119     void* PyBuffer_GetPointer(Py_buffer* view, Py_ssize_t* indices);
120 
121     /** Return the implied itemsize of the data-format area from a
122        struct-style description
123 
124        abstract.h lies; this function actually does not exist. We're lying too.
125      */
126     int PyBuffer_SizeFromFormat(const(char) *);
127 
128 /// _
129     int PyBuffer_ToContiguous(void* buf, Py_buffer* view,
130             Py_ssize_t len, char fort);
131 
132     /** Copy len bytes of data from the contiguous chunk of memory
133        pointed to by buf into the buffer exported by obj.  Return
134        0 on success and return -1 and raise a PyBuffer_Error on
135        error (i.e. the object does not have a buffer interface or
136        it is not working).
137 
138        If fort is 'F' and the object is multi-dimensional,
139        then the data will be copied into the array in
140        Fortran-style (first dimension varies the fastest).  If
141        fort is 'C', then the data will be copied into the array
142        in C-style (last dimension varies the fastest).  If fort
143        is 'A', then it does not matter and the copy will be made
144        in whatever way is more efficient.
145 
146      */
147     int PyBuffer_FromContiguous(Py_buffer* view, void* buf,
148             Py_ssize_t len, char fort);
149 
150 
151 
152     /** Copy the data from the src buffer to the buffer of dest
153      */
154     int PyObject_CopyData(PyObject* dest, PyObject* src);
155 
156 
157 /// _
158     int PyBuffer_IsContiguous(Py_buffer* view, char fort);
159 
160 
161     /**  Fill the strides array with byte-strides of a contiguous
162         (Fortran-style if fort is 'F' or C-style otherwise)
163         array of the given shape with the given number of bytes
164         per element.
165      */
166     void PyBuffer_FillContiguousStrides(int ndims,
167             Py_ssize_t* shape,
168             Py_ssize_t* strides,
169             int itemsize,
170             char fort);
171 
172     /** Fills in a buffer-info structure correctly for an exporter
173        that can only share a contiguous chunk of memory of
174        "unsigned bytes" of the given length. Returns 0 on success
175        and -1 (with raising an error) on error.
176      */
177     int PyBuffer_FillInfo(Py_buffer* view, PyObject* o, void* buf,
178             Py_ssize_t len, int readonly,
179             int flags);
180 
181     /** Releases a Py_buffer obtained from getbuffer ParseTuple's s*.
182      */
183     void PyBuffer_Release(Py_buffer* view);
184 
185     /**
186        Takes an arbitrary object and returns the result of
187        calling obj.__format__(format_spec).
188      */
189     PyObject* PyObject_Format(PyObject* obj,
190             PyObject* format_spec);
191 
192 }
193 
194 //-//////////////////////////////////////////////////////////////////////////
195 // ITERATORS
196 //-//////////////////////////////////////////////////////////////////////////
197 
198 /// _
199 PyObject* PyObject_GetIter(PyObject*);
200 
201 // D translation of C macro:
202 /// _
203 int PyIter_Check()(PyObject* obj) {
204     version(Python_3_0_Or_Later) {
205         return obj.ob_type.tp_iternext != null &&
206             obj.ob_type.tp_iternext != &_PyObject_NextNotImplemented;
207     }else version(Python_2_7_Or_Later) {
208         return PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_ITER)
209             && obj.ob_type.tp_iternext != null &&
210             obj.ob_type.tp_iternext != &_PyObject_NextNotImplemented;
211     }else {
212         return PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_ITER)
213             && obj.ob_type.tp_iternext != null;
214     }
215 }
216 
217 /// _
218 PyObject* PyIter_Next(PyObject*);
219 
220 /////////////////////////////////////////////////////////////////////////////
221 // NUMBERS
222 /////////////////////////////////////////////////////////////////////////////
223 
224 int PyNumber_Check(PyObject* o);
225 
226 /// _
227 PyObject* PyNumber_Add(PyObject* o1, PyObject* o2);
228 /// _
229 PyObject* PyNumber_Subtract(PyObject* o1, PyObject* o2);
230 /// _
231 PyObject* PyNumber_Multiply(PyObject* o1, PyObject* o2);
232 
233 version(Python_3_5_Or_Later) {
234     /// _
235     PyObject* PyNumber_MatrixMultiply(PyObject* o1, PyObject* o2);
236 }
237 
238 version(Python_3_0_Or_Later) {
239 }else{
240     /// Availability: 2.*
241     PyObject* PyNumber_Divide(PyObject* o1, PyObject* o2);
242 }
243 /// _
244 PyObject* PyNumber_FloorDivide(PyObject* o1, PyObject* o2);
245 /// _
246 PyObject* PyNumber_TrueDivide(PyObject* o1, PyObject* o2);
247 /// _
248 PyObject* PyNumber_Remainder(PyObject* o1, PyObject* o2);
249 /// _
250 PyObject* PyNumber_Divmod(PyObject* o1, PyObject* o2);
251 /// _
252 PyObject* PyNumber_Power(PyObject* o1, PyObject* o2, PyObject* o3);
253 /// _
254 PyObject* PyNumber_Negative(PyObject* o);
255 /// _
256 PyObject* PyNumber_Positive(PyObject* o);
257 /// _
258 PyObject* PyNumber_Absolute(PyObject* o);
259 /// _
260 PyObject* PyNumber_Invert(PyObject* o);
261 /// _
262 PyObject* PyNumber_Lshift(PyObject* o1, PyObject* o2);
263 /// _
264 PyObject* PyNumber_Rshift(PyObject* o1, PyObject* o2);
265 /// _
266 PyObject* PyNumber_And(PyObject* o1, PyObject* o2);
267 /// _
268 PyObject* PyNumber_Xor(PyObject* o1, PyObject* o2);
269 /// _
270 PyObject* PyNumber_Or(PyObject* o1, PyObject* o2);
271 
272 version(Python_2_5_Or_Later) {
273 /// Availability: >= 2.5
274     int PyIndex_Check()(PyObject* obj) {
275         version(Python_3_0_Or_Later) {
276             return obj.ob_type.tp_as_number !is null &&
277                 obj.ob_type.tp_as_number.nb_index !is null;
278         }else{
279             return obj.ob_type.tp_as_number !is null &&
280                 PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_INDEX) &&
281                 obj.ob_type.tp_as_number.nb_index !is null;
282         }
283     }
284 /// Availability: >= 2.5
285     PyObject* PyNumber_Index(PyObject* o);
286     /**
287        Returns the Integral instance converted to an int. The
288        instance is expected to be int or long or have an __int__
289        method. Steals integral's reference. error_format will be
290        used to create the TypeError if integral isn't actually an
291        Integral instance. error_format should be a format string
292        that can accept a char* naming integral's type.
293 
294         Availability: >= 2.5
295      */
296     Py_ssize_t PyNumber_AsSsize_t(PyObject* o, PyObject* exc);
297 }
298 version(Python_2_6_Or_Later) {
299 /// Availability: >= 2.6
300     PyObject*  _PyNumber_ConvertIntegralToInt(
301             PyObject* integral,
302             const(char)* error_format);
303 }
304 
305 version(Python_3_0_Or_Later) {
306 }else {
307     /// Availability: 2.*
308     PyObject* PyNumber_Int(PyObject* o);
309 }
310 /// _
311 PyObject* PyNumber_Long(PyObject* o);
312 /// _
313 PyObject* PyNumber_Float(PyObject* o);
314 /// _
315 PyObject* PyNumber_InPlaceAdd(PyObject* o1, PyObject* o2);
316 /// _
317 PyObject* PyNumber_InPlaceSubtract(PyObject* o1, PyObject* o2);
318 /// _
319 PyObject* PyNumber_InPlaceMultiply(PyObject* o1, PyObject* o2);
320 
321 version(Python_3_5_Or_Later) {
322     /// _
323      PyObject* PyNumber_InPlaceMatrixMultiply(PyObject* o1, PyObject* o2);
324 }
325 
326 version(Python_3_0_Or_Later) {
327 }else{
328     /// Availability: 2.*
329     PyObject* PyNumber_InPlaceDivide(PyObject* o1, PyObject* o2);
330 }
331 /// _
332 PyObject* PyNumber_InPlaceFloorDivide(PyObject* o1, PyObject* o2);
333 /// _
334 PyObject* PyNumber_InPlaceTrueDivide(PyObject* o1, PyObject* o2);
335 /// _
336 PyObject* PyNumber_InPlaceRemainder(PyObject* o1, PyObject* o2);
337 /// _
338 PyObject* PyNumber_InPlacePower(PyObject* o1, PyObject* o2, PyObject* o3);
339 /// _
340 PyObject* PyNumber_InPlaceLshift(PyObject* o1, PyObject* o2);
341 /// _
342 PyObject* PyNumber_InPlaceRshift(PyObject* o1, PyObject* o2);
343 /// _
344 PyObject* PyNumber_InPlaceAnd(PyObject* o1, PyObject* o2);
345 /// _
346 PyObject* PyNumber_InPlaceXor(PyObject* o1, PyObject* o2);
347 /// _
348 PyObject* PyNumber_InPlaceOr(PyObject* o1, PyObject* o2);
349 
350 version(Python_2_6_Or_Later){
351     /**
352        Returns the integer n converted to a string with a base, with a base
353        marker of 0b, 0o or 0x prefixed if applicable.
354        If n is not an int object, it is converted with PyNumber_Index first.
355 
356 Availability: >= 2.6
357      */
358     PyObject* PyNumber_ToBase(PyObject* n, int base);
359 }
360 
361 //-//////////////////////////////////////////////////////////////////////////
362 // SEQUENCES
363 //-//////////////////////////////////////////////////////////////////////////
364 
365 /// _
366 int PySequence_Check(PyObject* o);
367 /// _
368 Py_ssize_t PySequence_Size(PyObject* o);
369 /// _
370 alias PySequence_Size PySequence_Length;
371 /// _
372 PyObject* PySequence_Concat(PyObject* o1, PyObject* o2);
373 /// _
374 PyObject* PySequence_Repeat(PyObject* o, Py_ssize_t count);
375 /// _
376 PyObject* PySequence_GetItem(PyObject* o, Py_ssize_t i);
377 /// _
378 PyObject* PySequence_GetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2);
379 /// _
380 int PySequence_SetItem(PyObject* o, Py_ssize_t i, PyObject* v);
381 /// _
382 int PySequence_DelItem(PyObject* o, Py_ssize_t i);
383 /// _
384 int PySequence_SetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2, PyObject* v);
385 /// _
386 int PySequence_DelSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2);
387 /// _
388 PyObject* PySequence_Tuple(PyObject* o);
389 /// _
390 PyObject* PySequence_List(PyObject* o);
391 /// _
392 PyObject* PySequence_Fast(PyObject* o,  const(char)* m);
393 // D translations of C macros:
394 /// _
395 Py_ssize_t PySequence_Fast_GET_SIZE()(PyObject* o) {
396     return PyList_Check(o) ? cast(Py_ssize_t) PyList_GET_SIZE(o) :
397         cast(Py_ssize_t) PyTuple_GET_SIZE(o);
398 }
399 /// _
400 PyObject* PySequence_Fast_GET_ITEM()(PyObject* o, Py_ssize_t i) {
401     return PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i);
402 }
403 /// _
404 PyObject* PySequence_ITEM()(PyObject* o, Py_ssize_t i) {
405     return o.ob_type.tp_as_sequence.sq_item(o, i);
406 }
407 /// _
408 PyObject** PySequence_Fast_ITEMS()(PyObject* sf) {
409     return
410         PyList_Check(sf) ?
411         (cast(PyListObject *)sf).ob_item
412         : (cast(PyTupleObject *)sf).ob_item
413         ;
414 }
415 /// _
416 Py_ssize_t PySequence_Count(PyObject* o, PyObject* value);
417 /// _
418 enum PY_ITERSEARCH_COUNT    = 1;
419 /// _
420 enum PY_ITERSEARCH_INDEX    = 2;
421 /// _
422 enum PY_ITERSEARCH_CONTAINS = 3;
423 /// _
424 Py_ssize_t _PySequence_IterSearch(PyObject* seq, PyObject* obj, int operation);
425 
426 /// _
427 int PySequence_In(PyObject* o, PyObject* value);
428 /// _
429 alias PySequence_In PySequence_Contains;
430 /// _
431 Py_ssize_t PySequence_Index(PyObject* o, PyObject* value);
432 /// _
433 PyObject*  PySequence_InPlaceConcat(PyObject* o1, PyObject* o2);
434 /// _
435 PyObject*  PySequence_InPlaceRepeat(PyObject* o, Py_ssize_t count);
436 
437 //-//////////////////////////////////////////////////////////////////////////
438 // MAPPINGS
439 //-//////////////////////////////////////////////////////////////////////////
440 /// _
441 int PyMapping_Check(PyObject* o);
442 /// _
443 Py_ssize_t PyMapping_Length(PyObject* o);
444 /// _
445 alias PyMapping_Length PyMapping_Size;
446 
447 // D translations of C macros:
448 /// _
449 int PyMapping_DelItemString()(PyObject* o, char* k) {
450     return PyObject_DelItemString(o, k);
451 }
452 /// _
453 int PyMapping_DelItem()(PyObject* o, PyObject* k) {
454     return PyObject_DelItem(o, k);
455 }
456 /// _
457 int PyMapping_HasKeyString(PyObject* o, char* key);
458 /// _
459 int PyMapping_HasKey(PyObject* o, PyObject* key);
460 
461 version(Python_3_0_Or_Later) {
462     /// _
463      PyObject* PyMapping_Keys(PyObject* o);
464     /// _
465      PyObject* PyMapping_Values(PyObject* o);
466     /// _
467      PyObject* PyMapping_Items(PyObject* o);
468 }else {
469     // D translations of C macros:
470 /// Availability: 2.*
471     PyObject* PyMapping_Keys()(PyObject* o) {
472         return PyObject_CallMethod(o, "keys", null);
473     }
474 /// Availability: 2.*
475     PyObject* PyMapping_Values()(PyObject* o) {
476         return PyObject_CallMethod(o, "values", null);
477     }
478 /// Availability: 2.*
479     PyObject* PyMapping_Items()(PyObject* o) {
480         return PyObject_CallMethod(o, "items", null);
481     }
482 }
483 /// _
484 PyObject* PyMapping_GetItemString(PyObject* o, char* key);
485 /// _
486 int PyMapping_SetItemString(PyObject* o, char* key, PyObject* value);
487 
488 //-//////////////////////////////////////////////////////////////////////////
489 // GENERIC
490 //-//////////////////////////////////////////////////////////////////////////
491 int PyObject_IsInstance(PyObject* object, PyObject* typeorclass);
492 /// _
493 int PyObject_IsSubclass(PyObject* object, PyObject* typeorclass);
494 version(Python_2_6_Or_Later){
495 /// Availability: >= 2.6
496     int _PyObject_RealIsInstance(PyObject* inst, PyObject* cls);
497 /// Availability: >= 2.6
498     int _PyObject_RealIsSubclass(PyObject* derived, PyObject* cls);
499 }
500 
501 version(Python_3_0_Or_Later) {
502     /// _
503     const(char*)* _PySequence_BytesToCharpArray(PyObject* self);
504     /// _
505     void _Py_FreeCharPArray(const(char*)* array);
506 }
507 
508