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_Size(PyObject* o);
59 /// _
60 alias PyObject_Size PyObject_Length;
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 int PySequence_Contains(PyObject* seq, PyObject* ob);
419 /// _
420 enum PY_ITERSEARCH_COUNT    = 1;
421 /// _
422 enum PY_ITERSEARCH_INDEX    = 2;
423 /// _
424 enum PY_ITERSEARCH_CONTAINS = 3;
425 /// _
426 Py_ssize_t _PySequence_IterSearch(PyObject* seq, PyObject* obj, int operation);
427 /// _
428 alias PySequence_Contains PySequence_In;
429 /// _
430 Py_ssize_t PySequence_Index(PyObject* o, PyObject* value);
431 /// _
432 PyObject*  PySequence_InPlaceConcat(PyObject* o1, PyObject* o2);
433 /// _
434 PyObject*  PySequence_InPlaceRepeat(PyObject* o, Py_ssize_t count);
435 
436 //-//////////////////////////////////////////////////////////////////////////
437 // MAPPINGS
438 //-//////////////////////////////////////////////////////////////////////////
439 /// _
440 int PyMapping_Check(PyObject* o);
441 /// _
442 Py_ssize_t PyMapping_Size(PyObject* o);
443 /// _
444 alias PyMapping_Size PyMapping_Length;
445 
446 // D translations of C macros:
447 /// _
448 int PyMapping_DelItemString()(PyObject* o, char* k) {
449     return PyObject_DelItemString(o, k);
450 }
451 /// _
452 int PyMapping_DelItem()(PyObject* o, PyObject* k) {
453     return PyObject_DelItem(o, k);
454 }
455 /// _
456 int PyMapping_HasKeyString(PyObject* o, char* key);
457 /// _
458 int PyMapping_HasKey(PyObject* o, PyObject* key);
459 
460 version(Python_3_0_Or_Later) {
461     /// _
462      PyObject* PyMapping_Keys(PyObject* o);
463     /// _
464      PyObject* PyMapping_Values(PyObject* o);
465     /// _
466      PyObject* PyMapping_Items(PyObject* o);
467 }else {
468     // D translations of C macros:
469 /// Availability: 2.*
470     PyObject* PyMapping_Keys()(PyObject* o) {
471         return PyObject_CallMethod(o, "keys", null);
472     }
473 /// Availability: 2.*
474     PyObject* PyMapping_Values()(PyObject* o) {
475         return PyObject_CallMethod(o, "values", null);
476     }
477 /// Availability: 2.*
478     PyObject* PyMapping_Items()(PyObject* o) {
479         return PyObject_CallMethod(o, "items", null);
480     }
481 }
482 /// _
483 PyObject* PyMapping_GetItemString(PyObject* o, char* key);
484 /// _
485 int PyMapping_SetItemString(PyObject* o, char* key, PyObject* value);
486 
487 //-//////////////////////////////////////////////////////////////////////////
488 // GENERIC
489 //-//////////////////////////////////////////////////////////////////////////
490 int PyObject_IsInstance(PyObject* object, PyObject* typeorclass);
491 /// _
492 int PyObject_IsSubclass(PyObject* object, PyObject* typeorclass);
493 version(Python_2_6_Or_Later){
494 /// Availability: >= 2.6
495     int _PyObject_RealIsInstance(PyObject* inst, PyObject* cls);
496 /// Availability: >= 2.6
497     int _PyObject_RealIsSubclass(PyObject* derived, PyObject* cls);
498 }
499 
500 version(Python_3_0_Or_Later) {
501     /// _
502     const(char*)* _PySequence_BytesToCharpArray(PyObject* self);
503     /// _
504     void _Py_FreeCharPArray(const(char*)* array);
505 }
506 
507