File: unicodeobject.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (1628 lines) | stat: -rw-r--r-- 64,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rstring
from rpython.rlib.rarithmetic import widen, r_uint, r_uint32, intmask
from rpython.rlib import rstring, rutf8
from rpython.tool.sourcetools import func_renamer

from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.buffer import BufferInterfaceNotFound
from pypy.interpreter.unicodehelper import (
    wcharpsize2utf8, str_decode_utf_16_helper, str_decode_utf_32_helper,
    unicode_encode_decimal, utf8_encode_utf_16_helper, BYTEORDER,
    utf8_encode_utf_32_helper, str_decode_latin_1, utf8_encode_latin_1,
    utf8_encode_raw_unicode_escape, str_decode_raw_unicode_escape)
from pypy.objspace.std.unicodeobject import unicodedb
from pypy.module.cpyext.api import (
    CANNOT_FAIL, Py_ssize_t, cpython_api,
    bootstrap_function, CONST_STRING, INTP_real, Py_TPFLAGS_UNICODE_SUBCLASS,
    CONST_WSTRING, Py_CLEANUP_SUPPORTED, slot_function, cts, parse_dir,
    PyTypeObjectPtr, PyVarObject, PY_SSIZE_T_MAX)
from pypy.module.cpyext.pyerrors import PyErr_BadArgument, PyErr_BadInternalCall
from pypy.module.cpyext.pyobject import (
    PyObject, PyObjectP, decref, make_ref, from_ref, track_reference,
    make_typedescr, get_typedescr, as_pyobj, pyobj_has_w_obj, BaseCpyTypedescr,
    incref, decref)
from pypy.module.cpyext.bytesobject import PyBytes_Check, PyBytes_FromObject
from pypy.module.cpyext.pyfile import pyos_fspath
from pypy.module._codecs.interp_codecs import (
    CodecState, latin_1_decode, utf_16_decode, utf_32_decode)
from pypy.module.cpyext.state import State
from pypy.objspace.std import unicodeobject
from rpython.rlib.debug import fatalerror
import sys

## See comment in bytesobject.py.

cts.parse_header(parse_dir / 'cpyext_unicodeobject.h')
PyUnicodeObject = cts.gettype('PyUnicodeObject*')
Py_UNICODE = cts.gettype('Py_UNICODE')
Py_UCS4 = cts.gettype('Py_UCS4')
INT_realP = lltype.Ptr(lltype.Array(rffi.INT_real, hints={'nolength': True}))

@bootstrap_function
def init_unicodeobject(space):
    make_typedescr(space.w_unicode.layout.typedef,
                   basestruct=PyUnicodeObject.TO,
                   attach=unicode_attach,
                   alloc=unicode_alloc,
                   dealloc=unicode_dealloc,
                   realize=unicode_realize)

# Buffer for the default encoding (used by PyUnicode_GetDefaultEncoding)
DEFAULT_ENCODING_SIZE = 100
default_encoding = lltype.malloc(rffi.CCHARP.TO, DEFAULT_ENCODING_SIZE,
                                 flavor='raw', zero=True)

WCHAR_KIND = 0
_1BYTE_KIND = 1
_2BYTE_KIND = 2
_4BYTE_KIND = 4

kind_to_name = {
    0: 'WCHAR_KIND',
    1: '_1BYTE_KIND',
    2: '_2BYTE_KIND',
    4: '_4BYTE_KIND',
    }

def pyunicode_check(ref):
    return (widen(ref.c_ob_type.c_tp_flags) & Py_TPFLAGS_UNICODE_SUBCLASS) != 0

# Backward compatibility: in PyPy7.3.4 this function became a C macro. But
# since we do not change the API, we need to export this function from the
# dll/so. This requires giving the mangled name here and special casing it in
# mangle_name from api.py
@cts.decl("int PyPyUnicode_Check(void * obj)", error=CANNOT_FAIL)
def PyUnicode_Check(space, ref):
    if not ref:
        return False
    ref = rffi.cast(PyObject, ref)
    return pyunicode_check(ref)

# Backward compatibility: in PyPy7.3.4 this function became a C macro. But
# since we do not change the API, we need to also export this function from the
# dll/so. This requires giving the mangled name here and special casing it in
# mangle_name from api.py
@cts.decl("int PyPyUnicode_CheckExact(void * obj)", error=CANNOT_FAIL)
def PyUnicode_CheckExact(space, ref):
    if not ref:
        return False
    w_obj = from_ref(space, rffi.cast(PyObject, ref))
    w_obj_type = space.type(w_obj)
    return space.is_w(w_obj_type, space.w_unicode)

def new_empty_unicode(space, length):
    """
    Allocate a PyUnicodeObject and its buffer, but without a corresponding
    interpreter object.  The buffer may be mutated, until unicode_realize() is
    called.  Refcount of the result is 1.
    """
    typedescr = get_typedescr(space.w_unicode.layout.typedef)
    py_obj = typedescr.allocate(space, space.w_unicode, itemcount=length)

    buflen = length + 1
    set_wsize(py_obj, length)
    set_wbuffer(py_obj,
        lltype.malloc(
            rffi.CWCHARP.TO, buflen, flavor='raw', zero=True,
            add_memory_pressure=True))
    return py_obj

def unicode_attach(space, py_obj, w_obj, w_userdata=None):
    "Fills a newly allocated PyUnicodeObject with a unicode string"
    value = space.utf8_w(w_obj)
    length = space.len_w(w_obj)
    set_wsize(py_obj, length)
    set_wbuffer(py_obj, lltype.nullptr(rffi.CWCHARP.TO))
    _readify(space, py_obj, value)

def unicode_realize(space, py_obj):
    """
    Creates the unicode in the interpreter. The PyUnicodeObject buffer must not
    be modified after this call. Can raise in wcharpsize2utf8
    """
    if not get_wbuffer(py_obj):
        data = cts.cast('char *', get_data(py_obj))
        size = get_len(py_obj)
        kind = get_kind(py_obj)
        value = rffi.charpsize2str(data, size * kind)
        state = space.fromcache(CodecState)
        eh = state.decode_error_handler
        w_value = space.newbytes(value)
        if kind == _1BYTE_KIND:
            s_utf8, lgt, _ = str_decode_latin_1(space, value, w_value, 'strict', True, eh)
        elif kind == _2BYTE_KIND:
            decoded = str_decode_utf_16_helper(space, value, w_value, 'strict', True, eh,
                                               byteorder=BYTEORDER)
            s_utf8, lgt = decoded[:2]
        elif kind == _4BYTE_KIND:
            decoded = str_decode_utf_32_helper(space, value, w_value, 'strict',
                                               True, eh,
                                               byteorder=BYTEORDER)
            s_utf8, lgt = decoded[:2]
        else:
            assert False
    else:
        lgt = get_wsize(py_obj)
        s_utf8 = wcharpsize2utf8(space, get_wbuffer(py_obj), lgt)

    w_type = from_ref(space, rffi.cast(PyObject, py_obj.c_ob_type))
    w_obj = space.allocate_instance(unicodeobject.W_UnicodeObject, w_type)
    w_obj.__init__(s_utf8, lgt)
    track_reference(space, py_obj, w_obj)
    return w_obj

def unicode_alloc(typedescr, space, w_type, itemcount):
    if not w_type is space.w_unicode:
        # subclass, will not use compact format, so no need for extra space
        return BaseCpyTypedescr.allocate(typedescr, space, w_type, 1)
    # This could be optimized: PyUnicodeObject is 80 bytes, PyASCIIObject
    # is 48. In any case, leave room for a NULL char. Also override the
    # tp_itemsize since we use the compact form
    return BaseCpyTypedescr.allocate(typedescr, space, w_type, itemcount + 1,
                                     itemsize=1)

@slot_function([PyObject], lltype.Void)
def unicode_dealloc(space, py_obj):
    if has_wbuffer_memory(py_obj):
        lltype.free(get_wbuffer(py_obj), flavor="raw")
    if has_utf8_memory(py_obj):
        lltype.free(get_utf8(py_obj), flavor="raw")
    from pypy.module.cpyext.object import _dealloc
    _dealloc(space, py_obj)

def has_wbuffer_memory(py_obj):
    ptr = get_wbuffer(py_obj)
    if not ptr:
        return False
    elif not get_ready(py_obj):
        return True
    else:
        return cts.cast('void *', ptr) != get_data(py_obj)

def get_compact_ascii(py_obj):
    a = get_ascii(py_obj)
    if not a:
        return False
    return True

def has_utf8_memory(py_obj):
    if get_compact_ascii(py_obj):
        return False
    utf8 = get_utf8(py_obj)
    return bool(utf8) and cts.cast('void *', utf8) != get_data(py_obj)

def get_len(py_obj):
    py_obj = cts.cast('PyASCIIObject*', py_obj)
    return py_obj.c_length

def set_len(py_obj, n):
    py_obj = cts.cast('PyASCIIObject*', py_obj)
    py_obj.c_length = n

def get_state(py_obj):
    py_obj = cts.cast('PyASCIIObject*', py_obj)
    return py_obj.c_state

def get_kind(py_obj):
    return rffi.getintfield(get_state(py_obj), 'c_kind')

def set_kind(py_obj, value):
    get_state(py_obj).c_kind = cts.cast('unsigned char', value)

def get_ascii(py_obj):
    return rffi.getintfield(get_state(py_obj), 'c_ascii')

def set_ascii(py_obj, value):
    get_state(py_obj).c_ascii = cts.cast('unsigned char', value)

def get_ready(py_obj):
    return rffi.getintfield(get_state(py_obj), 'c_ready')

def set_ready(py_obj, value):
    get_state(py_obj).c_ready = cts.cast('unsigned char', value)

def get_wbuffer(py_obj):
    py_obj = cts.cast('PyASCIIObject*', py_obj)
    return py_obj.c_wstr

def set_wbuffer(py_obj, wbuf):
    py_obj = cts.cast('PyASCIIObject*', py_obj)
    py_obj.c_wstr = wbuf

def get_utf8_len(py_obj):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    return py_obj.c_utf8_length

def set_utf8_len(py_obj, n):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    py_obj.c_utf8_length = n

def get_utf8(py_obj):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    return py_obj.c_utf8

def set_utf8(py_obj, buf):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    py_obj.c_utf8 = buf

def get_wsize(py_obj):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    return py_obj.c_wstr_length

def set_wsize(py_obj, value):
    py_obj = cts.cast('PyCompactUnicodeObject*', py_obj)
    py_obj.c_wstr_length = value

def get_data(py_obj):
    if get_compact(py_obj):
        if get_ascii(py_obj):
            PyASCIIObject = cts.gettype('PyASCIIObject')
            struct_size = rffi.sizeof(PyASCIIObject)
        else:
            PyCompactUnicodeObject = cts.gettype('PyCompactUnicodeObject')
            struct_size = rffi.sizeof(PyCompactUnicodeObject)
        data = rffi.ptradd(rffi.cast(rffi.CCHARP, py_obj), struct_size)
        return cts.cast('void *', data)
    py_obj = cts.cast('PyUnicodeObject*', py_obj)
    return py_obj.c_data

def set_data_compact(py_obj, p_data, size):
    if get_ascii(py_obj):
        PyASCIIObject = cts.gettype('PyASCIIObject')
        struct_size = rffi.sizeof(PyASCIIObject)
    else:
        PyCompactUnicodeObject = cts.gettype('PyCompactUnicodeObject')
        struct_size = rffi.sizeof(PyCompactUnicodeObject)
    data = rffi.ptradd(rffi.cast(rffi.CCHARP, py_obj), struct_size)
    for i in range(size):
        data[i] = p_data[i]
    data[size] = '\x00'

def set_data(py_obj, p_data):
    py_obj = cts.cast('PyUnicodeObject*', py_obj)
    py_obj.c_data = p_data

def get_compact(py_obj):
    return rffi.getintfield(get_state(py_obj), 'c_compact')

def set_compact(py_obj, value):
    get_state(py_obj).c_compact = cts.cast('unsigned char', value)


@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsAlpha(space, ch):
    """Return 1 or 0 depending on whether ch is an alphabetic character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isalpha(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsDecimalDigit(space, ch):
    """Return 1 or 0 depending on whether ch is a decimal character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isdecimal(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsDigit(space, ch):
    """Return 1 or 0 depending on whether ch is a digit character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isdigit(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsNumeric(space, ch):
    """Return 1 or 0 depending on whether ch is a numeric character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isnumeric(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsLowercase(space, ch):
    """Return 1 or 0 depending on whether ch is a lowercase character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.islower(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsUppercase(space, ch):
    """Return 1 or 0 depending on whether ch is an uppercase character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isupper(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsTitlecase(space, ch):
    """Return 1 or 0 depending on whether ch is a titlecase character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.istitle(ch)

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_IsPrintable(space, ch):
    """Return 1 or 0 depending on whether ch is a printable character."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return 0
    return unicodedb.isprintable(ch)


@cpython_api([Py_UCS4], Py_UCS4, error=CANNOT_FAIL)
def _PyUnicode_ToLowercase(space, ch):
    """Return the character ch converted to lower case."""
    val = rffi.cast(lltype.Signed, ch)
    if val >= rutf8.MAXUNICODE:
        return ch
    return r_uint32(unicodedb.tolower(val))

@cpython_api([Py_UCS4], Py_UCS4, error=CANNOT_FAIL)
def _PyUnicode_ToUppercase(space, ch):
    """Return the character ch converted to upper case."""
    val = rffi.cast(lltype.Signed, ch)
    if val >= rutf8.MAXUNICODE:
        return ch
    return r_uint32(unicodedb.toupper(val))

@cpython_api([Py_UCS4], Py_UCS4, error=CANNOT_FAIL)
def _PyUnicode_ToTitlecase(space, ch):
    """Return the character ch converted to title case."""
    val = rffi.cast(lltype.Signed, ch)
    if val >= rutf8.MAXUNICODE:
        return ch
    return r_uint32(unicodedb.totitle(val))

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_ToDecimalDigit(space, ch):
    """Return the character ch converted to a decimal positive integer.  Return
    -1 if this is not possible.  This macro does not raise exceptions."""
    val = rffi.cast(lltype.Signed, ch)
    if val >= rutf8.MAXUNICODE:
        return -1
    try:
        return unicodedb.decimal(val)
    except KeyError:
        return -1

@cpython_api([Py_UCS4], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_ToDigit(space, ch):
    """Return the character ch converted to a single digit integer. Return -1 if
    this is not possible.  This macro does not raise exceptions."""
    ch = rffi.cast(lltype.Signed, ch)
    if ch >= rutf8.MAXUNICODE:
        return -1
    try:
        return unicodedb.digit(ch)
    except KeyError:
        return -1

@cpython_api([], Py_UNICODE, error=CANNOT_FAIL)
def PyUnicode_GetMax(space):
    """Get the maximum ordinal for a Unicode character."""
    from rpython.rlib import runicode, rutf8
    return runicode.UNICHR(rutf8.MAXUNICODE)

@cts.decl("int _PyUnicode_Ready(PyObject *unicode)", error=-1)
def _PyUnicode_Ready(space, py_obj):
    # conversion from pyobj to space.w_unicode can fail,
    # so create the rpython object here and not in the api wrapper
    kind = get_kind(py_obj)
    if kind == WCHAR_KIND:
        w_obj = from_ref(space, rffi.cast(PyObject, py_obj))
    else:
        s = kind_to_name.get(kind, "INVALID")
        raise oefmt(space.w_ValueError,
            "converting %s PyUnicodeObject not supported yet", s)
    return _readify(space, py_obj, space.utf8_w(w_obj))

def _readify(space, py_obj, value):
    PyUnicode_Type = rffi.cast(cts.gettype('PyTypeObject*'),
                               make_ref(space, space.w_unicode))
    maxchar = 0
    for c in rutf8.Utf8StringIterator(value):
        if c > maxchar:
            maxchar = c
            if maxchar > rutf8.MAXUNICODE:
                raise oefmt(space.w_ValueError,
                    "Character U+%s is not in range [U+0000; U+10ffff]",
                    '%x' % maxchar)
    use_compact = (py_obj.c_ob_type == PyUnicode_Type)
    if maxchar < 256:
        # Do this before the compact format overwrites the
        # PyCompactUnicodeObject.c_wstr_length
        alloc_len = get_wsize(py_obj)
        if use_compact:
            pyvarobj = rffi.cast(PyVarObject, py_obj)
            itemcount = pyvarobj.c_ob_size
            set_compact(py_obj, 1)
        set_len(py_obj, alloc_len)
        set_kind(py_obj, _1BYTE_KIND)
        set_utf8(py_obj, cts.cast('char *', 0))
        set_utf8_len(py_obj, 0)
        if maxchar < 128:
            set_ascii(py_obj, 1)
            if use_compact:
                set_data_compact(py_obj, value, len(value))
            else:
                ucs1_data = cts.cast('void *', rffi.str2charp(value))
                set_data(py_obj, ucs1_data)
                set_utf8(py_obj, cts.cast('char *', get_data(py_obj)))
                set_utf8_len(py_obj, get_wsize(py_obj))
        else:
            set_ascii(py_obj, 0)
            # re-encode as latin-1
            w_value = space.newtext(value)
            value = utf8_encode_latin_1(space, value, w_value, 'strict', None)
            if use_compact:
                set_data_compact(py_obj, value, len(value))
            else:
                ucs1_data = cts.cast('void *', rffi.str2charp(value))
                set_data(py_obj, ucs1_data)
    elif maxchar < 65536:
        ucs2_data = cts.cast('void *', 0)
        if rffi.sizeof(lltype.UniChar) == 2:
            ucs2_data = cts.cast('void *', get_wbuffer(py_obj))
        if not ucs2_data:
            w_value = space.newtext(value)
            ucs2_str = utf8_encode_utf_16_helper(
                space, value, w_value, 'strict',
                byteorder=BYTEORDER)
            ucs2_data = cts.cast('void *', rffi.str2charp(ucs2_str))
            if rffi.sizeof(lltype.UniChar) == 2:
                set_wbuffer(py_obj, cts.cast('wchar_t *', ucs2_data))
        set_data(py_obj, ucs2_data)
        set_len(py_obj, get_wsize(py_obj))
        set_kind(py_obj, _2BYTE_KIND)
        set_utf8(py_obj, cts.cast('char *', 0))
        set_utf8_len(py_obj, 0)
    else:
        ucs4_data = cts.cast('void *', 0)
        if rffi.sizeof(lltype.UniChar) == 4:
            ucs4_data = cts.cast('void *', get_wbuffer(py_obj))
        if not ucs4_data:
            w_value = space.newtext(value)
            ucs4_str = utf8_encode_utf_32_helper(
                space, value, w_value, 'strict',
                byteorder=BYTEORDER)
            ucs4_data = cts.cast('void *', rffi.str2charp(ucs4_str))
            if rffi.sizeof(lltype.UniChar) == 4:
                set_wbuffer(py_obj, cts.cast('wchar_t *', ucs4_data))
        set_data(py_obj, ucs4_data)
        set_len(py_obj, get_wsize(py_obj))
        set_kind(py_obj, _4BYTE_KIND)
        set_utf8(py_obj, cts.cast('char *', 0))
        set_utf8_len(py_obj, 0)
    set_ready(py_obj, 1)
    return 0

@cts.decl("""PyObject* PyUnicode_FromKindAndData(
        int kind, const void *buffer, Py_ssize_t size)""")
def PyUnicode_FromKindAndData(space, kind, data, size):
    if size < 0:
        raise oefmt(space.w_ValueError, "size must be positive")
    data = cts.cast('char *', data)
    kind = widen(kind)
    if kind == _1BYTE_KIND:
        value = rffi.charpsize2str(data, size)
        w_res = latin_1_decode(space, value, w_final=space.w_False)
    elif kind == _2BYTE_KIND:
        value = rffi.charpsize2str(data, 2 * size)
        w_res = utf_16_decode(space, value, errors='surrogatepass',
                              w_final=space.w_False)
    elif kind == _4BYTE_KIND:
        value = rffi.charpsize2str(data, 4 * size)
        state = space.fromcache(CodecState)
        eh = state.decode_error_handler
        w_value = space.newbytes(value)
        result, length, pos, _ = str_decode_utf_32_helper(space, value, w_value,
                                             'surrogatpass', True, eh,
                                             byteorder=BYTEORDER,
                                             allow_surrogates=True)
        return space.newutf8(result, length)
    else:
        raise oefmt(space.w_SystemError, "invalid kind")
    return space.unpackiterable(w_res)[0]

@cts.decl("Py_UNICODE * PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)")
def PyUnicode_AsUnicodeAndSize(space, ref, psize):
    """Return a read-only pointer to the Unicode object's internal Py_UNICODE
    buffer, NULL if unicode is not a Unicode object."""
    if not pyunicode_check(ref):
        raise oefmt(space.w_TypeError, "expected unicode object")
    ret = get_maybe_create_wbuffer(space, ref)
    if psize:
        psize[0] = get_len(ref)
    return ret

def get_maybe_create_wbuffer(space, ref):
    if not get_wbuffer(ref):
        # compact ascii for instance
        w_unicode = from_ref(space, rffi.cast(PyObject, ref))
        u = space.utf8_w(w_unicode)
        lgt = space.len_w(w_unicode)
        if rffi.sizeof(lltype.UniChar) == 2:
            # Handle surrogates
            wbuf = rffi.utf82wcharp_ex(u, lgt)
        else:
            wbuf = rffi.utf82wcharp(u, lgt)
        if lgt != get_len(ref):
            raise oefmt(space.w_SystemError, "inconsistent length")
        set_wbuffer(ref, wbuf)
        if not get_compact_ascii(ref):
            set_wsize(ref, lgt)
    return get_wbuffer(ref)

@cts.decl("char * PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)")
def PyUnicode_AsUTF8AndSize(space, ref, psize):
    if not pyunicode_check(ref):
        # PyUnicode_Check failed
        PyErr_BadArgument(space)
    if not get_ready(ref):
        res = _PyUnicode_Ready(space, ref)

    if get_compact_ascii(ref):
        if psize:
            psize[0] = get_len(ref)
        return cts.cast('char *', get_data(ref))
    ret = get_utf8(ref)
    if not ret:
        # Happens the first time through for compact, non-ascii
        # Copy unicode buffer
        w_unicode = from_ref(space, ref)
        w_encoded = unicodeobject.encode_object(space, w_unicode, "utf-8",
                                                "strict")
        s = space.bytes_w(w_encoded)
        set_utf8(ref, rffi.str2charp(s))
        set_utf8_len(ref, len(s))
        ret = get_utf8(ref)
    if psize:
        psize[0] = get_utf8_len(ref)
    return ret

@cts.decl("char * PyUnicode_AsUTF8(PyObject *unicode)")
def PyUnicode_AsUTF8(space, ref):
    return PyUnicode_AsUTF8AndSize(space, ref, cts.cast('Py_ssize_t *', 0))

@cpython_api([PyObject, rffi.CWCHARP, Py_ssize_t], Py_ssize_t, error=-1)
def PyUnicode_AsWideChar(space, ref, buf, size):
    """Copy the Unicode object contents into the wchar_t buffer w.  At most
    size wchar_t characters are copied (excluding a possibly trailing
    0-termination character).  Return the number of wchar_t characters
    copied or -1 in case of an error.  Note that the resulting wchar_t
    string may or may not be 0-terminated.  It is the responsibility of the caller
    to make sure that the wchar_t string is 0-terminated in case this is
    required by the application."""
    c_buffer = get_maybe_create_wbuffer(space, ref)
    c_length = get_len(ref)

    # If possible, try to copy the 0-termination as well
    if size > c_length:
        size = c_length + 1

    i = 0
    while i < size:
        buf[i] = c_buffer[i]
        i += 1

    if size > c_length:
        return c_length
    else:
        return size

@cpython_api([], rffi.CCHARP, error=CANNOT_FAIL)
def PyUnicode_GetDefaultEncoding(space):
    """Returns the currently active default encoding."""
    if default_encoding[0] == '\x00':
        encoding = unicodeobject.getdefaultencoding(space)
        i = 0
        while i < len(encoding) and i < DEFAULT_ENCODING_SIZE:
            default_encoding[i] = encoding[i]
            i += 1
    return default_encoding

def _unicode_as_encoded_object(space, pyobj, llencoding, llerrors):
    if not pyunicode_check(pyobj):
        PyErr_BadArgument(space)

    encoding = errors = None
    if llencoding:
        encoding = rffi.charp2str(llencoding)
    if llerrors:
        errors = rffi.charp2str(llerrors)
    w_unicode = from_ref(space, pyobj)
    return unicodeobject.encode_object(space, w_unicode, encoding, errors)

@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_AsEncodedObject(space, pyobj, llencoding, llerrors):
    """Encode a Unicode object and return the result as Python object.
    encoding and errors have the same meaning as the parameters of the same name
    in the Unicode encode() method. The codec to be used is looked up using
    the Python codec registry. Return NULL if an exception was raised by the
    codec."""
    return _unicode_as_encoded_object(space, pyobj, llencoding, llerrors)

@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_AsEncodedString(space, pyref, llencoding, llerrors):
    """Encode a Unicode object and return the result as Python string object.
    encoding and errors have the same meaning as the parameters of the same name
    in the Unicode encode() method. The codec to be used is looked up using
    the Python codec registry. Return NULL if an exception was raised by the
    codec."""
    w_str = _unicode_as_encoded_object(space, pyref, llencoding, llerrors)
    if not PyBytes_Check(space, w_str):
        raise oefmt(space.w_TypeError,
                    "encoder did not return a bytes object")
    return w_str

@cpython_api([PyObject], PyObject)
def PyUnicode_AsUnicodeEscapeString(space, pyobj):
    """Encode a Unicode object using Unicode-Escape and return the result as Python
    string object.  Error handling is "strict". Return NULL if an exception was
    raised by the codec."""
    if not pyunicode_check(pyobj):
        PyErr_BadArgument(space)

    w_unicode = from_ref(space, pyobj)
    return unicodeobject.encode_object(space, w_unicode, 'unicode-escape', 'strict')

@cpython_api([CONST_WSTRING, Py_ssize_t], PyObject, result_is_ll=True)
def PyUnicode_FromUnicode(space, wchar_p, length):
    """Create a legacy (non-compact) Unicode Object from the Py_UNICODE
    buffer wchar_p of the given size. wchar_p may be NULL which causes the
    contents to be undefined. It is the user's responsibility to fill in
    the needed data.  The buffer is copied into the data field of the
    PyUnicodeObject. If the buffer is not NULL, the return value might be a
    shared object.  Therefore, modification of the resulting Unicode object
    is only allowed when u is NULL."""
    if length < 0:
        length = 0
    if wchar_p:
        s = wcharpsize2utf8(space, wchar_p, length)
        # XXX this is for windows, since wchar_p is in utf16 so length may not
        # be codepoints. This could be pushed into the windows branch of
        # wcharpsize2utf8
        length = rutf8.codepoints_in_utf8(s)
        return make_ref(space, space.newutf8(s, length))
    else:
        return new_empty_unicode(space, length)

@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_Decode(space, s, size, encoding, errors):
    """Create a Unicode object by decoding size bytes of the encoded string s.
    encoding and errors have the same meaning as the parameters of the same name
    in the unicode() built-in function.  The codec to be used is looked up
    using the Python codec registry.  Return NULL if an exception was raised by
    the codec."""
    return _pyunicode_decode(space, rffi.charpsize2str(s, size),
                             encoding, errors)

def _pyunicode_decode(space, s, encoding, errors):
    if encoding:
        w_encoding = space.newtext(rffi.charp2str(encoding))
    else:
        # python 3.4 changed to this from defaultencoding
        w_encoding = space.newtext('utf-8')
    w_str = space.newbytes(s)
    if errors:
        w_errors = space.newtext(rffi.charp2str(errors))
    else:
        w_errors = None
    return space.call_method(w_str, 'decode', w_encoding, w_errors)

@cpython_api([PyObject], PyObject)
def PyUnicode_FromObject(space, w_obj):
    """Copy an instance of a Unicode subtype to a new true Unicode object if
    necessary. If obj is already a true Unicode object (not a subtype), return
    the reference with incremented refcount.

    Objects other than Unicode or its subtypes will cause a TypeError.
    """
    return pyunicode_fromobject(space, w_obj)

def pyunicode_fromobject(space, w_obj):
    if space.is_w(space.type(w_obj), space.w_unicode):
        return w_obj
    elif space.isinstance_w(w_obj, space.w_unicode):
        return space.call_function(space.w_unicode, w_obj)
    else:
        raise oefmt(space.w_TypeError,
                    "Can't convert '%T' object to str implicitly", w_obj)

@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_FromEncodedObject(space, w_obj, encoding, errors):
    """Coerce an encoded object obj to an Unicode object and return a reference with
    incremented refcount.

    String and other char buffer compatible objects are decoded according to the
    given encoding and using the error handling defined by errors.  Both can be
    NULL to have the interface use the default values (see the next section for
    details).

    All other objects, including Unicode objects, cause a TypeError to be
    set."""
    if space.isinstance_w(w_obj, space.w_bytes):
        s = space.bytes_w(w_obj)
        if not s:
            return space.newtext('')
    elif space.isinstance_w(w_obj, space.w_unicode):
        raise oefmt(space.w_TypeError, "decoding str is not supported")
    elif space.isinstance_w(w_obj, space.w_bytearray):   # Python 2.x specific
        raise oefmt(space.w_TypeError, "decoding bytearray is not supported")
    else:
        s = space.charbuf_w(w_obj)
    return _pyunicode_decode(space, s, encoding, errors)


@cpython_api([PyObject, CONST_STRING], PyObject)
def PyUnicode_EncodeLocale(space, w_obj, errors):
    from pypy.module._codecs.locale import utf8_encode_locale
    if errors:
        s = rffi.charp2str(errors)
    else:
        s = 'strict'
    if not s in ('strict', 'surrogateescape'):
        raise oefmt(space.w_ValueError, "only 'strict' and 'surrogateescape' "
                    "error handlers are supported, not '%s'", s)
    utf8 = space.utf8_w(w_obj)
    ulen = space.len_w(w_obj)
    return space.newbytes(utf8_encode_locale(utf8, ulen, s))

@cpython_api([CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_DecodeLocale(space, obj, errors):
    from pypy.module._codecs.locale import str_decode_locale
    if errors:
        s = rffi.charp2str(errors)
    else:
        s = 'strict'
    if not s in ('strict', 'surrogateescape'):
        raise oefmt(space.w_ValueError, "only 'strict' and 'surrogateescape' "
                    "error handlers are supported, not '%s'", s)
    utf8 = rffi.charp2str(obj)
    return space.newtext(*str_decode_locale(utf8, s))

@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
def PyUnicode_DecodeLocaleAndSize(space, obj, length, errors):
    from pypy.module._codecs.locale import str_decode_locale
    if errors:
        s = rffi.charp2str(errors)
    else:
        s = 'strict'
    if not s in ('strict', 'surrogateescape'):
        raise oefmt(space.w_ValueError, "only 'strict' and 'surrogateescape' "
                    "error handlers are supported, not '%s'", s)
    utf8 = rffi.charpsize2str(obj, length)
    return space.newtext(*str_decode_locale(utf8, s))

@cpython_api([PyObject, PyObjectP], rffi.INT_real, error=0)
def PyUnicode_FSConverter(space, w_obj, result):
    """ParseTuple converter: encode str objects to bytes using
    PyUnicode_EncodeFSDefault(); bytes objects are output as-is.
    result must be a PyBytesObject* which must be released when it is
    no longer used.
    """
    if not w_obj:
        # Implement ParseTuple cleanup support
        decref(space, result[0])
        return 1
    w_path = pyos_fspath(space, w_obj)
    if space.isinstance_w(w_path, space.w_bytes):
        w_output = w_path
    else:
        w_output = space.fsencode(w_path)
    
        if not space.isinstance_w(w_output, space.w_bytes):
            raise oefmt(space.w_TypeError, "encoder failed to return bytes")
    data = space.bytes0_w(w_output)  # Check for NUL bytes
    result[0] = make_ref(space, w_output)
    return Py_CLEANUP_SUPPORTED


@cpython_api([PyObject, PyObjectP], rffi.INT_real, error=0)
def PyUnicode_FSDecoder(space, w_obj, result):
    """ParseTuple converter: decode bytes objects to str using
    PyUnicode_DecodeFSDefaultAndSize(); str objects are output
    as-is. result must be a PyUnicodeObject* which must be released
    when it is no longer used.
    """
    if not w_obj:
        # Implement ParseTuple cleanup support
        decref(space, result[0])
        return 1
    if space.isinstance_w(w_obj, space.w_unicode):
        w_output = w_obj
    elif space.isinstance_w(w_obj, space.w_bytes):
        w_output = space.fsdecode(w_obj)
    else:
        allowed_types = "string, bytes or os.PathLike"
        try:
            space._try_buffer_w(w_obj, space.BUF_FULL_RO)
        except BufferInterfaceNotFound:
            w_fspath_method = space.lookup(w_obj, '__fspath__')
            if w_fspath_method:
                w_result = space.get_and_call_function(w_fspath_method, w_obj)
                if space.isinstance_w(w_result, space.w_unicode):
                    w_output = w_result
                elif space.isinstance_w(w_result, space.w_bytes):
                    w_output = space.fsdecode(w_result)
                else:
                    raise oefmt(space.w_TypeError,
                        "expected %S.__fspath__() to return str or bytes, not %T",
                        w_obj, w_result)
            else:
                raise oefmt(
                    space.w_TypeError,
                    "path should be %s, not %T",
                    allowed_types, w_obj)
        else:
            tp = space.type(w_obj).name
            space.warn(space.newtext(
                "path should be %s, not %s" % (allowed_types, tp,)),
                space.w_DeprecationWarning)
            buffer = space.buffer_w(w_obj, space.BUF_FULL_RO)
            w_output =  space.fsdecode(space.newbytes(buffer.as_str()))
    if not space.isinstance_w(w_output, space.w_unicode):
        raise oefmt(space.w_TypeError, "decoder failed to return unicode")
    data = space.utf8_0_w(w_output)  # Check for NUL bytes
    result[0] = make_ref(space, w_output)
    return Py_CLEANUP_SUPPORTED


@cpython_api([CONST_STRING, Py_ssize_t], PyObject)
def PyUnicode_DecodeFSDefaultAndSize(space, s, size):
    """Decode a string using Py_FileSystemDefaultEncoding and the
    'surrogateescape' error handler, or 'strict' on Windows.

    If Py_FileSystemDefaultEncoding is not set, fall back to the
    locale encoding.

    Use 'strict' error handler on Windows."""
    w_bytes = space.newbytes(rffi.charpsize2str(s, size))
    return space.fsdecode(w_bytes)


@cpython_api([CONST_STRING], PyObject)
def PyUnicode_DecodeFSDefault(space, s):
    """Decode a null-terminated string using Py_FileSystemDefaultEncoding
    and the 'surrogateescape' error handler, or 'strict' on Windows.

    If Py_FileSystemDefaultEncoding is not set, fall back to the
    locale encoding.

    Use PyUnicode_DecodeFSDefaultAndSize() if you know the string length.

    Use 'strict' error handler on Windows."""
    w_bytes = space.newbytes(rffi.charp2str(s))
    return space.fsdecode(w_bytes)


@cpython_api([PyObject], PyObject)
def PyUnicode_EncodeFSDefault(space, w_unicode):
    """Encode a Unicode object to Py_FileSystemDefaultEncoding with the
    'surrogateescape' error handler, or 'strict' on Windows, and return
    bytes. Note that the resulting bytes object may contain
    null bytes.

    If Py_FileSystemDefaultEncoding is not set, fall back to the
    locale encoding.
    """
    return space.fsencode(w_unicode)


@cpython_api([CONST_STRING], PyObject)
def PyUnicode_FromString(space, s):
    """Create a Unicode object from an UTF-8 encoded null-terminated char buffer"""
    w_str = space.newbytes(rffi.charp2str(s))
    return space.call_method(w_str, 'decode', space.newtext("utf-8"))

@cpython_api([PyObjectP], lltype.Void)
def PyUnicode_InternInPlace(space, string):
    """Intern the argument *string in place.  The argument must be the address
    of a pointer variable pointing to a Python unicode string object.  If there
    is an existing interned string that is the same as *string, it sets *string
    to it (decrementing the reference count of the old string object and
    incrementing the reference count of the interned string object), otherwise
    it leaves *string alone and interns it (incrementing its reference count).
    (Clarification: even though there is a lot of talk about reference counts,
    think of this function as reference-count-neutral; you own the object after
    the call if and only if you owned it before the call.)"""
    w_str = from_ref(space, string[0])
    w_str = space.new_interned_w_str(w_str)
    decref(space, string[0])
    string[0] = make_ref(space, w_str)

@cpython_api([CONST_STRING], PyObject)
def PyUnicode_InternFromString(space, s):
    """A combination of PyUnicode_FromString() and
    PyUnicode_InternInPlace(), returning either a new unicode string
    object that has been interned, or a new ("owned") reference to an
    earlier interned string object with the same value.
    """
    w_str = space.newtext(rffi.charp2str(s))
    return space.new_interned_w_str(w_str)

@cpython_api([CONST_STRING, Py_ssize_t], PyObject, result_is_ll=True)
def PyUnicode_FromStringAndSize(space, s, size):
    """Create a Unicode Object from the char buffer u. The bytes will be
    interpreted as being UTF-8 encoded. u may also be NULL which causes the
    contents to be undefined. It is the user's responsibility to fill in the
    needed data. The buffer is copied into the new object. If the buffer is not
    NULL, the return value might be a shared object. Therefore, modification of
    the resulting Unicode object is only allowed when u is NULL."""
    if s:
        return make_ref(space, PyUnicode_DecodeUTF8(
            space, s, size, lltype.nullptr(rffi.CCHARP.TO)))
    else:
        return new_empty_unicode(space, size)

@cpython_api([rffi.INT_real], PyObject)
def PyUnicode_FromOrdinal(space, ordinal):
    """Create a Unicode Object from the given Unicode code point ordinal.

    The ordinal must be in range(0x10000) on narrow Python builds
    (UCS2), and range(0x110000) on wide builds (UCS4). A ValueError is
    raised in case it is not."""
    w_ordinal = space.newint(rffi.cast(lltype.Signed, ordinal))
    return space.call_function(space.builtin.get('chr'), w_ordinal)

@cpython_api([PyObjectP, Py_ssize_t], rffi.INT_real, error=-1)
def PyUnicode_Resize(space, ref, newsize):
    # XXX always create a new string so far
    py_obj = ref[0]
    if not get_wbuffer(py_obj):
        raise oefmt(space.w_SystemError,
                    "PyUnicode_Resize called on already created string")
    try:
        py_newuni = new_empty_unicode(space, newsize)
    except MemoryError:
        decref(space, ref[0])
        ref[0] = lltype.nullptr(PyObject.TO)
        raise
    to_cp = newsize
    oldsize = get_wsize(py_obj)
    if oldsize < newsize:
        to_cp = oldsize
    for i in range(to_cp):
        get_wbuffer(py_newuni)[i] = get_wbuffer(py_obj)[i]
    decref(space, ref[0])
    ref[0] = rffi.cast(PyObject, py_newuni)
    return 0

def make_conversion_functions(suffix, encoding, only_for_asstring=False):
    @cpython_api([PyObject], PyObject)
    @func_renamer('PyUnicode_As%sString' % suffix)
    def PyUnicode_AsXXXString(space, pyobj):
        """Encode a Unicode object and return the result as Python
        string object.  Error handling is "strict".  Return NULL if an
        exception was raised by the codec."""
        if not pyunicode_check(pyobj):
            PyErr_BadArgument(space)
        w_unicode = from_ref(space, pyobj)
        return unicodeobject.encode_object(space, w_unicode, encoding, "strict")
    globals()['PyUnicode_As%sString' % suffix] = PyUnicode_AsXXXString

    if only_for_asstring:
        return

    @cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
    @func_renamer('PyUnicode_Decode%s' % suffix)
    def PyUnicode_DecodeXXX(space, s, size, errors):
        """Create a Unicode object by decoding size bytes of the
        encoded string s. Return NULL if an exception was raised by
        the codec.
        """
        w_s = space.newbytes(rffi.charpsize2str(s, size))
        if errors:
            w_errors = space.newtext(rffi.charp2str(errors))
        else:
            w_errors = None
        return space.call_method(w_s, 'decode', space.newtext(encoding), w_errors)
    globals()['PyUnicode_Decode%s' % suffix] = PyUnicode_DecodeXXX

    @cpython_api([CONST_WSTRING, Py_ssize_t, CONST_STRING], PyObject)
    @func_renamer('PyUnicode_Encode%s' % suffix)
    def PyUnicode_EncodeXXX(space, s, size, errors):
        """Encode the Py_UNICODE buffer of the given size and return a
        Python string object.  Return NULL if an exception was raised
        by the codec."""
        if size < 0:
            size = 0
        u = wcharpsize2utf8(space, s, size)
        w_u = space.newutf8(u, size)
        if errors:
            w_errors = space.newtext(rffi.charp2str(errors))
        else:
            w_errors = None
        return space.call_method(w_u, 'encode', space.newtext(encoding), w_errors)
    globals()['PyUnicode_Encode%s' % suffix] = PyUnicode_EncodeXXX

make_conversion_functions('UTF8', 'utf-8')
make_conversion_functions('UTF16', 'utf-16', only_for_asstring=True)
make_conversion_functions('UTF32', 'utf-32', only_for_asstring=True)
make_conversion_functions('ASCII', 'ascii')
make_conversion_functions('Latin1', 'latin-1')
if sys.platform == 'win32':
    from pypy.module._codecs.interp_codecs import code_page_encode
    make_conversion_functions('MBCS', 'mbcs')

    @cpython_api([rffi.INT_real, PyObject, CONST_STRING], PyObject)
    def PyUnicode_EncodeCodePage(space, code_page, w_obj, errors):
        if errors:
            errors = rffi.charp2str(errors)
        else:
            errors = None
        res = code_page_encode(space, widen(code_page), w_obj, errors)
        return space.listview(res)[0]

@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING, INTP_real], PyObject)
def PyUnicode_DecodeUTF16(space, s, size, llerrors, pbyteorder):
    """Decode length bytes from a UTF-16 encoded buffer string and return the
    corresponding Unicode object.  errors (if non-NULL) defines the error
    handling. It defaults to "strict".

    If byteorder is non-NULL, the decoder starts decoding using the given byte
    order:

    *byteorder == -1: little endian
    *byteorder == 0:  native order
    *byteorder == 1:  big endian

    If *byteorder is zero, and the first two bytes of the input data are a
    byte order mark (BOM), the decoder switches to this byte order and the BOM is
    not copied into the resulting Unicode string.  If *byteorder is -1 or
    1, any byte order mark is copied to the output (where it will result in
    either a \ufeff or a \ufffe character).

    After completion, *byteorder is set to the current byte order at the end
    of input data.

    If byteorder is NULL, the codec starts in native order mode.

    Return NULL if an exception was raised by the codec."""

    string = rffi.charpsize2str(s, size)

    if pbyteorder is not None:
        llbyteorder = rffi.cast(lltype.Signed, pbyteorder[0])
        if llbyteorder < 0:
            byteorder = "little"
        elif llbyteorder > 0:
            byteorder = "big"
        else:
            byteorder = "native"
    else:
        byteorder = "native"

    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = 'strict'

    state = space.fromcache(CodecState)
    w_string = space.newbytes(string)
    result, length, pos, bo = str_decode_utf_16_helper(
        space, string, w_string, errors, True, state.decode_error_handler,
        byteorder=byteorder)
    if pbyteorder is not None:
        pbyteorder[0] = rffi.cast(rffi.INT_real, bo)
    return space.newutf8(result, length)

@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING, INTP_real], PyObject)
def PyUnicode_DecodeUTF32(space, s, size, llerrors, pbyteorder):
    """Decode length bytes from a UTF-32 encoded buffer string and
    return the corresponding Unicode object.  errors (if non-NULL)
    defines the error handling. It defaults to "strict".

    If byteorder is non-NULL, the decoder starts decoding using the
    given byte order:
    *byteorder == -1: little endian
    *byteorder == 0:  native order
    *byteorder == 1:  big endian

    If *byteorder is zero, and the first four bytes of the input data
    are a byte order mark (BOM), the decoder switches to this byte
    order and the BOM is not copied into the resulting Unicode string.
    If *byteorder is -1 or 1, any byte order mark is copied to the
    output.

    After completion, *byteorder is set to the current byte order at
    the end of input data.

    In a narrow build codepoints outside the BMP will be decoded as
    surrogate pairs.

    If byteorder is NULL, the codec starts in native order mode.

    Return NULL if an exception was raised by the codec.
    """
    string = rffi.charpsize2str(s, size)

    if pbyteorder:
        llbyteorder = rffi.cast(lltype.Signed, pbyteorder[0])
        if llbyteorder < 0:
            byteorder = "little"
        elif llbyteorder > 0:
            byteorder = "big"
        else:
            byteorder = "native"
    else:
        byteorder = "native"

    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = 'strict'

    state = space.fromcache(CodecState)
    w_string = space.newbytes(string)
    result, length, pos, bo = str_decode_utf_32_helper(
        space, string, w_string, errors, True, state.decode_error_handler,
        byteorder=byteorder)
    if pbyteorder is not None:
        pbyteorder[0] = rffi.cast(rffi.INT_real, bo)
    return space.newutf8(result, length)

@cpython_api([rffi.CWCHARP, Py_ssize_t, rffi.CCHARP, CONST_STRING],
             rffi.INT_real, error=-1)
def PyUnicode_EncodeDecimal(space, s, length, output, llerrors):
    """Takes a Unicode string holding a decimal value and writes it
    into an output buffer using standard ASCII digit codes.

    The output buffer has to provide at least length+1 bytes of
    storage area. The output string is 0-terminated.

    The encoder converts whitespace to ' ', decimal characters to
    their corresponding ASCII digit and all other Latin-1 characters
    except \0 as-is. Characters outside this range (Unicode ordinals
    1-256) are treated as errors. This includes embedded NULL bytes.

    Returns 0 on success, -1 on failure.
    """
    u = wcharpsize2utf8(space, s, length)
    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = None
    state = space.fromcache(CodecState)
    w_u = space.newtext(u)
    result = unicode_encode_decimal(space, u, w_u, errors, state.encode_error_handler)
    i = len(result)
    output[i] = '\0'
    i -= 1
    while i >= 0:
        output[i] = result[i]
        i -= 1
    return 0

@cpython_api([rffi.CArrayPtr(Py_UNICODE), Py_ssize_t], PyObject)
def PyUnicode_TransformDecimalToASCII(space, s, size):
    """Create a Unicode object by replacing all decimal digits in
    Py_UNICODE buffer of the given size by ASCII digits 0--9
    according to their decimal value.  Return NULL if an exception
    occurs."""
    result = rutf8.Utf8StringBuilder(size)
    for i in range(size):
        ch = s[i]
        ordch = ord(ch)
        if ordch > 127:
            decimal = _PyUnicode_ToDecimalDigit(space, ordch)
            decimal = rffi.cast(lltype.Signed, decimal)
            if decimal >= 0:
                ordch = ord('0') + decimal
        result.append_code(ordch)
    u = result.build()
    return space.newtext(u, result.getlength())

@cpython_api([PyObject, PyObject], rffi.INT_real, error=-2)
def PyUnicode_Compare(space, w_left, w_right):
    """Compare two strings and return -1, 0, 1 for less than, equal, and greater
    than, respectively."""
    if space.is_true(space.lt(w_left, w_right)):
        return -1
    if space.is_true(space.lt(w_right, w_left)):
        return 1
    return 0

@cpython_api([PyObject, PyObject], PyObject)
def PyUnicode_Concat(space, w_left, w_right):
    """Concat two strings giving a new Unicode string."""
    return space.add(w_left, w_right)

@cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_EqualToASCIIString(space, w_uni, string):
    """Test whether a unicode is equal to ASCII string.  Return 1 if true,
   0 otherwise.  The right argument must be ASCII-encoded string.
   Any error occurs inside will be cleared before return."""
    utf8 = space.utf8_w(w_uni)
    lgt = space.len_w(w_uni)
    i = 0
    # Compare Unicode string and source character set string
    for ch in rutf8.Utf8StringIterator(utf8):
        if string[i] == '\0':
            break
        s = ord(string[i])
        if ch != s:
            if ch != s:
                return 0
        i += 1
    if i < lgt:
        return 0  # uni is longer
    if string[i] != '\0':
        return 0  # str is longer
    return 1

@cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL)
def _PyUnicode_EQ(space, w_aa, w_bb):
    if not space.isinstance_w(w_aa, space.w_unicode) or not space.isinstance_w(w_bb, space.w_unicode):
        raise oefmt(space.w_TypeError, "_PyUnicode_EQ(aa, bb) must be called with two str instances")
    aa = space.utf8_w(w_aa)
    la = space.len_w(w_aa)
    bb = space.utf8_w(w_bb)
    lb = space.len_w(w_bb)
    if la != lb:
        return 0
    if la == 0:
        return 1
    if aa == bb:
        return 1
    return 0 
@cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=CANNOT_FAIL)
def PyUnicode_CompareWithASCIIString(space, w_uni, string):
    """Compare a unicode object, uni, with string and return -1, 0, 1 for less
    than, equal, and greater than, respectively. It is best to pass only
    ASCII-encoded strings, but the function interprets the input string as
    ISO-8859-1 if it contains non-ASCII characters."""
    utf8 = space.utf8_w(w_uni)
    lgt = space.len_w(w_uni)
    i = 0
    # Compare Unicode string and source character set string
    for ch in rutf8.Utf8StringIterator(utf8):
        if string[i] == '\0':
            break
        s = ord(string[i])
        if ch != s:
            if ch < s:
                return -1
            else:
                return 1
        i += 1
    if i < lgt:
        return 1  # uni is longer
    if string[i] != '\0':
        return -1  # str is longer
    return 0


@cpython_api([rffi.CWCHARP, rffi.CWCHARP, Py_ssize_t], lltype.Void)
def Py_UNICODE_COPY(space, target, source, length):
    """Roughly equivalent to memcpy() only the base size is Py_UNICODE
    copies sizeof(Py_UNICODE) * length bytes from source to target"""
    for i in range(0, length):
        target[i] = source[i]

@cpython_api([PyObject, PyObject], PyObject)
def PyUnicode_Format(space, w_format, w_args):
    """Return a new string object from format and args; this is analogous to
    format % args.  The args argument must be a tuple."""
    return space.mod(w_format, w_args)

@cpython_api([PyObject, PyObject], PyObject)
def PyUnicode_Join(space, w_sep, w_seq):
    """Join a sequence of strings using the given separator and return
    the resulting Unicode string."""
    return space.call_method(w_sep, 'join', w_seq)

@cpython_api([PyObject, PyObject, PyObject, Py_ssize_t], PyObject)
def PyUnicode_Replace(space, w_str, w_substr, w_replstr, maxcount):
    """Replace at most maxcount occurrences of substr in str with replstr and
    return the resulting Unicode object. maxcount == -1 means replace all
    occurrences."""
    return space.call_method(w_str, "replace", w_substr, w_replstr,
                             space.newint(maxcount))

@cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t, rffi.INT_real],
             rffi.INT_real, error=-1)
def PyUnicode_Tailmatch(space, w_str, w_substr, start, end, direction):
    """Return 1 if substr matches str[start:end] at the given tail end
    (direction == -1 means to do a prefix match, direction == 1 a
    suffix match), 0 otherwise. Return -1 if an error occurred."""
    space.utf8_w(w_str)  # type check
    space.utf8_w(w_substr)
    w_start = space.newint(start)
    w_end = space.newint(end)
    if rffi.cast(lltype.Signed, direction) <= 0:
        w_result = space.call_method(
            w_str, "startswith", w_substr, w_start, w_end)
    else:
        w_result = space.call_method(
            w_str, "endswith", w_substr, w_start, w_end)
    return space.int_w(w_result)

@cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t], Py_ssize_t, error=-1)
def PyUnicode_Count(space, w_str, w_substr, start, end):
    """Return the number of non-overlapping occurrences of substr in
    str[start:end].  Return -1 if an error occurred."""
    w_count = space.call_method(w_str, "count", w_substr,
                                space.newint(start), space.newint(end))
    return space.int_w(w_count)

@cpython_api([PyObject, PyObject, Py_ssize_t, Py_ssize_t, rffi.INT_real],
             Py_ssize_t, error=-2)
def PyUnicode_Find(space, w_str, w_substr, start, end, direction):
    """Return the first position of substr in str*[*start:end] using
    the given direction (direction == 1 means to do a forward search,
    direction == -1 a backward search).  The return value is the index
    of the first match; a value of -1 indicates that no match was
    found, and -2 indicates that an error occurred and an exception
    has been set."""
    if rffi.cast(lltype.Signed, direction) > 0:
        w_pos = space.call_method(w_str, "find", w_substr,
                                  space.newint(start), space.newint(end))
    else:
        w_pos = space.call_method(w_str, "rfind", w_substr,
                                  space.newint(start), space.newint(end))
    return space.int_w(w_pos)

@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
def PyUnicode_Contains(space, w_str, w_substr):
    """Check whether element is contained in container and return true or false
    accordingly.

    element has to coerce to a one element Unicode string. -1 is returned if
    there was an error."""
    if not space.isinstance_w(w_substr, space.w_unicode):
        raise oefmt(space.w_TypeError,
                    "in <string> requires string as left operand, not %T",
                     w_substr)
    if not space.isinstance_w(w_str, space.w_unicode):
        raise oefmt(space.w_TypeError, "must be str, not %T", w_str)
    return space.int_w(space.call_method(w_str, '__contains__', w_substr))

@cpython_api([PyObject, PyObject, Py_ssize_t], PyObject)
def PyUnicode_Split(space, w_str, w_sep, maxsplit):
    """Split a string giving a list of Unicode strings.  If sep is
    NULL, splitting will be done at all whitespace substrings.
    Otherwise, splits occur at the given separator.  At most maxsplit
    splits will be done.  If negative, no limit is set.  Separators
    are not included in the resulting list."""
    if w_sep is None:
        w_sep = space.w_None
    return space.call_method(w_str, "split", w_sep, space.newint(maxsplit))

@cpython_api([PyObject, rffi.INT_real], PyObject)
def PyUnicode_Splitlines(space, w_str, keepend):
    """Split a Unicode string at line breaks, returning a list of
    Unicode strings.  CRLF is considered to be one line break.  If
    keepend is 0, the Line break characters are not included in the
    resulting strings."""
    w_keepend = space.newbool(bool(rffi.cast(lltype.Signed, keepend)))
    return space.call_method(w_str, "splitlines", w_keepend)

@cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject)
def PyUnicode_Substring(space, w_str, start, end):
    return space.call_method(w_str, '__getitem__',
                         space.newslice(space.newint(start), space.newint(end),
                                        space.newint(1)))

@cts.decl("Py_UCS4 *PyUnicode_AsUCS4(PyObject *u, Py_UCS4 *buffer, Py_ssize_t buflen, int copy_null)")
def PyUnicode_AsUCS4(space, w_obj, pbuffer, buflen, copy_null):
    from pypy.module._codecs.locale import _utf82rawwcharp_loop
    # Use the underlying RPython object
    utf8 = space.utf8_w(w_obj)
    ulen = space.len_w(w_obj)
    if rffi.cast(lltype.Signed, copy_null):
        ulen += 1
    if not pbuffer:   # internal, for PyUnicode_AsUCS4Copy()
        pbuffer = lltype.malloc(rffi.CArray(Py_UCS4), ulen,
                                flavor='raw', track_allocation=False)
    elif buflen < ulen:
        raise oefmt(space.w_SystemError, "PyUnicode_AsUCS4: buflen too short")
    u_iter = rutf8.Utf8StringIterator(utf8)
    count = 0
    for oc in u_iter:
        pbuffer[count] = rffi.cast(Py_UCS4, oc)
        count += 1
    if rffi.cast(lltype.Signed, copy_null):
        pbuffer[count] = rffi.cast(Py_UCS4, 0)
    return pbuffer


@cts.decl("Py_UCS4 *PyUnicode_AsUCS4Copy(PyObject *u)")
def PyUnicode_AsUCS4Copy(space, ref):
    return PyUnicode_AsUCS4(space, ref, cts.cast('Py_UCS4*', 0), 0,
                            rffi.cast(rffi.INT_real, 1))

@cts.decl("PyObject* PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)",
          result_is_ll=True)
def PyUnicode_New(space, size, maxchar):
    PyASCIIObject = cts.gettype('PyASCIIObject')
    PyCompactUnicodeObject = cts.gettype('PyCompactUnicodeObject')
    PyCompactUnicodeObject = cts.gettype('PyCompactUnicodeObject')

    is_ascii = False
    is_sharing = False
    is_compact = False
    maxchar = widen(maxchar)
    struct_size = rffi.sizeof(PyCompactUnicodeObject)
    if maxchar < 128:
        kind = _1BYTE_KIND
        char_size = 1
        is_ascii = True
        is_compact = True
        struct_size = rffi.sizeof(PyASCIIObject)
    elif maxchar < 256:
        kind = _1BYTE_KIND
        char_size = 1
        is_compact = True
    elif maxchar < 65536:
        kind = _2BYTE_KIND
        char_size = 2
        struct_size = rffi.sizeof(PyUnicodeObject.TO)
        if rffi.sizeof(lltype.UniChar) == 2:
            is_sharing = True
    else:
        if maxchar > rutf8.MAXUNICODE:
            raise oefmt(space.w_SystemError,
                        "invalid maximum character passed to PyUnicode_New")
        kind = _4BYTE_KIND
        char_size = 4
        struct_size = rffi.sizeof(PyUnicodeObject.TO)
        if rffi.sizeof(lltype.UniChar) == 4:
            is_sharing = True

    # Ensure we won't overflow the size.
    if size < 0:
        raise oefmt(space.w_SystemError,
                    "Negative size passed to PyUnicode_New")
    if size > ((sys.maxint - struct_size) / char_size - 1):
        raise oefmt(space.w_MemoryError, "PyUnicode_New: size too big")

    # Duplicated allocation code from _PyObject_New() instead of a call to
    # PyObject_New() so we are able to allocate space for the object and
    # its data buffer.
    pytype = as_pyobj(space, space.w_unicode)
    pytype = rffi.cast(PyTypeObjectPtr, pytype)
    buf = lltype.malloc(rffi.VOIDP.TO, struct_size + (size + 1) * char_size,
                        flavor='raw', zero=True,
                        add_memory_pressure=True)
    pyobj = rffi.cast(PyObject, buf)
    pyobj.c_ob_refcnt = 1
    pyvarobj = rffi.cast(PyVarObject, pyobj)
    pyvarobj.c_ob_size = size
    #pyobj.c_ob_pypy_link remains null for now
    pyobj.c_ob_type = pytype

    set_len(pyobj, size)
    set_kind(pyobj, kind)
    set_compact(pyobj, is_compact)
    if is_compact:
        set_ascii(pyobj, is_ascii)
    else:
        unicode_size = rffi.sizeof(PyUnicodeObject.TO)
        data = rffi.ptradd(rffi.cast(rffi.CCHARP, pyobj), unicode_size)
        set_data(pyobj, cts.cast('void *', data))
    if is_sharing:
        set_wbuffer(pyobj, rffi.cast(rffi.CWCHARP, get_data(pyobj)))
    if not is_ascii:
        set_wsize(pyobj, size)
    set_ready(pyobj, True)
    return pyobj

@cts.decl("""Py_ssize_t PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
          Py_ssize_t start, Py_ssize_t end, int direction)""", error=-1)
def PyUnicode_FindChar(space, ref, ch, start, end, direction):
    if not pyunicode_check(ref):
        PyErr_BadArgument(space)
    w_str = from_ref(space, ref)
    ch = widen(ch)
    if ch > rutf8.MAXUNICODE:
        raise oefmt(space.w_ValueError, "character out of range")
    w_ch = space.newtext(rutf8.unichr_as_utf8(r_uint(ch)), 1)
    if rffi.cast(lltype.Signed, direction) > 0:
        w_pos = space.call_method(w_str, "find", w_ch,
                                  space.newint(start), space.newint(end))
    else:
        w_pos = space.call_method(w_str, "rfind", w_ch,
                                  space.newint(start), space.newint(end))
    return space.int_w(w_pos)

@cts.decl("Py_UCS4 PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)", error=-1)
def PyUnicode_ReadChar(space, ref, index):
    if not pyunicode_check(ref):
        PyErr_BadArgument(space)
    if not get_ready(ref):
        PyErr_BadArgument(space)
    if index < 0 or index > get_len(ref):
        raise oefmt(space.w_IndexError, "string index out of range")
    w_obj = from_ref(space, ref)
    w_ch = space.getitem(w_obj, space.newint(index))
    return space.int_w(space.ord(w_ch))

@cts.decl("int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)", error=-1)
def PyUnicode_WriteChar(space, ref, index, ch):
    """ Write a single ch at index before ref is ready. In order for this to
    succeed:
    - ch and ref[index] when converted to utf8 must be the same length
    - ref must not have a RPython object
    - ref must not have been processed by _PyUnicode_Ready
    """
    if not pyunicode_check(ref):
        PyErr_BadArgument(space)
    if index < 0 or index > get_len(ref):
        raise oefmt(space.w_IndexError, "string index out of range")
    if get_ready(ref):
        raise oefmt(space.w_SystemError, "Cannot modify a string currently used")
    if not has_utf8_memory(ref):
        raise oefmt(space.w_SystemError, "Cannot modify a string currently used")
    # this is rarithetic.r_uint, not rffi.r_uint
    ch = r_uint(ch)
    if ch > rutf8.MAXUNICODE:
        raise oefmt(space.w_ValueError, "character out of range")
    if get_compact(ref):
        raise oefmt(space.w_SystemError, "Cannot modify compact via PyUnicode_WriteChar")
    utf8 = get_utf8(ref)
    as_str = rffi.charp2str(utf8)
    start = 0
    if index > 0:
        start = rutf8.next_codepoint_pos(as_str, index - 1)
    end = rutf8.next_codepoint_pos(as_str, index)
    ch_as_utf8 = rutf8.unichr_as_utf8(ch)
    if len(ch_as_utf8) != end - start:
        raise oefmt(space.w_ValueError,
                    'cannot write ch to string, would need to reallocate')
    j = 0
    for i in range(start, end):
        utf8[i] = ch_as_utf8[j]
        j += 1
    return 0

@cpython_api([PyObjectP, PyObject], lltype.Void)
def PyUnicode_Append(space, p_left, right):
    state = space.fromcache(State)
    operror = state.get_exception()
    if not p_left:
        if operror is not None:
            PyErr_BadInternalCall(space)
        return
    left = p_left[0]
    if not left or not right or not pyunicode_check(left) or not pyunicode_check(right):
        if operror is not None:
            PyErr_BadInternalCall(space)
        p_left[0] = rffi.cast(PyObject, 0)
        return
    w_left = from_ref(space, left)
    w_right = from_ref(space, right)
    w_append = space.add(w_left, w_right)
    p_left[0] = make_ref(space, w_append)

@cpython_api([PyObject], PyObject)
def PyUnicode_AsRawUnicodeEscapeString(space, w_obj):
    """Encode a Unicode object using Raw-Unicode-Escape and return the result as
    Python string object. Error handling is "strict". Return NULL if an exception
    was raised by the codec."""
    utf8 = space.utf8_w(w_obj)
    ulen = space.len_w(w_obj)
    res = utf8_encode_raw_unicode_escape(space, utf8, w_obj, "strict",  None)
    return space.newbytes(res)


@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
def PyUnicode_DecodeRawUnicodeEscape(space, p_string, length, p_errors):
    if not p_string:
            PyErr_BadInternalCall(space)
    if not p_errors:
        errors = "strict"
    else:
        errors = rffi.charp2str(p_errors)
    b = rffi.charpsize2str(p_string, length)
    w_b = space.newbytes(b)
    state = space.fromcache(CodecState)
    eh = state.decode_error_handler
    s, u_len, _ = str_decode_raw_unicode_escape(space, b, w_b, errors, True, eh)
    return space.newtext(s, u_len)