1 /**
2   Mirror _object.h
3 
4 Object and type object interface
5 
6 Objects are structures allocated on the heap.  Special rules apply to
7 the use of objects to ensure they are properly garbage-collected.
8 Objects are never allocated statically or on the stack; they must be
9 accessed through special macros and functions only.  (Type objects are
10 exceptions to the first rule; the standard types are represented by
11 statically initialized type objects, although work on type/class unification
12 for Python 2.2 made it possible to have heap-allocated type objects too).
13 
14 An object has a 'reference count' that is increased or decreased when a
15 pointer to the object is copied or deleted; when the reference count
16 reaches zero there are no references to the object left and it can be
17 removed from the heap.
18 
19 An object has a 'type' that determines what it represents and what kind
20 of data it contains.  An object's type is fixed when it is created.
21 Types themselves are represented as objects; an object contains a
22 pointer to the corresponding type object.  The type itself has a type
23 pointer pointing to the object representing the type 'type', which
24 contains a pointer to itself!).
25 
26 Objects do not float around in memory; once allocated an object keeps
27 the same size and address.  Objects that must hold variable-size data
28 can contain pointers to variable-size parts of the object.  Not all
29 objects of the same type have the same size; but the size cannot change
30 after allocation.  (These restrictions are made so a reference to an
31 object can be simply a pointer -- moving an object would require
32 updating all the pointers, and changing an object's size would require
33 moving it if there was another object right next to it.)
34 
35 Objects are always accessed through pointers of the type 'PyObject *'.
36 The type 'PyObject' is a structure that only contains the reference count
37 and the type pointer.  The actual memory allocated for an object
38 contains other data that can only be accessed after casting the pointer
39 to a pointer to a longer structure type.  This longer type must start
40 with the reference count and type fields; the macro PyObject_HEAD should be
41 used for this (to accommodate for future changes).  The implementation
42 of a particular object type can cast the object pointer to the proper
43 type and back.
44 
45 A standard interface exists for objects that contain an array of items
46 whose size is determined when the object is allocated.
47   */
48 module deimos.python.object;
49 
50 import core.stdc.stdio;
51 import deimos.python.pyport;
52 import deimos.python.methodobject;
53 import deimos.python.structmember;
54 import deimos.python.descrobject;
55 
56 extern(C):
57 // Python-header-file: Include/object.h:
58 
59 // XXX:Conditionalize in if running debug build of Python interpreter:
60 /*
61    version (Python_Debug_Build) {
62    template _PyObject_HEAD_EXTRA() {
63    PyObject *_ob_next;
64    PyObject *_ob_prev;
65    }
66    } else {
67  */
68 /// _
69 template _PyObject_HEAD_EXTRA() {}
70 /*}*/
71 
72 version(Python_3_0_Or_Later) {
73     /// _
74     mixin template PyObject_HEAD() {
75         /// _
76         PyObject ob_base;
77     }
78 }else{
79     /// _
80     mixin template PyObject_HEAD() {
81         mixin _PyObject_HEAD_EXTRA;
82         /// _
83         Py_ssize_t ob_refcnt;
84         /// _
85         PyTypeObject* ob_type;
86     }
87 }
88 
89 /** Nothing is actually declared to be a PyObject, but every pointer to
90  * a Python object can be cast to a PyObject*.  This is inheritance built
91  * by hand.  Similarly every pointer to a variable-size Python object can,
92  * in addition, be cast to PyVarObject*.
93  */
94 struct PyObject {
95     version(Python_3_0_Or_Later) {
96         version(Issue7758Fixed) {
97             mixin _PyObject_HEAD_EXTRA;
98         }
99         Py_ssize_t ob_refcnt;
100         PyTypeObject* ob_type;
101     }else {
102         mixin PyObject_HEAD;
103     }
104 }
105 
106 /**
107 Denotes a borrowed reference.
108 (Not part of Python api)
109 
110 Intended use: An api function Foo returning a borrowed reference will
111 have return type Borrowed!PyObject* instead of PyObject*. Py_INCREF can
112 be used to get the original type back.
113 
114 Params:
115 T = Python object type (PyObject, PyTypeObject, etc)
116 
117 Example:
118 ---
119 Borrowed!PyObject* borrowed = PyTuple_GetItem(tuple, 0);
120 PyObject* item = Py_XINCREF(borrowed);
121 ---
122 */
123 struct Borrowed(T) { }
124 alias Borrowed!PyObject PyObject_BorrowedRef;
125 /**
126 Convert a python reference to borrowed reference.
127 (Not part of Python api)
128 */
129 Borrowed!T* borrowed(T)(T* obj) {
130     return cast(Borrowed!T*) obj;
131 }
132 
133 version(Python_3_0_Or_Later) {
134     /// _
135     mixin template PyObject_VAR_HEAD() {
136         /// _
137         PyVarObject ob_base;
138     }
139 }else {
140     /** PyObject_VAR_HEAD defines the initial segment of all variable-size
141      * container objects.  These end with a declaration of an array with 1
142      * element, but enough space is malloc'ed so that the array actually
143      * has room for ob_size elements.  Note that ob_size is an element count,
144      * not necessarily a byte count.
145      */
146     mixin template PyObject_VAR_HEAD() {
147         mixin PyObject_HEAD;
148         /// _
149         Py_ssize_t ob_size; /* Number of items in variable part */
150     }
151 }
152 
153 /// _
154 struct PyVarObject {
155     version(Python_3_0_Or_Later) {
156         version(Issue7758Fixed) {
157             mixin PyObject_HEAD;
158         }else{
159             PyObject ob_base;
160         }
161         Py_ssize_t ob_size; /* Number of items in variable part */
162     }else{
163         mixin PyObject_VAR_HEAD;
164     }
165 }
166 
167 /// _
168 auto Py_REFCNT(T)(T* ob) {
169     return (cast(PyObject*)ob).ob_refcnt;
170 }
171 /// _
172 auto Py_TYPE(T)(T* ob) {
173     return (cast(PyObject*)ob).ob_type;
174 }
175 /// _
176 auto Py_SIZE(T)(T* ob) {
177     return (cast(PyVarObject*)ob).ob_size;
178 }
179 
180 /// Not part of the python api, but annoying to do without.
181 void Py_SET_REFCNT(T)(T* ob, int refcnt) {
182     (cast(PyObject*) ob).ob_refcnt = refcnt;
183 }
184 /// ditto
185 void Py_SET_TYPE(T)(T* ob, PyTypeObject* tipo) {
186     (cast(PyObject*)ob).ob_type = tipo;
187 }
188 /// ditto
189 void Py_SET_SIZE(T)(T* ob, Py_ssize_t size) {
190     (cast(PyVarObject*)ob).ob_size = size;
191 }
192 
193 /// _
194 alias PyObject* function(PyObject*) unaryfunc;
195 /// _
196 alias PyObject* function(PyObject*, PyObject*) binaryfunc;
197 /// _
198 alias PyObject* function(PyObject*, PyObject*, PyObject*) ternaryfunc;
199 /// _
200 alias Py_ssize_t function(PyObject*) lenfunc;
201 /// _
202 alias lenfunc inquiry;
203 version(Python_3_0_Or_Later) {
204 }else{
205     /// Availability: 2.*
206     alias int function(PyObject**, PyObject**) coercion;
207 }
208 /// _
209 alias PyObject* function(PyObject*, Py_ssize_t) ssizeargfunc;
210 /// _
211 alias PyObject* function(PyObject*, Py_ssize_t, Py_ssize_t) ssizessizeargfunc;
212 version(Python_2_5_Or_Later){
213 }else{
214     /// Availability: 2.4
215     alias ssizeargfunc intargfunc;
216     /// Availability: 2.4
217     alias ssizessizeargfunc intintargfunc;
218 }
219 /// _
220 alias int function(PyObject*, Py_ssize_t, PyObject*) ssizeobjargproc;
221 /// _
222 alias int function(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*) ssizessizeobjargproc;
223 version(Python_2_5_Or_Later){
224 }else{
225     /// Availability: 2.4
226     alias ssizeobjargproc intobjargproc;
227     /// Availability: 2.4
228     alias ssizessizeobjargproc intintobjargproc;
229 }
230 /// _
231 alias int function(PyObject*, PyObject*, PyObject*) objobjargproc;
232 
233 version(Python_3_0_Or_Later) {
234 }else{
235     /// ssize_t-based buffer interface
236     /// Availability: 2.*
237     alias Py_ssize_t function(PyObject*, Py_ssize_t, void**) readbufferproc;
238     /// ditto
239     alias Py_ssize_t function(PyObject*, Py_ssize_t, void**) writebufferproc;
240     /// ditto
241     alias Py_ssize_t function(PyObject*, Py_ssize_t*) segcountproc;
242     /// ditto
243     alias Py_ssize_t function(PyObject*, Py_ssize_t, char**) charbufferproc;
244 }
245 version(Python_2_5_Or_Later){
246 }else{
247     /// int-based buffer interface
248     /// Availability: 2.4
249     alias readbufferproc getreadbufferproc;
250     /// ditto
251     alias writebufferproc getwritebufferproc;
252     /// ditto
253     alias segcountproc getsegcountproc;
254     /// ditto
255     alias charbufferproc getcharbufferproc;
256 }
257 
258 version(Python_2_6_Or_Later){
259     /** Py3k buffer interface */
260     /// Availability: >= 2.6
261     struct Py_buffer{
262         void* buf;
263         /** borrowed reference */
264         Borrowed!PyObject* obj;
265         /// _
266         Py_ssize_t len;
267         /** This is Py_ssize_t so it can be
268           pointed to by strides in simple case.*/
269         Py_ssize_t itemsize;
270         /// _
271         int readonly;
272         /// _
273         int ndim;
274         /// _
275         char* format;
276         /// _
277         Py_ssize_t* shape;
278         /// _
279         Py_ssize_t* strides;
280         /// _
281         Py_ssize_t* suboffsets;
282         version(Python_2_7_Or_Later) {
283             /** static store for shape and strides of
284               mono-dimensional buffers. */
285             /// Availability: >= 2.7
286             Py_ssize_t[2] smalltable;
287         }
288         /// _
289         void* internal;
290     };
291 
292     /// Availability: >= 2.6
293     alias int function(PyObject*, Py_buffer*, int) getbufferproc;
294     /// Availability: >= 2.6
295     alias void function(PyObject*, Py_buffer*) releasebufferproc;
296 
297     /** Flags for getting buffers */
298     /// Availability: >= 2.6
299     enum PyBUF_SIMPLE = 0;
300     /// ditto
301     enum PyBUF_WRITABLE = 0x0001;
302     /*  we used to include an E, backwards compatible alias  */
303     /// ditto
304     enum PyBUF_WRITEABLE = PyBUF_WRITABLE;
305     /// ditto
306     enum PyBUF_FORMAT = 0x0004;
307     /// ditto
308     enum PyBUF_ND = 0x0008;
309     /// ditto
310     enum PyBUF_STRIDES = (0x0010 | PyBUF_ND);
311     /// ditto
312     enum PyBUF_C_CONTIGUOUS = (0x0020 | PyBUF_STRIDES);
313     /// ditto
314     enum PyBUF_F_CONTIGUOUS = (0x0040 | PyBUF_STRIDES);
315     /// ditto
316     enum PyBUF_ANY_CONTIGUOUS = (0x0080 | PyBUF_STRIDES);
317     /// ditto
318     enum PyBUF_INDIRECT = (0x0100 | PyBUF_STRIDES);
319 
320     /// ditto
321     enum PyBUF_CONTIG = (PyBUF_ND | PyBUF_WRITABLE);
322     /// ditto
323     enum PyBUF_CONTIG_RO = (PyBUF_ND);
324 
325     /// ditto
326     enum PyBUF_STRIDED = (PyBUF_STRIDES | PyBUF_WRITABLE);
327     /// ditto
328     enum PyBUF_STRIDED_RO = (PyBUF_STRIDES);
329 
330     /// ditto
331     enum PyBUF_RECORDS = (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT);
332     /// ditto
333     enum PyBUF_RECORDS_RO = (PyBUF_STRIDES | PyBUF_FORMAT);
334 
335     /// ditto
336     enum PyBUF_FULL = (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT);
337     /// ditto
338     enum PyBUF_FULL_RO = (PyBUF_INDIRECT | PyBUF_FORMAT);
339 
340 
341     /// ditto
342     enum PyBUF_READ  = 0x100;
343     /// ditto
344     enum PyBUF_WRITE = 0x200;
345     /// ditto
346     enum PyBUF_SHADOW = 0x400;
347     /* end Py3k buffer interface */
348 }
349 
350 /// _
351 alias int function(PyObject*, PyObject*) objobjproc;
352 /// _
353 alias int function(PyObject*, void*) visitproc;
354 /// _
355 alias int function(PyObject*, visitproc, void*) traverseproc;
356 
357 /** For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
358    arguments are guaranteed to be of the object's type (modulo
359    coercion hacks -- i.e. if the type's coercion function
360    returns other types, then these are allowed as well).  Numbers that
361    have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
362    arguments for proper type and implement the necessary conversions
363    in the slot functions themselves. */
364 struct PyNumberMethods {
365     binaryfunc nb_add;
366     binaryfunc nb_subtract;
367     binaryfunc nb_multiply;
368     version(Python_3_0_Or_Later) {
369     }else {
370         binaryfunc nb_divide;
371     }
372     binaryfunc nb_remainder;
373     binaryfunc nb_divmod;
374     ternaryfunc nb_power;
375     unaryfunc nb_negative;
376     unaryfunc nb_positive;
377     unaryfunc nb_absolute;
378     version(Python_3_0_Or_Later) {
379         inquiry nb_bool;
380     }else {
381         inquiry nb_nonzero;
382     }
383     unaryfunc nb_invert;
384     binaryfunc nb_lshift;
385     binaryfunc nb_rshift;
386     binaryfunc nb_and;
387     binaryfunc nb_xor;
388     binaryfunc nb_or;
389     version(Python_3_0_Or_Later) {
390     }else{
391         coercion nb_coerce;
392     }
393     unaryfunc nb_int;
394     version(Python_3_0_Or_Later) {
395         void* nb_reserved;  /* the slot formerly known as nb_long */
396     }else{
397         unaryfunc nb_long;
398     }
399     unaryfunc nb_float;
400     version(Python_3_0_Or_Later) {
401     }else{
402         unaryfunc nb_oct;
403         unaryfunc nb_hex;
404     }
405 
406     binaryfunc nb_inplace_add;
407     binaryfunc nb_inplace_subtract;
408     binaryfunc nb_inplace_multiply;
409     version(Python_3_0_Or_Later) {
410     }else{
411         binaryfunc nb_inplace_divide;
412     }
413     binaryfunc nb_inplace_remainder;
414     ternaryfunc nb_inplace_power;
415     binaryfunc nb_inplace_lshift;
416     binaryfunc nb_inplace_rshift;
417     binaryfunc nb_inplace_and;
418     binaryfunc nb_inplace_xor;
419     binaryfunc nb_inplace_or;
420 
421     /** These require the Py_TPFLAGS_HAVE_CLASS flag */
422     binaryfunc nb_floor_divide;
423     ///ditto
424     binaryfunc nb_true_divide;
425     ///ditto
426     binaryfunc nb_inplace_floor_divide;
427     ///ditto
428     binaryfunc nb_inplace_true_divide;
429 
430     version(Python_2_5_Or_Later){
431         /// Availability: >= 2.5
432         unaryfunc nb_index;
433     }
434 
435     version(Python_3_5_Or_Later) {
436         binaryfunc nb_matrix_multiply;
437         binaryfunc nb_inplace_matrix_multiply;
438     }
439 }
440 
441 /// _
442 struct PySequenceMethods {
443     /// _
444     lenfunc sq_length;
445     /// _
446     binaryfunc sq_concat;
447     /// _
448     ssizeargfunc sq_repeat;
449     /// _
450     ssizeargfunc sq_item;
451     version(Python_3_0_Or_Later) {
452         /// _
453         void* was_sq_slice;
454     }else{
455         /// Availability: 2.*
456         ssizessizeargfunc sq_slice;
457     }
458     /// _
459     ssizeobjargproc sq_ass_item;
460     version(Python_3_0_Or_Later) {
461         /// _
462         void* was_sq_ass_slice;
463     }else{
464         /// Availability: 2.*
465         ssizessizeobjargproc sq_ass_slice;
466     }
467     /// _
468     objobjproc sq_contains;
469     /// _
470     binaryfunc sq_inplace_concat;
471     /// _
472     ssizeargfunc sq_inplace_repeat;
473 }
474 
475 /// _
476 struct PyMappingMethods {
477     /// _
478     lenfunc mp_length;
479     /// _
480     binaryfunc mp_subscript;
481     /// _
482     objobjargproc mp_ass_subscript;
483 }
484 
485 version(Python_3_5_Or_Later) {
486     /// _
487     struct PyAsyncMethods {
488         unaryfunc am_await;
489         unaryfunc am_aiter;
490         unaryfunc am_anext;
491     }
492 }
493 
494 /// _
495 struct PyBufferProcs {
496     version(Python_3_0_Or_Later) {
497     }else{
498         /// Availability: 3.*
499         readbufferproc bf_getreadbuffer;
500         /// Availability: 3.*
501         writebufferproc bf_getwritebuffer;
502         /// Availability: 3.*
503         segcountproc bf_getsegcount;
504         /// Availability: 3.*
505         charbufferproc bf_getcharbuffer;
506     }
507     version(Python_2_6_Or_Later){
508         /// Availability: 2.6, 2.7
509         getbufferproc bf_getbuffer;
510         /// Availability: 2.6, 2.7
511         releasebufferproc bf_releasebuffer;
512     }
513 }
514 
515 
516 /// _
517 alias void function(void*) freefunc;
518 /// _
519 alias void function(PyObject*) destructor;
520 /// _
521 alias int function(PyObject*, FILE*, int) printfunc;
522 /// _
523 alias PyObject* function(PyObject*, char*) getattrfunc;
524 /// _
525 alias PyObject* function(PyObject*, PyObject*) getattrofunc;
526 /// _
527 alias int function(PyObject*, char*, PyObject*) setattrfunc;
528 /// _
529 alias int function(PyObject*, PyObject*, PyObject*) setattrofunc;
530 version(Python_3_0_Or_Later) {
531 }else{
532     /// Availability: 2.*
533     alias int function(PyObject*, PyObject*) cmpfunc;
534 }
535 /// _
536 alias PyObject* function(PyObject*) reprfunc;
537 /// _
538 alias Py_hash_t function(PyObject*) hashfunc;
539 /// _
540 alias PyObject* function(PyObject*, PyObject*, int) richcmpfunc;
541 /// _
542 alias PyObject* function(PyObject*) getiterfunc;
543 /// _
544 alias PyObject* function(PyObject*) iternextfunc;
545 /// _
546 alias PyObject* function(PyObject*, PyObject*, PyObject*) descrgetfunc;
547 /// _
548 alias int function(PyObject*, PyObject*, PyObject*) descrsetfunc;
549 /// _
550 alias int function(PyObject*, PyObject*, PyObject*) initproc;
551 /// _
552 alias PyObject* function(PyTypeObject*, PyObject*, PyObject*) newfunc;
553 /// _
554 alias PyObject* function(PyTypeObject*, Py_ssize_t) allocfunc;
555 
556 /**
557 Type objects contain a string containing the type name (to help somewhat
558 in debugging), the allocation parameters (see PyObject_New() and
559 PyObject_NewVar()),
560 and methods for accessing objects of the type.  Methods are optional, a
561 nil pointer meaning that particular kind of access is not available for
562 this type.  The Py_DECREF() macro uses the tp_dealloc method without
563 checking for a nil pointer; it should always be implemented except if
564 the implementation can guarantee that the reference count will never
565 reach zero (e.g., for statically allocated type objects).
566 
567 NB: the methods for certain type groups are now contained in separate
568 method blocks.
569 */
570 struct PyTypeObject {
571     version(Issue7758Fixed) {
572         mixin PyObject_VAR_HEAD;
573     }else{
574         version(Python_3_0_Or_Later) {
575             PyVarObject ob_base;
576         }else {
577             Py_ssize_t ob_refcnt;
578             PyTypeObject* ob_type;
579             Py_ssize_t ob_size; /* Number of items in variable part */
580         }
581     }
582     /** For printing, in format "<module>.<name>" */
583     const(char)* tp_name;
584     /** For allocation */
585     Py_ssize_t tp_basicsize, tp_itemsize;
586 
587     /** Methods to implement standard operations */
588     destructor tp_dealloc;
589     /// ditto
590     printfunc tp_print;
591     /// ditto
592     getattrfunc tp_getattr;
593     /// ditto
594     setattrfunc tp_setattr;
595     /// ditto
596     version(Python_3_5_Or_Later) {
597         PyAsyncMethods* tp_as_async;
598     }else version(Python_3_0_Or_Later) {
599         void* tp_reserved; 
600     }else{
601         cmpfunc tp_compare;
602     }
603     /// ditto
604     reprfunc tp_repr;
605 
606     /** Method suites for standard classes */
607     PyNumberMethods* tp_as_number;
608     /// ditto
609     PySequenceMethods* tp_as_sequence;
610     /// ditto
611     PyMappingMethods* tp_as_mapping;
612 
613     /** More standard operations (here for binary compatibility) */
614     hashfunc tp_hash;
615     /// ditto
616     ternaryfunc tp_call;
617     /// ditto
618     reprfunc tp_str;
619     /// ditto
620     getattrofunc tp_getattro;
621     /// ditto
622     setattrofunc tp_setattro;
623 
624     /** Functions to access object as input/output buffer */
625     PyBufferProcs* tp_as_buffer;
626 
627     /** Flags to define presence of optional/expanded features */
628     C_ulong tp_flags;
629 
630     /** Documentation string */
631     const(char)* tp_doc;
632 
633     /** call function for all accessible objects */
634     traverseproc tp_traverse;
635 
636     /** delete references to contained objects */
637     inquiry tp_clear;
638 
639     /** rich comparisons */
640     richcmpfunc tp_richcompare;
641 
642     /** weak reference enabler */
643     version(Python_2_5_Or_Later){
644         Py_ssize_t tp_weaklistoffset;
645     }else{
646         C_long tp_weaklistoffset;
647     }
648 
649     /** Iterators */
650     getiterfunc tp_iter;
651     /// ditto
652     iternextfunc tp_iternext;
653 
654     /** Attribute descriptor and subclassing stuff */
655     PyMethodDef* tp_methods;
656     /// ditto
657     PyMemberDef* tp_members;
658     /// ditto
659     PyGetSetDef* tp_getset;
660     /// ditto
661     PyTypeObject* tp_base;
662     /// ditto
663     PyObject* tp_dict;
664     /// ditto
665     descrgetfunc tp_descr_get;
666     /// ditto
667     descrsetfunc tp_descr_set;
668     /// ditto
669     version(Python_2_5_Or_Later){
670         Py_ssize_t tp_dictoffset;
671     }else{
672         C_long tp_dictoffset;
673     }
674     /// ditto
675     initproc tp_init;
676     /// ditto
677     allocfunc tp_alloc;
678     /// ditto
679     newfunc tp_new;
680     /** Low-level free-memory routine */
681     freefunc tp_free;
682     /** For PyObject_IS_GC */
683     inquiry tp_is_gc;
684     /// _
685     PyObject* tp_bases;
686     /** method resolution order */
687     PyObject* tp_mro;
688     /// _
689     PyObject* tp_cache;
690     /// _
691     PyObject* tp_subclasses;
692     /// _
693     PyObject* tp_weaklist;
694     /// _
695     destructor tp_del;
696     version(Python_2_6_Or_Later){
697         /** Type attribute cache version tag. Added in version 2.6 */
698         uint tp_version_tag;
699     }
700 }
701 
702 version(Python_3_0_Or_Later) {
703     /// Availability: 3.*
704     struct PyType_Slot{
705         /** slot id, see below */
706         int slot;
707         /** function pointer */
708         void* pfunc;
709     }
710 
711     /// Availability: 3.*
712     struct PyType_Spec{
713         /// _
714         const(char)* name;
715         /// _
716         int basicsize;
717         /// _
718         int itemsize;
719         /// _
720         int flags;
721         /** terminated by slot==0. */
722         PyType_Slot* slots;
723     }
724 
725     /// Availability: 3.*
726     PyObject* PyType_FromSpec(PyType_Spec*);
727 }
728 
729 /** The *real* layout of a type object when allocated on the heap */
730 struct PyHeapTypeObject {
731     version(Python_2_5_Or_Later){
732         /// Availability: >= 2.5
733         PyTypeObject ht_type;
734     }else{
735         /// Availability: 2.4
736         PyTypeObject type;
737     }
738     version(Python_3_5_Or_Later) {
739         /// Availability: >= 3.5
740         PyAsyncMethods as_async;
741     }
742     /// _
743     PyNumberMethods as_number;
744     /// _
745     PyMappingMethods as_mapping;
746     /** as_sequence comes after as_mapping,
747        so that the mapping wins when both
748        the mapping and the sequence define
749        a given operator (e.g. __getitem__).
750        see add_operators() in typeobject.c . */
751     PySequenceMethods as_sequence;
752     /// _
753     PyBufferProcs as_buffer;
754     version(Python_2_5_Or_Later){
755         /// Availability: >= 2.5
756         PyObject* ht_name;
757         /// Availability: >= 2.5
758         PyObject* ht_slots;
759     }else{
760         /// Availability: 2.4
761         PyObject* name;
762         /// Availability: 2.4
763         PyObject* slots;
764     }
765 }
766 
767 /** Generic type check */
768 int PyType_IsSubtype(PyTypeObject*, PyTypeObject*);
769 
770 // D translation of C macro:
771 /// _
772 int PyObject_TypeCheck()(PyObject* ob, PyTypeObject* tp) {
773     return (ob.ob_type == tp || PyType_IsSubtype(ob.ob_type, tp));
774 }
775 
776 /** built-in 'type' */
777 mixin(PyAPI_DATA!"PyTypeObject PyType_Type");
778 /** built-in 'object' */
779 mixin(PyAPI_DATA!"PyTypeObject PyBaseObject_Type");
780 /** built-in 'super' */
781 mixin(PyAPI_DATA!"PyTypeObject PySuper_Type");
782 
783 version(Python_3_2_Or_Later) {
784     /// Availability: >= 3.2
785     C_long PyType_GetFlags(PyTypeObject*);
786 }
787 
788 // D translation of C macro:
789 /// _
790 int PyType_Check()(PyObject* op) {
791     return PyObject_TypeCheck(op, &PyType_Type);
792 }
793 // D translation of C macro:
794 /// _
795 int PyType_CheckExact()(PyObject* op) {
796     return op.ob_type == &PyType_Type;
797 }
798 
799 /// _
800 int PyType_Ready(PyTypeObject*);
801 /// _
802 PyObject* PyType_GenericAlloc(PyTypeObject*, Py_ssize_t);
803 /// _
804 PyObject* PyType_GenericNew(PyTypeObject*, PyObject*, PyObject*);
805 /// _
806 PyObject* _PyType_Lookup(PyTypeObject*, PyObject*);
807 /// _
808 version(Python_2_7_Or_Later) {
809     /// Availability: >= 2.7
810     PyObject* _PyObject_LookupSpecial(PyObject*, char*, PyObject**);
811 }
812 version(Python_3_0_Or_Later) {
813     /// Availability: 3.*
814     PyTypeObject* _PyType_CalculateMetaclass(PyTypeObject*, PyObject*);
815 }
816 version(Python_2_6_Or_Later){
817     /// Availability: >= 2.6
818     uint PyType_ClearCache();
819     /// Availability: >= 2.6
820     void PyType_Modified(PyTypeObject *);
821 }
822 
823 /// _
824 int PyObject_Print(PyObject*, FILE*, int);
825 version(Python_3_0_Or_Later) {
826     /// Availability: 3.*
827     void _Py_BreakPoint();
828 }
829 /// _
830 PyObject* PyObject_Repr(PyObject*);
831 version(Python_3_0_Or_Later) {
832 }else version(Python_2_5_Or_Later) {
833     /// Availability: 2.5, 2.6, 2.7
834     PyObject* _PyObject_Str(PyObject*);
835 }
836 /// _
837 PyObject* PyObject_Str(PyObject*);
838 
839 version(Python_3_0_Or_Later) {
840     /// Availability: 3.*
841     PyObject* PyObject_ASCII(PyObject*);
842     /// Availability: 3.*
843     PyObject* PyObject_Bytes(PyObject*);
844 }else{
845     /// Availability: 2.*
846     alias PyObject_Str PyObject_Bytes;
847     /// Availability: 2.*
848     PyObject * PyObject_Unicode(PyObject*);
849     /// Availability: 2.*
850     int PyObject_Compare(PyObject*, PyObject*);
851 }
852 /// _
853 PyObject* PyObject_RichCompare(PyObject*, PyObject*, int);
854 /// _
855 int PyObject_RichCompareBool(PyObject*, PyObject*, int);
856 /// _
857 PyObject* PyObject_GetAttrString(PyObject*, const(char)*);
858 /// _
859 int PyObject_SetAttrString(PyObject*, const(char)*, PyObject*);
860 /// _
861 int PyObject_HasAttrString(PyObject*, const(char)*);
862 /// _
863 PyObject* PyObject_GetAttr(PyObject*, PyObject*);
864 /// _
865 int PyObject_SetAttr(PyObject*, PyObject*, PyObject*);
866 /// _
867 int PyObject_HasAttr(PyObject*, PyObject*);
868 /// _
869 PyObject* PyObject_SelfIter(PyObject*);
870 /// _
871 PyObject* PyObject_GenericGetAttr(PyObject*, PyObject*);
872 /// _
873 int PyObject_GenericSetAttr(PyObject*,
874         PyObject*, PyObject*);
875 /// _
876 Py_hash_t PyObject_Hash(PyObject*);
877 version(Python_2_6_Or_Later) {
878     /// Availability: >= 2.6
879     Py_hash_t PyObject_HashNotImplemented(PyObject*);
880 }
881 /// _
882 int PyObject_IsTrue(PyObject*);
883 /// _
884 int PyObject_Not(PyObject*);
885 /// _
886 int PyCallable_Check(PyObject*);
887 version(Python_3_0_Or_Later) {
888 }else{
889     /// Availability: 2.*
890     int PyNumber_Coerce(PyObject**, PyObject**);
891     /// Availability: 2.*
892     int PyNumber_CoerceEx(PyObject**, PyObject**);
893 }
894 
895 /// _
896 void PyObject_ClearWeakRefs(PyObject*);
897 
898 /** PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
899    list of strings.  PyObject_Dir(NULL) is like __builtin__.dir(),
900    returning the names of the current locals.  In this case, if there are
901    no current locals, NULL is returned, and PyErr_Occurred() is false.
902 */
903 PyObject * PyObject_Dir(PyObject *);
904 
905 /** Helpers for printing recursive container types */
906 int Py_ReprEnter(PyObject *);
907 /// ditto
908 void Py_ReprLeave(PyObject *);
909 
910 /// _
911 Py_hash_t _Py_HashDouble(double);
912 /// _
913 Py_hash_t _Py_HashPointer(void*);
914 
915 version(Python_3_1_Or_Later) {
916     version = Py_HashSecret;
917 }else version(Python_3_0_Or_Later) {
918 }else version(Python_2_7_Or_Later) {
919     version = Py_HashSecret;
920 }
921 version(Py_HashSecret) {
922     /// Availability: 2.7, >= 3.1
923     struct _Py_HashSecret_t{
924         /// _
925         Py_hash_t prefix;
926         /// _
927         Py_hash_t suffix;
928     }
929     /// Availability: 2.7, >= 3.1
930     mixin(PyAPI_DATA!"_Py_HashSecret_t _Py_HashSecret");
931 }
932 
933 /// _
934 auto PyObject_REPR()(PyObject* obj) {
935     version(Python_3_0_Or_Later) {
936         import deimos.python.unicodeobject;
937         return _PyUnicode_AsString(PyObject_Repr(obj));
938     }else{
939         import deimos.python.stringobject;
940         return PyString_AS_STRING(PyObject_Repr(obj));
941     }
942 }
943 /// _
944 enum int Py_PRINT_RAW = 1;
945 
946 
947 version(Python_3_0_Or_Later) {
948 }else{
949     /** PyBufferProcs contains bf_getcharbuffer */
950     /// Availability: 2.*
951     enum int Py_TPFLAGS_HAVE_GETCHARBUFFER       = 1L<<0;
952     /** PySequenceMethods contains sq_contains */
953     /// Availability: 2.*
954     enum int Py_TPFLAGS_HAVE_SEQUENCE_IN         = 1L<<1;
955     /** This is here for backwards compatibility.
956       Extensions that use the old GC
957       API will still compile but the objects will not be tracked by the GC. */
958     /// Availability: 2.*
959     enum int Py_TPFLAGS_GC                       = 0;
960     /** PySequenceMethods and PyNumberMethods contain in-place operators */
961     /// Availability: 2.*
962     enum int Py_TPFLAGS_HAVE_INPLACEOPS          = 1L<<3;
963     /** PyNumberMethods do their own coercion */
964     /// Availability: 2.*
965     enum int Py_TPFLAGS_CHECKTYPES               = 1L<<4;
966     /** tp_richcompare is defined */
967     /// Availability: 2.*
968     enum int Py_TPFLAGS_HAVE_RICHCOMPARE         = 1L<<5;
969     /** Objects which are weakly referencable if their tp_weaklistoffset is >0 */
970     /// Availability: 2.*
971     enum int Py_TPFLAGS_HAVE_WEAKREFS            = 1L<<6;
972     /** tp_iter is defined */
973     /// Availability: 2.*
974     enum int Py_TPFLAGS_HAVE_ITER                = 1L<<7;
975     /** New members introduced by Python 2.2 exist */
976     /// Availability: 2.*
977     enum int Py_TPFLAGS_HAVE_CLASS               = 1L<<8;
978 }
979 /** Set if the type object is dynamically allocated */
980 enum int Py_TPFLAGS_HEAPTYPE                 = 1L<<9;
981 /** Set if the type allows subclassing */
982 enum int Py_TPFLAGS_BASETYPE                 = 1L<<10;
983 /** Set if the type is 'ready' -- fully initialized */
984 enum int Py_TPFLAGS_READY                    = 1L<<12;
985 /** Set while the type is being 'readied', to prevent recursive ready calls */
986 enum int Py_TPFLAGS_READYING                 = 1L<<13;
987 /** Objects support garbage collection (see objimp.h) */
988 enum int Py_TPFLAGS_HAVE_GC                  = 1L<<14;
989 
990 // YYY: Should conditionalize for stackless:
991 //#ifdef STACKLESS
992 //#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
993 //#else
994 /// _
995 enum int Py_TPFLAGS_HAVE_STACKLESS_EXTENSION = 0;
996 //#endif
997 version(Python_3_0_Or_Later) {
998 }else version(Python_2_5_Or_Later){
999     /** Objects support nb_index in PyNumberMethods */
1000     /// Availability: 2.*
1001     enum Py_TPFLAGS_HAVE_INDEX               = 1L<<17;
1002 }
1003 version(Python_2_6_Or_Later){
1004     /** Objects support type attribute cache */
1005     /// Availability: >= 2.6
1006     enum Py_TPFLAGS_HAVE_VERSION_TAG =  (1L<<18);
1007     /// ditto
1008     enum Py_TPFLAGS_VALID_VERSION_TAG =  (1L<<19);
1009 
1010     /** Type is abstract and cannot be instantiated */
1011     /// Availability: >= 2.6
1012     enum Py_TPFLAGS_IS_ABSTRACT = (1L<<20);
1013 
1014     version(Python_3_0_Or_Later) {
1015     }else {
1016         /** Has the new buffer protocol */
1017         /// Availability: 2.6,2.7
1018         enum Py_TPFLAGS_HAVE_NEWBUFFER = (1L<<21);
1019     }
1020 
1021     /** These flags are used to determine if a type is a subclass. */
1022     /// Availability: >= 2.6
1023     enum Py_TPFLAGS_INT_SUBCLASS         =(1L<<23);
1024     /// ditto
1025     enum Py_TPFLAGS_LONG_SUBCLASS        =(1L<<24);
1026     /// ditto
1027     enum Py_TPFLAGS_LIST_SUBCLASS        =(1L<<25);
1028     /// ditto
1029     enum Py_TPFLAGS_TUPLE_SUBCLASS       =(1L<<26);
1030     /// ditto
1031     version(Python_3_0_Or_Later) {
1032         enum Py_TPFLAGS_BYTES_SUBCLASS      =(1L<<27);
1033     }else{
1034         enum Py_TPFLAGS_STRING_SUBCLASS      =(1L<<27);
1035     }
1036     /// ditto
1037     enum Py_TPFLAGS_UNICODE_SUBCLASS     =(1L<<28);
1038     /// ditto
1039     enum Py_TPFLAGS_DICT_SUBCLASS        =(1L<<29);
1040     /// ditto
1041     enum Py_TPFLAGS_BASE_EXC_SUBCLASS    =(1L<<30);
1042     /// ditto
1043     enum Py_TPFLAGS_TYPE_SUBCLASS        =(1L<<31);
1044 }
1045 
1046 version(Python_3_0_Or_Later) {
1047     /// _
1048     enum Py_TPFLAGS_DEFAULT = Py_TPFLAGS_HAVE_STACKLESS_EXTENSION |
1049         Py_TPFLAGS_HAVE_VERSION_TAG;
1050 }else version(Python_2_5_Or_Later){
1051     /// _
1052     enum Py_TPFLAGS_DEFAULT =
1053         Py_TPFLAGS_HAVE_GETCHARBUFFER |
1054         Py_TPFLAGS_HAVE_SEQUENCE_IN |
1055         Py_TPFLAGS_HAVE_INPLACEOPS |
1056         Py_TPFLAGS_HAVE_RICHCOMPARE |
1057         Py_TPFLAGS_HAVE_WEAKREFS |
1058         Py_TPFLAGS_HAVE_ITER |
1059         Py_TPFLAGS_HAVE_CLASS |
1060         Py_TPFLAGS_HAVE_STACKLESS_EXTENSION |
1061         Py_TPFLAGS_HAVE_INDEX |
1062         0
1063         ;
1064     version(Python_2_6_Or_Later) {
1065         // meh
1066         enum Py_TPFLAGS_DEFAULT_EXTERNAL = Py_TPFLAGS_DEFAULT;
1067     }
1068 }else{
1069     /// _
1070     enum int Py_TPFLAGS_DEFAULT =
1071         Py_TPFLAGS_HAVE_GETCHARBUFFER |
1072         Py_TPFLAGS_HAVE_SEQUENCE_IN |
1073         Py_TPFLAGS_HAVE_INPLACEOPS |
1074         Py_TPFLAGS_HAVE_RICHCOMPARE |
1075         Py_TPFLAGS_HAVE_WEAKREFS |
1076         Py_TPFLAGS_HAVE_ITER |
1077         Py_TPFLAGS_HAVE_CLASS |
1078         Py_TPFLAGS_HAVE_STACKLESS_EXTENSION |
1079         0
1080         ;
1081 }
1082 
1083 // D translation of C macro:
1084 /// _
1085 int PyType_HasFeature()(PyTypeObject* t, int f) {
1086     version(Python_3_2_Or_Later) {
1087         return (PyType_GetFlags(t) & f) != 0;
1088     }else{
1089         return (t.tp_flags & f) != 0;
1090     }
1091 }
1092 
1093 version(Python_2_6_Or_Later){
1094     alias PyType_HasFeature PyType_FastSubclass;
1095 }
1096 
1097 /**
1098 Initializes reference counts to 1, and
1099 in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
1100 bookkeeping appropriate to the special build.
1101 */
1102 void _Py_NewReference()(PyObject* op) {
1103     Py_SET_REFCNT(op, 1);
1104 }
1105 
1106 /**
1107 Increment reference counts.  Can be used wherever a void expression is allowed.
1108 The argument must not be a NULL pointer. If it may be NULL, use
1109 Py_XINCREF instead.
1110 
1111 In addition, converts and returns Borrowed references to their base types.
1112 */
1113 auto Py_INCREF(T)(T op)
1114 if(is(T == PyObject*) || is(T _unused : Borrowed!P*, P))
1115 {
1116     static if(is(T _unused : Borrowed!P*, P)) {
1117         PyObject* pop = cast(PyObject*) op;
1118         ++pop.ob_refcnt;
1119         return cast(P*) pop;
1120     }else {
1121         ++op.ob_refcnt;
1122     }
1123 }
1124 
1125 /**
1126 Increment reference counts.  Can be used wherever a void expression is allowed.
1127 The argument may be a NULL pointer.
1128 
1129 In addition, converts and returns Borrowed references to their base types.
1130 The argument may not be null.
1131 */
1132 auto Py_XINCREF(T)(T op) {
1133     if (op == null) {
1134         //static if(is(typeof(return) == void))
1135         static if(is(typeof(Py_INCREF!T(op)) == void))
1136             return;
1137         else {
1138             assert(0, "INCREF on null");
1139         }
1140     }
1141     return Py_INCREF(op);
1142 }
1143 
1144 /**
1145 Used to decrement reference counts. Calls the object's deallocator function
1146 when the refcount falls to 0; for objects that don't contain references to
1147 other objects or heap memory this can be the standard function free().
1148 Can be used wherever a void expression is allowed.  The argument must not be a
1149 NULL pointer.  If it may be NULL, use Py_XDECREF instead.
1150 */
1151 void Py_DECREF()(PyObject *op) {
1152     // version(PY_REF_DEBUG) _Py_RefTotal++
1153     --op.ob_refcnt;
1154 
1155     // EMN: this is a horrible idea because it takes forever to figure out
1156     //      what's going on if this is being called from within the garbage
1157     //      collector.
1158 
1159     // EMN: if we do keep it, don't change the assert!
1160     // assert(0) or assert(condition) mess up linking somehow.
1161     if(op.ob_refcnt < 0) assert (0, "refcount negative");
1162     if(op.ob_refcnt != 0) {
1163         // version(PY_REF_DEBUG) _Py_NegativeRefcount(__FILE__, __LINE__, cast(PyObject*)op);
1164     }else {
1165         op.ob_type.tp_dealloc(op);
1166     }
1167 }
1168 
1169 /** Same as Py_DECREF, except is a no-op if op is null.
1170   */
1171 void Py_XDECREF()(PyObject* op)
1172 {
1173     if(op == null) {
1174         return;
1175     }
1176 
1177     Py_DECREF(op);
1178 }
1179 
1180 /**
1181 These are provided as conveniences to Python runtime embedders, so that
1182 they can have object code that is not dependent on Python compilation flags.
1183 */
1184 void Py_IncRef(PyObject *);
1185 /// ditto
1186 void Py_DecRef(PyObject *);
1187 
1188 mixin(PyAPI_DATA!"PyObject _Py_NoneStruct");
1189 
1190 // issue 8683 gets in the way of this being a property
1191 Borrowed!PyObject* Py_None()() {
1192     return borrowed(&_Py_NoneStruct);
1193 }
1194 /** Rich comparison opcodes */
1195 enum Py_LT = 0;
1196 /// ditto
1197 enum Py_LE = 1;
1198 /// ditto
1199 enum Py_EQ = 2;
1200 /// ditto
1201 enum Py_NE = 3;
1202 /// ditto
1203 enum Py_GT = 4;
1204 /// ditto
1205 enum Py_GE = 5;
1206 
1207 version(Python_3_0_Or_Later) {
1208     /// Availability: 3.*
1209     void _Py_Dealloc(PyObject*);
1210 }