root/trunk/infrastructure/python/python.d

Revision 121, 139.3 kB (checked in by KirkMcDonald, 1 year ago)

Since the D 2.0 series has been found lacking at the moment, Pyd once again compiles under the 1.0 series. (Specifically 1.016 or later; tested with 1.020.)

Line 
1 /* DSR:2005.10.26.16.28:
2 // Updated to Python 2.5 by Kirk McDonald
3
4 XXX:
5
6 - In a build process controlled by Python distutils, need to detect whether the
7   Python interpreter was built in debug build mode, and if so, make the
8   appropriate adjustments to the header mixins.
9
10 */
11
12 module python;
13
14 version (build) {
15     version (DigitalMars) {
16         version (Windows) {
17             version (Python_2_5_Or_Later) {
18                 pragma(link, "python25_digitalmars");
19             } else {
20                 pragma(link, "python24_digitalmars");
21             }
22         }
23     } else {
24         version (Python_2_5_Or_Later) {
25             pragma(link, "python2.5");
26         } else {
27             pragma(link, "python2.4");
28         }
29     }
30 }
31
32 version (Tango) {
33     import tango.stdc.stdio;
34     import tango.stdc.time;
35     import tango.stdc.string;
36 } else {
37     import std.c.stdio;
38     import std.c.time;
39     import std.c.string;
40 }
41
42 /* D long is always 64 bits, but when the Python/C API mentions long, it is of
43  * course referring to the C type long, the size of which is 32 bits on both
44  * X86 and X86_64 under Windows, but 32 bits on X86 and 64 bits on X86_64 under
45  * most other operating systems. */
46
47 alias long C_longlong;
48 alias ulong C_ulonglong;
49
50 version(Windows) {
51   alias int C_long;
52   alias uint C_ulong;
53 } else {
54   version (X86) {
55     alias int C_long;
56     alias uint C_ulong;
57   } else {
58     alias long C_long;
59     alias ulong C_ulong;
60   }
61 }
62
63 version (D_Version2) {
64     // Need to define const(char)* in a way which is syntactically valid in a
65     // D 1.0 compiler.
66     mixin("alias const(char)* c_str;");
67 } else {
68     alias char* c_str;
69 }
70
71 /*
72  * Py_ssize_t is defined as a signed type which is 8 bytes on X86_64 and 4
73  * bytes on X86.
74  */
75 version (Python_2_5_Or_Later) {
76     version (X86_64) {
77         alias long Py_ssize_t;
78     } else {
79         alias int Py_ssize_t;
80     }
81 } else {
82     alias C_long Py_ssize_t;
83 }
84
85 extern (C) {
86 ///////////////////////////////////////////////////////////////////////////////
87 // PYTHON DATA STRUCTURES AND ALIASES
88 ///////////////////////////////////////////////////////////////////////////////
89   // Python-header-file: Include/Python.h:
90   const int Py_single_input = 256;
91   const int Py_file_input = 257;
92   const int Py_eval_input = 258;
93
94   // Python-header-file: Include/object.h:
95
96   // XXX:Conditionalize in if running debug build of Python interpreter:
97   /*
98   version (Python_Debug_Build) {
99     template _PyObject_HEAD_EXTRA() {
100       PyObject *_ob_next;
101       PyObject *_ob_prev;
102     }
103   } else {
104   */
105     template _PyObject_HEAD_EXTRA() {}
106   /*}*/
107
108   template PyObject_HEAD() {
109     mixin _PyObject_HEAD_EXTRA;
110     Py_ssize_t ob_refcnt;
111     PyTypeObject *ob_type;
112   }
113
114   struct PyObject {
115     mixin PyObject_HEAD;
116   }
117
118   template PyObject_VAR_HEAD() {
119     mixin PyObject_HEAD;
120     Py_ssize_t ob_size; /* Number of items in variable part */
121   }
122
123   struct PyVarObject {
124     mixin PyObject_VAR_HEAD;
125   }
126
127   alias PyObject * (*unaryfunc)(PyObject *);
128   alias PyObject * (*binaryfunc)(PyObject *, PyObject *);
129   alias PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
130   alias int (*inquiry)(PyObject *);
131   alias Py_ssize_t (*lenfunc)(PyObject *);
132   alias int (*coercion)(PyObject **, PyObject **);
133   alias PyObject *(*intargfunc)(PyObject *, int);
134   alias PyObject *(*intintargfunc)(PyObject *, int, int);
135   alias PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
136   alias PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
137   alias int(*intobjargproc)(PyObject *, int, PyObject *);
138   alias int(*intintobjargproc)(PyObject *, int, int, PyObject *);
139   alias int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
140   alias int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
141   alias int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
142
143   // int-based buffer interface
144   alias int (*getreadbufferproc)(PyObject *, int, void **);
145   alias int (*getwritebufferproc)(PyObject *, int, void **);
146   alias int (*getsegcountproc)(PyObject *, int *);
147   alias int (*getcharbufferproc)(PyObject *, int, c_str*);
148   // ssize_t-based buffer interface
149   alias Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
150   alias Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
151   alias Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
152   alias Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, c_str*);
153
154   alias int (*objobjproc)(PyObject *, PyObject *);
155   alias int (*visitproc)(PyObject *, void *);
156   alias int (*traverseproc)(PyObject *, visitproc, void *);
157
158   // Python-header-file: Include/object.h:
159   struct PyNumberMethods {
160     binaryfunc nb_add;
161     binaryfunc nb_subtract;
162     binaryfunc nb_multiply;
163     binaryfunc nb_divide;
164     binaryfunc nb_remainder;
165     binaryfunc nb_divmod;
166     ternaryfunc nb_power;
167     unaryfunc nb_negative;
168     unaryfunc nb_positive;
169     unaryfunc nb_absolute;
170     inquiry nb_nonzero;
171     unaryfunc nb_invert;
172     binaryfunc nb_lshift;
173     binaryfunc nb_rshift;
174     binaryfunc nb_and;
175     binaryfunc nb_xor;
176     binaryfunc nb_or;
177     coercion nb_coerce;
178     unaryfunc nb_int;
179     unaryfunc nb_long;
180     unaryfunc nb_float;
181     unaryfunc nb_oct;
182     unaryfunc nb_hex;
183
184     binaryfunc nb_inplace_add;
185     binaryfunc nb_inplace_subtract;
186     binaryfunc nb_inplace_multiply;
187     binaryfunc nb_inplace_divide;
188     binaryfunc nb_inplace_remainder;
189     ternaryfunc nb_inplace_power;
190     binaryfunc nb_inplace_lshift;
191     binaryfunc nb_inplace_rshift;
192     binaryfunc nb_inplace_and;
193     binaryfunc nb_inplace_xor;
194     binaryfunc nb_inplace_or;
195
196     binaryfunc nb_floor_divide;
197     binaryfunc nb_true_divide;
198     binaryfunc nb_inplace_floor_divide;
199     binaryfunc nb_inplace_true_divide;
200
201     version (Python_2_5_Or_Later) {
202         unaryfunc nb_index;
203     }
204   }
205
206   struct PySequenceMethods {
207     lenfunc sq_length;
208     binaryfunc sq_concat;
209     ssizeargfunc sq_repeat;
210     ssizeargfunc sq_item;
211     ssizessizeargfunc sq_slice;
212     ssizeobjargproc sq_ass_item;
213     ssizessizeobjargproc sq_ass_slice;
214     objobjproc sq_contains;
215     binaryfunc sq_inplace_concat;
216     ssizeargfunc sq_inplace_repeat;
217   }
218
219   struct PyMappingMethods {
220     lenfunc mp_length;
221     binaryfunc mp_subscript;
222     objobjargproc mp_ass_subscript;
223   }
224
225   struct PyBufferProcs {
226     readbufferproc bf_getreadbuffer;
227     writebufferproc bf_getwritebuffer;
228     segcountproc bf_getsegcount;
229     charbufferproc bf_getcharbuffer;
230   }
231
232
233   alias void (*freefunc)(void *);
234   alias void (*destructor)(PyObject *);
235   alias int (*printfunc)(PyObject *, FILE *, int);
236   alias PyObject *(*getattrfunc)(PyObject *, c_str);
237   alias PyObject *(*getattrofunc)(PyObject *, PyObject *);
238   alias int (*setattrfunc)(PyObject *, c_str, PyObject *);
239   alias int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
240   alias int (*cmpfunc)(PyObject *, PyObject *);
241   alias PyObject *(*reprfunc)(PyObject *);
242   alias C_long (*hashfunc)(PyObject *);
243   alias PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
244   alias PyObject *(*getiterfunc) (PyObject *);
245   alias PyObject *(*iternextfunc) (PyObject *);
246   alias PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
247   alias int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
248   alias int (*initproc)(PyObject *, PyObject *, PyObject *);
249   alias PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
250   alias PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
251
252   struct PyTypeObject {
253     mixin PyObject_VAR_HEAD;
254
255     c_str tp_name;
256     Py_ssize_t tp_basicsize, tp_itemsize;
257
258     destructor tp_dealloc;
259     printfunc tp_print;
260     getattrfunc tp_getattr;
261     setattrfunc tp_setattr;
262     cmpfunc tp_compare;
263     reprfunc tp_repr;
264
265     PyNumberMethods *tp_as_number;
266     PySequenceMethods *tp_as_sequence;
267     PyMappingMethods *tp_as_mapping;
268
269     hashfunc tp_hash;
270     ternaryfunc tp_call;
271     reprfunc tp_str;
272     getattrofunc tp_getattro;
273     setattrofunc tp_setattro;
274
275     PyBufferProcs *tp_as_buffer;
276
277     C_long tp_flags;
278
279     c_str tp_doc;
280
281     traverseproc tp_traverse;
282
283     inquiry tp_clear;
284
285     richcmpfunc tp_richcompare;
286
287     Py_ssize_t tp_weaklistoffset;
288
289     getiterfunc tp_iter;
290     iternextfunc tp_iternext;
291
292     PyMethodDef *tp_methods;
293     PyMemberDef *tp_members;
294     PyGetSetDef *tp_getset;
295     PyTypeObject *tp_base;
296     PyObject *tp_dict;
297     descrgetfunc tp_descr_get;
298     descrsetfunc tp_descr_set;
299     Py_ssize_t tp_dictoffset;
300     initproc tp_init;
301     allocfunc tp_alloc;
302     newfunc tp_new;
303     freefunc tp_free;
304     inquiry tp_is_gc;
305     PyObject *tp_bases;
306     PyObject *tp_mro;
307     PyObject *tp_cache;
308     PyObject *tp_subclasses;
309     PyObject *tp_weaklist;
310     destructor tp_del;
311   }
312
313   //alias _typeobject PyTypeObject;
314
315   struct _heaptypeobject {
316     // Some of these names changed between 2.4 and 2.5; not a serious issue.
317     PyTypeObject ht_type;
318     PyNumberMethods as_number;
319     PyMappingMethods as_mapping;
320     PySequenceMethods as_sequence;
321     PyBufferProcs as_buffer;
322     PyObject *ht_name;
323     PyObject *ht_slots;
324   }
325   alias _heaptypeobject PyHeapTypeObject;
326
327
328   // Python-header-file: Include/pymem.h:
329   void * PyMem_Malloc(size_t);
330   void * PyMem_Realloc(void *, size_t);
331   void PyMem_Free(void *);
332
333
334 ///////////////////////////////////////////////////////////////////////////////
335 // GENERIC TYPE CHECKING
336 ///////////////////////////////////////////////////////////////////////////////
337
338   int PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
339
340   // D translation of C macro:
341   int PyObject_TypeCheck(PyObject *ob, PyTypeObject *tp) {
342     return (ob.ob_type == tp || PyType_IsSubtype(ob.ob_type, tp));
343   }
344
345   /* Note that this Python support module makes pointers to PyType_Type and
346    * other global variables exposed by the Python API available to D
347    * programmers indirectly (see this module's static initializer). */
348
349   // D translation of C macro:
350   int PyType_Check(PyObject *op) {
351     return PyObject_TypeCheck(op, PyType_Type_p);
352   }
353   // D translation of C macro:
354   int PyType_CheckExact(PyObject *op) {
355     return op.ob_type == PyType_Type_p;
356   }
357
358   int PyType_Ready(PyTypeObject *);
359   PyObject * PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
360   PyObject * PyType_GenericNew(PyTypeObject *, PyObject *, PyObject *);
361
362
363   int PyObject_Print(PyObject *, FILE *, int);
364   PyObject * PyObject_Repr(PyObject *);
365   PyObject * PyObject_Str(PyObject *);
366
367   PyObject * PyObject_Unicode(PyObject *);
368
369   int PyObject_Compare(PyObject *, PyObject *);
370   PyObject * PyObject_RichCompare(PyObject *, PyObject *, int);
371   int PyObject_RichCompareBool(PyObject *, PyObject *, int);
372   PyObject * PyObject_GetAttrString(PyObject *, c_str);
373   int PyObject_SetAttrString(PyObject *, c_str, PyObject *);
374   int PyObject_HasAttrString(PyObject *, c_str);
375   PyObject * PyObject_GetAttr(PyObject *, PyObject *);
376   int PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
377   int PyObject_HasAttr(PyObject *, PyObject *);
378   PyObject * PyObject_SelfIter(PyObject *);
379   PyObject * PyObject_GenericGetAttr(PyObject *, PyObject *);
380   int PyObject_GenericSetAttr(PyObject *,
381                 PyObject *, PyObject *);
382   C_long PyObject_Hash(PyObject *);
383   int PyObject_IsTrue(PyObject *);
384   int PyObject_Not(PyObject *);
385   //int PyCallable_Check(PyObject *);
386   int PyNumber_Coerce(PyObject **, PyObject **);
387   int PyNumber_CoerceEx(PyObject **, PyObject **);
388
389   void PyObject_ClearWeakRefs(PyObject *);
390
391   PyObject * PyObject_Dir(PyObject *);
392
393   int Py_ReprEnter(PyObject *);
394   void Py_ReprLeave(PyObject *);
395
396   const int Py_PRINT_RAW = 1;
397
398
399   const int Py_TPFLAGS_HAVE_GETCHARBUFFER       = 1L<<0;
400   const int Py_TPFLAGS_HAVE_SEQUENCE_IN         = 1L<<1;
401   const int Py_TPFLAGS_GC                       = 0;
402   const int Py_TPFLAGS_HAVE_INPLACEOPS          = 1L<<3;
403   const int Py_TPFLAGS_CHECKTYPES               = 1L<<4;
404   const int Py_TPFLAGS_HAVE_RICHCOMPARE         = 1L<<5;
405   const int Py_TPFLAGS_HAVE_WEAKREFS            = 1L<<6;
406   const int Py_TPFLAGS_HAVE_ITER                = 1L<<7;
407   const int Py_TPFLAGS_HAVE_CLASS               = 1L<<8;
408   const int Py_TPFLAGS_HEAPTYPE                 = 1L<<9;
409   const int Py_TPFLAGS_BASETYPE                 = 1L<<10;
410   const int Py_TPFLAGS_READY                    = 1L<<12;
411   const int Py_TPFLAGS_READYING                 = 1L<<13;
412   const int Py_TPFLAGS_HAVE_GC                  = 1L<<14;
413
414   // YYY: Should conditionalize for stackless:
415   //#ifdef STACKLESS
416   //#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
417   //#else
418   const int Py_TPFLAGS_HAVE_STACKLESS_EXTENSION = 0;
419   //#endif
420   version (Python_2_5_Or_Later) {
421       const int Py_TPFLAGS_HAVE_INDEX = 1L<<17;
422   } else {
423       const int Py_TPFLAGS_HAVE_INDEX = 0;
424   }
425
426   const int Py_TPFLAGS_DEFAULT =
427       Py_TPFLAGS_HAVE_GETCHARBUFFER |
428       Py_TPFLAGS_HAVE_SEQUENCE_IN |
429       Py_TPFLAGS_HAVE_INPLACEOPS |
430       Py_TPFLAGS_HAVE_RICHCOMPARE |
431       Py_TPFLAGS_HAVE_WEAKREFS |
432       Py_TPFLAGS_HAVE_ITER |
433       Py_TPFLAGS_HAVE_CLASS |
434       Py_TPFLAGS_HAVE_STACKLESS_EXTENSION |
435       Py_TPFLAGS_HAVE_INDEX |
436       0
437     ;
438
439   // D translation of C macro:
440   int PyType_HasFeature(PyTypeObject *t, int f) {
441     return (t.tp_flags & f) != 0;
442   }
443
444
445 ///////////////////////////////////////////////////////////////////////////////
446 // REFERENCE COUNTING
447 ///////////////////////////////////////////////////////////////////////////////
448   // Python-header-file: Include/object.h:
449
450   void Py_INCREF(PyObject *op) {
451     ++op.ob_refcnt;
452   }
453
454   void Py_XINCREF(PyObject *op) {
455     if (op == null) {
456       return;
457     }
458     Py_INCREF(op);
459   }
460
461   void Py_DECREF(PyObject *op) {
462     --op.ob_refcnt;
463     assert (op.ob_refcnt >= 0);
464     if (op.ob_refcnt == 0) {
465       op.ob_type.tp_dealloc(op);
466     }
467   }
468
469   void Py_XDECREF(PyObject* op)
470   {
471     if(op == null) {
472       return;
473     }
474
475     Py_DECREF(op);
476   }
477
478   void Py_IncRef(PyObject *);
479   void Py_DecRef(PyObject *);
480
481   /* Rich comparison opcodes */
482   const int Py_LT = 0;
483   const int Py_LE = 1;
484   const int Py_EQ = 2;
485   const int Py_NE = 3;
486   const int Py_GT = 4;
487   const int Py_GE = 5;
488
489
490 ///////////////////////////////////////////////////////////////////////////////////////////////
491 // UNICODE
492 ///////////////////////////////////////////////////////////////////////////////////////////////
493   // Python-header-file: Include/unicodeobject.h:
494   /* The Python header explains:
495    *   Unicode API names are mangled to assure that UCS-2 and UCS-4 builds
496    *   produce different external names and thus cause import errors in
497    *   case Python interpreters and extensions with mixed compiled in
498    *   Unicode width assumptions are combined. */
499
500   version (Python_Unicode_UCS2) {
501     version (Windows) {
502       alias wchar Py_UNICODE;
503     } else {
504       alias ushort Py_UNICODE;
505     }
506   } else {
507     alias uint Py_UNICODE;
508   }
509
510   struct PyUnicodeObject {
511     mixin PyObject_HEAD;
512
513     Py_ssize_t length;
514     Py_UNICODE *str;
515     C_long hash;
516     PyObject *defenc;
517   }
518
519   // &PyUnicode_Type is accessible via PyUnicode_Type_p.
520   // D translations of C macros:
521   int PyUnicode_Check(PyObject *op) {
522     return PyObject_TypeCheck(op, PyUnicode_Type_p);
523   }
524   int PyUnicode_CheckExact(PyObject *op) {
525     return op.ob_type == PyUnicode_Type_p;
526   }
527
528   int PyUnicode_GET_SIZE(PyUnicodeObject *op) {
529     return op.length;
530   }
531   int PyUnicode_GET_DATA_SIZE(PyUnicodeObject *op) {
532     return op.length * Py_UNICODE.sizeof;
533   }
534   Py_UNICODE *PyUnicode_AS_UNICODE(PyUnicodeObject *op) {
535     return op.str;
536   }
537   char *PyUnicode_AS_DATA(PyUnicodeObject *op) {
538     return cast(char *) op.str;
539   }
540
541   Py_UNICODE Py_UNICODE_REPLACEMENT_CHARACTER = 0xFFFD;
542
543   // YYY: Unfortunately, we have to do it the tedious way since there's no
544   // preprocessor in D:
545   version (Python_Unicode_UCS2) {
546     PyObject *PyUnicodeUCS2_FromUnicode(Py_UNICODE *u, Py_ssize_t size);
547     Py_UNICODE *PyUnicodeUCS2_AsUnicode(PyObject *unicode);
548     Py_ssize_t PyUnicodeUCS2_GetSize(PyObject *unicode);
549     Py_UNICODE PyUnicodeUCS2_GetMax();
550
551     int PyUnicodeUCS2_Resize(PyObject **unicode, Py_ssize_t length);
552     PyObject *PyUnicodeUCS2_FromEncodedObject(PyObject *obj, c_str encoding, c_str errors);
553     PyObject *PyUnicodeUCS2_FromObject(PyObject *obj);
554
555     PyObject *PyUnicodeUCS2_FromWideChar(wchar *w, Py_ssize_t size);
556     Py_ssize_t PyUnicodeUCS2_AsWideChar(PyUnicodeObject *unicode, wchar *w, Py_ssize_t size);
557
558     PyObject *PyUnicodeUCS2_FromOrdinal(int ordinal);
559
560     PyObject *_PyUnicodeUCS2_AsDefaultEncodedString(PyObject *, c_str);
561
562     c_str PyUnicodeUCS2_GetDefaultEncoding();
563     int PyUnicodeUCS2_SetDefaultEncoding(c_str encoding);
564
565     PyObject *PyUnicodeUCS2_Decode(c_str s, Py_ssize_t size, c_str encoding, c_str errors);
566     PyObject *PyUnicodeUCS2_Encode(Py_UNICODE *s, Py_ssize_t size, c_str encoding, c_str errors);
567     PyObject *PyUnicodeUCS2_AsEncodedObject(PyObject *unicode, c_str encoding, c_str errors);
568     PyObject *PyUnicodeUCS2_AsEncodedString(PyObject *unicode, c_str encoding, c_str errors);
569
570     PyObject *PyUnicodeUCS2_DecodeUTF7(c_str s, Py_ssize_t length, c_str errors);
571     PyObject *PyUnicodeUCS2_EncodeUTF7(Py_UNICODE *data, Py_ssize_t length,
572         int encodeSetO, int encodeWhiteSpace, c_str errors
573       );
574
575     PyObject *PyUnicodeUCS2_DecodeUTF8(c_str s, Py_ssize_t length, c_str errors);
576     PyObject *PyUnicodeUCS2_DecodeUTF8Stateful(c_str s, Py_ssize_t length,
577         c_str errors, Py_ssize_t *consumed
578       );
579     PyObject *PyUnicodeUCS2_AsUTF8String(PyObject *unicode);
580     PyObject *PyUnicodeUCS2_EncodeUTF8(Py_UNICODE *data, Py_ssize_t length, c_str errors);
581
582     PyObject *PyUnicodeUCS2_DecodeUTF16(c_str s, Py_ssize_t length, c_str errors, int *byteorder);
583     PyObject *PyUnicodeUCS2_DecodeUTF16Stateful(c_str s, Py_ssize_t length,
584         c_str errors, int *byteorder, Py_ssize_t *consumed
585       );
586     PyObject *PyUnicodeUCS2_AsUTF16String(PyObject *unicode);
587     PyObject *PyUnicodeUCS2_EncodeUTF16(Py_UNICODE *data, Py_ssize_t length,
588         c_str errors, int byteorder
589       );
590
591     PyObject *PyUnicodeUCS2_DecodeUnicodeEscape(c_str s, Py_ssize_t length, c_str errors);
592     PyObject *PyUnicodeUCS2_AsUnicodeEscapeString(PyObject *unicode);
593     PyObject *PyUnicodeUCS2_EncodeUnicodeEscape(Py_UNICODE *data, Py_ssize_t length);
594     PyObject *PyUnicodeUCS2_DecodeRawUnicodeEscape(c_str s, Py_ssize_t length, c_str errors);
595     PyObject *PyUnicodeUCS2_AsRawUnicodeEscapeString(PyObject *unicode);
596     PyObject *PyUnicodeUCS2_EncodeRawUnicodeEscape(Py_UNICODE *data, Py_ssize_t length);
597
598     PyObject *_PyUnicodeUCS2_DecodeUnicodeInternal(c_str s, Py_ssize_t length, c_str errors);
599
600     PyObject *PyUnicodeUCS2_DecodeLatin1(c_str s, Py_ssize_t length, c_str errors);
601     PyObject *PyUnicodeUCS2_AsLatin1String(PyObject *unicode);
602     PyObject *PyUnicodeUCS2_EncodeLatin1(Py_UNICODE *data, Py_ssize_t length, c_str errors);
603
604     PyObject *PyUnicodeUCS2_DecodeASCII(c_str s, Py_ssize_t length, c_str errors);
605     PyObject *PyUnicodeUCS2_AsASCIIString(PyObject *unicode);
606     PyObject *PyUnicodeUCS2_EncodeASCII(Py_UNICODE *data, Py_ssize_t length, c_str errors);
607
608     PyObject *PyUnicodeUCS2_DecodeCharmap(c_str s, Py_ssize_t length,
609         PyObject *mapping, c_str errors
610       );
611     PyObject *PyUnicodeUCS2_AsCharmapString(PyObject *unicode, PyObject *mapping);
612     PyObject *PyUnicodeUCS2_EncodeCharmap(Py_UNICODE *data, Py_ssize_t length,
613         PyObject *mapping, c_str errors
614       );
615     PyObject *PyUnicodeUCS2_TranslateCharmap(Py_UNICODE *data, Py_ssize_t length,
616         PyObject *table, c_str errors
617       );
618
619     version (Windows) {
620       PyObject *PyUnicodeUCS2_DecodeMBCS(c_str s, Py_ssize_t length, c_str errors);
621       PyObject *PyUnicodeUCS2_AsMBCSString(PyObject *unicode);
622       PyObject *PyUnicodeUCS2_EncodeMBCS(Py_UNICODE *data, Py_ssize_t length, c_str errors);
623     }
624
625     int PyUnicodeUCS2_EncodeDecimal(Py_UNICODE *s, Py_ssize_t length, c_str output, c_str errors);
626
627     PyObject *PyUnicodeUCS2_Concat(PyObject *left, PyObject *right);
628     PyObject *PyUnicodeUCS2_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit);
629     PyObject *PyUnicodeUCS2_Splitlines(PyObject *s, int keepends);
630     version (Python_2_5_Or_Later) {
631         PyObject *PyUnicodeUCS2_Partition(PyObject* s, PyObject* sep);
632         PyObject *PyUnicodeUCS2_RPartition(PyObject* s, PyObject* sep);
633     }
634     PyObject *PyUnicodeUCS2_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit);
635     PyObject *PyUnicodeUCS2_Translate(PyObject *str, PyObject *table, c_str errors);
636     PyObject *PyUnicodeUCS2_Join(