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