root/lang/d/pythond/python.d @ 10698

Revision 7202, 132.3 kB (checked in by itkz, 6 years ago)

call Python interpreter

Line 
1/* DSR:2005.10.26.16.28:
2
3XXX:
4
5- In a build process controlled by Python distutils, need to detect whether the
6  Python interpreter was built in debug build mode, and if so, make the
7  appropriate adjustments to the header mixins.
8
9*/
10
11module python;
12
13import std.c.stdio;
14import std.c.time;
15import std.string;
16
17/* D long is always 64 bits, but when the Python/C API mentions long, it is of
18 * course referring to the C type long, the size of which is 32 bits on both
19 * X86 and X86_64 under Windows, but 32 bits on X86 and 64 bits on X86_64 under
20 * most other operating systems. */
21
22alias long C_longlong;
23alias ulong C_ulonglong;
24
25version(Windows) {
26  alias int C_long;
27  alias uint C_ulong;
28} else {
29  version (X86) {
30    alias int C_long;
31    alias uint C_ulong;
32  } else {
33    alias long C_long;
34    alias ulong C_ulong;
35  }
36}
37
38
39extern (C) {
40///////////////////////////////////////////////////////////////////////////////
41// PYTHON DATA STRUCTURES AND ALIASES
42///////////////////////////////////////////////////////////////////////////////
43  // Python-header-file: Include/Python.h:
44  const int Py_single_input = 256;
45  const int Py_file_input = 257;
46  const int Py_eval_input = 258;
47
48  // Python-header-file: Include/object.h:
49
50  // XXX:Conditionalize in if running debug build of Python interpreter:
51  /*
52  version (Python_Debug_Build) {
53    template _PyObject_HEAD_EXTRA() {
54      PyObject *_ob_next;
55      PyObject *_ob_prev;
56    }
57  } else {
58  */
59    template _PyObject_HEAD_EXTRA() {}
60  /*}*/
61
62  template PyObject_HEAD() {
63    mixin _PyObject_HEAD_EXTRA;
64    int ob_refcnt;
65    PyTypeObject *ob_type;
66  }
67
68  struct PyObject {
69    mixin PyObject_HEAD;
70  }
71
72  template PyObject_VAR_HEAD() {
73    mixin PyObject_HEAD;
74    int ob_size; /* Number of items in variable part */
75  }
76
77  struct PyVarObject {
78    mixin PyObject_VAR_HEAD;
79  }
80
81  alias PyObject * (*unaryfunc)(PyObject *);
82  alias PyObject * (*binaryfunc)(PyObject *, PyObject *);
83  alias PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
84  alias int (*inquiry)(PyObject *);
85  alias int (*coercion)(PyObject **, PyObject **);
86  alias PyObject *(*intargfunc)(PyObject *, int);
87  alias PyObject *(*intintargfunc)(PyObject *, int, int);
88  alias int(*intobjargproc)(PyObject *, int, PyObject *);
89  alias int(*intintobjargproc)(PyObject *, int, int, PyObject *);
90  alias int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
91  alias int (*getreadbufferproc)(PyObject *, int, void **);
92  alias int (*getwritebufferproc)(PyObject *, int, void **);
93  alias int (*getsegcountproc)(PyObject *, int *);
94  alias int (*getcharbufferproc)(PyObject *, int, char **);
95  alias int (*objobjproc)(PyObject *, PyObject *);
96  alias int (*visitproc)(PyObject *, void *);
97  alias int (*traverseproc)(PyObject *, visitproc, void *);
98
99  // Python-header-file: Include/object.h:
100  struct PyNumberMethods {
101    binaryfunc nb_add;
102    binaryfunc nb_subtract;
103    binaryfunc nb_multiply;
104    binaryfunc nb_divide;
105    binaryfunc nb_remainder;
106    binaryfunc nb_divmod;
107    ternaryfunc nb_power;
108    unaryfunc nb_negative;
109    unaryfunc nb_positive;
110    unaryfunc nb_absolute;
111    inquiry nb_nonzero;
112    unaryfunc nb_invert;
113    binaryfunc nb_lshift;
114    binaryfunc nb_rshift;
115    binaryfunc nb_and;
116    binaryfunc nb_xor;
117    binaryfunc nb_or;
118    coercion nb_coerce;
119    unaryfunc nb_int;
120    unaryfunc nb_long;
121    unaryfunc nb_float;
122    unaryfunc nb_oct;
123    unaryfunc nb_hex;
124
125    binaryfunc nb_inplace_add;
126    binaryfunc nb_inplace_subtract;
127    binaryfunc nb_inplace_multiply;
128    binaryfunc nb_inplace_divide;
129    binaryfunc nb_inplace_remainder;
130    ternaryfunc nb_inplace_power;
131    binaryfunc nb_inplace_lshift;
132    binaryfunc nb_inplace_rshift;
133    binaryfunc nb_inplace_and;
134    binaryfunc nb_inplace_xor;
135    binaryfunc nb_inplace_or;
136
137
138    binaryfunc nb_floor_divide;
139    binaryfunc nb_true_divide;
140    binaryfunc nb_inplace_floor_divide;
141    binaryfunc nb_inplace_true_divide;
142  }
143
144  struct PySequenceMethods {
145    inquiry sq_length;
146    binaryfunc sq_concat;
147    intargfunc sq_repeat;
148    intargfunc sq_item;
149    intintargfunc sq_slice;
150    intobjargproc sq_ass_item;
151    intintobjargproc sq_ass_slice;
152    objobjproc sq_contains;
153    binaryfunc sq_inplace_concat;
154    intargfunc sq_inplace_repeat;
155  }
156
157  struct PyMappingMethods {
158    inquiry mp_length;
159    binaryfunc mp_subscript;
160    objobjargproc mp_ass_subscript;
161  }
162
163  struct PyBufferProcs {
164    getreadbufferproc bf_getreadbuffer;
165    getwritebufferproc bf_getwritebuffer;
166    getsegcountproc bf_getsegcount;
167    getcharbufferproc bf_getcharbuffer;
168  }
169
170
171  alias void (*freefunc)(void *);
172  alias void (*destructor)(PyObject *);
173  alias int (*printfunc)(PyObject *, FILE *, int);
174  alias PyObject *(*getattrfunc)(PyObject *, char *);
175  alias PyObject *(*getattrofunc)(PyObject *, PyObject *);
176  alias int (*setattrfunc)(PyObject *, char *, PyObject *);
177  alias int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
178  alias int (*cmpfunc)(PyObject *, PyObject *);
179  alias PyObject *(*reprfunc)(PyObject *);
180  alias C_long (*hashfunc)(PyObject *);
181  alias PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
182  alias PyObject *(*getiterfunc) (PyObject *);
183  alias PyObject *(*iternextfunc) (PyObject *);
184  alias PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
185  alias int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
186  alias int (*initproc)(PyObject *, PyObject *, PyObject *);
187  alias PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
188  alias PyObject *(*allocfunc)(PyTypeObject *, int);
189
190/+
191  struct PyTypeObject {
192    mixin PyObject_VAR_HEAD;
193
194    char *tp_name;
195    int tp_basicsize, tp_itemsize;
196
197    destructor tp_dealloc;
198    printfunc tp_print;
199    getattrfunc tp_getattr;
200    setattrfunc tp_setattr;
201    cmpfunc tp_compare;
202    reprfunc tp_repr;
203
204    PyNumberMethods *tp_as_number;
205    PySequenceMethods *tp_as_sequence;
206    PyMappingMethods *tp_as_mapping;
207
208    hashfunc tp_hash;
209    ternaryfunc tp_call;
210    reprfunc tp_str;
211    getattrofunc tp_getattro;
212    setattrofunc tp_setattro;
213
214    PyBufferProcs *tp_as_buffer;
215
216    C_long tp_flags;
217
218    char *tp_doc;
219
220    traverseproc tp_traverse;
221
222    inquiry tp_clear;
223
224    richcmpfunc tp_richcompare;
225
226    C_long tp_weaklistoffset;
227
228    getiterfunc tp_iter;
229    iternextfunc tp_iternext;
230
231    PyMethodDef *tp_methods;
232    PyMemberDef *tp_members;
233    PyGetSetDef *tp_getset;
234    PyTypeObject *tp_base;
235    PyObject *tp_dict;
236    descrgetfunc tp_descr_get;
237    descrsetfunc tp_descr_set;
238    C_long tp_dictoffset;
239    initproc tp_init;
240    allocfunc tp_alloc;
241    newfunc tp_new;
242    freefunc tp_free;
243    inquiry tp_is_gc;
244    PyObject *tp_bases;
245    PyObject *tp_mro;
246    PyObject *tp_cache;
247    PyObject *tp_subclasses;
248    PyObject *tp_weaklist;
249    destructor tp_del;
250  }
251+/
252
253  //alias _typeobject PyTypeObject;
254  alias void PyTypeObject;
255
256PyTypeObject* Py_CreateTypeObject();
257void Py_FreeTypeObject(PyTypeObject* pyTypeObject);
258
259char* PyTypeObject_GetName(PyTypeObject* pyTypeObject);
260void PyTypeObject_SetName(PyTypeObject* pyTypeObject, char* name);
261int PyTypeObject_GetBasicSize(PyTypeObject* pyTypeObject);
262void PyTypeObject_SetBasicSize(PyTypeObject* pyTypeObject, int basicsize);
263int PyTypeObject_GetItemSize(PyTypeObject* pyTypeObject);
264void PyTypeObject_SetItemSize(PyTypeObject* pyTypeObject, int itemsize);
265destructor PyTypeObject_GetDealloc(PyTypeObject* pyTypeObject);
266void PyTypeObject_SetDealloc(PyTypeObject* pyTypeObject, destructor dealloc);
267printfunc PyTypeObject_GetPrint(PyTypeObject* pyTypeObject);
268void PyTypeObject_SetPrint(PyTypeObject* pyTypeObject, printfunc print);
269getattrfunc PyTypeObject_GetGetAttr(PyTypeObject* pyTypeObject);
270void PyTypeObject_SetGetAttr(PyTypeObject* pyTypeObject, getattrfunc getattr);
271setattrfunc PyTypeObject_GetSetAttr(PyTypeObject* pyTypeObject);
272void PyTypeObject_SetSetAttr(PyTypeObject* pyTypeObject, setattrfunc setattr);
273reprfunc PyTypeObject_GetRepr(PyTypeObject* pyTypeObject);
274void PyTypeObject_SetRepr(PyTypeObject* pyTypeObject, reprfunc repr);
275PyNumberMethods* PyTypeObject_GetAsNumber(PyTypeObject* pyTypeObject);
276void PyTypeObject_SetAsNumber(PyTypeObject* pyTypeObject, PyNumberMethods* as_number);
277PySequenceMethods* PyTypeObject_GetAsSequence(PyTypeObject* pyTypeObject);
278void PyTypeObject_SetAsSequence(PyTypeObject* pyTypeObject, PySequenceMethods* as_sequence);
279PyMappingMethods* PyTypeObject_GetAsMapping(PyTypeObject* pyTypeObject);
280void PyTypeObject_SetAsMapping(PyTypeObject* pyTypeObject, PyMappingMethods* as_mapping);
281hashfunc PyTypeObject_GetHash(PyTypeObject* pyTypeObject);
282void PyTypeObject_SetHash(PyTypeObject* pyTypeObject, hashfunc hash);
283ternaryfunc PyTypeObject_GetCall(PyTypeObject* pyTypeObject);
284void PyTypeObject_SetCall(PyTypeObject* pyTypeObject, ternaryfunc call);
285reprfunc PyTypeObject_GetStr(PyTypeObject* pyTypeObject);
286void PyTypeObject_SetStr(PyTypeObject* pyTypeObject, reprfunc str);
287getattrofunc PyTypeObject_GetGetAttro(PyTypeObject* pyTypeObject);
288void PyTypeObject_SetGetAttro(PyTypeObject* pyTypeObject, getattrofunc getattro);
289setattrofunc PyTypeObject_GetSetAttro(PyTypeObject* pyTypeObject);
290void PyTypeObject_SetSetAttro(PyTypeObject* pyTypeObject, setattrofunc setattro);
291PyBufferProcs* PyTypeObject_GetAsBuffer(PyTypeObject* pyTypeObject);
292void PyTypeObject_SetAsBuffer(PyTypeObject* pyTypeObject, PyBufferProcs* as_buffer);
293long PyTypeObject_GetFlags(PyTypeObject* pyTypeObject);
294void PyTypeObject_SetFlags(PyTypeObject* pyTypeObject, long flags);
295char* PyTypeObject_GetDoc(PyTypeObject* pyTypeObject);
296void PyTypeObject_SetDoc(PyTypeObject* pyTypeObject, char* doc);
297traverseproc PyTypeObject_GetTraverse(PyTypeObject* pyTypeObject);
298void PyTypeObject_SetTraverse(PyTypeObject* pyTypeObject, traverseproc traverse);
299inquiry PyTypeObject_GetClear(PyTypeObject* pyTypeObject);
300void PyTypeObject_SetClear(PyTypeObject* pyTypeObject, inquiry clear);
301richcmpfunc PyTypeObject_GetRichCompare(PyTypeObject* pyTypeObject);
302void PyTypeObject_SetRichCompare(PyTypeObject* pyTypeObject, richcmpfunc richcompare);
303long PyTypeObject_GetWeakListOffset(PyTypeObject* pyTypeObject);
304void PyTypeObject_SetWeakListOffset(PyTypeObject* pyTypeObject, long weaklistoffset);
305getiterfunc PyTypeObject_GetIter(PyTypeObject* pyTypeObject);
306void PyTypeObject_SetIter(PyTypeObject* pyTypeObject, getiterfunc iter);
307iternextfunc PyTypeObject_GetIterNext(PyTypeObject* pyTypeObject);
308void PyTypeObject_SetIterNext(PyTypeObject* pyTypeObject, iternextfunc iternext);
309PyMethodDef* PyTypeObject_GetMethods(PyTypeObject* pyTypeObject);
310void PyTypeObject_SetMethods(PyTypeObject* pyTypeObject, PyMethodDef* methods);
311PyMemberDef* PyTypeObject_GetMembers(PyTypeObject* pyTypeObject);
312void PyTypeObject_SetMembers(PyTypeObject* pyTypeObject, PyMemberDef* members);
313PyGetSetDef* PyTypeObject_GetGetSet(PyTypeObject* pyTypeObject);
314void PyTypeObject_SetGetSet(PyTypeObject* pyTypeObject, PyGetSetDef* getset);
315PyTypeObject* PyTypeObject_GetBase(PyTypeObject* pyTypeObject);
316void PyTypeObject_SetBase(PyTypeObject* pyTypeObject, PyTypeObject* base);
317PyObject *PyTypeObject_GetDict(PyTypeObject *pyTypeObject);
318void PyTypeObject_SetDict(PyTypeObject *pyTypeObject, PyObject *dict);
319descrgetfunc PyTypeObject_GetDescrGet(PyTypeObject *pyTypeObject);
320void PyTypeObject_SetDescrGet(PyTypeObject *pyTypeObject, descrgetfunc descr_get);
321descrsetfunc PyTypeObject_GetDescrSet(PyTypeObject *pyTypeObject);
322void PyTypeObject_SetDescrSet(PyTypeObject *pyTypeObject, descrsetfunc descr_set);
323long PyTypeObject_GetDictOffset(PyTypeObject *pyTypeObject);
324void PyTypeObject_SetDictOffset(PyTypeObject *pyTypeObject, long dictoffset);
325initproc PyTypeObject_GetInit(PyTypeObject *pyTypeObject);
326void PyTypeObject_SetInit(PyTypeObject *pyTypeObject, initproc init);
327allocfunc PyTypeObject_GetAlloc(PyTypeObject* pyTypeObject);
328void PyTypeObject_SetAlloc(PyTypeObject* pyTypeObject, allocfunc allocf);
329newfunc PyTypeObject_GetNew(PyTypeObject* pyTypeObject);
330void PyTypeObject_SetNew(PyTypeObject* pyTypeObject, newfunc newf);
331freefunc PyTypeObject_GetFree(PyTypeObject* pyTypeObject);
332void PyTypeObject_SetFree(PyTypeObject* pyTypeObject, freefunc freef);
333inquiry PyTypeObject_GetIsGc(PyTypeObject* pyTypeObject);
334void PyTypeObject_SetIsGc(PyTypeObject* pyTypeObject, inquiry is_gc);
335PyObject* PyTypeObject_GetBases(PyTypeObject* pyTypeObject);
336void PyTypeObject_SetBases(PyTypeObject* pyTypeObject, PyObject* bases);
337PyObject* PyTypeObject_GetMro(PyTypeObject* pyTypeObject);
338void PyTypeObject_SetMro(PyTypeObject* pyTypeObject, PyObject* mro);
339PyObject* PyTypeObject_GetCache(PyTypeObject* pyTypeObject);
340void PyTypeObject_SetCache(PyTypeObject* pyTypeObject, PyObject* cache);
341PyObject* PyTypeObject_GetSubclasses(PyTypeObject* pyTypeObject);
342void PyTypeObject_SetSubclasses(PyTypeObject* pyTypeObject, PyObject* subclasses);
343PyObject* PyTypeObject_GetWeakList(PyTypeObject* pyTypeObject);
344void PyTypeObject_SetWeakList(PyTypeObject* pyTypeObject, PyObject* weaklist);
345destructor PyTypeObject_GetDel(PyTypeObject* pyTypeObject);
346void PyTypeObject_SetDel(PyTypeObject* pyTypeObject, destructor del);
347
348/*
349  struct _heaptypeobject {
350    PyTypeObject type;
351    PyNumberMethods as_number;
352    PyMappingMethods as_mapping;
353    PySequenceMethods as_sequence;
354    PyBufferProcs as_buffer;
355    PyObject* name;
356    PyObject* slots;
357  }
358  alias _heaptypeobject PyHeapTypeObject;
359*/
360
361size_t PyObject_GetSizeOfBaseMember();
362void* PyObject_GetEntity(PyObject* pyObject);
363
364  // Python-header-file: Include/pymem.h:
365  void * PyMem_Malloc(size_t);
366  void * PyMem_Realloc(void *, size_t);
367  void PyMem_Free(void *);
368
369
370///////////////////////////////////////////////////////////////////////////////
371// GENERIC TYPE CHECKING
372///////////////////////////////////////////////////////////////////////////////
373
374  int PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
375
376  // D translation of C macro:
377  int PyObject_TypeCheck(PyObject *ob, PyTypeObject *tp) {
378    return (ob.ob_type == tp || PyType_IsSubtype(ob.ob_type, tp));
379  }
380
381  /* Note that this Python support module makes pointers to PyType_Type and
382   * other global variables exposed by the Python API available to D
383   * programmers indirectly (see this module's static initializer). */
384
385  // D translation of C macro:
386  int PyType_Check(PyObject *op) {
387    return PyObject_TypeCheck(op, PyType_Type_p);
388  }
389  // D translation of C macro:
390  int PyType_CheckExact(PyObject *op) {
391    return op.ob_type == PyType_Type_p;
392  }
393
394  int PyType_Ready(PyTypeObject *);
395  PyObject * PyType_GenericAlloc(PyTypeObject *, int);
396  PyObject * PyType_GenericNew(PyTypeObject *, PyObject *, PyObject *);
397
398
399  int PyObject_Print(PyObject *, FILE *, int);
400  PyObject * PyObject_Repr(PyObject *);
401  PyObject * PyObject_Str(PyObject *);
402
403  PyObject * PyObject_Unicode(PyObject *);
404
405  int PyObject_Compare(PyObject *, PyObject *);
406  PyObject * PyObject_RichCompare(PyObject *, PyObject *, int);
407  int PyObject_RichCompareBool(PyObject *, PyObject *, int);
408  PyObject * PyObject_GetAttrString(PyObject *, char *);
409  int PyObject_SetAttrString(PyObject *, char *, PyObject *);
410  int PyObject_HasAttrString(PyObject *, char *);
411  PyObject * PyObject_GetAttr(PyObject *, PyObject *);
412  int PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
413  int PyObject_HasAttr(PyObject *, PyObject *);
414  PyObject * PyObject_SelfIter(PyObject *);
415  PyObject * PyObject_GenericGetAttr(PyObject *, PyObject *);
416  int PyObject_GenericSetAttr(PyObject *,
417                PyObject *, PyObject *);
418  C_long PyObject_Hash(PyObject *);
419  int PyObject_IsTrue(PyObject *);
420  int PyObject_Not(PyObject *);
421  int PyCallable_Check(PyObject *);
422  int PyNumber_Coerce(PyObject **, PyObject **);
423  int PyNumber_CoerceEx(PyObject **, PyObject **);
424
425  void PyObject_ClearWeakRefs(PyObject *);
426
427  PyObject * PyObject_Dir(PyObject *);
428
429  int Py_ReprEnter(PyObject *);
430  void Py_ReprLeave(PyObject *);
431
432  const int Py_PRINT_RAW = 1;
433
434
435  const int Py_TPFLAGS_HAVE_GETCHARBUFFER       = 1L<<0;
436  const int Py_TPFLAGS_HAVE_SEQUENCE_IN         = 1L<<1;
437  const int Py_TPFLAGS_GC                       = 0;
438  const int Py_TPFLAGS_HAVE_INPLACEOPS          = 1L<<3;
439  const int Py_TPFLAGS_CHECKTYPES               = 1L<<4;
440  const int Py_TPFLAGS_HAVE_RICHCOMPARE         = 1L<<5;
441  const int Py_TPFLAGS_HAVE_WEAKREFS            = 1L<<6;
442  const int Py_TPFLAGS_HAVE_ITER                = 1L<<7;
443  const int Py_TPFLAGS_HAVE_CLASS               = 1L<<8;
444  const int Py_TPFLAGS_HEAPTYPE                 = 1L<<9;
445  const int Py_TPFLAGS_BASETYPE                 = 1L<<10;
446  const int Py_TPFLAGS_READY                    = 1L<<12;
447  const int Py_TPFLAGS_READYING                 = 1L<<13;
448  const int Py_TPFLAGS_HAVE_GC                  = 1L<<14;
449
450  // YYY: Should conditionalize for stackless:
451  //#ifdef STACKLESS
452  //#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
453  //#else
454  const int Py_TPFLAGS_HAVE_STACKLESS_EXTENSION = 0;
455  //#endif
456
457  const int Py_TPFLAGS_DEFAULT =
458      Py_TPFLAGS_HAVE_GETCHARBUFFER |
459      Py_TPFLAGS_HAVE_SEQUENCE_IN |
460      Py_TPFLAGS_HAVE_INPLACEOPS |
461      Py_TPFLAGS_HAVE_RICHCOMPARE |
462      Py_TPFLAGS_HAVE_WEAKREFS |
463      Py_TPFLAGS_HAVE_ITER |
464      Py_TPFLAGS_HAVE_CLASS |
465      Py_TPFLAGS_HAVE_STACKLESS_EXTENSION |
466      0
467    ;
468
469  // D translation of C macro:
470  int PyType_HasFeature(PyTypeObject *t, int f) {
471    return (PyTypeObject_GetFlags(t) & f) != 0;
472  }
473
474
475///////////////////////////////////////////////////////////////////////////////
476// REFERENCE COUNTING
477///////////////////////////////////////////////////////////////////////////////
478  // Python-header-file: Include/object.h:
479
480  void Py_INCREF(PyObject *op) {
481    ++op.ob_refcnt;
482  }
483
484  void Py_XINCREF(PyObject *op) {
485    if (op == null) {
486      return;
487    }
488    Py_INCREF(op);
489  }
490
491  void Py_DECREF(PyObject *op) {
492    --op.ob_refcnt;
493    assert (op.ob_refcnt >= 0);
494    if (op.ob_refcnt == 0) {
495      PyTypeObject_GetDealloc(op.ob_type)(op);
496    }
497  }
498
499  void Py_XDECREF(PyObject* op)
500  {
501    if(op == null) {
502      return;
503    }
504
505    Py_DECREF(op);
506  }
507
508  void Py_IncRef(PyObject *);
509  void Py_DecRef(PyObject *);
510
511  /* Rich comparison opcodes */
512  const int Py_LT = 0;
513  const int Py_LE = 1;
514  const int Py_EQ = 2;
515  const int Py_NE = 3;
516  const int Py_GT = 4;
517  const int Py_GE = 5;
518
519
520///////////////////////////////////////////////////////////////////////////////////////////////
521// UNICODE
522///////////////////////////////////////////////////////////////////////////////////////////////
523  // Python-header-file: Include/unicodeobject.h:
524  /* The Python header explains:
525   *   Unicode API names are mangled to assure that UCS-2 and UCS-4 builds
526   *   produce different external names and thus cause import errors in
527   *   case Python interpreters and extensions with mixed compiled in
528   *   Unicode width assumptions are combined. */
529
530  version (Python_Unicode_UCS2) {
531    version (Windows) {
532      alias wchar Py_UNICODE;
533    } else {
534      alias ushort Py_UNICODE;
535    }
536  } else {
537    alias uint Py_UNICODE;
538  }
539
540  struct PyUnicodeObject {
541    mixin PyObject_HEAD;
542
543    int length;
544    Py_UNICODE *str;
545    C_long hash;
546    PyObject *defenc;
547  }
548
549  // &PyUnicode_Type is accessible via PyUnicode_Type_p.
550  // D translations of C macros:
551  int PyUnicode_Check(PyObject *op) {
552    return PyObject_TypeCheck(op, PyUnicode_Type_p);
553  }
554  int PyUnicode_CheckExact(PyObject *op) {
555    return op.ob_type == PyUnicode_Type_p;
556  }
557
558  int PyUnicode_GET_SIZE(PyUnicodeObject *op) {
559    return op.length;
560  }
561  int PyUnicode_GET_DATA_SIZE(PyUnicodeObject *op) {
562    return op.length * Py_UNICODE.sizeof;
563  }
564  Py_UNICODE *PyUnicode_AS_UNICODE(PyUnicodeObject *op) {
565    return op.str;
566  }
567  char *PyUnicode_AS_DATA(PyUnicodeObject *op) {
568    return cast(char *) op.str;
569  }
570
571  Py_UNICODE Py_UNICODE_REPLACEMENT_CHARACTER = 0xFFFD;
572
573  // YYY: Unfortunately, we have to do it the tedious way since there's no
574  // preprocessor in D:
575  version (Python_Unicode_UCS2) {
576    PyObject *PyUnicodeUCS2_FromUnicode(Py_UNICODE *u, int size);
577    Py_UNICODE *PyUnicodeUCS2_AsUnicode(PyObject *unicode);
578    int PyUnicodeUCS2_GetSize(PyObject *unicode);
579    Py_UNICODE PyUnicodeUCS2_GetMax();
580
581    int PyUnicodeUCS2_Resize(PyObject **unicode, int length);
582    PyObject *PyUnicodeUCS2_FromEncodedObject(PyObject *obj, char *encoding, char *errors);
583    PyObject *PyUnicodeUCS2_FromObject(PyObject *obj);
584
585    PyObject *PyUnicodeUCS2_FromWideChar(wchar *w, int size);
586    int PyUnicodeUCS2_AsWideChar(PyUnicodeObject *unicode, wchar *w, int size);
587
588    PyObject *PyUnicodeUCS2_FromOrdinal(int ordinal);
589
590    PyObject *_PyUnicodeUCS2_AsDefaultEncodedString(PyObject *, char *);
591
592    char *PyUnicodeUCS2_GetDefaultEncoding();
593    int PyUnicodeUCS2_SetDefaultEncoding(char *encoding);
594
595    PyObject *PyUnicodeUCS2_Decode(char *s, int size, char *encoding, char *errors);
596    PyObject *PyUnicodeUCS2_Encode(Py_UNICODE *s, int size, char *encoding, char *errors);
597    PyObject *PyUnicodeUCS2_AsEncodedObject(PyObject *unicode, char *encoding, char *errors);
598    PyObject *PyUnicodeUCS2_AsEncodedString(PyObject *unicode, char *encoding, char *errors);
599
600    PyObject *PyUnicodeUCS2_DecodeUTF7(char *string, int length, char *errors);
601    PyObject *PyUnicodeUCS2_EncodeUTF7(Py_UNICODE *data, int length,
602        int encodeSetO, int encodeWhiteSpace, char *errors
603      );
604
605    PyObject *PyUnicodeUCS2_DecodeUTF8(char *string, int length, char *errors);
606    PyObject *PyUnicodeUCS2_DecodeUTF8Stateful(char *string, int length,
607        char *errors, int *consumed
608      );
609    PyObject *PyUnicodeUCS2_AsUTF8String(PyObject *unicode);
610    PyObject *PyUnicodeUCS2_EncodeUTF8(Py_UNICODE *data, int length, char *errors);
611
612    PyObject *PyUnicodeUCS2_DecodeUTF16(char *string, int length, char *errors, int *byteorder);
613    PyObject *PyUnicodeUCS2_DecodeUTF16Stateful(char *string, int length,
614        char *errors, int *byteorder, int *consumed
615      );
616    PyObject *PyUnicodeUCS2_AsUTF16String(PyObject *unicode);
617    PyObject *PyUnicodeUCS2_EncodeUTF16(Py_UNICODE *data, int length,
618        char *errors, int byteorder
619      );
620
621    PyObject *PyUnicodeUCS2_DecodeUnicodeEscape(char *string, int length, char *errors);
622    PyObject *PyUnicodeUCS2_AsUnicodeEscapeString(PyObject *unicode);
623    PyObject *PyUnicodeUCS2_EncodeUnicodeEscape(Py_UNICODE *data, int length);
624    PyObject *PyUnicodeUCS2_DecodeRawUnicodeEscape(char *string, int length, char *errors);
625    PyObject *PyUnicodeUCS2_AsRawUnicodeEscapeString(PyObject *unicode);
626    PyObject *PyUnicodeUCS2_EncodeRawUnicodeEscape(Py_UNICODE *data, int length);
627
628    PyObject *_PyUnicodeUCS2_DecodeUnicodeInternal(char *string, int length, char *errors);
629
630    PyObject *PyUnicodeUCS2_DecodeLatin1(char *string, int length, char *errors);
631    PyObject *PyUnicodeUCS2_AsLatin1String(PyObject *unicode);
632    PyObject *PyUnicodeUCS2_EncodeLatin1(Py_UNICODE *data, int length, char *errors);
633
634    PyObject *PyUnicodeUCS2_DecodeASCII(char *string, int length, char *errors);
635    PyObject *PyUnicodeUCS2_AsASCIIString(PyObject *unicode);
636    PyObject *PyUnicodeUCS2_EncodeASCII(Py_UNICODE *data, int length, char *errors);
637
638    PyObject *PyUnicodeUCS2_DecodeCharmap(char *string, int length,
639        PyObject *mapping, char *errors
640      );
641    PyObject *PyUnicodeUCS2_AsCharmapString(PyObject *unicode, PyObject *mapping);
642    PyObject *PyUnicodeUCS2_EncodeCharmap(Py_UNICODE *data, int length,
643        PyObject *mapping, char *errors
644      );
645    PyObject *PyUnicodeUCS2_TranslateCharmap(Py_UNICODE *data, int length,
646        PyObject *table, char *errors
647      );
648
649    version (Windows) {
650      PyObject *PyUnicodeUCS2_DecodeMBCS(char *string, int length, char *errors);
651      PyObject *PyUnicodeUCS2_AsMBCSString(PyObject *unicode);
652      PyObject *PyUnicodeUCS2_EncodeMBCS(Py_UNICODE *data, int length, char *errors);
653    }
654
655    int PyUnicodeUCS2_EncodeDecimal(Py_UNICODE *s, int length, char *output, char *errors);
656
657    PyObject *PyUnicodeUCS2_Concat(PyObject *left, PyObject *right);
658    PyObject *PyUnicodeUCS2_Split(PyObject *s, PyObject *sep, int maxsplit);
659    PyObject *PyUnicodeUCS2_Splitlines(PyObject *s, int keepends);
660    PyObject *PyUnicodeUCS2_RSplit(PyObject *s, PyObject *sep, int maxsplit);
661    PyObject *PyUnicodeUCS2_Translate(PyObject *str, PyObject *table, char *errors);
662    PyObject *PyUnicodeUCS2_Join(PyObject *separator, PyObject *seq);
663    int PyUnicodeUCS2_Tailmatch(PyObject *str, PyObject *substr,
664        int start, int end, int direction
665      );
666    int PyUnicodeUCS2_Find(PyObject *str, PyObject *substr,
667        int start, int end, int direction
668      );
669    int PyUnicodeUCS2_Count(PyObject *str, PyObject *substr, int start, int end);
670    PyObject *PyUnicodeUCS2_Replace(PyObject *str, PyObject *substr,
671        PyObject *replstr, int maxcount
672      );
673    int PyUnicodeUCS2_Compare(PyObject *left, PyObject *right);
674    PyObject *PyUnicodeUCS2_Format(PyObject *format, PyObject *args);
675    int PyUnicodeUCS2_Contains(PyObject *container, PyObject *element);
676    PyObject *_PyUnicodeUCS2_XStrip(PyUnicodeObject *self, int striptype,
677        PyObject *sepobj
678      );
679
680    int _PyUnicodeUCS2_IsLowercase(Py_UNICODE ch);
681    int _PyUnicodeUCS2_IsUppercase(Py_UNICODE ch);
682    int _PyUnicodeUCS2_IsTitlecase(Py_UNICODE ch);
683    int _PyUnicodeUCS2_IsWhitespace(Py_UNICODE ch);
684    int _PyUnicodeUCS2_IsLinebreak(Py_UNICODE ch);
685    Py_UNICODE _PyUnicodeUCS2_ToLowercase(Py_UNICODE ch);
686    Py_UNICODE _PyUnicodeUCS2_ToUppercase(Py_UNICODE ch);
687    Py_UNICODE _PyUnicodeUCS2_ToTitlecase(Py_UNICODE ch);
688    int _PyUnicodeUCS2_ToDecimalDigit(Py_UNICODE ch);
689    int _PyUnicodeUCS2_ToDigit(Py_UNICODE ch);
690    double _PyUnicodeUCS2_ToNumeric(Py_UNICODE ch);
691    int _PyUnicodeUCS2_IsDecimalDigit(Py_UNICODE ch);
692    int _PyUnicodeUCS2_IsDigit(Py_UNICODE ch);
693    int _PyUnicodeUCS2_IsNumeric(Py_UNICODE ch);
694    int _PyUnicodeUCS2_IsAlpha(Py_UNICODE ch);
695  } else { /* not Python_Unicode_UCS2: */
696    PyObject *PyUnicodeUCS4_FromUnicode(Py_UNICODE *u, int size);
697    Py_UNICODE *PyUnicodeUCS4_AsUnicode(PyObject *unicode);
698    int PyUnicodeUCS4_GetSize(PyObject *unicode);
699    Py_UNICODE PyUnicodeUCS4_GetMax();
700
701    int PyUnicodeUCS4_Resize(PyObject **unicode, int length);
702    PyObject *PyUnicodeUCS4_FromEncodedObject(PyObject *obj, char *encoding, char *errors);
703    PyObject *PyUnicodeUCS4_FromObject(PyObject *obj);
704
705    PyObject *PyUnicodeUCS4_FromWideChar(wchar *w, int size);
706    int PyUnicodeUCS4_AsWideChar(PyUnicodeObject *unicode, wchar *w, int size);
707
708    PyObject *PyUnicodeUCS4_FromOrdinal(int ordinal);
709
710    PyObject *_PyUnicodeUCS4_AsDefaultEncodedString(PyObject *, char *);
711
712    char *PyUnicodeUCS4_GetDefaultEncoding();
713    int PyUnicodeUCS4_SetDefaultEncoding(char *encoding);
714
715    PyObject *PyUnicodeUCS4_Decode(char *s, int size, char *encoding, char *errors);
716    PyObject *PyUnicodeUCS4_Encode(Py_UNICODE *s, int size, char *encoding, char *errors);
717    PyObject *PyUnicodeUCS4_AsEncodedObject(PyObject *unicode, char *encoding, char *errors);
718    PyObject *PyUnicodeUCS4_AsEncodedString(PyObject *unicode, char *encoding, char *errors);
719
720    PyObject *PyUnicodeUCS4_DecodeUTF7(char *string, int length, char *errors);
721    PyObject *PyUnicodeUCS4_EncodeUTF7(Py_UNICODE *data, int length,
722        int encodeSetO, int encodeWhiteSpace, char *errors
723      );
724
725    PyObject *PyUnicodeUCS4_DecodeUTF8(char *string, int length, char *errors);
726    PyObject *PyUnicodeUCS4_DecodeUTF8Stateful(char *string, int length,
727        char *errors, int *consumed
728      );
729    PyObject *PyUnicodeUCS4_AsUTF8String(PyObject *unicode);
730    PyObject *PyUnicodeUCS4_EncodeUTF8(Py_UNICODE *data, int length, char *errors);
731
732    PyObject *PyUnicodeUCS4_DecodeUTF16(char *string, int length, char *errors, int *byteorder);
733    PyObject *PyUnicodeUCS4_DecodeUTF16Stateful(char *string, int length,
734        char *errors, int *byteorder, int *consumed
735      );
736    PyObject *PyUnicodeUCS4_AsUTF16String(PyObject *unicode);
737    PyObject *PyUnicodeUCS4_EncodeUTF16(Py_UNICODE *data, int length,
738        char *errors, int byteorder
739      );
740
741    PyObject *PyUnicodeUCS4_DecodeUnicodeEscape(char *string, int length, char *errors);
742    PyObject *PyUnicodeUCS4_AsUnicodeEscapeString(PyObject *unicode);
743    PyObject *PyUnicodeUCS4_EncodeUnicodeEscape(Py_UNICODE *data, int length);
744    PyObject *PyUnicodeUCS4_DecodeRawUnicodeEscape(char *string, int length, char *errors);
745    PyObject *PyUnicodeUCS4_AsRawUnicodeEscapeString(PyObject *unicode);
746    PyObject *PyUnicodeUCS4_EncodeRawUnicodeEscape(Py_UNICODE *data, int length);
747
748    PyObject *_PyUnicodeUCS4_DecodeUnicodeInternal(char *string, int length, char *errors);
749
750    PyObject *PyUnicodeUCS4_DecodeLatin1(char *string, int length, char *errors);
751    PyObject *PyUnicodeUCS4_AsLatin1String(PyObject *unicode);
752    PyObject *PyUnicodeUCS4_EncodeLatin1(Py_UNICODE *data, int length, char *errors);
753
754    PyObject *PyUnicodeUCS4_DecodeASCII(char *string, int length, char *errors);
755    PyObject *PyUnicodeUCS4_AsASCIIString(PyObject *unicode);
756    PyObject *PyUnicodeUCS4_EncodeASCII(Py_UNICODE *data, int length, char *errors);
757
758    PyObject *PyUnicodeUCS4_DecodeCharmap(char *string, int length,
759        PyObject *mapping, char *errors
760      );
761    PyObject *PyUnicodeUCS4_AsCharmapString(PyObject *unicode, PyObject *mapping);
762    PyObject *PyUnicodeUCS4_EncodeCharmap(Py_UNICODE *data, int length,
763        PyObject *mapping, char *errors
764      );
765    PyObject *PyUnicodeUCS4_TranslateCharmap(Py_UNICODE *data, int length,
766        PyObject *table, char *errors
767      );
768
769    version (Windows) {
770      PyObject *PyUnicodeUCS4_DecodeMBCS(char *string, int length, char *errors);
771      PyObject *PyUnicodeUCS4_AsMBCSString(PyObject *unicode);
772      PyObject *PyUnicodeUCS4_EncodeMBCS(Py_UNICODE *data, int length, char *errors);
773    }
774
775    int PyUnicodeUCS4_EncodeDecimal(Py_UNICODE *s, int length, char *output, char *errors);
776
777    PyObject *PyUnicodeUCS4_Concat(PyObject *left, PyObject *right);
778    PyObject *PyUnicodeUCS4_Split(PyObject *s, PyObject *sep, int maxsplit);
779    PyObject *PyUnicodeUCS4_Splitlines(PyObject *s, int keepends);
780    PyObject *PyUnicodeUCS4_RSplit(PyObject *s, PyObject *sep, int maxsplit);
781    PyObject *PyUnicodeUCS4_Translate(PyObject *str, PyObject *table, char *errors);
782    PyObject *PyUnicodeUCS4_Join(PyObject *separator, PyObject *seq);
783    int PyUnicodeUCS4_Tailmatch(PyObject *str, PyObject *substr,
784        int start, int end, int direction
785      );
786    int PyUnicodeUCS4_Find(PyObject *str, PyObject *substr,
787        int start, int end, int direction
788      );
789    int PyUnicodeUCS4_Count(PyObject *str, PyObject *substr, int start, int end);
790    PyObject *PyUnicodeUCS4_Replace(PyObject *str, PyObject *substr,
791        PyObject *replstr, int maxcount
792      );
793    int PyUnicodeUCS4_Compare(PyObject *left, PyObject *right);
794    PyObject *PyUnicodeUCS4_Format(PyObject *format, PyObject *args);
795    int PyUnicodeUCS4_Contains(PyObject *container, PyObject *element);
796    PyObject *_PyUnicodeUCS4_XStrip(PyUnicodeObject *self, int striptype,
797        PyObject *sepobj
798      );
799
800    int _PyUnicodeUCS4_IsLowercase(Py_UNICODE ch);
801    int _PyUnicodeUCS4_IsUppercase(Py_UNICODE ch);
802    int _PyUnicodeUCS4_IsTitlecase(Py_UNICODE ch);
803    int _PyUnicodeUCS4_IsWhitespace(Py_UNICODE ch);
804    int _PyUnicodeUCS4_IsLinebreak(Py_UNICODE ch);
805    Py_UNICODE _PyUnicodeUCS4_ToLowercase(Py_UNICODE ch);
806    Py_UNICODE _PyUnicodeUCS4_ToUppercase(Py_UNICODE ch);
807    Py_UNICODE _PyUnicodeUCS4_ToTitlecase(Py_UNICODE ch);
808    int _PyUnicodeUCS4_ToDecimalDigit(Py_UNICODE ch);
809    int _PyUnicodeUCS4_ToDigit(Py_UNICODE ch);
810    double _PyUnicodeUCS4_ToNumeric(Py_UNICODE ch);
811    int _PyUnicodeUCS4_IsDecimalDigit(Py_UNICODE ch);
812    int _PyUnicodeUCS4_IsDigit(Py_UNICODE ch);
813    int _PyUnicodeUCS4_IsNumeric(Py_UNICODE ch);
814    int _PyUnicodeUCS4_IsAlpha(Py_UNICODE ch);
815  }
816
817
818  /* The client programmer should call PyUnicode_XYZ, but linkage should be
819   * done via either PyUnicodeUCS2_XYZ or PyUnicodeUCS4_XYZ. */
820  version (Python_Unicode_UCS2) {
821    alias PyUnicodeUCS2_AsASCIIString PyUnicode_AsASCIIString;
822    alias PyUnicodeUCS2_AsCharmapString PyUnicode_AsCharmapString;
823    alias PyUnicodeUCS2_AsEncodedObject PyUnicode_AsEncodedObject;
824    alias PyUnicodeUCS2_AsEncodedString PyUnicode_AsEncodedString;
825    alias PyUnicodeUCS2_AsLatin1String PyUnicode_AsLatin1String;
826    alias PyUnicodeUCS2_AsRawUnicodeEscapeString PyUnicode_AsRawUnicodeEscapeString;
827    alias PyUnicodeUCS2_AsUTF16String PyUnicode_AsUTF16String;
828    alias PyUnicodeUCS2_AsUTF8String PyUnicode_AsUTF8String;
829    alias PyUnicodeUCS2_AsUnicode PyUnicode_AsUnicode;
830    alias PyUnicodeUCS2_AsUnicodeEscapeString PyUnicode_AsUnicodeEscapeString;
831    alias PyUnicodeUCS2_AsWideChar PyUnicode_AsWideChar;
832    alias PyUnicodeUCS2_Compare PyUnicode_Compare;
833    alias PyUnicodeUCS2_Concat PyUnicode_Concat;
834    alias PyUnicodeUCS2_Contains PyUnicode_Contains;
835    alias PyUnicodeUCS2_Count PyUnicode_Count;
836    alias PyUnicodeUCS2_Decode PyUnicode_Decode;
837    alias PyUnicodeUCS2_DecodeASCII PyUnicode_DecodeASCII;
838    alias PyUnicodeUCS2_DecodeCharmap PyUnicode_DecodeCharmap;
839    alias PyUnicodeUCS2_DecodeLatin1 PyUnicode_DecodeLatin1;
840    alias PyUnicodeUCS2_DecodeRawUnicodeEscape PyUnicode_DecodeRawUnicodeEscape;
841    alias PyUnicodeUCS2_DecodeUTF16 PyUnicode_DecodeUTF16;
842    alias PyUnicodeUCS2_DecodeUTF16Stateful PyUnicode_DecodeUTF16Stateful;
843    alias PyUnicodeUCS2_DecodeUTF8 PyUnicode_DecodeUTF8;
844    alias PyUnicodeUCS2_DecodeUTF8Stateful PyUnicode_DecodeUTF8Stateful;
845    alias PyUnicodeUCS2_DecodeUnicodeEscape PyUnicode_DecodeUnicodeEscape;
846    alias PyUnicodeUCS2_Encode PyUnicode_Encode;
847    alias PyUnicodeUCS2_EncodeASCII PyUnicode_EncodeASCII;
848    alias PyUnicodeUCS2_EncodeCharmap PyUnicode_EncodeCharmap;
849    alias PyUnicodeUCS2_EncodeDecimal PyUnicode_EncodeDecimal;
850    alias PyUnicodeUCS2_EncodeLatin1 PyUnicode_EncodeLatin1;
851    alias PyUnicodeUCS2_EncodeRawUnicodeEscape PyUnicode_EncodeRawUnicodeEscape;
852    alias PyUnicodeUCS2_EncodeUTF16 PyUnicode_EncodeUTF16;
853    alias PyUnicodeUCS2_EncodeUTF8 PyUnicode_EncodeUTF8;
854    alias PyUnicodeUCS2_EncodeUnicodeEscape PyUnicode_EncodeUnicodeEscape;
855    alias PyUnicodeUCS2_Find PyUnicode_Find;
856    alias PyUnicodeUCS2_Format PyUnicode_Format;
857    alias PyUnicodeUCS2_FromEncodedObject PyUnicode_FromEncodedObject;
858    alias PyUnicodeUCS2_FromObject PyUnicode_FromObject;
859    alias PyUnicodeUCS2_FromOrdinal PyUnicode_FromOrdinal;
860    alias PyUnicodeUCS2_FromUnicode PyUnicode_FromUnicode;
861    alias PyUnicodeUCS2_FromWideChar PyUnicode_FromWideChar;
862    alias PyUnicodeUCS2_GetDefaultEncoding PyUnicode_GetDefaultEncoding;
863    alias PyUnicodeUCS2_GetMax PyUnicode_GetMax;
864    alias PyUnicodeUCS2_GetSize PyUnicode_GetSize;
865    alias PyUnicodeUCS2_Join PyUnicode_Join;
866    alias PyUnicodeUCS2_Replace PyUnicode_Replace;
867    alias PyUnicodeUCS2_Resize PyUnicode_Resize;
868    alias PyUnicodeUCS2_SetDefaultEncoding PyUnicode_SetDefaultEncoding;
869    alias PyUnicodeUCS2_Split PyUnicode_Split;
870    alias PyUnicodeUCS2_RSplit PyUnicode_RSplit;
871    alias PyUnicodeUCS2_Splitlines PyUnicode_Splitlines;
872    alias PyUnicodeUCS2_Tailmatch PyUnicode_Tailmatch;
873    alias PyUnicodeUCS2_Translate PyUnicode_Translate;
874    alias PyUnicodeUCS2_TranslateCharmap PyUnicode_TranslateCharmap;
875    alias _PyUnicodeUCS2_AsDefaultEncodedString _PyUnicode_AsDefaultEncodedString;
876    // omitted _PyUnicode_Fini
877    // omitted _PyUnicode_Init
878    alias _PyUnicodeUCS2_IsAlpha _PyUnicode_IsAlpha;
879    alias _PyUnicodeUCS2_IsDecimalDigit _PyUnicode_IsDecimalDigit;
880    alias _PyUnicodeUCS2_IsDigit _PyUnicode_IsDigit;
881    alias _PyUnicodeUCS2_IsLinebreak _PyUnicode_IsLinebreak;
882    alias _PyUnicodeUCS2_IsLowercase _PyUnicode_IsLowercase;
883    alias _PyUnicodeUCS2_IsNumeric _PyUnicode_IsNumeric;
884    alias _PyUnicodeUCS2_IsTitlecase _PyUnicode_IsTitlecase;
885    alias _PyUnicodeUCS2_IsUppercase _PyUnicode_IsUppercase;
886    alias _PyUnicodeUCS2_IsWhitespace _PyUnicode_IsWhitespace;
887    alias _PyUnicodeUCS2_ToDecimalDigit _PyUnicode_ToDecimalDigit;
888    alias _PyUnicodeUCS2_ToDigit _PyUnicode_ToDigit;
889    alias _PyUnicodeUCS2_ToLowercase _PyUnicode_ToLowercase;
890    alias _PyUnicodeUCS2_ToNumeric _PyUnicode_ToNumeric;
891    alias _PyUnicodeUCS2_ToTitlecase _PyUnicode_ToTitlecase;
892    alias _PyUnicodeUCS2_ToUppercase _PyUnicode_ToUppercase;
893  } else {
894    alias PyUnicodeUCS4_AsASCIIString PyUnicode_AsASCIIString;
895    alias PyUnicodeUCS4_AsCharmapString PyUnicode_AsCharmapString;
896    alias PyUnicodeUCS4_AsEncodedObject PyUnicode_AsEncodedObject;
897    alias PyUnicodeUCS4_AsEncodedString PyUnicode_AsEncodedString;
898    alias PyUnicodeUCS4_AsLatin1String PyUnicode_AsLatin1String;
899    alias PyUnicodeUCS4_AsRawUnicodeEscapeString PyUnicode_AsRawUnicodeEscapeString;
900    alias PyUnicodeUCS4_AsUTF16String PyUnicode_AsUTF16String;
901    alias PyUnicodeUCS4_AsUTF8String PyUnicode_AsUTF8String;
902    alias PyUnicodeUCS4_AsUnicode PyUnicode_AsUnicode;
903    alias PyUnicodeUCS4_AsUnicodeEscapeString PyUnicode_AsUnicodeEscapeString;
904    alias PyUnicodeUCS4_AsWideChar PyUnicode_AsWideChar;
905    alias PyUnicodeUCS4_Compare PyUnicode_Compare;
906    alias PyUnicodeUCS4_Concat PyUnicode_Concat;
907    alias PyUnicodeUCS4_Contains PyUnicode_Contains;
908    alias PyUnicodeUCS4_Count PyUnicode_Count;
909    alias PyUnicodeUCS4_Decode PyUnicode_Decode;
910    alias PyUnicodeUCS4_DecodeASCII PyUnicode_DecodeASCII;
911    alias PyUnicodeUCS4_DecodeCharmap PyUnicode_DecodeCharmap;
912    alias PyUnicodeUCS4_DecodeLatin1 PyUnicode_DecodeLatin1;
913    alias PyUnicodeUCS4_DecodeRawUnicodeEscape PyUnicode_DecodeRawUnicodeEscape;
914    alias PyUnicodeUCS4_DecodeUTF16 PyUnicode_DecodeUTF16;
915    alias PyUnicodeUCS4_DecodeUTF16Stateful PyUnicode_DecodeUTF16Stateful;
916    alias PyUnicodeUCS4_DecodeUTF8 PyUnicode_DecodeUTF8;
917    alias PyUnicodeUCS4_DecodeUTF8Stateful PyUnicode_DecodeUTF8Stateful;
918    alias PyUnicodeUCS4_DecodeUnicodeEscape PyUnicode_DecodeUnicodeEscape;
919    alias PyUnicodeUCS4_Encode PyUnicode_Encode;
920    alias PyUnicodeUCS4_EncodeASCII PyUnicode_EncodeASCII;
921    alias PyUnicodeUCS4_EncodeCharmap PyUnicode_EncodeCharmap;
922    alias PyUnicodeUCS4_EncodeDecimal PyUnicode_EncodeDecimal;
923    alias PyUnicodeUCS4_EncodeLatin1 PyUnicode_EncodeLatin1;
924    alias PyUnicodeUCS4_EncodeRawUnicodeEscape PyUnicode_EncodeRawUnicodeEscape;
925    alias PyUnicodeUCS4_EncodeUTF16 PyUnicode_EncodeUTF16;
926    alias PyUnicodeUCS4_EncodeUTF8 PyUnicode_EncodeUTF8;
927    alias PyUnicodeUCS4_EncodeUnicodeEscape PyUnicode_EncodeUnicodeEscape;
928    alias PyUnicodeUCS4_Find PyUnicode_Find;
929    alias PyUnicodeUCS4_Format PyUnicode_Format;
930    alias PyUnicodeUCS4_FromEncodedObject PyUnicode_FromEncodedObject;
931    alias PyUnicodeUCS4_FromObject PyUnicode_FromObject;
932    alias PyUnicodeUCS4_FromOrdinal PyUnicode_FromOrdinal;
933    alias PyUnicodeUCS4_FromUnicode PyUnicode_FromUnicode;
934    alias PyUnicodeUCS4_FromWideChar PyUnicode_FromWideChar;
935    alias PyUnicodeUCS4_GetDefaultEncoding PyUnicode_GetDefaultEncoding;
936    alias PyUnicodeUCS4_GetMax PyUnicode_GetMax;
937    alias PyUnicodeUCS4_GetSize PyUnicode_GetSize;
938    alias PyUnicodeUCS4_Join PyUnicode_Join;
939    alias PyUnicodeUCS4_Replace PyUnicode_Replace;
940    alias PyUnicodeUCS4_Resize PyUnicode_Resize;
941    alias PyUnicodeUCS4_SetDefaultEncoding PyUnicode_SetDefaultEncoding;
942    alias PyUnicodeUCS4_Split PyUnicode_Split;
943    alias PyUnicodeUCS4_Splitlines PyUnicode_Splitlines;
944    alias PyUnicodeUCS4_Tailmatch PyUnicode_Tailmatch;
945    alias PyUnicodeUCS4_Translate PyUnicode_Translate;
946    alias PyUnicodeUCS4_TranslateCharmap PyUnicode_TranslateCharmap;
947    alias _PyUnicodeUCS4_AsDefaultEncodedString _PyUnicode_AsDefaultEncodedString;
948    // omitted _PyUnicode_Fini
949    // omitted _PyUnicode_Init
950    alias _PyUnicodeUCS4_IsAlpha _PyUnicode_IsAlpha;
951    alias _PyUnicodeUCS4_IsDecimalDigit _PyUnicode_IsDecimalDigit;
952    alias _PyUnicodeUCS4_IsDigit _PyUnicode_IsDigit;
953    alias _PyUnicodeUCS4_IsLinebreak _PyUnicode_IsLinebreak;
954    alias _PyUnicodeUCS4_IsLowercase _PyUnicode_IsLowercase;
955    alias _PyUnicodeUCS4_IsNumeric _PyUnicode_IsNumeric;
956    alias _PyUnicodeUCS4_IsTitlecase _PyUnicode_IsTitlecase;
957    alias _PyUnicodeUCS4_IsUppercase _PyUnicode_IsUppercase;
958    alias _PyUnicodeUCS4_IsWhitespace _PyUnicode_IsWhitespace;
959    alias _PyUnicodeUCS4_ToDecimalDigit _PyUnicode_ToDecimalDigit;
960    alias _PyUnicodeUCS4_ToDigit _PyUnicode_ToDigit;
961    alias _PyUnicodeUCS4_ToLowercase _PyUnicode_ToLowercase;
962    alias _PyUnicodeUCS4_ToNumeric _PyUnicode_ToNumeric;
963    alias _PyUnicodeUCS4_ToTitlecase _PyUnicode_ToTitlecase;
964    alias _PyUnicodeUCS4_ToUppercase _PyUnicode_ToUppercase;
965  }
966
967  alias _PyUnicode_IsWhitespace Py_UNICODE_ISSPACE;
968  alias _PyUnicode_IsLowercase Py_UNICODE_ISLOWER;
969  alias _PyUnicode_IsUppercase Py_UNICODE_ISUPPER;
970  alias _PyUnicode_IsTitlecase Py_UNICODE_ISTITLE;
971  alias _PyUnicode_IsLinebreak Py_UNICODE_ISLINEBREAK;
972  alias _PyUnicode_ToLowercase Py_UNICODE_TOLOWER;
973  alias _PyUnicode_ToUppercase Py_UNICODE_TOUPPER;
974  alias _PyUnicode_ToTitlecase Py_UNICODE_TOTITLE;
975  alias _PyUnicode_IsDecimalDigit Py_UNICODE_ISDECIMAL;
976  alias _PyUnicode_IsDigit Py_UNICODE_ISDIGIT;
977  alias _PyUnicode_IsNumeric Py_UNICODE_ISNUMERIC;
978  alias _PyUnicode_ToDecimalDigit Py_UNICODE_TODECIMAL;
979  alias _PyUnicode_ToDigit Py_UNICODE_TODIGIT;
980  alias _PyUnicode_ToNumeric Py_UNICODE_TONUMERIC;
981  alias _PyUnicode_IsAlpha Py_UNICODE_ISALPHA;
982
983/* FIXME: Phobos?
984  int Py_UNICODE_ISALNUM(Py_UNICODE ch) {
985    return (
986           Py_UNICODE_ISALPHA(ch)
987        || Py_UNICODE_ISDECIMAL(ch)
988        || Py_UNICODE_ISDIGIT(ch)
989        || Py_UNICODE_ISNUMERIC(ch)
990      );
991  }
992*/
993
994  void Py_UNICODE_COPY(void *target, void *source, int length) {
995    std.string.memcpy(target, source, cast(uint)(length * Py_UNICODE.sizeof));
996  }
997
998  void Py_UNICODE_FILL(Py_UNICODE *target, Py_UNICODE value, int length) {
999    for (int i = 0; i < length; i++) {
1000      target[i] = value;
1001    }
1002  }
1003
1004/* FIXME
1005  int Py_UNICODE_MATCH(PyUnicodeObject *string, int offset,
1006      PyUnicodeObject *substring
1007    )
1008  {
1009    return (
1010         (*(string.str + offset) == *(substring.str))
1011      && !memcmp(string.str + offset, substring.str,
1012             substring.length * Py_UNICODE.sizeof
1013          )
1014      );
1015  }
1016*/
1017
1018
1019///////////////////////////////////////////////////////////////////////////////
1020// INT INTERFACE
1021///////////////////////////////////////////////////////////////////////////////
1022  // Python-header-file: Include/intobject.h:
1023
1024  struct PyIntObject {
1025    mixin PyObject_HEAD;
1026
1027    C_long ob_ival;
1028  }
1029
1030  // &PyInt_Type is accessible via PyInt_Type_p.
1031
1032  // D translation of C macro:
1033  int PyInt_Check(PyObject *op) {
1034    return PyObject_TypeCheck(op, PyInt_Type_p);
1035  }
1036  // D translation of C macro:
1037  int PyInt_CheckExact(PyObject *op) {
1038    return op.ob_type == PyInt_Type_p;
1039  }
1040
1041  PyObject *PyInt_FromString(char *, char **, int);
1042  PyObject *PyInt_FromUnicode(Py_UNICODE *, int, int);
1043  PyObject *PyInt_FromLong(C_long);
1044
1045  C_long PyInt_AsLong(PyObject *);
1046  C_ulong PyInt_AsUnsignedLongMask(PyObject *);
1047  C_ulonglong PyInt_AsUnsignedLongLongMask(PyObject *);
1048
1049  C_long PyInt_GetMax(); /* Accessible at the Python level as sys.maxint */
1050
1051  C_ulong PyOS_strtoul(char *, char **, int);
1052  C_long PyOS_strtol(char *, char **, int);
1053
1054
1055///////////////////////////////////////////////////////////////////////////////
1056// BOOL INTERFACE
1057///////////////////////////////////////////////////////////////////////////////
1058  // Python-header-file: Include/boolobject.h:
1059
1060  alias PyIntObject PyBoolObject;
1061
1062  // &PyBool_Type is accessible via PyBool_Type_p.
1063
1064  // D translation of C macro:
1065  int PyBool_Check(PyObject *x) {
1066    return x.ob_type == PyBool_Type_p;
1067  }
1068
1069  // Py_False and Py_True are actually macros in the Python/C API, so they're
1070  // loaded as PyObject pointers in this module static initializer.
1071
1072  PyObject * PyBool_FromLong(C_long);
1073
1074
1075///////////////////////////////////////////////////////////////////////////////
1076// LONG INTERFACE
1077///////////////////////////////////////////////////////////////////////////////
1078  // Python-header-file: Include/longobject.h:
1079
1080  // &PyLong_Type is accessible via PyLong_Type_p.
1081
1082  // D translation of C macro:
1083  int PyLong_Check(PyObject *op) {
1084    return PyObject_TypeCheck(op, PyLong_Type_p);
1085  }
1086  // D translation of C macro:
1087  int PyLong_CheckExact(PyObject *op) {
1088    return op.ob_type == PyLong_Type_p;
1089  }
1090
1091  PyObject * PyLong_FromLong(C_long);
1092  PyObject * PyLong_FromUnsignedLong(C_ulong);
1093
1094  PyObject * PyLong_FromLongLong(C_longlong);
1095  PyObject * PyLong_FromUnsignedLongLong(C_ulonglong);
1096
1097  PyObject * PyLong_FromDouble(double);
1098  PyObject * PyLong_FromVoidPtr(void *);
1099
1100  C_long PyLong_AsLong(PyObject *);
1101  C_ulong PyLong_AsUnsignedLong(PyObject *);
1102  C_ulong PyLong_AsUnsignedLongMask(PyObject *);
1103
1104  C_longlong PyLong_AsLongLong(PyObject *);
1105  C_ulonglong PyLong_AsUnsignedLongLong(PyObject *);
1106  C_ulonglong PyLong_AsUnsignedLongLongMask(PyObject *);
1107
1108  double PyLong_AsDouble(PyObject *);
1109  void * PyLong_AsVoidPtr(PyObject *);
1110
1111  PyObject * PyLong_FromString(char *, char **, int);
1112  PyObject * PyLong_FromUnicode(Py_UNICODE *, int, int);
1113
1114
1115///////////////////////////////////////////////////////////////////////////////
1116// FLOAT INTERFACE
1117///////////////////////////////////////////////////////////////////////////////
1118  // Python-header-file: Include/floatobject.h:
1119
1120  struct PyFloatObject {
1121    mixin PyObject_HEAD;
1122
1123    double ob_fval;
1124  }
1125
1126  // &PyFloat_Type is accessible via PyFloat_Type_p.
1127
1128  // D translation of C macro:
1129  int PyFloat_Check(PyObject *op) {
1130    return PyObject_TypeCheck(op, PyFloat_Type_p);
1131  }
1132  // D translation of C macro:
1133  int PyFloat_CheckExact(PyObject *op) {
1134    return op.ob_type == PyFloat_Type_p;
1135  }
1136
1137  PyObject * PyFloat_FromString(PyObject *, char** junk);
1138  PyObject * PyFloat_FromDouble(double);
1139
1140  double PyFloat_AsDouble(PyObject *);
1141  void PyFloat_AsReprString(char *, PyFloatObject *v);
1142  void PyFloat_AsString(char *, PyFloatObject *v);
1143
1144
1145///////////////////////////////////////////////////////////////////////////////
1146// COMPLEX INTERFACE
1147///////////////////////////////////////////////////////////////////////////////
1148  // Python-header-file: Include/complexobject.h:
1149
1150  struct Py_complex {
1151    double real_; // real is the name of a D type, so must rename
1152    double imag;
1153  }
1154
1155  struct PyComplexObject {
1156    mixin PyObject_HEAD;
1157
1158    Py_complex cval;
1159  }
1160
1161  Py_complex c_sum(Py_complex, Py_complex);
1162  Py_complex c_diff(Py_complex, Py_complex);
1163  Py_complex c_neg(Py_complex);
1164  Py_complex c_prod(Py_complex, Py_complex);
1165  Py_complex c_quot(Py_complex, Py_complex);
1166  Py_complex c_pow(Py_complex, Py_complex);
1167
1168  // &PyComplex_Type is accessible via PyComplex_Type_p.
1169
1170  // D translation of C macro:
1171  int PyComplex_Check(PyObject *op) {
1172    return PyObject_TypeCheck(op, PyComplex_Type_p);
1173  }
1174  // D translation of C macro:
1175  int PyComplex_CheckExact(PyObject *op) {
1176    return op.ob_type == PyComplex_Type_p;
1177  }
1178
1179  PyObject * PyComplex_FromCComplex(Py_complex);
1180  PyObject * PyComplex_FromDoubles(double real_, double imag);
1181
1182  double PyComplex_RealAsDouble(PyObject *op);
1183  double PyComplex_ImagAsDouble(PyObject *op);
1184  Py_complex PyComplex_AsCComplex(PyObject *op);
1185
1186
1187///////////////////////////////////////////////////////////////////////////////
1188// RANGE INTERFACE
1189///////////////////////////////////////////////////////////////////////////////
1190  // Python-header-file: Include/rangeobject.h:
1191
1192  // &PyRange_Type is accessible via PyRange_Type_p.
1193
1194  // D translation of C macro:
1195  int PyRange_Check(PyObject *op) {
1196    return op.ob_type == PyRange_Type_p;
1197  }
1198
1199  PyObject * PyRange_New(C_long, C_long, C_long, int);
1200
1201
1202///////////////////////////////////////////////////////////////////////////////
1203// STRING INTERFACE
1204///////////////////////////////////////////////////////////////////////////////
1205  // Python-header-file: Include/stringobject.h:
1206
1207  struct PyStringObject {
1208    mixin PyObject_VAR_HEAD;
1209
1210    C_long ob_shash;
1211    int ob_sstate;
1212    // DSR:XXX:LAYOUT:
1213    // Will the D layout for a 1-char array be the same as the C layout?  I
1214    // think the D array will be larger.
1215    char ob_sval[1];
1216  }
1217
1218  // &PyBaseString_Type is accessible via PyBaseString_Type_p.
1219  // &PyString_Type is accessible via PyString_Type_p.
1220
1221  // D translation of C macro:
1222  int PyString_Check(PyObject *op) {
1223    return PyObject_TypeCheck(op, PyString_Type_p);
1224  }
1225  // D translation of C macro:
1226  int PyString_CheckExact(PyObject *op) {
1227    return op.ob_type == PyString_Type_p;
1228  }
1229
1230  PyObject * PyString_FromStringAndSize(char *, int);
1231  PyObject * PyString_FromString(char *);
1232  // PyString_FromFormatV omitted
1233  PyObject * PyString_FromFormat(char*, ...);
1234  int PyString_Size(PyObject *);
1235  char * PyString_AsString(PyObject *);
1236  PyObject * PyString_Repr(PyObject *, int);
1237  void PyString_Concat(PyObject **, PyObject *);
1238  void PyString_ConcatAndDel(PyObject **, PyObject *);
1239  PyObject * PyString_Format(PyObject *, PyObject *);
1240  PyObject * PyString_DecodeEscape(char *, int, char *, int, char *);
1241
1242  void PyString_InternInPlace(PyObject **);
1243  void PyString_InternImmortal(PyObject **);
1244  PyObject * PyString_InternFromString(char *);
1245
1246  PyObject * _PyString_Join(PyObject *sep, PyObject *x);
1247
1248
1249  PyObject* PyString_Decode(char *s, int size, char *encoding, char *errors);
1250  PyObject* PyString_Encode(char *s, int size, char *encoding, char *errors);
1251
1252  PyObject* PyString_AsEncodedObject(PyObject *str, char *encoding, char *errors);
1253  PyObject* PyString_AsDecodedObject(PyObject *str, char *encoding, char *errors);
1254
1255  // Since no one has legacy Python extensions written in D, the deprecated
1256  // functions PyString_AsDecodedString and PyString_AsEncodedString were
1257  // omitted.
1258
1259  int PyString_AsStringAndSize(PyObject *obj, char **s, int *len);
1260
1261
1262///////////////////////////////////////////////////////////////////////////////
1263// BUFFER INTERFACE
1264///////////////////////////////////////////////////////////////////////////////
1265  // Python-header-file: Include/bufferobject.h:
1266
1267  // &PyBuffer_Type is accessible via PyBuffer_Type_p.
1268
1269  // D translation of C macro:
1270  int PyBuffer_Check(PyObject *op) {
1271    return op.ob_type == PyBuffer_Type_p;
1272  }
1273
1274  const int Py_END_OF_BUFFER = -1;
1275
1276  PyObject * PyBuffer_FromObject(PyObject *base, int offset, int size);
1277  PyObject * PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size);
1278
1279  PyObject * PyBuffer_FromMemory(void *ptr, int size);
1280  PyObject * PyBuffer_FromReadWriteMemory(void *ptr, int size);
1281
1282  PyObject * PyBuffer_New(int size);
1283
1284
1285///////////////////////////////////////////////////////////////////////////////
1286// TUPLE INTERFACE
1287///////////////////////////////////////////////////////////////////////////////
1288  // Python-header-file: Include/tupleobject.h:
1289
1290  struct PyTupleObject {
1291    mixin PyObject_VAR_HEAD;
1292
1293    // DSR:XXX:LAYOUT:
1294    // Will the D layout for a 1-PyObject* array be the same as the C layout?
1295    // I think the D array will be larger.
1296    PyObject *ob_item[1];
1297  }
1298
1299  // &PyTuple_Type is accessible via PyTuple_Type_p.
1300
1301  // D translation of C macro:
1302  int PyTuple_Check(PyObject *op) {
1303    return PyObject_TypeCheck(op, PyTuple_Type_p);
1304  }
1305  // D translation of C macro:
1306  int PyTuple_CheckExact(PyObject *op) {
1307    return op.ob_type == PyTuple_Type_p;
1308  }
1309
1310  PyObject * PyTuple_New(int size);
1311  int PyTuple_Size(PyObject *);
1312  PyObject * PyTuple_GetItem(PyObject *, int);
1313  int PyTuple_SetItem(PyObject *, int, PyObject *);
1314  PyObject * PyTuple_GetSlice(PyObject *, int, int);
1315  int _PyTuple_Resize(PyObject **, int);
1316  PyObject * PyTuple_Pack(int, ...);
1317
1318  // D translations of C macros:
1319  PyObject *PyTuple_GET_ITEM(PyObject *op, int i) {
1320    return (cast(PyTupleObject *) op).ob_item[i];
1321  }
1322  int PyTuple_GET_SIZE(PyObject *op) {
1323    return (cast(PyTupleObject *) op).ob_size;
1324  }
1325  PyObject *PyTuple_SET_ITEM(PyObject *op, int i, PyObject *v) {
1326    PyTupleObject *opAsTuple = cast(PyTupleObject *) op;
1327    opAsTuple.ob_item[i] = v;
1328    return v;
1329  }
1330
1331
1332///////////////////////////////////////////////////////////////////////////////
1333// LIST INTERFACE
1334///////////////////////////////////////////////////////////////////////////////
1335  // Python-header-file: Include/listobject.h:
1336
1337  struct PyListObject {
1338    mixin PyObject_VAR_HEAD;
1339
1340    PyObject **ob_item;
1341    int allocated;
1342  }
1343
1344  // &PyList_Type is accessible via PyList_Type_p.
1345
1346  // D translation of C macro:
1347  int PyList_Check(PyObject *op) {
1348    return PyObject_TypeCheck(op, PyList_Type_p);
1349  }
1350  // D translation of C macro:
1351  int PyList_CheckExact(PyObject *op) {
1352    return op.ob_type == PyList_Type_p;
1353  }
1354
1355  PyObject * PyList_New(int size);
1356  int PyList_Size(PyObject *);
1357
1358  PyObject * PyList_GetItem(PyObject *, int);
1359  int PyList_SetItem(PyObject *, int, PyObject *);
1360  int PyList_Insert(PyObject *, int, PyObject *);
1361  int PyList_Append(PyObject *, PyObject *);
1362  PyObject * PyList_GetSlice(PyObject *, int, int);
1363  int PyList_SetSlice(PyObject *, int, int, PyObject *);
1364  int PyList_Sort(PyObject *);
1365  int PyList_Reverse(PyObject *);
1366  PyObject * PyList_AsTuple(PyObject *);
1367
1368  // D translations of C macros:
1369  PyObject *PyList_GET_ITEM(PyObject *op, int i) {
1370    return (cast(PyListObject *) op).ob_item[i];
1371  }
1372  PyObject *PyList_SET_ITEM(PyObject *op, int i, PyObject *v) {
1373    PyListObject *opAsList = cast(PyListObject *) op;
1374    opAsList.ob_item[i] = v;
1375    return v;
1376  }
1377  int PyList_GET_SIZE(PyObject *op) {
1378    return (cast(PyListObject *) op).ob_size;
1379  }
1380
1381
1382///////////////////////////////////////////////////////////////////////////////
1383// DICTIONARY OBJECT TYPE
1384///////////////////////////////////////////////////////////////////////////////
1385  // Python-header-file: Include/dictobject.h:
1386
1387  const int PyDict_MINSIZE = 8;
1388
1389  struct PyDictEntry {
1390    C_long me_hash;
1391    PyObject *me_key;
1392    PyObject *me_value;
1393  }
1394
1395  struct _dictobject {
1396    mixin PyObject_HEAD;
1397
1398    int ma_fill;
1399    int ma_used;
1400    int ma_mask;
1401    PyDictEntry *ma_table;
1402    PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, C_long hash);
1403    PyDictEntry ma_smalltable[PyDict_MINSIZE];
1404  }
1405  alias _dictobject PyDictObject;
1406
1407  // &PyDict_Type is accessible via PyDict_Type_p.
1408
1409  // D translation of C macro:
1410  int PyDict_Check(PyObject *op) {
1411    return PyObject_TypeCheck(op, PyDict_Type_p);
1412  }
1413  // D translation of C macro:
1414  int PyDict_CheckExact(PyObject *op) {
1415    return op.ob_type == PyDict_Type_p;
1416  }
1417
1418  PyObject * PyDict_New();
1419  PyObject * PyDict_GetItem(PyObject *mp, PyObject *key);
1420  int PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
1421  int PyDict_DelItem(PyObject *mp, PyObject *key);
1422  void PyDict_Clear(PyObject *mp);
1423  int PyDict_Next(PyObject *mp, int *pos, PyObject **key, PyObject **value);
1424  PyObject * PyDict_Keys(PyObject *mp);
1425  PyObject * PyDict_Values(PyObject *mp);
1426  PyObject * PyDict_Items(PyObject *mp);
1427  int PyDict_Size(PyObject *mp);
1428  PyObject * PyDict_Copy(PyObject *mp);
1429  int PyDict_Contains(PyObject *mp, PyObject *key);
1430
1431  int PyDict_Update(PyObject *mp, PyObject *other);
1432  int PyDict_Merge(PyObject *mp, PyObject *other, int override_);
1433  int PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override_);
1434
1435  PyObject * PyDict_GetItemString(PyObject *dp, char *key);
1436  int PyDict_SetItemString(PyObject *dp, char *key, PyObject *item);
1437  int PyDict_DelItemString(PyObject *dp, char *key);
1438
1439
1440///////////////////////////////////////////////////////////////////////////////
1441// PYTHON EXTENSION FUNCTION INTERFACE
1442///////////////////////////////////////////////////////////////////////////////
1443  // Python-header-file: Include/methodobject.h:
1444
1445  // &PyCFunction_Type is accessible via PyCFunction_Type_p.
1446
1447  // D translation of C macro:
1448  int PyCFunction_Check(PyObject *op) {
1449    return op.ob_type == PyCFunction_Type_p;
1450  }
1451
1452  alias PyObject *(*PyCFunction)(PyObject *, PyObject *);
1453  alias PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,PyObject *);
1454  alias PyObject *(*PyNoArgsFunction)(PyObject *);
1455
1456  PyCFunction PyCFunction_GetFunction(PyObject *);
1457  PyObject * PyCFunction_GetSelf(PyObject *);
1458  int PyCFunction_GetFlags(PyObject *);
1459
1460  PyObject * PyCFunction_Call(PyObject *, PyObject *, PyObject *);
1461
1462/+
1463  struct PyMethodDef {
1464    char        *ml_name;
1465    PyCFunction  ml_meth;
1466    int          ml_flags;
1467    char        *ml_doc;
1468  }
1469+/
1470
1471  alias void PyMethodDef;
1472
1473  PyMethodDef* Py_CreateMethodDef(int num);
1474  void Py_FreeMethodDef(PyMethodDef* pyMethodDef);
1475  char* PyMethodDef_GetName(PyMethodDef* pyMethodDef, int num);
1476  void PyMethodDef_SetName(PyMethodDef* pyMethodDef, int num, char* name);
1477  PyCFunction PyMethodDef_GetMeth(PyMethodDef* pyMethodDef, int num);
1478  void PyMethodDef_SetMeth(PyMethodDef* pyMethodDef, int num, PyCFunction meth);
1479  int PyMethodDef_GetFlags(PyMethodDef* pyMethodDef, int num);
1480  void PyMethodDef_SetFlags(PyMethodDef* pyMethodDef, int num, int flags);
1481  char* PyMethodDef_GetDoc(PyMethodDef* pyMethodDef, int num);
1482  void PyMethodDef_SetDoc(PyMethodDef* pyMethodDef, int num, char* doc);
1483  void PyMethodDef_Clear(PyMethodDef* pyMethodDef, int num);
1484
1485//FIXME  PyObject * Py_FindMethod(PyMethodDef[], PyObject *, char *);
1486  PyObject * PyCFunction_NewEx(PyMethodDef *, PyObject *,PyObject *);
1487
1488  const int METH_OLDARGS = 0x0000;
1489  const int METH_VARARGS = 0x0001;
1490  const int METH_KEYWORDS= 0x0002;
1491  const int METH_NOARGS  = 0x0004;
1492  const int METH_O       = 0x0008;
1493  const int METH_CLASS   = 0x0010;
1494  const int METH_STATIC  = 0x0020;
1495  const int METH_COEXIST = 0x0040;
1496
1497  struct PyMethodChain {
1498    PyMethodDef *methods;
1499    PyMethodChain *link;
1500  }
1501
1502  PyObject * Py_FindMethodInChain(PyMethodChain *, PyObject *, char *);
1503
1504  struct PyCFunctionObject {
1505    mixin PyObject_HEAD;
1506
1507    PyMethodDef *m_ml;
1508    PyObject    *m_self;
1509    PyObject    *m_module;
1510  }
1511
1512
1513///////////////////////////////////////////////////////////////////////////////
1514// MODULE INTERFACE
1515///////////////////////////////////////////////////////////////////////////////
1516  // Python-header-file: Include/moduleobject.h:
1517
1518  // &PyModule_Type is accessible via PyModule_Type_p.
1519
1520  // D translation of C macro:
1521  int PyModule_Check(PyObject *op) {
1522    return PyObject_TypeCheck(op, PyModule_Type_p);
1523  }
1524  // D translation of C macro:
1525  int PyModule_CheckExact(PyObject *op) {
1526    return op.ob_type == PyModule_Type_p;
1527  }
1528
1529  PyObject * PyModule_New(char *);
1530  PyObject * PyModule_GetDict(PyObject *);
1531  char * PyModule_GetName(PyObject *);
1532  char * PyModule_GetFilename(PyObject *);
1533
1534  // Python-header-file: Include/modsupport.h:
1535
1536  const int PYTHON_API_VERSION = 1012;
1537  const char[] PYTHON_API_STRING = "1012";
1538
1539  int PyArg_Parse(PyObject *, char *, ...);
1540  int PyArg_ParseTuple(PyObject *, char *, ...);
1541  int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
1542                            char *, char **, ...);
1543  int PyArg_UnpackTuple(PyObject *, char *, int, int, ...);
1544  PyObject * Py_BuildValue(char *, ...);
1545
1546  int PyModule_AddObject(PyObject *, char *, PyObject *);
1547  int PyModule_AddIntConstant(PyObject *, char *, C_long);
1548  int PyModule_AddStringConstant(PyObject *, char *, char *);
1549
1550  PyObject * Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
1551                            PyObject *self, int apiver);
1552
1553  PyObject * Py_InitModule(char *name, PyMethodDef *methods)
1554  {
1555    return Py_InitModule4(name, methods, cast(char *)(null),
1556      cast(PyObject *)(null), PYTHON_API_VERSION);
1557  }
1558
1559  PyObject *Py_InitModule3(char *name, PyMethodDef *methods, char *doc) {
1560    return Py_InitModule4(name, methods, doc, cast(PyObject *)null,
1561      PYTHON_API_VERSION);
1562  }
1563
1564
1565///////////////////////////////////////////////////////////////////////////////
1566// PYTHON FUNCTION INTERFACE (to functions created by the 'def' statement)
1567///////////////////////////////////////////////////////////////////////////////
1568  // Python-header-file: Include/funcobject.h:
1569
1570  struct PyFunctionObject {
1571    mixin PyObject_HEAD;
1572
1573    PyObject *func_code;
1574    PyObject *func_globals;
1575    PyObject *func_defaults;
1576    PyObject *func_closure;
1577    PyObject *func_doc;
1578    PyObject *func_name;
1579    PyObject *func_dict;
1580    PyObject *func_weakreflist;
1581    PyObject *func_module;
1582  }
1583
1584  // &PyFunction_Type is accessible via PyFunction_Type_p.
1585
1586  // D translation of C macro:
1587  int PyFunction_Check(PyObject *op) {
1588    return op.ob_type == PyFunction_Type_p;
1589  }
1590
1591  PyObject * PyFunction_New(PyObject *, PyObject *);
1592  PyObject * PyFunction_GetCode(PyObject *);
1593  PyObject * PyFunction_GetGlobals(PyObject *);
1594  PyObject * PyFunction_GetModule(PyObject *);
1595  PyObject * PyFunction_GetDefaults(PyObject *);
1596  int PyFunction_SetDefaults(PyObject *, PyObject *);
1597  PyObject * PyFunction_GetClosure(PyObject *);
1598  int PyFunction_SetClosure(PyObject *, PyObject *);
1599
1600  // &PyClassMethod_Type is accessible via PyClassMethod_Type_p.
1601  // &PyStaticMethod_Type is accessible via PyStaticMethod_Type_p.
1602
1603  PyObject * PyClassMethod_New(PyObject *);
1604  PyObject * PyStaticMethod_New(PyObject *);
1605
1606
1607///////////////////////////////////////////////////////////////////////////////
1608// PYTHON CLASS INTERFACE
1609///////////////////////////////////////////////////////////////////////////////
1610  // Python-header-file: Include/classobject.h:
1611
1612  struct PyClassObject {
1613    mixin PyObject_HEAD;
1614
1615    PyObject    *cl_bases;      /* A tuple of class objects */
1616    PyObject    *cl_dict;       /* A dictionary */
1617    PyObject    *cl_name;       /* A string */
1618    /* The following three are functions or null */
1619    PyObject    *cl_getattr;
1620    PyObject    *cl_setattr;
1621    PyObject    *cl_delattr;
1622  }
1623
1624  struct PyInstanceObject {
1625    mixin PyObject_HEAD;
1626
1627    PyClassObject *in_class;
1628    PyObject      *in_dict;
1629    PyObject      *in_weakreflist;
1630  }
1631
1632  struct PyMethodObject {
1633    mixin PyObject_HEAD;
1634
1635    PyObject *im_func;
1636    PyObject *im_self;
1637    PyObject *im_class;
1638    PyObject *im_weakreflist;
1639  }
1640
1641  // &PyClass_Type is accessible via PyClass_Type_p.
1642  // D translation of C macro:
1643  int PyClass_Check(PyObject *op) {
1644    return op.ob_type == PyClass_Type_p;
1645  }
1646
1647  // &PyInstance_Type is accessible via PyInstance_Type_p.
1648  // D translation of C macro:
1649  int PyInstance_Check(PyObject *op) {
1650    return op.ob_type == PyInstance_Type_p;
1651  }
1652
1653  // &PyMethod_Type is accessible via PyMethod_Type_p.
1654  // D translation of C macro:
1655  int PyMethod_Check(PyObject *op) {
1656    return op.ob_type == PyMethod_Type_p;
1657  }
1658
1659  PyObject * PyClass_New(PyObject *, PyObject *, PyObject *);
1660  PyObject * PyInstance_New(PyObject *, PyObject *,
1661                        PyObject *);
1662  PyObject * PyInstance_NewRaw(PyObject *, PyObject *);
1663  PyObject * PyMethod_New(PyObject *, PyObject *, PyObject *);
1664
1665  PyObject * PyMethod_Function(PyObject *);
1666  PyObject * PyMethod_Self(PyObject *);
1667  PyObject * PyMethod_Class(PyObject *);
1668
1669  PyObject * _PyInstance_Lookup(PyObject *pinst, PyObject *name);
1670
1671  int PyClass_IsSubclass(PyObject *, PyObject *);
1672
1673
1674///////////////////////////////////////////////////////////////////////////////
1675// FILE INTERFACE
1676///////////////////////////////////////////////////////////////////////////////
1677  // Python-header-file: Include/fileobject.h:
1678
1679  struct PyFileObject {
1680    mixin PyObject_HEAD;
1681
1682    FILE *f_fp;
1683    PyObject *f_name;
1684    PyObject *f_mode;
1685    int (*f_close)(FILE *);
1686    int f_softspace;
1687    int f_binary;
1688    char* f_buf;
1689    char* f_bufend;
1690    char* f_bufptr;
1691    char *f_setbuf;
1692    int f_univ_newline;
1693    int f_newlinetypes;
1694    int f_skipnextlf;
1695    PyObject *f_encoding;
1696    PyObject *weakreflist;
1697  }
1698
1699  // &PyFile_Type is accessible via PyFile_Type_p.
1700  // D translation of C macro:
1701  int PyFile_Check(PyObject *op) {
1702    return PyObject_TypeCheck(op, PyFile_Type_p);
1703  }
1704  // D translation of C macro:
1705  int PyFile_CheckExact(PyObject *op) {
1706    return op.ob_type == PyFile_Type_p;
1707  }
1708
1709  PyObject * PyFile_FromString(char *, char *);
1710  void PyFile_SetBufSize(PyObject *, int);
1711  int PyFile_SetEncoding(PyObject *,  char *);
1712  PyObject * PyFile_FromFile(FILE *, char *, char *,
1713                         int (*)(FILE *));
1714  FILE * PyFile_AsFile(PyObject *);
1715  PyObject * PyFile_Name(PyObject *);
1716  PyObject * PyFile_GetLine(PyObject *, int);
1717  int PyFile_WriteObject(PyObject *, PyObject *, int);
1718  int PyFile_SoftSpace(PyObject *, int);
1719  int PyFile_WriteString(char *, PyObject *);
1720  int PyObject_AsFileDescriptor(PyObject *);
1721
1722  // We deal with char *Py_FileSystemDefaultEncoding in the global variables
1723  // section toward the bottom of this file.
1724
1725  const char[] PY_STDIOTEXTMODE = "b";
1726
1727  char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
1728  size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
1729
1730
1731///////////////////////////////////////////////////////////////////////////////
1732// COBJECT INTERFACE
1733///////////////////////////////////////////////////////////////////////////////
1734  // Python-header-file: Include/cobject.h:
1735
1736  // PyCObject_Type is a Python type for transporting an arbitrary C pointer
1737  // from the C level to Python and back (in essence, an opaque handle).
1738
1739  // &PyCObject_Type is accessible via PyCObject_Type_p.
1740  // D translation of C macro:
1741  int PyCObject_Check(PyObject *op) {
1742    return op.ob_type == PyCObject_Type_p;
1743  }
1744
1745  PyObject * PyCObject_FromVoidPtr(void *cobj, void (*destruct)(void*));
1746  PyObject * PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
1747    void (*destruct)(void*,void*));
1748  void * PyCObject_AsVoidPtr(PyObject *);
1749  void * PyCObject_GetDesc(PyObject *);
1750  void * PyCObject_Import(char *module_name, char *cobject_name);
1751  int PyCObject_SetVoidPtr(PyObject *self, void *cobj);
1752
1753
1754///////////////////////////////////////////////////////////////////////////////////////////////
1755// TRACEBACK INTERFACE
1756///////////////////////////////////////////////////////////////////////////////////////////////
1757  // Python-header-file: Include/traceback.h:
1758
1759  struct PyTracebackObject {
1760    mixin PyObject_HEAD;
1761
1762    PyTracebackObject *tb_next;
1763    PyFrameObject *tb_frame;
1764    int tb_lasti;
1765    int tb_lineno;
1766  }
1767
1768  int PyTraceBack_Here(PyFrameObject *);
1769  int PyTraceBack_Print(PyObject *, PyObject *);
1770
1771  // &PyTraceBack_Type is accessible via PyTraceBack_Type_p.
1772  // D translation of C macro:
1773  int PyTraceBack_Check(PyObject *v) {
1774    return v.ob_type == PyTraceBack_Type_p;
1775  }
1776
1777
1778///////////////////////////////////////////////////////////////////////////////
1779// SLICE INTERFACE
1780///////////////////////////////////////////////////////////////////////////////
1781  // Python-header-file: Include/sliceobject.h:
1782
1783  // We deal with Py_Ellipsis in the global variables section toward the bottom
1784  // of this file.
1785
1786  struct PySliceObject {
1787    mixin PyObject_HEAD;
1788
1789    PyObject *start;
1790    PyObject *stop;
1791    PyObject *step;
1792  }
1793
1794  // &PySlice_Type is accessible via PySlice_Type_p.
1795  // D translation of C macro:
1796  int PySlice_Check(PyObject *op) {
1797    return op.ob_type == PySlice_Type_p;
1798  }
1799
1800  PyObject * PySlice_New(PyObject *start, PyObject *stop, PyObject *step);
1801  int PySlice_GetIndices(PySliceObject *r, int length, int *start, int *stop, int *step);
1802  int PySlice_GetIndicesEx(PySliceObject *r, int length, int *start, int *stop, int *step, int *slicelength);
1803
1804
1805///////////////////////////////////////////////////////////////////////////////
1806// CELL INTERFACE
1807///////////////////////////////////////////////////////////////////////////////
1808  // Python-header-file: Include/cellobject.h:
1809
1810  struct PyCellObject {
1811    mixin PyObject_HEAD;
1812
1813    PyObject *ob_ref;
1814  }
1815
1816  // &PyCell_Type is accessible via PyCell_Type_p.
1817  // D translation of C macro:
1818  int PyCell_Check(PyObject *op) {
1819    return op.ob_type == PyCell_Type_p;
1820  }
1821
1822  PyObject * PyCell_New(PyObject *);
1823  PyObject * PyCell_Get(PyObject *);
1824  int PyCell_Set(PyObject *, PyObject *);
1825
1826
1827///////////////////////////////////////////////////////////////////////////////
1828// ITERATOR INTERFACE
1829///////////////////////////////////////////////////////////////////////////////
1830  // Python-header-file: Include/iterobject.h:
1831
1832  // &PySeqIter_Type is accessible via PySeqIter_Type_p.
1833  // D translation of C macro:
1834  int PySeqIter_Check(PyObject *op) {
1835    return op.ob_type == PySeqIter_Type_p;
1836  }
1837
1838  PyObject * PySeqIter_New(PyObject *);
1839
1840  // &PyCallIter_Type is accessible via PyCallIter_Type_p.
1841  // D translation of C macro:
1842  int PyCallIter_Check(PyObject *op) {
1843    return op.ob_type == PyCallIter_Type_p;
1844  }
1845
1846  PyObject * PyCallIter_New(PyObject *, PyObject *);
1847
1848
1849///////////////////////////////////////////////////////////////////////////////
1850// DESCRIPTOR INTERFACE
1851///////////////////////////////////////////////////////////////////////////////
1852  // Python-header-file: Include/descrobject.h:
1853
1854  alias PyObject *(*getter)(PyObject *, void *);
1855  alias int (*setter)(PyObject *, PyObject *, void *);
1856
1857/+
1858  struct PyGetSetDef {
1859    char *name;
1860    getter get;
1861    setter set;
1862    char *doc;
1863    void *closure;
1864  }
1865+/
1866
1867  alias void PyGetSetDef;
1868
1869  PyGetSetDef* Py_CreateGetSetDef(int num);
1870  void Py_FreeGetSetDef(PyGetSetDef* pyGetSetDef);
1871  char* PyGetSetDef_GetName(PyGetSetDef* pyGetSetDef, int num);
1872  void PyGetSetDef_SetName(PyGetSetDef* pyGetSetDef, int num, char* name);
1873  getter PyGetSetDef_GetGet(PyGetSetDef* pyGetSetDef, int num);
1874  void PyGetSetDef_SetGet(PyGetSetDef* pyGetSetDef, int num, getter get);
1875  setter PyGetSetDef_GetSet(PyGetSetDef* pyGetSetDef, int num);
1876  void PyGetSetDef_SetSet(PyGetSetDef* pyGetSetDef, int num, setter set);
1877  char* PyGetSetDef_GetDoc(PyGetSetDef* pyGetSetDef, int num);
1878  void PyGetSetDef_SetDoc(PyGetSetDef* pyGetSetDef, int num, char* doc);
1879  void* PyGetSetDef_GetClosure(PyGetSetDef* pyGetSetDef, int num);
1880  void PyGetSetDef_SetClosure(PyGetSetDef* pyGetSetDef, int num, void* closure);
1881  void PyGetSetDef_Clear(PyGetSetDef* pyGetSetDef, int num);
1882
1883  alias PyObject *(*wrapperfunc)(PyObject *, PyObject *, void *);
1884  alias PyObject *(*wrapperfunc_kwds)(PyObject *, PyObject *, void *, PyObject *);
1885
1886  struct wrapperbase {
1887    char *name;
1888    int offset;
1889    void *function_;
1890    wrapperfunc wrapper;
1891    char *doc;
1892    int flags;
1893    PyObject *name_strobj;
1894  }
1895
1896  const int PyWrapperFlag_KEYWORDS = 1;
1897
1898  template PyDescr_COMMON() {
1899    mixin PyObject_HEAD;
1900    PyTypeObject *d_type;
1901    PyObject *d_name;
1902  }
1903
1904  struct PyDescrObject {
1905    mixin PyDescr_COMMON;
1906  }
1907
1908  struct PyMethodDescrObject {
1909    mixin PyDescr_COMMON;
1910    PyMethodDef *d_method;
1911  }
1912
1913  struct PyMemberDescrObject {
1914    mixin PyDescr_COMMON;
1915    PyMemberDef *d_member;
1916  }
1917
1918  struct PyGetSetDescrObject {
1919    mixin PyDescr_COMMON;
1920    PyGetSetDef *d_getset;
1921  }
1922
1923  struct PyWrapperDescrObject {
1924    mixin PyDescr_COMMON;
1925    wrapperbase *d_base;
1926    void *d_wrapped;
1927  }
1928
1929  // PyWrapperDescr_Type is currently not accessible from D.
1930
1931  PyObject * PyDescr_NewMethod(PyTypeObject *, PyMethodDef *);
1932  PyObject * PyDescr_NewClassMethod(PyTypeObject *, PyMethodDef *);
1933  PyObject * PyDescr_NewMember(PyTypeObject *, PyMemberDef *);
1934  PyObject * PyDescr_NewGetSet(PyTypeObject *, PyGetSetDef *);
1935  PyObject * PyDescr_NewWrapper(PyTypeObject *, wrapperbase *, void *);
1936  PyObject * PyDictProxy_New(PyObject *);
1937  PyObject * PyWrapper_New(PyObject *, PyObject *);
1938
1939  // &PyProperty_Type is accessible via PyProperty_Type_p.
1940
1941
1942///////////////////////////////////////////////////////////////////////////////
1943// WEAK REFERENCE INTERFACE
1944///////////////////////////////////////////////////////////////////////////////
1945  // Python-header-file: Include/weakrefobject.h:
1946
1947  struct PyWeakReference {
1948    mixin PyObject_HEAD;
1949
1950    PyObject *wr_object;
1951    PyObject *wr_callback;
1952    C_long hash;
1953    PyWeakReference *wr_prev;
1954    PyWeakReference *wr_next;
1955  }
1956
1957  // &_PyWeakref_RefType is accessible via _PyWeakref_RefType_p.
1958  // &_PyWeakref_ProxyType is accessible via _PyWeakref_ProxyType_p.
1959  // &_PyWeakref_CallableProxyType is accessible via _PyWeakref_CallableProxyType_p.
1960
1961  // D translations of C macros:
1962  int PyWeakref_CheckRef(PyObject *op) {
1963    return PyObject_TypeCheck(op, _PyWeakref_RefType_p);
1964  }
1965  int PyWeakref_CheckRefExact(PyObject *op) {
1966    return op.ob_type == _PyWeakref_RefType_p;
1967  }
1968  int PyWeakref_CheckProxy(PyObject *op) {
1969    return op.ob_type == _PyWeakref_ProxyType_p
1970        || op.ob_type == _PyWeakref_CallableProxyType_p;
1971  }
1972  int PyWeakref_Check(PyObject *op) {
1973    return PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op);
1974  }
1975
1976  PyObject * PyWeakref_NewRef(PyObject *ob, PyObject *callback);
1977  PyObject * PyWeakref_NewProxy(PyObject *ob, PyObject *callback);
1978  PyObject * PyWeakref_GetObject(PyObject *ref_);
1979
1980  C_long _PyWeakref_GetWeakrefCount(PyWeakReference *head);
1981  void _PyWeakref_ClearRef(PyWeakReference *self);
1982
1983  PyObject *PyWeakref_GET_OBJECT(PyObject *ref_) {
1984    return (cast(PyWeakReference *) ref_).wr_object;
1985  }
1986
1987
1988///////////////////////////////////////////////////////////////////////////////
1989// CODEC REGISTRY AND SUPPORT INTERFACE
1990///////////////////////////////////////////////////////////////////////////////
1991  // Python-header-file: Include/codecs.h:
1992
1993  int PyCodec_Register(PyObject *search_function);
1994  PyObject * _PyCodec_Lookup(char *encoding);
1995  PyObject * PyCodec_Encode(PyObject *object, char *encoding, char *errors);
1996  PyObject * PyCodec_Decode(PyObject *object, char *encoding, char *errors);
1997  PyObject * PyCodec_Encoder(char *encoding);
1998  PyObject * PyCodec_Decoder(char *encoding);
1999  PyObject * PyCodec_StreamReader(char *encoding, PyObject *stream, char *errors);
2000  PyObject * PyCodec_StreamWriter(char *encoding, PyObject *stream, char *errors);
2001
2002  /////////////////////////////////////////////////////////////////////////////
2003  // UNICODE ENCODING INTERFACE
2004  /////////////////////////////////////////////////////////////////////////////
2005
2006  int PyCodec_RegisterError(char *name, PyObject *error);
2007  PyObject * PyCodec_LookupError(char *name);
2008  PyObject * PyCodec_StrictErrors(PyObject *exc);
2009  PyObject * PyCodec_IgnoreErrors(PyObject *exc);
2010  PyObject * PyCodec_ReplaceErrors(PyObject *exc);
2011  PyObject * PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
2012  PyObject * PyCodec_BackslashReplaceErrors(PyObject *exc);
2013
2014
2015///////////////////////////////////////////////////////////////////////////////
2016// ERROR HANDLING INTERFACE
2017///////////////////////////////////////////////////////////////////////////////
2018  // Python-header-file: Include/pyerrors.h:
2019
2020  void PyErr_SetNone(PyObject *);
2021  void PyErr_SetObject(PyObject *, PyObject *);
2022  void PyErr_SetString(PyObject *, char *);
2023  PyObject * PyErr_Occurred();
2024  void PyErr_Clear();
2025  void PyErr_Fetch(PyObject **, PyObject **, PyObject **);
2026  void PyErr_Restore(PyObject *, PyObject *, PyObject *);
2027
2028  int PyErr_GivenExceptionMatches(PyObject *, PyObject *);
2029  int PyErr_ExceptionMatches(PyObject *);
2030  void PyErr_NormalizeException(PyObject **, PyObject **, PyObject **);
2031
2032  // All predefined Python exception types are dealt with in the global
2033  // variables section toward the end of this file.
2034
2035  int PyErr_BadArgument();
2036  PyObject * PyErr_NoMemory();
2037  PyObject * PyErr_SetFromErrno(PyObject *);
2038  PyObject * PyErr_SetFromErrnoWithFilenameObject(PyObject *, PyObject *);
2039  PyObject * PyErr_SetFromErrnoWithFilename(PyObject *, char *);
2040  PyObject * PyErr_SetFromErrnoWithUnicodeFilename(PyObject *, Py_UNICODE *);
2041
2042  PyObject * PyErr_Format(PyObject *, char *, ...);
2043
2044  version (Windows) {
2045    PyObject * PyErr_SetFromWindowsErrWithFilenameObject(int,  char *);
2046    PyObject * PyErr_SetFromWindowsErrWithFilename(int, char *);
2047    PyObject * PyErr_SetFromWindowsErrWithUnicodeFilename(int, Py_UNICODE *);
2048    PyObject * PyErr_SetFromWindowsErr(int);
2049    PyObject * PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *, int, PyObject *);
2050    PyObject * PyErr_SetExcFromWindowsErrWithFilename(PyObject *, int,  char *);
2051    PyObject * PyErr_SetExcFromWindowsErrWithUnicodeFilename(PyObject *, int, Py_UNICODE *);
2052    PyObject * PyErr_SetExcFromWindowsErr(PyObject *, int);
2053  }
2054
2055  // PyErr_BadInternalCall and friends purposely omitted.
2056
2057  PyObject * PyErr_NewException(char *name, PyObject *base, PyObject *dict);
2058  void PyErr_WriteUnraisable(PyObject *);
2059
2060  int PyErr_Warn(PyObject *, char *);
2061  int PyErr_WarnExplicit(PyObject *, char *, char *, int, char *, PyObject *);
2062
2063  int PyErr_CheckSignals();
2064  void PyErr_SetInterrupt();
2065
2066  void PyErr_SyntaxLocation(char *, int);
2067  PyObject * PyErr_ProgramText(char *, int);
2068
2069  /////////////////////////////////////////////////////////////////////////////
2070  // UNICODE ENCODING ERROR HANDLING INTERFACE
2071  /////////////////////////////////////////////////////////////////////////////
2072  PyObject *PyUnicodeDecodeError_Create(char *, char *, int, int, int, char *);
2073
2074  PyObject *PyUnicodeEncodeError_Create(char *, Py_UNICODE *, int, int, int, char *);
2075
2076  PyObject *PyUnicodeTranslateError_Create(Py_UNICODE *, int, int, int, char *);
2077
2078  PyObject *PyUnicodeEncodeError_GetEncoding(PyObject *);
2079  PyObject *PyUnicodeDecodeError_GetEncoding(PyObject *);
2080
2081  PyObject *PyUnicodeEncodeError_GetObject(PyObject *);
2082  PyObject *PyUnicodeDecodeError_GetObject(PyObject *);
2083  PyObject *PyUnicodeTranslateError_GetObject(PyObject *);
2084
2085  int PyUnicodeEncodeError_GetStart(PyObject *, int *);
2086  int PyUnicodeDecodeError_GetStart(PyObject *, int *);
2087  int PyUnicodeTranslateError_GetStart(PyObject *, int *);
2088
2089  int PyUnicodeEncodeError_SetStart(PyObject *, int);
2090  int PyUnicodeDecodeError_SetStart(PyObject *, int);
2091  int PyUnicodeTranslateError_SetStart(PyObject *, int);
2092
2093  int PyUnicodeEncodeError_GetEnd(PyObject *, int *);
2094  int PyUnicodeDecodeError_GetEnd(PyObject *, int *);
2095  int PyUnicodeTranslateError_GetEnd(PyObject *, int *);
2096
2097  int PyUnicodeEncodeError_SetEnd(PyObject *, int);
2098  int PyUnicodeDecodeError_SetEnd(PyObject *, int);
2099  int PyUnicodeTranslateError_SetEnd(PyObject *, int);
2100
2101  PyObject *PyUnicodeEncodeError_GetReason(PyObject *);
2102  PyObject *PyUnicodeDecodeError_GetReason(PyObject *);
2103  PyObject *PyUnicodeTranslateError_GetReason(PyObject *);
2104
2105  int PyUnicodeEncodeError_SetReason(PyObject *, char *);
2106  int PyUnicodeDecodeError_SetReason(PyObject *, char *);
2107  int PyUnicodeTranslateError_SetReason(PyObject *, char *);
2108
2109  int PyOS_snprintf(char *str, size_t size, char *format, ...);
2110
2111
2112///////////////////////////////////////////////////////////////////////////////
2113// COMPILATION INTERFACE
2114///////////////////////////////////////////////////////////////////////////////
2115  // Python-header-file: Include/compile.h:
2116
2117  struct PyCodeObject { /* Bytecode object */
2118    mixin PyObject_HEAD;
2119
2120    int co_argcount;
2121    int co_nlocals;
2122    int co_stacksize;
2123    int co_flags;
2124    PyObject *co_code;
2125    PyObject *co_consts;
2126    PyObject *co_names;
2127    PyObject *co_varnames;
2128    PyObject *co_freevars;
2129    PyObject *co_cellvars;
2130
2131    PyObject *co_filename;
2132    PyObject *co_name;
2133    int co_firstlineno;
2134    PyObject *co_lnotab;
2135  }
2136
2137  /* Masks for co_flags above */
2138  const int CO_OPTIMIZED   = 0x0001;
2139  const int CO_NEWLOCALS   = 0x0002;
2140  const int CO_VARARGS     = 0x0004;
2141  const int CO_VARKEYWORDS = 0x0008;
2142  const int CO_NESTED      = 0x0010;
2143  const int CO_GENERATOR   = 0x0020;
2144  const int CO_NOFREE      = 0x0040;
2145
2146  const int CO_GENERATOR_ALLOWED = 0x1000;
2147  const int CO_FUTURE_DIVISION   = 0x2000;
2148
2149  // &PyCode_Type is accessible via PyCode_Type_p.
2150  // D translations of C macros:
2151  int PyCode_Check(PyObject *op) {
2152    return op.ob_type == PyCode_Type_p;
2153  }
2154  int PyCode_GetNumFree(PyObject *op) {
2155    return PyObject_Length((cast(PyCodeObject *) op).co_freevars);
2156  }
2157
2158  const int CO_MAXBLOCKS = 20;
2159
2160  struct node {
2161    short       n_type;
2162    char        *n_str;
2163    int         n_lineno;
2164    int         n_nchildren;
2165    node        *n_child;
2166  }
2167
2168  PyCodeObject *PyNode_Compile(node *, char *);
2169  PyCodeObject *PyCode_New(
2170    int, int, int, int, PyObject *, PyObject *, PyObject *, PyObject *,
2171    PyObject *, PyObject *, PyObject *, PyObject *, int, PyObject *);
2172  int PyCode_Addr2Line(PyCodeObject *, int);
2173
2174  struct PyFutureFeatures {
2175    int ff_found_docstring;
2176    int ff_last_lineno;
2177    int ff_features;
2178  }
2179
2180  PyFutureFeatures *PyNode_Future(node *, char *);
2181  PyCodeObject *PyNode_CompileFlags(node *, char *, PyCompilerFlags *);
2182
2183  const char[] FUTURE_NESTED_SCOPES = "nested_scopes";
2184  const char[] FUTURE_GENERATORS = "generators";
2185  const char[] FUTURE_DIVISION = "division";
2186
2187
2188///////////////////////////////////////////////////////////////////////////////
2189// CODE EXECUTION INTERFACE
2190///////////////////////////////////////////////////////////////////////////////
2191  // Python-header-file: Include/pythonrun.h:
2192
2193  struct PyCompilerFlags {
2194    int cf_flags;
2195  }
2196
2197  void Py_SetProgramName(char *);
2198  char *Py_GetProgramName();
2199
2200  void Py_SetPythonHome(char *);
2201  char *Py_GetPythonHome();
2202
2203  void Py_Initialize();
2204  void Py_InitializeEx(int);
2205  void Py_Finalize();
2206  int Py_IsInitialized();
2207  PyThreadState *Py_NewInterpreter();
2208  void Py_EndInterpreter(PyThreadState *);
2209
2210  int PyRun_AnyFile(FILE *, char *);
2211  int PyRun_AnyFileEx(FILE *, char *, int);
2212
2213  int PyRun_AnyFileFlags(FILE *, char *, PyCompilerFlags *);
2214  int PyRun_AnyFileExFlags(FILE *, char *, int, PyCompilerFlags *);
2215
2216  int PyRun_SimpleString(char *);
2217  int PyRun_SimpleStringFlags(char *, PyCompilerFlags *);
2218  int PyRun_SimpleFile(FILE *, char *);
2219  int PyRun_SimpleFileEx(FILE *, char *, int);
2220  int PyRun_SimpleFileExFlags(FILE *,  char *, int, PyCompilerFlags *);
2221  int PyRun_InteractiveOne(FILE *, char *);
2222  int PyRun_InteractiveOneFlags(FILE *, char *, PyCompilerFlags *);
2223  int PyRun_InteractiveLoop(FILE *, char *);
2224  int PyRun_InteractiveLoopFlags(FILE *, char *, PyCompilerFlags *);
2225
2226  node *PyParser_SimpleParseString(char *, int);
2227  node *PyParser_SimpleParseFile(FILE *, char *, int);
2228  node *PyParser_SimpleParseStringFlags(char *, int, int);
2229  node *PyParser_SimpleParseStringFlagsFilename(char *, char *, int, int);
2230  node *PyParser_SimpleParseFileFlags(FILE *, char *,int, int);
2231
2232  PyObject *PyRun_String(char *, int, PyObject *, PyObject *);
2233  PyObject *PyRun_File(FILE *, char *, int, PyObject *, PyObject *);
2234  PyObject *PyRun_FileEx(FILE *, char *, int, PyObject *, PyObject *, int);
2235  PyObject *PyRun_StringFlags( char *, int, PyObject *, PyObject *, PyCompilerFlags *);
2236  PyObject *PyRun_FileFlags(FILE *, char *, int, PyObject *, PyObject *, PyCompilerFlags *);
2237  PyObject *PyRun_FileExFlags(FILE *, char *, int, PyObject *, PyObject *, int, PyCompilerFlags *);
2238
2239  PyObject *Py_CompileString(char *, char *, int);
2240  PyObject *Py_CompileStringFlags(char *, char *, int, PyCompilerFlags *);
2241  // Py_SymtableString is undocumented, so it's omitted here.
2242
2243  void PyErr_Print();
2244  void PyErr_PrintEx(int);
2245  void PyErr_Display(PyObject *, PyObject *, PyObject *);
2246
2247  int Py_AtExit(void (*func)());
2248
2249  void Py_Exit(int);
2250
2251  int Py_FdIsInteractive(FILE *, char *);
2252
2253
2254///////////////////////////////////////////////////////////////////////////////
2255// BOOTSTRAPPING INTERFACE (for embedding Python in D)
2256///////////////////////////////////////////////////////////////////////////////
2257  // Python-header-file: Include/pythonrun.h:
2258
2259  int Py_Main(int argc, char **argv);
2260
2261  char *Py_GetProgramFullPath();
2262  char *Py_GetPrefix();
2263  char *Py_GetExecPrefix();
2264  char *Py_GetPath();
2265
2266  char *Py_GetVersion();
2267  char *Py_GetPlatform();
2268  char *Py_GetCopyright();
2269  char *Py_GetCompiler();
2270  char *Py_GetBuildInfo();
2271
2272  /////////////////////////////////////////////////////////////////////////////
2273  // ONE-TIME INITIALIZERS
2274  /////////////////////////////////////////////////////////////////////////////
2275
2276  PyObject *_PyBuiltin_Init();
2277  PyObject *_PySys_Init();
2278  void _PyImport_Init();
2279  void _PyExc_Init();
2280  void _PyImportHooks_Init();
2281  int _PyFrame_Init();
2282  int _PyInt_Init();
2283
2284  /////////////////////////////////////////////////////////////////////////////
2285  // FINALIZERS
2286  /////////////////////////////////////////////////////////////////////////////
2287
2288  void _PyExc_Fini();
2289  void _PyImport_Fini();
2290  void PyMethod_Fini();
2291  void PyFrame_Fini();
2292  void PyCFunction_Fini();
2293  void PyTuple_Fini();
2294  void PyString_Fini();
2295  void PyInt_Fini();
2296  void PyFloat_Fini();
2297  void PyOS_FiniInterrupts();
2298
2299  /////////////////////////////////////////////////////////////////////////////
2300  // VARIOUS (API members documented as having "no proper home")
2301  /////////////////////////////////////////////////////////////////////////////
2302  char *PyOS_Readline(FILE *, FILE *, char *);
2303  int (*PyOS_InputHook)();
2304  char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
2305  // _PyOS_ReadlineTState omitted.
2306  const int PYOS_STACK_MARGIN = 2048;
2307  // PyOS_CheckStack omitted.
2308
2309  /////////////////////////////////////////////////////////////////////////////
2310  // SIGNALS
2311  /////////////////////////////////////////////////////////////////////////////
2312
2313  alias void (*PyOS_sighandler_t)(int);
2314  PyOS_sighandler_t PyOS_getsig(int);
2315  PyOS_sighandler_t PyOS_setsig(int, PyOS_sighandler_t);
2316
2317
2318///////////////////////////////////////////////////////////////////////////////
2319// EVAL CALLS (documented as "Interface to random parts in ceval.c")
2320///////////////////////////////////////////////////////////////////////////////
2321  // Python-header-file: Include/ceval.h:
2322  PyObject *PyEval_CallObjectWithKeywords(PyObject *, PyObject *, PyObject *);
2323  PyObject *PyEval_CallObject(PyObject *, PyObject *);
2324  PyObject *PyEval_CallFunction(PyObject *obj, char *format, ...);
2325  PyObject *PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...);
2326
2327  void PyEval_SetProfile(Py_tracefunc, PyObject *);
2328  void PyEval_SetTrace(Py_tracefunc, PyObject *);
2329
2330  PyObject *PyEval_GetBuiltins();
2331  PyObject *PyEval_GetGlobals();
2332  PyObject *PyEval_GetLocals();
2333  PyFrameObject *PyEval_GetFrame();
2334  int PyEval_GetRestricted();
2335
2336  int PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
2337  int Py_FlushLine();
2338  int Py_AddPendingCall(int (*func)(void *), void *arg);
2339  int Py_MakePendingCalls();
2340
2341  void Py_SetRecursionLimit(int);
2342  int Py_GetRecursionLimit();
2343
2344  // The following API members are undocumented, so they're omitted here:
2345    // Py_EnterRecursiveCall
2346    // Py_LeaveRecursiveCall
2347    // _Py_CheckRecursiveCall
2348    // _Py_CheckRecursionLimit
2349    // _Py_MakeRecCheck
2350
2351  char *PyEval_GetFuncName(PyObject *);
2352  char *PyEval_GetFuncDesc(PyObject *);
2353
2354  PyObject *PyEval_GetCallStats(PyObject *);
2355  PyObject *PyEval_EvalFrame(PyFrameObject *);
2356
2357
2358///////////////////////////////////////////////////////////////////////////////
2359// SYSTEM MODULE INTERFACE
2360///////////////////////////////////////////////////////////////////////////////
2361  // Python-header-file: Include/sysmodule.h:
2362
2363  PyObject *PySys_GetObject(char *);
2364  int PySys_SetObject(char *, PyObject *);
2365  FILE *PySys_GetFile(char *, FILE *);
2366  void PySys_SetArgv(int, char **);
2367  void PySys_SetPath(char *);
2368
2369  void PySys_WriteStdout(char *format, ...);
2370  void PySys_WriteStderr(char *format, ...);
2371
2372  void PySys_ResetWarnOptions();
2373  void PySys_AddWarnOption(char *);
2374
2375
2376///////////////////////////////////////////////////////////////////////////////
2377// INTERRUPT INTERFACE
2378///////////////////////////////////////////////////////////////////////////////
2379  // Python-header-file: Include/intrcheck.h:
2380
2381  int PyOS_InterruptOccurred();
2382  void PyOS_InitInterrupts();
2383  void PyOS_AfterFork();
2384
2385
2386///////////////////////////////////////////////////////////////////////////////
2387// FRAME INTERFACE
2388///////////////////////////////////////////////////////////////////////////////
2389  // Python-header-file: Include/frameobject.h:
2390
2391  struct PyTryBlock {
2392    int b_type;
2393    int b_handler;
2394    int b_level;
2395  }
2396
2397  struct PyFrameObject {
2398    mixin PyObject_VAR_HEAD;
2399
2400    PyFrameObject *f_back;
2401    PyCodeObject *f_code;
2402    PyObject *f_builtins;
2403    PyObject *f_globals;
2404    PyObject *f_locals;
2405    PyObject **f_valuestack;
2406    PyObject **f_stacktop;
2407    PyObject *f_trace;
2408    PyObject *f_exc_type;
2409    PyObject *f_exc_value;
2410    PyObject *f_exc_traceback;
2411    PyThreadState *f_tstate;
2412    int f_lasti;
2413    int f_lineno;
2414    int f_restricted;
2415    int f_iblock;
2416    PyTryBlock f_blockstack[CO_MAXBLOCKS];
2417    int f_nlocals;
2418    int f_ncells;
2419    int f_nfreevars;
2420    int f_stacksize;
2421    PyObject *f_localsplus[1];
2422  }
2423
2424  // &PyFrame_Type is accessible via PyFrame_Type_p.
2425  // D translation of C macro:
2426  int PyFrame_Check(PyObject *op) {
2427    return op.ob_type == PyFrame_Type_p;
2428  }
2429
2430  PyFrameObject *PyFrame_New(PyThreadState *, PyCodeObject *,
2431                             PyObject *, PyObject *);
2432
2433  void PyFrame_BlockSetup(PyFrameObject *, int, int, int);
2434  PyTryBlock *PyFrame_BlockPop(PyFrameObject *);
2435  PyObject **PyFrame_ExtendStack(PyFrameObject *, int, int);
2436
2437  void PyFrame_LocalsToFast(PyFrameObject *, int);
2438  void PyFrame_FastToLocals(PyFrameObject *);
2439
2440
2441///////////////////////////////////////////////////////////////////////////////
2442// INTERPRETER STATE AND THREAD STATE INTERFACES
2443///////////////////////////////////////////////////////////////////////////////
2444  // Python-header-file: Include/pystate.h:
2445
2446  struct PyInterpreterState {
2447    PyInterpreterState *next;
2448    PyThreadState *tstate_head;
2449
2450    PyObject *modules;
2451    PyObject *sysdict;
2452    PyObject *builtins;
2453
2454    PyObject *codec_search_path;
2455    PyObject *codec_search_cache;
2456    PyObject *codec_error_registry;
2457
2458    int dlopenflags;
2459
2460    // XXX: Not sure what WITH_TSC refers to, or how to conditionalize it in D:
2461    //#ifdef WITH_TSC
2462    //  int tscdump;
2463    //#endif
2464  }
2465
2466  alias int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
2467
2468  const int PyTrace_CALL        = 0;
2469  const int PyTrace_EXCEPTION = 1;
2470  const int PyTrace_LINE                = 2;
2471  const int PyTrace_RETURN      = 3;
2472  const int PyTrace_C_CALL = 4;
2473  const int PyTrace_C_EXCEPTION = 5;
2474  const int PyTrace_C_RETURN = 6;
2475
2476  struct PyThreadState {
2477    PyThreadState *next;
2478    PyInterpreterState *interp;
2479
2480    PyFrameObject *frame;
2481    int recursion_depth;
2482    int tracing;
2483    int use_tracing;
2484
2485    Py_tracefunc c_profilefunc;
2486    Py_tracefunc c_tracefunc;
2487    PyObject *c_profileobj;
2488    PyObject *c_traceobj;
2489
2490    PyObject *curexc_type;
2491    PyObject *curexc_value;
2492    PyObject *curexc_traceback;
2493
2494    PyObject *exc_type;
2495    PyObject *exc_value;
2496    PyObject *exc_traceback;
2497
2498    PyObject *dict;
2499
2500    int tick_counter;
2501    int gilstate_counter;
2502
2503    PyObject *async_exc;
2504    C_long thread_id;
2505  }
2506
2507  PyInterpreterState *PyInterpreterState_New();
2508  void PyInterpreterState_Clear(PyInterpreterState *);
2509  void PyInterpreterState_Delete(PyInterpreterState *);
2510
2511  PyThreadState *PyThreadState_New(PyInterpreterState *);
2512  void PyThreadState_Clear(PyThreadState *);
2513  void PyThreadState_Delete(PyThreadState *);
2514  void PyThreadState_DeleteCurrent();
2515
2516  PyThreadState *PyThreadState_Get();
2517  PyThreadState *PyThreadState_Swap(PyThreadState *);
2518  PyObject *PyThreadState_GetDict();
2519  int PyThreadState_SetAsyncExc(C_long, PyObject *);
2520
2521  enum PyGILState_STATE {PyGILState_LOCKED, PyGILState_UNLOCKED};
2522
2523  PyGILState_STATE PyGILState_Ensure();
2524  void PyGILState_Release(PyGILState_STATE);
2525  PyThreadState *PyGILState_GetThisThreadState();
2526  PyInterpreterState *PyInterpreterState_Head();
2527  PyInterpreterState *PyInterpreterState_Next(PyInterpreterState *);
2528  PyThreadState *PyInterpreterState_ThreadHead(PyInterpreterState *);
2529  PyThreadState *PyThreadState_Next(PyThreadState *);
2530
2531  alias PyFrameObject *(*PyThreadFrameGetter)(PyThreadState *self_);
2532
2533  // Python-header-file: Include/ceval.h:
2534  PyThreadState *PyEval_SaveThread();
2535  void PyEval_RestoreThread(PyThreadState *);
2536
2537  int PyEval_ThreadsInitialized();
2538  void PyEval_InitThreads();
2539  void PyEval_AcquireLock();
2540  void PyEval_ReleaseLock();
2541  void PyEval_AcquireThread(PyThreadState *tstate);
2542  void PyEval_ReleaseThread(PyThreadState *tstate);
2543  void PyEval_ReInitThreads();
2544
2545  // YYY: The following macros need to be implemented somehow, but DSR doesn't
2546  // think D's mixin feature is up to the job.
2547  // Py_BEGIN_ALLOW_THREADS
2548  // Py_BLOCK_THREADS
2549  // Py_UNBLOCK_THREADS
2550  // Py_END_ALLOW_THREADS
2551
2552
2553///////////////////////////////////////////////////////////////////////////////
2554// MODULE IMPORT INTERFACE
2555///////////////////////////////////////////////////////////////////////////////
2556  // Python-header-file: Include/import.h:
2557
2558  C_long PyImport_GetMagicNumber();
2559  PyObject *PyImport_ExecCodeModule(char *name, PyObject *co);
2560  PyObject *PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname);
2561  PyObject *PyImport_GetModuleDict();
2562  PyObject *PyImport_AddModule(char *name);
2563  PyObject *PyImport_ImportModule(char *name);
2564  PyObject *PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist);
2565  PyObject *PyImport_Import(PyObject *name);
2566  PyObject *PyImport_ReloadModule(PyObject *m);
2567  void PyImport_Cleanup();
2568  int PyImport_ImportFrozenModule(char *);
2569
2570  // The following API members are undocumented, so they're omitted here:
2571    // _PyImport_FindModule
2572    // _PyImport_IsScript
2573    // _PyImport_ReInitLock
2574
2575  PyObject *_PyImport_FindExtension(char *, char *);
2576  PyObject *_PyImport_FixupExtension(char *, char *);
2577
2578  struct _inittab {
2579    char *name;
2580    void (*initfunc)();
2581  }
2582
2583  int PyImport_AppendInittab(char *name, void (*initfunc)());
2584  int PyImport_ExtendInittab(_inittab *newtab);
2585
2586  struct _frozen {
2587    char *name;
2588    ubyte *code;
2589    int size;
2590  }
2591
2592  // Omitted:
2593    // PyImport_FrozenModules
2594
2595
2596///////////////////////////////////////////////////////////////////////////////
2597// ABSTRACT OBJECT INTERFACE
2598///////////////////////////////////////////////////////////////////////////////
2599  // Python-header-file: Include/abstract.h:
2600
2601  // D translations of C macros:
2602  int PyObject_DelAttrString(PyObject *o, char *a) {
2603    return PyObject_SetAttrString(o, a, null);
2604  }
2605  int PyObject_DelAttr(PyObject *o, PyObject *a) {
2606    return PyObject_SetAttr(o, a, null);
2607  }
2608
2609  int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result);
2610
2611  /////////////////////////////////////////////////////////////////////////////
2612  // CALLABLES
2613  /////////////////////////////////////////////////////////////////////////////
2614//FIXME  int PyCallable_Check(PyObject *o);
2615
2616  PyObject *PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw);
2617  PyObject *PyObject_CallObject(PyObject *callable_object, PyObject *args);
2618  PyObject *PyObject_CallFunction(PyObject *callable_object, char *format, ...);
2619  PyObject *PyObject_CallMethod(PyObject *o, char *m, char *format, ...);
2620  PyObject *PyObject_CallFunctionObjArgs(PyObject *callable, ...);
2621  PyObject *PyObject_CallMethodObjArgs(PyObject *o,PyObject *m, ...);
2622
2623  /////////////////////////////////////////////////////////////////////////////
2624  // GENERIC
2625  /////////////////////////////////////////////////////////////////////////////
2626  PyObject *PyObject_Type(PyObject *o);
2627
2628  /////////////////////////////////////////////////////////////////////////////
2629  // CONTAINERS
2630  /////////////////////////////////////////////////////////////////////////////
2631
2632  int PyObject_Size(PyObject *o);
2633  int PyObject_Length(PyObject *o);
2634
2635  PyObject *PyObject_GetItem(PyObject *o, PyObject *key);
2636  int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
2637  int PyObject_DelItemString(PyObject *o, char *key);
2638  int PyObject_DelItem(PyObject *o, PyObject *key);
2639
2640  int PyObject_AsCharBuffer(PyObject *obj, char **buffer, int *buffer_len);
2641  int PyObject_CheckReadBuffer(PyObject *obj);
2642  int PyObject_AsReadBuffer(PyObject *obj, void **buffer, int *buffer_len);
2643  int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, int *buffer_len);
2644
2645  /////////////////////////////////////////////////////////////////////////////
2646  // ITERATORS
2647  /////////////////////////////////////////////////////////////////////////////
2648  PyObject *PyObject_GetIter(PyObject *);
2649
2650  // D translation of C macro:
2651  int PyIter_Check(PyObject *obj) {
2652    return PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_ITER)
2653        && PyTypeObject_GetIterNext(obj.ob_type) != null;
2654  }
2655
2656  PyObject *PyIter_Next(PyObject *);
2657
2658  /////////////////////////////////////////////////////////////////////////////
2659  // NUMBERS
2660  /////////////////////////////////////////////////////////////////////////////
2661
2662  int PyNumber_Check(PyObject *o);
2663
2664  PyObject *PyNumber_Add(PyObject *o1, PyObject *o2);
2665  PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2);
2666  PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2);
2667  PyObject *PyNumber_Divide(PyObject *o1, PyObject *o2);
2668  PyObject *PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
2669  PyObject *PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
2670  PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2);
2671  PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2);
2672  PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3);
2673  PyObject *PyNumber_Negative(PyObject *o);
2674  PyObject *PyNumber_Positive(PyObject *o);
2675  PyObject *PyNumber_Absolute(PyObject *o);
2676  PyObject *PyNumber_Invert(PyObject *o);
2677  PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2);
2678  PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2);
2679  PyObject *PyNumber_And(PyObject *o1, PyObject *o2);
2680  PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2);
2681  PyObject *PyNumber_Or(PyObject *o1, PyObject *o2);
2682
2683  PyObject *PyNumber_Int(PyObject *o);
2684  PyObject *PyNumber_Long(PyObject *o);
2685  PyObject *PyNumber_Float(PyObject *o);
2686
2687  PyObject *PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
2688  PyObject *PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
2689  PyObject *PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
2690  PyObject *PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2);
2691  PyObject *PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2);
2692  PyObject *PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2);
2693  PyObject *PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
2694  PyObject *PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3);
2695  PyObject *PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
2696  PyObject *PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
2697  PyObject *PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
2698  PyObject *PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
2699  PyObject *PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
2700
2701  /////////////////////////////////////////////////////////////////////////////
2702  // SEQUENCES
2703  /////////////////////////////////////////////////////////////////////////////
2704
2705  int PySequence_Check(PyObject *o);
2706  int PySequence_Size(PyObject *o);
2707  int PySequence_Length(PyObject *o);
2708
2709  PyObject *PySequence_Concat(PyObject *o1, PyObject *o2);
2710  PyObject *PySequence_Repeat(PyObject *o, int count);
2711  PyObject *PySequence_GetItem(PyObject *o, int i);
2712  PyObject *PySequence_GetSlice(PyObject *o, int i1, int i2);
2713
2714  int PySequence_SetItem(PyObject *o, int i, PyObject *v);
2715  int PySequence_DelItem(PyObject *o, int i);
2716  int PySequence_SetSlice(PyObject *o, int i1, int i2, PyObject *v);
2717  int PySequence_DelSlice(PyObject *o, int i1, int i2);
2718
2719  PyObject *PySequence_Tuple(PyObject *o);
2720  PyObject *PySequence_List(PyObject *o);
2721
2722  PyObject *PySequence_Fast(PyObject *o,  char* m);
2723  // D translations of C macros:
2724  int PySequence_Fast_GET_SIZE(PyObject *o) {
2725    return PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o);
2726  }
2727  PyObject *PySequence_Fast_GET_ITEM(PyObject *o, int i) {
2728    return PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i);
2729  }
2730  PyObject *PySequence_ITEM(PyObject *o, int i) {
2731    return PyTypeObject_GetAsSequence(o.ob_type).sq_item(o, i);
2732  }
2733/* FIXME
2734  PyObject **PySequence_Fast_ITEMS(PyObject *sf) {
2735    return
2736        PyList_Check(sf) ?
2737            (cast(PyListObject *)sf).ob_item
2738          : (cast(PyTupleObject *)sf).ob_item
2739      ;
2740  }
2741*/
2742
2743  int PySequence_Count(PyObject *o, PyObject *value);
2744  int PySequence_Contains(PyObject *seq, PyObject *ob);
2745
2746  int PY_ITERSEARCH_COUNT    = 1;
2747  int PY_ITERSEARCH_INDEX    = 2;
2748  int PY_ITERSEARCH_CONTAINS = 3;
2749
2750  int _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation);
2751  int PySequence_In(PyObject *o, PyObject *value);
2752  int PySequence_Index(PyObject *o, PyObject *value);
2753
2754  PyObject * PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
2755  PyObject * PySequence_InPlaceRepeat(PyObject *o, int count);
2756
2757  /////////////////////////////////////////////////////////////////////////////
2758  // MAPPINGS
2759  /////////////////////////////////////////////////////////////////////////////
2760  int PyMapping_Check(PyObject *o);
2761  int PyMapping_Size(PyObject *o);
2762  int PyMapping_Length(PyObject *o);
2763  alias PyMapping_Size PyMapping_Length;
2764
2765  // D translations of C macros:
2766  int PyMapping_DelItemString(PyObject *o, char *k) {
2767    return PyObject_DelItemString(o, k);
2768  }
2769  int PyMapping_DelItem(PyObject *o, PyObject *k) {
2770    return PyObject_DelItem(o, k);
2771  }
2772
2773  int PyMapping_HasKeyString(PyObject *o, char *key);
2774  int PyMapping_HasKey(PyObject *o, PyObject *key);
2775
2776  // D translations of C macros:
2777  PyObject *PyMapping_Keys(PyObject *o) {
2778    return PyObject_CallMethod(o, "keys", null);
2779  }
2780  PyObject *PyMapping_Values(PyObject *o) {
2781    return PyObject_CallMethod(o, "values", null);
2782  }
2783  PyObject *PyMapping_Items(PyObject *o) {
2784    return PyObject_CallMethod(o, "items", null);
2785  }
2786
2787  PyObject *PyMapping_GetItemString(PyObject *o, char *key);
2788  int PyMapping_SetItemString(PyObject *o, char *key, PyObject *value);
2789
2790  /////////////////////////////////////////////////////////////////////////////
2791  // GENERIC
2792  /////////////////////////////////////////////////////////////////////////////
2793  int PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
2794  int PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
2795
2796
2797///////////////////////////////////////////////////////////////////////////////
2798// OBJECT CREATION AND GARBAGE COLLECTION
2799///////////////////////////////////////////////////////////////////////////////
2800  // Python-header-file: Include/objimpl.h:
2801
2802  void * PyObject_Malloc(size_t);
2803  void * PyObject_Realloc(void *, size_t);
2804  void PyObject_Free(void *);
2805
2806  PyObject * PyObject_Init(PyObject *, PyTypeObject *);
2807  PyVarObject * PyObject_InitVar(PyVarObject *,
2808                           PyTypeObject *, int);
2809  /* Without macros, DSR knows of no way to translate PyObject_New and
2810   * PyObject_NewVar to D; the lower-level _PyObject_New and _PyObject_NewVar
2811   * will have to suffice.
2812   * YYY: Perhaps D's mixins could be used? */
2813  PyObject * _PyObject_New(PyTypeObject *);
2814  PyVarObject * _PyObject_NewVar(PyTypeObject *, int);
2815
2816
2817  C_long PyGC_Collect();
2818
2819  // D translations of C macros:
2820  int PyType_IS_GC(PyTypeObject *t) {
2821    return PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC);
2822  }
2823  int PyObject_IS_GC(PyObject *o) {
2824    return PyType_IS_GC(o.ob_type)
2825        && (PyTypeObject_GetIsGc(o.ob_type) == null || PyTypeObject_GetIsGc(o.ob_type)(o));
2826  }
2827  PyVarObject *_PyObject_GC_Resize(PyVarObject *, int);
2828  // XXX: Can D mixins allows trans of PyObject_GC_Resize?
2829
2830  union PyGC_Head {
2831    struct gc {
2832      PyGC_Head *gc_next;
2833      PyGC_Head *gc_prev;
2834      int gc_refs;
2835    }
2836    real dummy; // XXX: C type was long double; is this equiv?
2837  }
2838
2839  // Numerous macro definitions that appear in objimpl.h at this point are not
2840  // document.  They appear to be for internal use, so they're omitted here.
2841
2842  PyObject *_PyObject_GC_Malloc(size_t);
2843  PyObject *_PyObject_GC_New(PyTypeObject *);
2844  PyVarObject *_PyObject_GC_NewVar(PyTypeObject *, int);
2845  PyGC_Head *_PyGC_generation0;
2846  void PyObject_GC_Track(void *);
2847  void PyObject_GC_UnTrack(void *);
2848  void PyObject_GC_Del(void *);
2849
2850  // XXX: DSR currently knows of no way to translate the PyObject_GC_New and
2851  // PyObject_GC_NewVar macros to D.
2852
2853  /////////////////////////////////////////////////////////////////////////////
2854  // MISCELANEOUS
2855  /////////////////////////////////////////////////////////////////////////////
2856  // Python-header-file: Include/pydebug.h:
2857  void Py_FatalError(char *message);
2858
2859
2860///////////////////////////////////////////////////////////////////////////////
2861// cStringIO (Must be explicitly imported with PycString_IMPORT().)
2862///////////////////////////////////////////////////////////////////////////////
2863// Python-header-file: Include/cStringIO.h:
2864
2865PycStringIO_CAPI *PycStringIO = null;
2866
2867PycStringIO_CAPI *PycString_IMPORT() {
2868  if (PycStringIO == null) {
2869    PycStringIO = cast(PycStringIO_CAPI *)
2870      PyCObject_Import("cStringIO", "cStringIO_CAPI");
2871  }
2872  return PycStringIO;
2873}
2874
2875struct PycStringIO_CAPI {
2876  int(*cread)(PyObject *, char **, int);
2877  int(*creadline)(PyObject *, char **);
2878  int(*cwrite)(PyObject *, char *, int);
2879  PyObject *(*cgetvalue)(PyObject *);
2880  PyObject *(*NewOutput)(int);
2881  PyObject *(*NewInput)(PyObject *);
2882  PyTypeObject *InputType;
2883  PyTypeObject *OutputType;
2884}
2885
2886// D translations of C macros:
2887int PycStringIO_InputCheck(PyObject *o) {
2888  return o.ob_type == PycStringIO.InputType;
2889}
2890int PycStringIO_OutputCheck(PyObject *o) {
2891  return o.ob_type == PycStringIO.OutputType;
2892}
2893
2894
2895///////////////////////////////////////////////////////////////////////////////
2896// datetime (Must be explicitly imported with PycString_IMPORT().)
2897///////////////////////////////////////////////////////////////////////////////
2898// Python-header-file: Include/datetime.h:
2899
2900const int _PyDateTime_DATE_DATASIZE = 4;
2901const int _PyDateTime_TIME_DATASIZE = 6;
2902const int _PyDateTime_DATETIME_DATASIZE = 10;
2903
2904struct PyDateTime_Delta {
2905  mixin PyObject_HEAD;
2906
2907  C_long hashcode;
2908  int days;
2909  int seconds;
2910  int microseconds;
2911}
2912
2913struct PyDateTime_TZInfo {
2914  mixin PyObject_HEAD;
2915}
2916
2917template _PyTZINFO_HEAD() {
2918  mixin PyObject_HEAD;
2919  C_long hashcode;
2920  ubyte hastzinfo;
2921}
2922
2923struct _PyDateTime_BaseTZInfo {
2924  mixin _PyTZINFO_HEAD;
2925}
2926
2927template _PyDateTime_TIMEHEAD() {
2928  mixin _PyTZINFO_HEAD;
2929  ubyte data[_PyDateTime_TIME_DATASIZE];
2930}
2931
2932struct _PyDateTime_BaseTime {
2933  mixin _PyDateTime_TIMEHEAD;
2934}
2935
2936struct PyDateTime_Time {
2937  mixin _PyDateTime_TIMEHEAD;
2938  PyObject *tzinfo;
2939}
2940
2941struct PyDateTime_Date {
2942  mixin _PyTZINFO_HEAD;
2943  ubyte data[_PyDateTime_DATE_DATASIZE];
2944}
2945
2946template _PyDateTime_DATETIMEHEAD() {
2947  mixin _PyTZINFO_HEAD;
2948  ubyte data[_PyDateTime_DATETIME_DATASIZE];
2949}
2950
2951struct _PyDateTime_BaseDateTime {
2952  mixin _PyDateTime_DATETIMEHEAD;
2953}
2954
2955struct PyDateTime_DateTime {
2956  mixin _PyDateTime_DATETIMEHEAD;
2957  PyObject *tzinfo;
2958}
2959
2960// D translations of C macros:
2961int PyDateTime_GET_YEAR(PyObject *o) {
2962  PyDateTime_Date *ot = cast(PyDateTime_Date *) o;
2963  return (ot.data[0] << 8) | ot.data[1];
2964}
2965int PyDateTime_GET_MONTH(PyObject *o) {
2966  PyDateTime_Date *ot = cast(PyDateTime_Date *) o;
2967  return ot.data[2];
2968}
2969int PyDateTime_GET_DAY(PyObject *o) {
2970  PyDateTime_Date *ot = cast(PyDateTime_Date *) o;
2971  return ot.data[3];
2972}
2973
2974int PyDateTime_DATE_GET_HOUR(PyObject *o) {
2975  PyDateTime_DateTime *ot = cast(PyDateTime_DateTime *) o;
2976  return ot.data[4];
2977}
2978int PyDateTime_DATE_GET_MINUTE(PyObject *o) {
2979  PyDateTime_DateTime *ot = cast(PyDateTime_DateTime *) o;
2980  return ot.data[5];
2981}
2982int PyDateTime_DATE_GET_SECOND(PyObject *o) {
2983  PyDateTime_DateTime *ot = cast(PyDateTime_DateTime *) o;
2984  return ot.data[6];
2985}
2986int PyDateTime_DATE_GET_MICROSECOND(PyObject *o) {
2987  PyDateTime_DateTime *ot = cast(PyDateTime_DateTime *) o;
2988  return (ot.data[7] << 16) | (ot.data[8] << 8) | ot.data[9];
2989}
2990
2991int PyDateTime_TIME_GET_HOUR(PyObject *o) {
2992  PyDateTime_Time *ot = cast(PyDateTime_Time *) o;
2993  return ot.data[0];
2994}
2995int PyDateTime_TIME_GET_MINUTE(PyObject *o) {
2996  PyDateTime_Time *ot = cast(PyDateTime_Time *) o;
2997  return ot.data[1];
2998}
2999int PyDateTime_TIME_GET_SECOND(PyObject *o) {
3000  PyDateTime_Time *ot = cast(PyDateTime_Time *) o;
3001  return ot.data[2];
3002}
3003int PyDateTime_TIME_GET_MICROSECOND(PyObject *o) {
3004  PyDateTime_Time *ot = cast(PyDateTime_Time *) o;
3005  return (ot.data[3] << 16) | (ot.data[4] << 8) | ot.data[5];
3006}
3007
3008struct PyDateTime_CAPI {
3009  PyTypeObject *DateType;
3010  PyTypeObject *DateTimeType;
3011  PyTypeObject *TimeType;
3012  PyTypeObject *DeltaType;
3013  PyTypeObject *TZInfoType;
3014
3015  PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
3016  PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
3017          PyObject*, PyTypeObject*);
3018  PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
3019  PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
3020
3021  PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
3022  PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
3023}
3024
3025const int DATETIME_API_MAGIC = 0x414548d5;
3026PyDateTime_CAPI *PyDateTimeAPI;
3027
3028PyDateTime_CAPI *PyDateTime_IMPORT() {
3029  if (PyDateTimeAPI == null) {
3030    PyDateTimeAPI = cast(PyDateTime_CAPI *)
3031      PyCObject_Import("datetime", "datetime_CAPI");
3032  }
3033  return PyDateTimeAPI;
3034}
3035
3036// D translations of C macros:
3037int PyDate_Check(PyObject *op) {
3038  return PyObject_TypeCheck(op, PyDateTimeAPI.DateType);
3039}
3040int PyDate_CheckExact(PyObject *op) {
3041  return op.ob_type == PyDateTimeAPI.DateType;
3042}
3043int PyDateTime_Check(PyObject *op) {
3044  return PyObject_TypeCheck(op, PyDateTimeAPI.DateTimeType);
3045}
3046int PyDateTime_CheckExact(PyObject *op) {
3047  return op.ob_type == PyDateTimeAPI.DateTimeType;
3048}
3049int PyTime_Check(PyObject *op) {
3050  return PyObject_TypeCheck(op, PyDateTimeAPI.TimeType);
3051}
3052int PyTime_CheckExact(PyObject *op) {
3053  return op.ob_type == PyDateTimeAPI.TimeType;
3054}
3055int PyDelta_Check(PyObject *op) {
3056  return PyObject_TypeCheck(op, PyDateTimeAPI.DeltaType);
3057}
3058int PyDelta_CheckExact(PyObject *op) {
3059  return op.ob_type == PyDateTimeAPI.DeltaType;
3060}
3061int PyTZInfo_Check(PyObject *op) {
3062  return PyObject_TypeCheck(op, PyDateTimeAPI.TZInfoType);
3063}
3064int PyTZInfo_CheckExact(PyObject *op) {
3065  return op.ob_type == PyDateTimeAPI.TZInfoType;
3066}
3067
3068PyObject *PyDate_FromDate(int year, int month, int day) {
3069  return PyDateTimeAPI.Date_FromDate(year, month, day, PyDateTimeAPI.DateType);
3070}
3071PyObject *PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int min, int sec, int usec) {
3072  return PyDateTimeAPI.DateTime_FromDateAndTime(year, month, day, hour,
3073    min, sec, usec, Py_None, PyDateTimeAPI.DateTimeType);
3074}
3075PyObject *PyTime_FromTime(int hour, int minute, int second, int usecond) {
3076  return PyDateTimeAPI.Time_FromTime(hour, minute, second, usecond,
3077    Py_None, PyDateTimeAPI.TimeType);
3078}
3079PyObject *PyDelta_FromDSU(int days, int seconds, int useconds) {
3080  return PyDateTimeAPI.Delta_FromDelta(days, seconds, useconds, 1,
3081    PyDateTimeAPI.DeltaType);
3082}
3083PyObject *PyDateTime_FromTimestamp(PyObject *args) {
3084  return PyDateTimeAPI.DateTime_FromTimestamp(
3085    cast(PyObject*) (PyDateTimeAPI.DateTimeType), args, null);
3086}
3087PyObject *PyDate_FromTimestamp(PyObject *args) {
3088  return PyDateTimeAPI.Date_FromTimestamp(
3089    cast(PyObject*) (PyDateTimeAPI.DateType), args);
3090}
3091
3092
3093///////////////////////////////////////////////////////////////////////////////
3094// Interface to execute compiled code
3095///////////////////////////////////////////////////////////////////////////////
3096// Python-header-file: Include/eval.h:
3097PyObject *PyEval_EvalCode(PyCodeObject *, PyObject *, PyObject *);
3098PyObject *PyEval_EvalCodeEx(
3099    PyCodeObject *co,
3100    PyObject *globals,
3101    PyObject *locals,
3102    PyObject **args, int argc,
3103    PyObject **kwds, int kwdc,
3104    PyObject **defs, int defc,
3105    PyObject *closure
3106  );
3107PyObject *_PyEval_CallTracing(PyObject *func, PyObject *args);
3108
3109
3110///////////////////////////////////////////////////////////////////////////////
3111// Generator object interface
3112///////////////////////////////////////////////////////////////////////////////
3113// Python-header-file: Include/genobject.h:
3114struct PyGenObject {
3115  mixin PyObject_HEAD;
3116  PyFrameObject *gi_frame;
3117  int gi_running;
3118  PyObject *gi_weakreflist;
3119}
3120
3121// &PyGen_Type is accessible via PyGen_Type_p.
3122// D translations of C macros:
3123int PyGen_Check(PyObject *op) {
3124  return PyObject_TypeCheck(op, PyGen_Type_p);
3125}
3126int PyGen_CheckExact(PyObject *op) {
3127  return op.ob_type == PyGen_Type_p;
3128}
3129
3130PyObject *PyGen_New(PyFrameObject *);
3131
3132
3133///////////////////////////////////////////////////////////////////////////////
3134// Interface for marshal.c
3135///////////////////////////////////////////////////////////////////////////////
3136// Python-header-file: Include/marshal.h:
3137
3138const int Py_MARSHAL_VERSION = 1;
3139
3140void PyMarshal_WriteLongToFile(C_long, FILE *, int);
3141void PyMarshal_WriteObjectToFile(PyObject *, FILE *, int);
3142PyObject * PyMarshal_WriteObjectToString(PyObject *, int);
3143
3144C_long PyMarshal_ReadLongFromFile(FILE *);
3145int PyMarshal_ReadShortFromFile(FILE *);
3146PyObject *PyMarshal_ReadObjectFromFile(FILE *);
3147PyObject *PyMarshal_ReadLastObjectFromFile(FILE *);
3148PyObject *PyMarshal_ReadObjectFromString(char *, int);
3149
3150
3151///////////////////////////////////////////////////////////////////////////////
3152// Platform-independent wrappers around strod, etc (probably not needed in D)
3153///////////////////////////////////////////////////////////////////////////////
3154// Python-header-file: Include/pystrtod.h:
3155
3156double PyOS_ascii_strtod(char *str, char **ptr);
3157double PyOS_ascii_atof(char *str);
3158char *PyOS_ascii_formatd(char *buffer, int buf_len, char *format, double d);
3159
3160
3161///////////////////////////////////////////////////////////////////////////////
3162// INTERFACE TO THE STDLIB 'THREAD' MODULE
3163///////////////////////////////////////////////////////////////////////////////
3164// Python-header-file: Include/pythread.h:
3165
3166alias void * PyThread_type_lock;
3167alias void * PyThread_type_sema;
3168
3169void PyThread_init_thread();
3170C_long PyThread_start_new_thread(void (*)(void *), void *);
3171void PyThread_exit_thread();
3172void PyThread__PyThread_exit_thread();
3173C_long PyThread_get_thread_ident();
3174
3175PyThread_type_lock PyThread_allocate_lock();
3176void PyThread_free_lock(PyThread_type_lock);
3177int PyThread_acquire_lock(PyThread_type_lock, int);
3178const int WAIT_LOCK = 1;
3179const int NOWAIT_LOCK = 0;
3180void PyThread_release_lock(PyThread_type_lock);
3181
3182void PyThread_exit_prog(int);
3183void PyThread__PyThread_exit_prog(int);
3184
3185int PyThread_create_key();
3186void PyThread_delete_key(int);
3187int PyThread_set_key_value(int, void *);
3188void *PyThread_get_key_value(int);
3189void PyThread_delete_key_value(int key);
3190
3191
3192///////////////////////////////////////////////////////////////////////////////
3193// SET INTERFACE (built-in types set and frozenset)
3194///////////////////////////////////////////////////////////////////////////////
3195// Python-header-file: Include/setobject.h:
3196
3197struct PySetObject {
3198  mixin PyObject_HEAD;
3199
3200  PyObject *data;
3201  C_long hash;
3202  PyObject *weakreflist;
3203}
3204
3205// &PySet_Type is accessible via PySet_Type_p.
3206// &PyFrozenSet_Type is accessible via PyFrozenSet_Type_p.
3207// D translations of C macros:
3208int PyFrozenSet_CheckExact(PyObject *ob) {
3209  return ob.ob_type == PyFrozenSet_Type_p;
3210}
3211int PyAnySet_Check(PyObject *ob) {
3212  return (
3213         ob.ob_type == PySet_Type_p
3214      || ob.ob_type == PyFrozenSet_Type_p
3215      || PyType_IsSubtype(ob.ob_type, PySet_Type_p)
3216      || PyType_IsSubtype(ob.ob_type, PyFrozenSet_Type_p)
3217    );
3218}
3219
3220
3221///////////////////////////////////////////////////////////////////////////////
3222// Interface to map C struct members to Python object attributes
3223///////////////////////////////////////////////////////////////////////////////
3224// Python-header-file: Include/structmember.h:
3225
3226/+
3227struct PyMemberDef {
3228  char *name;
3229  int type;
3230  int offset;
3231  int flags;
3232  char *doc;
3233}
3234+/
3235
3236alias void PyMemberDef;
3237
3238PyMemberDef* Py_CreateMemberDef(int num);
3239void Py_FreeMemberDef(PyMemberDef* pyMemberDef);
3240char *PyMemberDef_GetName(PyMemberDef* pyMemberDef, int num);
3241void PyMemberDef_SetName(PyMemberDef* pyMemberDef, int num, char *name);
3242int PyMemberDef_GetType(PyMemberDef* pyMemberDef, int num);
3243void PyMemberDef_SetType(PyMemberDef* pyMemberDef, int num, int type);
3244int PyMemberDef_GetOffset(PyMemberDef* pyMemberDef, int num);
3245void PyMemberDef_SetOffset(PyMemberDef* pyMemberDef, int num, int offset);
3246int PyMemberDef_GetFlags(PyMemberDef* pyMemberDef, int num);
3247void PyMemberDef_SetFlags(PyMemberDef* pyMemberDef, int num, int flags);
3248char *PyMemberDef_GetDoc(PyMemberDef* pyMemberDef, int num);
3249void PyMemberDef_SetDoc(PyMemberDef* pyMemberDef, int num, char *doc);
3250void PyMemberDef_Clear(PyMemberDef* pyMemberDef, int num);
3251
3252const int T_SHORT = 0;
3253const int T_INT = 1;
3254const int T_LONG = 2;
3255const int T_FLOAT = 3;
3256const int T_DOUBLE = 4;
3257const int T_STRING = 5;
3258const int T_OBJECT = 6;
3259const int T_CHAR = 7;
3260const int T_BYTE = 8;
3261const int T_UBYTE = 9;
3262const int T_USHORT = 10;
3263const int T_UINT = 11;
3264const int T_ULONG = 12;
3265const int T_STRING_INPLACE = 13;
3266const int T_OBJECT_EX = 16;
3267
3268const int READONLY = 1;
3269alias READONLY RO;
3270const int READ_RESTRICTED = 2;
3271const int WRITE_RESTRICTED = 4;
3272const int RESTRICTED = (READ_RESTRICTED | WRITE_RESTRICTED);
3273
3274PyObject *PyMember_GetOne(char *, PyMemberDef *);
3275int PyMember_SetOne(char *, PyMemberDef *, PyObject *);
3276
3277
3278///////////////////////////////////////////////////////////////////////////////
3279// INTERFACE FOR TUPLE-LIKE "STRUCTURED SEQUENCES"
3280///////////////////////////////////////////////////////////////////////////////
3281  // Python-header-file: Include/structseq.h:
3282
3283struct PyStructSequence_Field {
3284  char *name;
3285  char *doc;
3286}
3287
3288struct PyStructSequence_Desc {
3289  char *name;
3290  char *doc;
3291  PyStructSequence_Field *fields;
3292  int n_in_sequence;
3293}
3294
3295// XXX: What about global var PyStructSequence_UnnamedField?
3296
3297void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc);
3298PyObject *PyStructSequence_New(PyTypeObject* type);
3299
3300struct PyStructSequence {
3301  mixin PyObject_VAR_HEAD;
3302  // DSR:XXX:LAYOUT:
3303  // Will the D layout for a 1-obj array be the same as the C layout?  I
3304  // think the D array will be larger.
3305  PyObject *ob_item[1];
3306}
3307
3308// D translation of C macro:
3309PyObject *PyStructSequence_SET_ITEM(PyObject *op, int i, PyObject *v) {
3310  PyStructSequence *ot = cast(PyStructSequence *) op;
3311  ot.ob_item[i] = v;
3312  return v;
3313}
3314
3315
3316///////////////////////////////////////////////////////////////////////////////
3317// UTILITY FUNCTION RELATED TO TIMEMODULE.C
3318///////////////////////////////////////////////////////////////////////////////
3319  // Python-header-file: Include/timefuncs.h:
3320
3321  time_t _PyTime_DoubleToTimet(double x);
3322
3323
3324
3325} /* extern (C) */
3326
3327
3328/* The following global variables will contain pointers to certain immutable
3329 * Python objects that Python/C API programmers expect.
3330 *
3331 * In order to make these global variables from the Python library available
3332 * to D, I tried the extern workaround documented at:
3333 *   http://www.digitalmars.com/d/archives/digitalmars/D/15427.html
3334 * but it didn't work (Python crashed when attempting to manipulate the
3335 * pointers).
3336 * Besides, in some cases, canonical use of the Python/C API *requires* macros.
3337 * I ultimately resorted to traversing the Python module structure and loading
3338 * pointers to the required objects manually (see
3339 * python_support.d/_loadPythonSupport). */
3340
3341/* Singletons: */
3342/*const*/ PyObject *Py_None;
3343/*const*/ PyObject *Py_NotImplemented;
3344/*const*/ PyObject *Py_Ellipsis;
3345/*const*/ PyObject *Py_True;
3346/*const*/ PyObject *Py_False;
3347
3348/* Types: */
3349/*const*/ PyTypeObject *PyType_Type_p;
3350/*const*/ PyTypeObject *PyBaseObject_Type_p;
3351/*const*/ PyTypeObject *PySuper_Type_p;
3352
3353/*const*/ PyTypeObject *PyNone_Type_p;
3354
3355/*const*/ PyTypeObject *PyUnicode_Type_p;
3356/*const*/ PyTypeObject *PyInt_Type_p;
3357/*const*/ PyTypeObject *PyBool_Type_p;
3358/*const*/ PyTypeObject *PyLong_Type_p;
3359/*const*/ PyTypeObject *PyFloat_Type_p;
3360/*const*/ PyTypeObject *PyComplex_Type_p;
3361/*const*/ PyTypeObject *PyRange_Type_p;
3362/*const*/ PyTypeObject *PyBaseString_Type_p;
3363/*const*/ PyTypeObject *PyString_Type_p;
3364/*const*/ PyTypeObject *PyBuffer_Type_p;
3365/*const*/ PyTypeObject *PyTuple_Type_p;
3366/*const*/ PyTypeObject *PyList_Type_p;
3367/*const*/ PyTypeObject *PyDict_Type_p;
3368/*const*/ PyTypeObject *PyEnum_Type_p;
3369/*const*/ PyTypeObject *PyReversed_Type_p;
3370/*const*/ PyTypeObject *PyCFunction_Type_p;
3371/*const*/ PyTypeObject *PyModule_Type_p;
3372/*const*/ PyTypeObject *PyFunction_Type_p;
3373/*const*/ PyTypeObject *PyClassMethod_Type_p;
3374/*const*/ PyTypeObject *PyStaticMethod_Type_p;
3375/*const*/ PyTypeObject *PyClass_Type_p;
3376/*const*/ PyTypeObject *PyInstance_Type_p;
3377/*const*/ PyTypeObject *PyMethod_Type_p;
3378/*const*/ PyTypeObject *PyFile_Type_p;
3379/*const*/ PyTypeObject *PyCode_Type_p;
3380/*const*/ PyTypeObject *PyFrame_Type_p;
3381/*const*/ PyTypeObject *PyGen_Type_p;
3382/*const*/ PyTypeObject *PySet_Type_p;
3383/*const*/ PyTypeObject *PyFrozenSet_Type_p;
3384
3385/* YYY: Python's default encoding can actually be changed during program
3386 * with sys.setdefaultencoding, so perhaps it would be better not to expose
3387 * this at all: */
3388/*const*/ char *Py_FileSystemDefaultEncoding;
3389
3390/*const*/ PyTypeObject *PyCObject_Type_p;
3391/*const*/ PyTypeObject *PyTraceBack_Type_p;
3392/*const*/ PyTypeObject *PySlice_Type_p;
3393/*const*/ PyTypeObject *PyCell_Type_p;
3394/*const*/ PyTypeObject *PySeqIter_Type_p;
3395/*const*/ PyTypeObject *PyCallIter_Type_p;
3396/* PyWrapperDescr_Type_p omitted. */
3397/*const*/ PyTypeObject *PyProperty_Type_p;
3398
3399/*const*/ PyTypeObject *_PyWeakref_RefType_p;
3400/*const*/ PyTypeObject *_PyWeakref_ProxyType_p;
3401/*const*/ PyTypeObject *_PyWeakref_CallableProxyType_p;
3402
3403/* Exceptions: */
3404/*const*/ PyObject *PyExc_Exception;
3405/*const*/ PyObject *PyExc_StopIteration;
3406/*const*/ PyObject *PyExc_StandardError;
3407/*const*/ PyObject *PyExc_ArithmeticError;
3408/*const*/ PyObject *PyExc_LookupError;
3409
3410/*const*/ PyObject *PyExc_AssertionError;
3411/*const*/ PyObject *PyExc_AttributeError;
3412/*const*/ PyObject *PyExc_EOFError;
3413/*const*/ PyObject *PyExc_FloatingPointError;
3414/*const*/ PyObject *PyExc_EnvironmentError;
3415/*const*/ PyObject *PyExc_IOError;
3416/*const*/ PyObject *PyExc_OSError;
3417/*const*/ PyObject *PyExc_ImportError;
3418/*const*/ PyObject *PyExc_IndexError;
3419/*const*/ PyObject *PyExc_KeyError;
3420/*const*/ PyObject *PyExc_KeyboardInterrupt;
3421/*const*/ PyObject *PyExc_MemoryError;
3422/*const*/ PyObject *PyExc_NameError;
3423/*const*/ PyObject *PyExc_OverflowError;
3424/*const*/ PyObject *PyExc_RuntimeError;
3425/*const*/ PyObject *PyExc_NotImplementedError;
3426/*const*/ PyObject *PyExc_SyntaxError;
3427/*const*/ PyObject *PyExc_IndentationError;
3428/*const*/ PyObject *PyExc_TabError;
3429/*const*/ PyObject *PyExc_ReferenceError;
3430/*const*/ PyObject *PyExc_SystemError;
3431/*const*/ PyObject *PyExc_SystemExit;
3432/*const*/ PyObject *PyExc_TypeError;
3433/*const*/ PyObject *PyExc_UnboundLocalError;
3434/*const*/ PyObject *PyExc_UnicodeError;
3435/*const*/ PyObject *PyExc_UnicodeEncodeError;
3436/*const*/ PyObject *PyExc_UnicodeDecodeError;
3437/*const*/ PyObject *PyExc_UnicodeTranslateError;
3438/*const*/ PyObject *PyExc_ValueError;
3439/*const*/ PyObject *PyExc_ZeroDivisionError;
3440version (Windows) {
3441  /*const*/ PyObject *PyExc_WindowsError;
3442}
3443/* PyExc_MemoryErrorInst omitted. */
3444
3445/*const*/ PyObject *PyExc_Warning;
3446/*const*/ PyObject *PyExc_UserWarning;
3447/*const*/ PyObject *PyExc_DeprecationWarning;
3448/*const*/ PyObject *PyExc_PendingDeprecationWarning;
3449/*const*/ PyObject *PyExc_SyntaxWarning;
3450/* PyExc_OverflowWarning omitted, because it'll go away in Python 2.5. */
3451/*const*/ PyObject *PyExc_RuntimeWarning;
3452/*const*/ PyObject *PyExc_FutureWarning;
3453
3454
3455private PyObject *eval(char* code) {
3456  PyObject *pyGlobals = PyEval_GetGlobals(); /* borrowed ref */
3457  PyObject *res = PyRun_String(code, Py_eval_input,
3458      pyGlobals, pyGlobals
3459    ); /* New ref, or NULL on error. */
3460  if (res == null) {
3461    throw new Exception("XXX: write error message; make PythonException D class");
3462  }
3463  return res;
3464}
3465
3466
3467void _loadPythonSupport() {
3468//static this() {
3469printf("[_loadPythonSupport started (Py_None is null: %d)]\n", Py_None is null);
3470
3471  PyObject *sys_modules = PyImport_GetModuleDict();
3472
3473  PyObject *builtins = PyDict_GetItemString(sys_modules, "__builtin__");
3474  assert (builtins != null);
3475  PyObject *types = PyDict_GetItemString(sys_modules, "types");
3476  assert (types != null);
3477
3478  PyObject *weakref = PyImport_ImportModule("weakref");
3479  assert (weakref != null);
3480
3481  /* Since Python never unloads an extension module once it has been loaded,
3482   * we make no attempt to release these references. */
3483
3484  /* Singletons: */
3485  Py_None = PyObject_GetAttrString(builtins, "None");
3486  Py_NotImplemented = PyObject_GetAttrString(builtins, "NotImplemented");
3487  Py_Ellipsis = PyObject_GetAttrString(builtins, "Ellipsis");
3488  Py_True = PyObject_GetAttrString(builtins, "True");
3489  Py_False = PyObject_GetAttrString(builtins, "False");
3490
3491  /* Types: */
3492  PyType_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "type");
3493  PyBaseObject_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "object");
3494  PySuper_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "super");
3495
3496  PyNone_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "NoneType");
3497
3498  PyUnicode_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "unicode");
3499  PyInt_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "int");
3500  PyBool_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "bool");
3501  PyLong_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "long");
3502  PyFloat_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "float");
3503  PyComplex_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "complex");
3504  PyRange_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "xrange");
3505  PyBaseString_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "basestring");
3506  PyString_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "str");
3507  PyBuffer_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "buffer");
3508  PyTuple_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "tuple");
3509  PyList_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "list");
3510  PyDict_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "dict");
3511  PyEnum_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "enumerate");
3512  PyReversed_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "reversed");
3513
3514  PyCFunction_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "BuiltinFunctionType");
3515  PyModule_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "ModuleType");
3516  PyFunction_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "FunctionType");
3517
3518  PyClassMethod_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "classmethod");
3519  PyStaticMethod_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "staticmethod");
3520
3521  PyClass_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "ClassType");
3522  PyInstance_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "InstanceType");
3523  PyMethod_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "MethodType");
3524
3525  PyFile_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "file");
3526
3527/* FIXME: Phobos?
3528  Py_FileSystemDefaultEncoding = PyUnicode_GetDefaultEncoding();
3529*/
3530
3531  /* Python's "CObject" type is intended to serve as an opaque handle for
3532   * passing a C void pointer from C code to Python code and back. */
3533  PyObject *aCObject = PyCObject_FromVoidPtr(null, null);
3534  PyCObject_Type_p = cast(PyTypeObject *) PyObject_Type(aCObject);
3535  Py_DECREF(aCObject);
3536
3537  PyTraceBack_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "TracebackType");
3538  PySlice_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "SliceType");
3539
3540  /* PyCell_Type_p: */
3541  PyObject *cell = PyCell_New(null);
3542  assert (cell != null);
3543  PyCell_Type_p = cast(PyTypeObject *) PyObject_Type(cell);
3544  assert (PyCell_Type_p != null);
3545  Py_DECREF(cell);
3546
3547/+ FIXME
3548  PySeqIter_Type_p = cast(PyTypeObject *) eval("type(iter(''))");
3549  PyCallIter_Type_p = cast(PyTypeObject *) eval("type(iter(lambda: None, None))");
3550+/
3551  /* PyWrapperDescr_Type_p omitted. */
3552  PyProperty_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "property");
3553
3554  _PyWeakref_RefType_p = cast(PyTypeObject *) PyObject_GetAttrString(weakref, "ReferenceType");
3555  assert (_PyWeakref_RefType_p != null);
3556  _PyWeakref_ProxyType_p = cast(PyTypeObject *) PyObject_GetAttrString(weakref, "ProxyType");
3557  assert (_PyWeakref_ProxyType_p != null);
3558  _PyWeakref_CallableProxyType_p = cast(PyTypeObject *) PyObject_GetAttrString(weakref, "CallableProxyType");
3559  assert (_PyWeakref_CallableProxyType_p != null);
3560
3561  PyCode_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "CodeType");
3562  PyFrame_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "FrameType");
3563  PyGen_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(types, "GeneratorType");
3564
3565  PySet_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "set");
3566  PyFrozenSet_Type_p = cast(PyTypeObject *) PyObject_GetAttrString(builtins, "frozenset");
3567
3568  /* Exceptions: */
3569  PyExc_ArithmeticError = PyObject_GetAttrString(builtins, "ArithmeticError");
3570  PyExc_AssertionError = PyObject_GetAttrString(builtins, "AssertionError");
3571  PyExc_AttributeError = PyObject_GetAttrString(builtins, "AttributeError");
3572  PyExc_DeprecationWarning = PyObject_GetAttrString(builtins, "DeprecationWarning");
3573  PyExc_EOFError = PyObject_GetAttrString(builtins, "EOFError");
3574  PyExc_EnvironmentError = PyObject_GetAttrString(builtins, "EnvironmentError");
3575  PyExc_Exception = PyObject_GetAttrString(builtins, "Exception");
3576  PyExc_FloatingPointError = PyObject_GetAttrString(builtins, "FloatingPointError");
3577  PyExc_FutureWarning = PyObject_GetAttrString(builtins, "FutureWarning");
3578  PyExc_IOError = PyObject_GetAttrString(builtins, "IOError");
3579  PyExc_ImportError = PyObject_GetAttrString(builtins, "ImportError");
3580  PyExc_IndentationError = PyObject_GetAttrString(builtins, "IndentationError");
3581  PyExc_IndexError = PyObject_GetAttrString(builtins, "IndexError");
3582  PyExc_KeyError = PyObject_GetAttrString(builtins, "KeyError");
3583  PyExc_KeyboardInterrupt = PyObject_GetAttrString(builtins, "KeyboardInterrupt");
3584  PyExc_LookupError = PyObject_GetAttrString(builtins, "LookupError");
3585  PyExc_MemoryError = PyObject_GetAttrString(builtins, "MemoryError");
3586  /* PyExc_MemoryErrorInst omitted. */
3587  PyExc_NameError = PyObject_GetAttrString(builtins, "NameError");
3588  PyExc_NotImplementedError = PyObject_GetAttrString(builtins, "NotImplementedError");
3589  PyExc_OSError = PyObject_GetAttrString(builtins, "OSError");
3590  PyExc_OverflowError = PyObject_GetAttrString(builtins, "OverflowError");
3591  PyExc_PendingDeprecationWarning = PyObject_GetAttrString(builtins, "PendingDeprecationWarning");
3592  PyExc_ReferenceError = PyObject_GetAttrString(builtins, "ReferenceError");
3593  PyExc_RuntimeError = PyObject_GetAttrString(builtins, "RuntimeError");
3594  PyExc_RuntimeWarning = PyObject_GetAttrString(builtins, "RuntimeWarning");
3595  PyExc_StandardError = PyObject_GetAttrString(builtins, "StandardError");
3596  PyExc_StopIteration = PyObject_GetAttrString(builtins, "StopIteration");
3597  PyExc_SyntaxError = PyObject_GetAttrString(builtins, "SyntaxError");
3598  PyExc_SyntaxWarning = PyObject_GetAttrString(builtins, "SyntaxWarning");
3599  PyExc_SystemError = PyObject_GetAttrString(builtins, "SystemError");
3600  PyExc_SystemExit = PyObject_GetAttrString(builtins, "SystemExit");
3601  PyExc_TabError = PyObject_GetAttrString(builtins, "TabError");
3602  PyExc_TypeError = PyObject_GetAttrString(builtins, "TypeError");
3603  PyExc_UnboundLocalError = PyObject_GetAttrString(builtins, "UnboundLocalError");
3604  PyExc_UnicodeDecodeError = PyObject_GetAttrString(builtins, "UnicodeDecodeError");
3605  PyExc_UnicodeEncodeError = PyObject_GetAttrString(builtins, "UnicodeEncodeError");
3606  PyExc_UnicodeError = PyObject_GetAttrString(builtins, "UnicodeError");
3607  PyExc_UnicodeTranslateError = PyObject_GetAttrString(builtins, "UnicodeTranslateError");
3608  PyExc_UserWarning = PyObject_GetAttrString(builtins, "UserWarning");
3609  PyExc_ValueError = PyObject_GetAttrString(builtins, "ValueError");
3610  PyExc_Warning = PyObject_GetAttrString(builtins, "Warning");
3611
3612  version (Windows) {
3613    PyExc_WindowsError = PyObject_GetAttrString(builtins, "WindowsError");
3614    assert (PyExc_WindowsError != null);
3615  }
3616
3617  PyExc_ZeroDivisionError = PyObject_GetAttrString(builtins, "ZeroDivisionError");
3618
3619
3620// XXX:debugging code to make sure the right obj ref is loaded in:
3621PyObject *noneStr = PyObject_Str(Py_None);
3622assert (noneStr !is null);
3623printf("[_loadPythonSupport finished (str(Py_None) is '%s')]\n", PyString_AsString(noneStr));
3624Py_DECREF(noneStr);
3625}
Note: See TracBrowser for help on using the browser.