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 version(Python_3_0_Or_Later) {
233 }else{
234     /// Availability: 2.*
235     PyObject* PyNumber_Divide(PyObject* o1, PyObject* o2);
236 }
237 /// _
238 PyObject* PyNumber_FloorDivide(PyObject* o1, PyObject* o2);
239 /// _
240 PyObject* PyNumber_TrueDivide(PyObject* o1, PyObject* o2);
241 /// _
242 PyObject* PyNumber_Remainder(PyObject* o1, PyObject* o2);
243 /// _
244 PyObject* PyNumber_Divmod(PyObject* o1, PyObject* o2);
245 /// _
246 PyObject* PyNumber_Power(PyObject* o1, PyObject* o2, PyObject* o3);
247 /// _
248 PyObject* PyNumber_Negative(PyObject* o);
249 /// _
250 PyObject* PyNumber_Positive(PyObject* o);
251 /// _
252 PyObject* PyNumber_Absolute(PyObject* o);
253 /// _
254 PyObject* PyNumber_Invert(PyObject* o);
255 /// _
256 PyObject* PyNumber_Lshift(PyObject* o1, PyObject* o2);
257 /// _
258 PyObject* PyNumber_Rshift(PyObject* o1, PyObject* o2);
259 /// _
260 PyObject* PyNumber_And(PyObject* o1, PyObject* o2);
261 /// _
262 PyObject* PyNumber_Xor(PyObject* o1, PyObject* o2);
263 /// _
264 PyObject* PyNumber_Or(PyObject* o1, PyObject* o2);
265 
266 version(Python_2_5_Or_Later) {
267 /// Availability: >= 2.5
268     int PyIndex_Check()(PyObject* obj) {
269         version(Python_3_0_Or_Later) {
270             return obj.ob_type.tp_as_number !is null &&
271                 obj.ob_type.tp_as_number.nb_index !is null;
272         }else{
273             return obj.ob_type.tp_as_number !is null &&
274                 PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_INDEX) &&
275                 obj.ob_type.tp_as_number.nb_index !is null;
276         }
277     }
278 /// Availability: >= 2.5
279     PyObject* PyNumber_Index(PyObject* o);
280     /**
281        Returns the Integral instance converted to an int. The
282        instance is expected to be int or long or have an __int__
283        method. Steals integral's reference. error_format will be
284        used to create the TypeError if integral isn't actually an
285        Integral instance. error_format should be a format string
286        that can accept a char* naming integral's type.
287 
288         Availability: >= 2.5
289      */
290     Py_ssize_t PyNumber_AsSsize_t(PyObject* o, PyObject* exc);
291 }
292 version(Python_2_6_Or_Later) {
293 /// Availability: >= 2.6
294     PyObject*  _PyNumber_ConvertIntegralToInt(
295             PyObject* integral,
296             const(char)* error_format);
297 }
298 
299 version(Python_3_0_Or_Later) {
300 }else {
301     /// Availability: 2.*
302     PyObject* PyNumber_Int(PyObject* o);
303 }
304 /// _
305 PyObject* PyNumber_Long(PyObject* o);
306 /// _
307 PyObject* PyNumber_Float(PyObject* o);
308 /// _
309 PyObject* PyNumber_InPlaceAdd(PyObject* o1, PyObject* o2);
310 /// _
311 PyObject* PyNumber_InPlaceSubtract(PyObject* o1, PyObject* o2);
312 /// _
313 PyObject* PyNumber_InPlaceMultiply(PyObject* o1, PyObject* o2);
314 version(Python_3_0_Or_Later) {
315 }else{
316     /// Availability: 2.*
317     PyObject* PyNumber_InPlaceDivide(PyObject* o1, PyObject* o2);
318 }
319 /// _
320 PyObject* PyNumber_InPlaceFloorDivide(PyObject* o1, PyObject* o2);
321 /// _
322 PyObject* PyNumber_InPlaceTrueDivide(PyObject* o1, PyObject* o2);
323 /// _
324 PyObject* PyNumber_InPlaceRemainder(PyObject* o1, PyObject* o2);
325 /// _
326 PyObject* PyNumber_InPlacePower(PyObject* o1, PyObject* o2, PyObject* o3);
327 /// _
328 PyObject* PyNumber_InPlaceLshift(PyObject* o1, PyObject* o2);
329 /// _
330 PyObject* PyNumber_InPlaceRshift(PyObject* o1, PyObject* o2);
331 /// _
332 PyObject* PyNumber_InPlaceAnd(PyObject* o1, PyObject* o2);
333 /// _
334 PyObject* PyNumber_InPlaceXor(PyObject* o1, PyObject* o2);
335 /// _
336 PyObject* PyNumber_InPlaceOr(PyObject* o1, PyObject* o2);
337 
338 version(Python_2_6_Or_Later){
339     /**
340        Returns the integer n converted to a string with a base, with a base
341        marker of 0b, 0o or 0x prefixed if applicable.
342        If n is not an int object, it is converted with PyNumber_Index first.
343 
344 Availability: >= 2.6
345      */
346     PyObject* PyNumber_ToBase(PyObject* n, int base);
347 }
348 
349 //-//////////////////////////////////////////////////////////////////////////
350 // SEQUENCES
351 //-//////////////////////////////////////////////////////////////////////////
352 
353 /// _
354 int PySequence_Check(PyObject* o);
355 /// _
356 Py_ssize_t PySequence_Size(PyObject* o);
357 /// _
358 alias PySequence_Size PySequence_Length;
359 /// _
360 PyObject* PySequence_Concat(PyObject* o1, PyObject* o2);
361 /// _
362 PyObject* PySequence_Repeat(PyObject* o, Py_ssize_t count);
363 /// _
364 PyObject* PySequence_GetItem(PyObject* o, Py_ssize_t i);
365 /// _
366 PyObject* PySequence_GetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2);
367 /// _
368 int PySequence_SetItem(PyObject* o, Py_ssize_t i, PyObject* v);
369 /// _
370 int PySequence_DelItem(PyObject* o, Py_ssize_t i);
371 /// _
372 int PySequence_SetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2, PyObject* v);
373 /// _
374 int PySequence_DelSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2);
375 /// _
376 PyObject* PySequence_Tuple(PyObject* o);
377 /// _
378 PyObject* PySequence_List(PyObject* o);
379 /// _
380 PyObject* PySequence_Fast(PyObject* o,  const(char)* m);
381 // D translations of C macros:
382 /// _
383 Py_ssize_t PySequence_Fast_GET_SIZE()(PyObject* o) {
384     return PyList_Check(o) ? cast(Py_ssize_t) PyList_GET_SIZE(o) :
385         cast(Py_ssize_t) PyTuple_GET_SIZE(o);
386 }
387 /// _
388 PyObject* PySequence_Fast_GET_ITEM()(PyObject* o, Py_ssize_t i) {
389     return PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i);
390 }
391 /// _
392 PyObject* PySequence_ITEM()(PyObject* o, Py_ssize_t i) {
393     return o.ob_type.tp_as_sequence.sq_item(o, i);
394 }
395 /// _
396 PyObject** PySequence_Fast_ITEMS()(PyObject* sf) {
397     return
398         PyList_Check(sf) ?
399         (cast(PyListObject *)sf).ob_item
400         : (cast(PyTupleObject *)sf).ob_item
401         ;
402 }
403 /// _
404 Py_ssize_t PySequence_Count(PyObject* o, PyObject* value);
405 /// _
406 int PySequence_Contains(PyObject* seq, PyObject* ob);
407 /// _
408 enum PY_ITERSEARCH_COUNT    = 1;
409 /// _
410 enum PY_ITERSEARCH_INDEX    = 2;
411 /// _
412 enum PY_ITERSEARCH_CONTAINS = 3;
413 /// _
414 Py_ssize_t _PySequence_IterSearch(PyObject* seq, PyObject* obj, int operation);
415 /// _
416 alias PySequence_Contains PySequence_In;
417 /// _
418 Py_ssize_t PySequence_Index(PyObject* o, PyObject* value);
419 /// _
420 PyObject*  PySequence_InPlaceConcat(PyObject* o1, PyObject* o2);
421 /// _
422 PyObject*  PySequence_InPlaceRepeat(PyObject* o, Py_ssize_t count);
423 
424 //-//////////////////////////////////////////////////////////////////////////
425 // MAPPINGS
426 //-//////////////////////////////////////////////////////////////////////////
427 /// _
428 int PyMapping_Check(PyObject* o);
429 /// _
430 Py_ssize_t PyMapping_Size(PyObject* o);
431 /// _
432 alias PyMapping_Size PyMapping_Length;
433 
434 // D translations of C macros:
435 /// _
436 int PyMapping_DelItemString()(PyObject* o, char* k) {
437     return PyObject_DelItemString(o, k);
438 }
439 /// _
440 int PyMapping_DelItem()(PyObject* o, PyObject* k) {
441     return PyObject_DelItem(o, k);
442 }
443 /// _
444 int PyMapping_HasKeyString(PyObject* o, char* key);
445 /// _
446 int PyMapping_HasKey(PyObject* o, PyObject* key);
447 
448 version(Python_3_0_Or_Later) {
449     /// _
450      PyObject* PyMapping_Keys(PyObject* o);
451     /// _
452      PyObject* PyMapping_Values(PyObject* o);
453     /// _
454      PyObject* PyMapping_Items(PyObject* o);
455 }else {
456     // D translations of C macros:
457 /// Availability: 2.*
458     PyObject* PyMapping_Keys()(PyObject* o) {
459         return PyObject_CallMethod(o, "keys", null);
460     }
461 /// Availability: 2.*
462     PyObject* PyMapping_Values()(PyObject* o) {
463         return PyObject_CallMethod(o, "values", null);
464     }
465 /// Availability: 2.*
466     PyObject* PyMapping_Items()(PyObject* o) {
467         return PyObject_CallMethod(o, "items", null);
468     }
469 }
470 /// _
471 PyObject* PyMapping_GetItemString(PyObject* o, char* key);
472 /// _
473 int PyMapping_SetItemString(PyObject* o, char* key, PyObject* value);
474 
475 //-//////////////////////////////////////////////////////////////////////////
476 // GENERIC
477 //-//////////////////////////////////////////////////////////////////////////
478 int PyObject_IsInstance(PyObject* object, PyObject* typeorclass);
479 /// _
480 int PyObject_IsSubclass(PyObject* object, PyObject* typeorclass);
481 version(Python_2_6_Or_Later){
482 /// Availability: >= 2.6
483     int _PyObject_RealIsInstance(PyObject* inst, PyObject* cls);
484 /// Availability: >= 2.6
485     int _PyObject_RealIsSubclass(PyObject* derived, PyObject* cls);
486 }
487 
488 version(Python_3_0_Or_Later) {
489     /// _
490     const(char*)* _PySequence_BytesToCharpArray(PyObject* self);
491     /// _
492     void _Py_FreeCharPArray(const(char*)* array);
493 }
494 
495