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