forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunicode.c
1938 lines (1666 loc) · 62.9 KB
/
unicode.c
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
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
// Need limited C API 3.13 to test PyUnicode_EqualToUTF8()
# define Py_LIMITED_API 0x030d0000
#endif
#include "parts.h"
#include "util.h"
#include <stddef.h> // ptrdiff_t
#include <string.h> // memset()
static PyObject *
codec_incrementalencoder(PyObject *self, PyObject *args)
{
const char *encoding, *errors = NULL;
if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
&encoding, &errors))
return NULL;
return PyCodec_IncrementalEncoder(encoding, errors);
}
static PyObject *
codec_incrementaldecoder(PyObject *self, PyObject *args)
{
const char *encoding, *errors = NULL;
if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
&encoding, &errors))
return NULL;
return PyCodec_IncrementalDecoder(encoding, errors);
}
static PyObject *
test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
int result;
if (py_s == NULL)
return NULL;
result = PyUnicode_CompareWithASCIIString(py_s, "str");
Py_DECREF(py_s);
if (!result) {
PyErr_SetString(PyExc_AssertionError, "Python string ending in NULL "
"should not compare equal to c string.");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
{
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
size_t wtextlen = 1;
const wchar_t invalid[1] = {(wchar_t)0x110000u};
#else
const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
size_t wtextlen = 2;
#endif
PyObject *wide, *utf8;
wide = PyUnicode_FromWideChar(wtext, wtextlen);
if (wide == NULL)
return NULL;
utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
if (utf8 == NULL) {
Py_DECREF(wide);
return NULL;
}
if (PyUnicode_GetLength(wide) != PyUnicode_GetLength(utf8)) {
Py_DECREF(wide);
Py_DECREF(utf8);
PyErr_SetString(PyExc_AssertionError,
"test_widechar: "
"wide string and utf8 string "
"have different length");
return NULL;
}
if (PyUnicode_Compare(wide, utf8)) {
Py_DECREF(wide);
Py_DECREF(utf8);
if (PyErr_Occurred())
return NULL;
PyErr_SetString(PyExc_AssertionError,
"test_widechar: "
"wide string and utf8 string "
"are different");
return NULL;
}
Py_DECREF(wide);
Py_DECREF(utf8);
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
wide = PyUnicode_FromWideChar(invalid, 1);
if (wide == NULL)
PyErr_Clear();
else {
PyErr_SetString(PyExc_AssertionError,
"test_widechar: "
"PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
return NULL;
}
#endif
Py_RETURN_NONE;
}
static PyObject *
unicode_copy(PyObject *unicode)
{
if (!unicode) {
return NULL;
}
if (!PyUnicode_Check(unicode)) {
Py_INCREF(unicode);
return unicode;
}
// Create a new string by encoding to UTF-8 and then decode from UTF-8
PyObject *utf8 = PyUnicode_AsUTF8String(unicode);
if (!utf8) {
return NULL;
}
PyObject *copy = PyUnicode_DecodeUTF8(
PyBytes_AsString(utf8),
PyBytes_Size(utf8),
NULL);
Py_DECREF(utf8);
return copy;
}
/* Test PyUnicode_WriteChar() */
static PyObject *
unicode_writechar(PyObject *self, PyObject *args)
{
PyObject *to, *to_copy;
Py_ssize_t index;
unsigned int character;
int result;
if (!PyArg_ParseTuple(args, "OnI", &to, &index, &character)) {
return NULL;
}
NULLABLE(to);
if (!(to_copy = unicode_copy(to)) && to) {
return NULL;
}
result = PyUnicode_WriteChar(to_copy, index, (Py_UCS4)character);
if (result == -1 && PyErr_Occurred()) {
Py_DECREF(to_copy);
return NULL;
}
return Py_BuildValue("(Ni)", to_copy, result);
}
static void
unicode_fill(PyObject *str, Py_ssize_t start, Py_ssize_t end, Py_UCS4 ch)
{
assert(0 <= start);
assert(end <= PyUnicode_GetLength(str));
for (Py_ssize_t i = start; i < end; i++) {
int res = PyUnicode_WriteChar(str, i, ch);
assert(res == 0);
}
}
/* Test PyUnicode_Resize() */
static PyObject *
unicode_resize(PyObject *self, PyObject *args)
{
PyObject *obj, *copy;
Py_ssize_t length;
int result;
if (!PyArg_ParseTuple(args, "On", &obj, &length)) {
return NULL;
}
NULLABLE(obj);
if (!(copy = unicode_copy(obj)) && obj) {
return NULL;
}
result = PyUnicode_Resize(©, length);
if (result == -1 && PyErr_Occurred()) {
Py_XDECREF(copy);
return NULL;
}
if (obj && PyUnicode_Check(obj) && length > PyUnicode_GetLength(obj)) {
unicode_fill(copy, PyUnicode_GetLength(obj), length, 0U);
}
return Py_BuildValue("(Ni)", copy, result);
}
/* Test PyUnicode_Append() */
static PyObject *
unicode_append(PyObject *self, PyObject *args)
{
PyObject *left, *right, *left_copy;
if (!PyArg_ParseTuple(args, "OO", &left, &right))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (!(left_copy = unicode_copy(left)) && left) {
return NULL;
}
PyUnicode_Append(&left_copy, right);
return left_copy;
}
/* Test PyUnicode_AppendAndDel() */
static PyObject *
unicode_appendanddel(PyObject *self, PyObject *args)
{
PyObject *left, *right, *left_copy;
if (!PyArg_ParseTuple(args, "OO", &left, &right))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (!(left_copy = unicode_copy(left)) && left) {
return NULL;
}
Py_XINCREF(right);
PyUnicode_AppendAndDel(&left_copy, right);
return left_copy;
}
/* Test PyUnicode_FromStringAndSize() */
static PyObject *
unicode_fromstringandsize(PyObject *self, PyObject *args)
{
const char *s;
Py_ssize_t bsize;
Py_ssize_t size = -100;
if (!PyArg_ParseTuple(args, "z#|n", &s, &bsize, &size)) {
return NULL;
}
if (size == -100) {
size = bsize;
}
return PyUnicode_FromStringAndSize(s, size);
}
/* Test PyUnicode_FromString() */
static PyObject *
unicode_fromstring(PyObject *self, PyObject *arg)
{
const char *s;
Py_ssize_t size;
if (!PyArg_Parse(arg, "z#", &s, &size)) {
return NULL;
}
return PyUnicode_FromString(s);
}
/* Test PyUnicode_Substring() */
static PyObject *
unicode_substring(PyObject *self, PyObject *args)
{
PyObject *str;
Py_ssize_t start, end;
if (!PyArg_ParseTuple(args, "Onn", &str, &start, &end)) {
return NULL;
}
NULLABLE(str);
return PyUnicode_Substring(str, start, end);
}
/* Test PyUnicode_GetLength() */
static PyObject *
unicode_getlength(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
RETURN_SIZE(PyUnicode_GetLength(arg));
}
/* Test PyUnicode_ReadChar() */
static PyObject *
unicode_readchar(PyObject *self, PyObject *args)
{
PyObject *unicode;
Py_ssize_t index;
Py_UCS4 result;
if (!PyArg_ParseTuple(args, "On", &unicode, &index)) {
return NULL;
}
NULLABLE(unicode);
result = PyUnicode_ReadChar(unicode, index);
if (result == (Py_UCS4)-1)
return NULL;
return PyLong_FromUnsignedLong(result);
}
/* Test PyUnicode_FromEncodedObject() */
static PyObject *
unicode_fromencodedobject(PyObject *self, PyObject *args)
{
PyObject *obj;
const char *encoding;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "Oz|z", &obj, &encoding, &errors)) {
return NULL;
}
NULLABLE(obj);
return PyUnicode_FromEncodedObject(obj, encoding, errors);
}
/* Test PyUnicode_FromObject() */
static PyObject *
unicode_fromobject(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_FromObject(arg);
}
/* Test PyUnicode_InternInPlace() */
static PyObject *
unicode_interninplace(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
Py_XINCREF(arg);
PyUnicode_InternInPlace(&arg);
return arg;
}
/* Test PyUnicode_InternFromString() */
static PyObject *
unicode_internfromstring(PyObject *self, PyObject *arg)
{
const char *s;
Py_ssize_t size;
if (!PyArg_Parse(arg, "z#", &s, &size)) {
return NULL;
}
return PyUnicode_InternFromString(s);
}
/* Test PyUnicode_FromWideChar() */
static PyObject *
unicode_fromwidechar(PyObject *self, PyObject *args)
{
const char *s;
Py_ssize_t bsize;
Py_ssize_t size = -100;
if (!PyArg_ParseTuple(args, "z#|n", &s, &bsize, &size)) {
return NULL;
}
if (size == -100) {
if (bsize % SIZEOF_WCHAR_T) {
PyErr_SetString(PyExc_AssertionError,
"invalid size in unicode_fromwidechar()");
return NULL;
}
size = bsize / SIZEOF_WCHAR_T;
}
return PyUnicode_FromWideChar((const wchar_t *)s, size);
}
/* Test PyUnicode_AsWideChar() */
static PyObject *
unicode_aswidechar(PyObject *self, PyObject *args)
{
PyObject *unicode, *result;
Py_ssize_t buflen, size;
wchar_t *buffer;
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
return NULL;
NULLABLE(unicode);
buffer = PyMem_New(wchar_t, buflen);
if (buffer == NULL)
return PyErr_NoMemory();
size = PyUnicode_AsWideChar(unicode, buffer, buflen);
if (size == -1) {
PyMem_Free(buffer);
return NULL;
}
if (size < buflen)
buflen = size + 1;
else
buflen = size;
result = PyUnicode_FromWideChar(buffer, buflen);
PyMem_Free(buffer);
if (result == NULL)
return NULL;
return Py_BuildValue("(Nn)", result, size);
}
/* Test PyUnicode_AsWideCharString() with NULL as buffer */
static PyObject *
unicode_aswidechar_null(PyObject *self, PyObject *args)
{
PyObject *unicode;
Py_ssize_t buflen;
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
return NULL;
NULLABLE(unicode);
RETURN_SIZE(PyUnicode_AsWideChar(unicode, NULL, buflen));
}
/* Test PyUnicode_AsWideCharString() */
static PyObject *
unicode_aswidecharstring(PyObject *self, PyObject *args)
{
PyObject *unicode, *result;
Py_ssize_t size = UNINITIALIZED_SIZE;
wchar_t *buffer;
if (!PyArg_ParseTuple(args, "O", &unicode))
return NULL;
NULLABLE(unicode);
buffer = PyUnicode_AsWideCharString(unicode, &size);
if (buffer == NULL) {
assert(size == UNINITIALIZED_SIZE);
return NULL;
}
result = PyUnicode_FromWideChar(buffer, size + 1);
PyMem_Free(buffer);
if (result == NULL)
return NULL;
return Py_BuildValue("(Nn)", result, size);
}
/* Test PyUnicode_AsWideCharString() with NULL as the size address */
static PyObject *
unicode_aswidecharstring_null(PyObject *self, PyObject *args)
{
PyObject *unicode, *result;
wchar_t *buffer;
if (!PyArg_ParseTuple(args, "O", &unicode))
return NULL;
NULLABLE(unicode);
buffer = PyUnicode_AsWideCharString(unicode, NULL);
if (buffer == NULL)
return NULL;
result = PyUnicode_FromWideChar(buffer, -1);
PyMem_Free(buffer);
if (result == NULL)
return NULL;
return result;
}
/* Test PyUnicode_FromOrdinal() */
static PyObject *
unicode_fromordinal(PyObject *self, PyObject *args)
{
int ordinal;
if (!PyArg_ParseTuple(args, "i", &ordinal))
return NULL;
return PyUnicode_FromOrdinal(ordinal);
}
/* Test PyUnicode_AsUTF8AndSize() */
static PyObject *
unicode_asutf8andsize(PyObject *self, PyObject *args)
{
PyObject *unicode;
Py_ssize_t buflen;
const char *s;
Py_ssize_t size = UNINITIALIZED_SIZE;
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
return NULL;
NULLABLE(unicode);
s = PyUnicode_AsUTF8AndSize(unicode, &size);
if (s == NULL) {
assert(size == -1);
return NULL;
}
return Py_BuildValue("(y#n)", s, buflen, size);
}
/* Test PyUnicode_AsUTF8AndSize() with NULL as the size address */
static PyObject *
unicode_asutf8andsize_null(PyObject *self, PyObject *args)
{
PyObject *unicode;
Py_ssize_t buflen;
const char *s;
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
return NULL;
NULLABLE(unicode);
s = PyUnicode_AsUTF8AndSize(unicode, NULL);
if (s == NULL)
return NULL;
return PyBytes_FromStringAndSize(s, buflen);
}
/* Test PyUnicode_GetDefaultEncoding() */
static PyObject *
unicode_getdefaultencoding(PyObject *self, PyObject *Py_UNUSED(ignored))
{
const char *s = PyUnicode_GetDefaultEncoding();
if (s == NULL)
return NULL;
return PyBytes_FromString(s);
}
/* Test PyUnicode_Decode() */
static PyObject *
unicode_decode(PyObject *self, PyObject *args)
{
const char *s;
Py_ssize_t size;
const char *encoding;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#z|z", &s, &size, &encoding, &errors))
return NULL;
return PyUnicode_Decode(s, size, encoding, errors);
}
/* Test PyUnicode_AsEncodedString() */
static PyObject *
unicode_asencodedstring(PyObject *self, PyObject *args)
{
PyObject *unicode;
const char *encoding;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "Oz|z", &unicode, &encoding, &errors))
return NULL;
NULLABLE(unicode);
return PyUnicode_AsEncodedString(unicode, encoding, errors);
}
/* Test PyUnicode_BuildEncodingMap() */
static PyObject *
unicode_buildencodingmap(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_BuildEncodingMap(arg);
}
/* Test PyUnicode_DecodeUTF7() */
static PyObject *
unicode_decodeutf7(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeUTF7(data, size, errors);
}
/* Test PyUnicode_DecodeUTF7Stateful() */
static PyObject *
unicode_decodeutf7stateful(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF7Stateful(data, size, errors, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(Nn)", result, consumed);
}
/* Test PyUnicode_DecodeUTF8() */
static PyObject *
unicode_decodeutf8(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeUTF8(data, size, errors);
}
/* Test PyUnicode_DecodeUTF8Stateful() */
static PyObject *
unicode_decodeutf8stateful(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF8Stateful(data, size, errors, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(Nn)", result, consumed);
}
/* Test PyUnicode_AsUTF8String() */
static PyObject *
unicode_asutf8string(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsUTF8String(arg);
}
/* Test PyUnicode_DecodeUTF32() */
static PyObject *
unicode_decodeutf32(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
int byteorder = UNINITIALIZED_INT;
PyObject *result;
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF32(data, size, errors, &byteorder);
if (!result) {
return NULL;
}
return Py_BuildValue("(iN)", byteorder, result);
}
/* Test PyUnicode_DecodeUTF32Stateful() */
static PyObject *
unicode_decodeutf32stateful(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
int byteorder = UNINITIALIZED_INT;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(iNn)", byteorder, result, consumed);
}
/* Test PyUnicode_AsUTF32String() */
static PyObject *
unicode_asutf32string(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsUTF32String(arg);
}
/* Test PyUnicode_DecodeUTF16() */
static PyObject *
unicode_decodeutf16(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
int byteorder = UNINITIALIZED_INT;
PyObject *result;
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF16(data, size, errors, &byteorder);
if (!result) {
return NULL;
}
return Py_BuildValue("(iN)", byteorder, result);
}
/* Test PyUnicode_DecodeUTF16Stateful() */
static PyObject *
unicode_decodeutf16stateful(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
int byteorder = UNINITIALIZED_INT;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(iNn)", byteorder, result, consumed);
}
/* Test PyUnicode_AsUTF16String() */
static PyObject *
unicode_asutf16string(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsUTF16String(arg);
}
/* Test PyUnicode_DecodeUnicodeEscape() */
static PyObject *
unicode_decodeunicodeescape(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeUnicodeEscape(data, size, errors);
}
/* Test PyUnicode_AsUnicodeEscapeString() */
static PyObject *
unicode_asunicodeescapestring(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsUnicodeEscapeString(arg);
}
static PyObject *
unicode_decoderawunicodeescape(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeRawUnicodeEscape(data, size, errors);
}
/* Test PyUnicode_AsRawUnicodeEscapeString() */
static PyObject *
unicode_asrawunicodeescapestring(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsRawUnicodeEscapeString(arg);
}
static PyObject *
unicode_decodelatin1(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeLatin1(data, size, errors);
}
/* Test PyUnicode_AsLatin1String() */
static PyObject *
unicode_aslatin1string(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsLatin1String(arg);
}
/* Test PyUnicode_DecodeASCII() */
static PyObject *
unicode_decodeascii(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeASCII(data, size, errors);
}
/* Test PyUnicode_AsASCIIString() */
static PyObject *
unicode_asasciistring(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsASCIIString(arg);
}
/* Test PyUnicode_DecodeCharmap() */
static PyObject *
unicode_decodecharmap(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
PyObject *mapping;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#O|z", &data, &size, &mapping, &errors))
return NULL;
NULLABLE(mapping);
return PyUnicode_DecodeCharmap(data, size, mapping, errors);
}
/* Test PyUnicode_AsCharmapString() */
static PyObject *
unicode_ascharmapstring(PyObject *self, PyObject *args)
{
PyObject *unicode;
PyObject *mapping;
if (!PyArg_ParseTuple(args, "OO", &unicode, &mapping))
return NULL;
NULLABLE(unicode);
NULLABLE(mapping);
return PyUnicode_AsCharmapString(unicode, mapping);
}
#ifdef MS_WINDOWS
/* Test PyUnicode_DecodeMBCS() */
static PyObject *
unicode_decodembcs(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeMBCS(data, size, errors);
}
/* Test PyUnicode_DecodeMBCSStateful() */
static PyObject *
unicode_decodembcsstateful(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeMBCSStateful(data, size, errors, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(Nn)", result, consumed);
}
/* Test PyUnicode_DecodeCodePageStateful() */
static PyObject *
unicode_decodecodepagestateful(PyObject *self, PyObject *args)
{
int code_page;
const char *data;
Py_ssize_t size;
const char *errors = NULL;
Py_ssize_t consumed = UNINITIALIZED_SIZE;
PyObject *result;
if (!PyArg_ParseTuple(args, "iy#|z", &code_page, &data, &size, &errors))
return NULL;
result = PyUnicode_DecodeCodePageStateful(code_page, data, size, errors, &consumed);
if (!result) {
assert(consumed == UNINITIALIZED_SIZE);
return NULL;
}
return Py_BuildValue("(Nn)", result, consumed);
}
/* Test PyUnicode_AsMBCSString() */
static PyObject *
unicode_asmbcsstring(PyObject *self, PyObject *arg)
{
NULLABLE(arg);
return PyUnicode_AsMBCSString(arg);
}
/* Test PyUnicode_EncodeCodePage() */
static PyObject *
unicode_encodecodepage(PyObject *self, PyObject *args)
{
int code_page;
PyObject *unicode;
const char *errors;
if (!PyArg_ParseTuple(args, "iO|z", &code_page, &unicode, &errors))
return NULL;
NULLABLE(unicode);
return PyUnicode_EncodeCodePage(code_page, unicode, errors);
}
#endif /* MS_WINDOWS */
/* Test PyUnicode_DecodeLocaleAndSize() */
static PyObject *
unicode_decodelocaleandsize(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeLocaleAndSize(data, size, errors);
}
/* Test PyUnicode_DecodeLocale() */
static PyObject *
unicode_decodelocale(PyObject *self, PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors;
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
return NULL;
return PyUnicode_DecodeLocale(data, errors);
}
/* Test PyUnicode_EncodeLocale() */
static PyObject *
unicode_encodelocale(PyObject *self, PyObject *args)
{
PyObject *unicode;
const char *errors;
if (!PyArg_ParseTuple(args, "O|z", &unicode, &errors))
return NULL;
NULLABLE(unicode);
return PyUnicode_EncodeLocale(unicode, errors);
}
/* Test PyUnicode_DecodeFSDefault() */