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
|
/* ------------------------------------------------------------------------
_codecs -- Provides access to the codec registry and the builtin
codecs.
This module should never be imported directly. The standard library
module "codecs" wraps this builtin module for use within Python.
The codec registry is accessible via:
register(search_function) -> None
lookup(encoding) -> CodecInfo object
The builtin Unicode codecs use the following interface:
<encoding>_encode(Unicode_object[,errors='strict']) ->
(string object, bytes consumed)
<encoding>_decode(char_buffer_obj[,errors='strict']) ->
(Unicode object, bytes consumed)
<encoding>_encode() interfaces also accept non-Unicode object as
input. The objects are then converted to Unicode using
PyUnicode_FromObject() prior to applying the conversion.
These <encoding>s are available: utf_8, unicode_escape,
raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
mbcs (on win32).
Written by Marc-Andre Lemburg (mal@lemburg.com).
Copyright (c) Corporation for National Research Initiatives.
------------------------------------------------------------------------ */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifdef MS_WINDOWS
#include <windows.h>
#endif
/*[clinic input]
module _codecs
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/
#include "clinic/_codecsmodule.c.h"
/* --- Registry ----------------------------------------------------------- */
/*[clinic input]
_codecs.register
search_function: object
/
Register a codec search function.
Search functions are expected to take one argument, the encoding name in
all lower case letters, and either return None, or a tuple of functions
(encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object).
[clinic start generated code]*/
static PyObject *
_codecs_register(PyModuleDef *module, PyObject *search_function)
/*[clinic end generated code: output=d17608b6ad380eb8 input=369578467955cae4]*/
{
if (PyCodec_Register(search_function))
return NULL;
Py_RETURN_NONE;
}
/*[clinic input]
_codecs.lookup
encoding: str
/
Looks up a codec tuple in the Python codec registry and returns a CodecInfo object.
[clinic start generated code]*/
static PyObject *
_codecs_lookup_impl(PyModuleDef *module, const char *encoding)
/*[clinic end generated code: output=798e41aff0c04ef6 input=3c572c0db3febe9c]*/
{
return _PyCodec_Lookup(encoding);
}
/*[clinic input]
_codecs.encode
obj: object
encoding: str(c_default="NULL") = sys.getdefaultencoding()
errors: str(c_default="NULL") = "strict"
Encodes obj using the codec registered for encoding.
encoding defaults to the default encoding. errors may be given to set a
different error handling scheme. Default is 'strict' meaning that encoding
errors raise a ValueError. Other possible values are 'ignore', 'replace'
and 'backslashreplace' as well as any other name registered with
codecs.register_error that can handle ValueErrors.
[clinic start generated code]*/
static PyObject *
_codecs_encode_impl(PyModuleDef *module, PyObject *obj, const char *encoding,
const char *errors)
/*[clinic end generated code: output=5c073f62249c8d7c input=2440d769df020a0e]*/
{
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
return PyCodec_Encode(obj, encoding, errors);
}
/*[clinic input]
_codecs.decode
obj: object
encoding: str(c_default="NULL") = sys.getdefaultencoding()
errors: str(c_default="NULL") = "strict"
Decodes obj using the codec registered for encoding.
encoding defaults to the default encoding. errors may be given to set a
different error handling scheme. Default is 'strict' meaning that encoding
errors raise a ValueError. Other possible values are 'ignore', 'replace'
and 'backslashreplace' as well as any other name registered with
codecs.register_error that can handle ValueErrors.
[clinic start generated code]*/
static PyObject *
_codecs_decode_impl(PyModuleDef *module, PyObject *obj, const char *encoding,
const char *errors)
/*[clinic end generated code: output=c81cbf6189a7f878 input=a351e5f5baad1544]*/
{
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
return PyCodec_Decode(obj, encoding, errors);
}
/* --- Helpers ------------------------------------------------------------ */
/*[clinic input]
_codecs._forget_codec
encoding: str
/
Purge the named codec from the internal codec lookup cache
[clinic start generated code]*/
static PyObject *
_codecs__forget_codec_impl(PyModuleDef *module, const char *encoding)
/*[clinic end generated code: output=b56a9b99d2d28080 input=18d5d92d0e386c38]*/
{
if (_PyCodec_Forget(encoding) < 0) {
return NULL;
};
Py_RETURN_NONE;
}
static
PyObject *codec_tuple(PyObject *decoded,
Py_ssize_t len)
{
if (decoded == NULL)
return NULL;
return Py_BuildValue("Nn", decoded, len);
}
/* --- String codecs ------------------------------------------------------ */
/*[clinic input]
_codecs.escape_decode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=648fa3e78d03e658 input=0018edfd99db714d]*/
{
PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len,
errors, 0, NULL);
return codec_tuple(decoded, data->len);
}
/*[clinic input]
_codecs.escape_encode
data: object(subclass_of='&PyBytes_Type')
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_escape_encode_impl(PyModuleDef *module, PyObject *data,
const char *errors)
/*[clinic end generated code: output=fcd6f34fe4111c50 input=da9ded00992f32f2]*/
{
Py_ssize_t size;
Py_ssize_t newsize;
PyObject *v;
size = PyBytes_GET_SIZE(data);
if (size > PY_SSIZE_T_MAX / 4) {
PyErr_SetString(PyExc_OverflowError,
"string is too large to encode");
return NULL;
}
newsize = 4*size;
v = PyBytes_FromStringAndSize(NULL, newsize);
if (v == NULL) {
return NULL;
}
else {
Py_ssize_t i;
char c;
char *p = PyBytes_AS_STRING(v);
for (i = 0; i < size; i++) {
/* There's at least enough room for a hex escape */
assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
c = PyBytes_AS_STRING(data)[i];
if (c == '\'' || c == '\\')
*p++ = '\\', *p++ = c;
else if (c == '\t')
*p++ = '\\', *p++ = 't';
else if (c == '\n')
*p++ = '\\', *p++ = 'n';
else if (c == '\r')
*p++ = '\\', *p++ = 'r';
else if (c < ' ' || c >= 0x7f) {
*p++ = '\\';
*p++ = 'x';
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
*p++ = Py_hexdigits[c & 0xf];
}
else
*p++ = c;
}
*p = '\0';
if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
return NULL;
}
}
return codec_tuple(v, size);
}
/* --- Decoder ------------------------------------------------------------ */
/*[clinic input]
_codecs.unicode_internal_decode
obj: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_unicode_internal_decode_impl(PyModuleDef *module, PyObject *obj,
const char *errors)
/*[clinic end generated code: output=9fe47c2cd8807d92 input=8d57930aeda170c6]*/
{
if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) < 0)
return NULL;
Py_INCREF(obj);
return codec_tuple(obj, PyUnicode_GET_LENGTH(obj));
}
else {
Py_buffer view;
PyObject *result;
if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
return NULL;
result = codec_tuple(
_PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors),
view.len);
PyBuffer_Release(&view);
return result;
}
}
/*[clinic input]
_codecs.utf_7_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_7_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=ca945e907e72e827 input=bc4d6247ecdb01e6]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len,
errors,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_8_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_8_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=7309f9ff4ef5c9b6 input=39161d71e7422ee2]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len,
errors,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_16_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=8d2fa0507d9bef2c input=f3cf01d1461007ce]*/
{
int byteorder = 0;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_16_le_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_le_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=4fd621515ef4ce18 input=a77e3bf97335d94e]*/
{
int byteorder = -1;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_16_be_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_be_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=792f4eacb3e1fa05 input=606f69fae91b5563]*/
{
int byteorder = 1;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/* This non-standard version also provides access to the byteorder
parameter of the builtin UTF-16 codec.
It returns a tuple (unicode, bytesread, byteorder) with byteorder
being the value in effect at the end of data.
*/
/*[clinic input]
_codecs.utf_16_ex_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_ex_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int byteorder, int final)
/*[clinic end generated code: output=f136a186dc2defa0 input=f6e7f697658c013e]*/
{
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
if (decoded == NULL)
return NULL;
return Py_BuildValue("Nni", decoded, consumed, byteorder);
}
/*[clinic input]
_codecs.utf_32_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=b7635e55857e8efb input=86d4f41c6c2e763d]*/
{
int byteorder = 0;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_32_le_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_le_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=a79d1787d8ddf988 input=d18b650772d188ba]*/
{
int byteorder = -1;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.utf_32_be_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_be_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=a8356b0f36779981 input=19c271b5d34926d8]*/
{
int byteorder = 1;
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/* This non-standard version also provides access to the byteorder
parameter of the builtin UTF-32 codec.
It returns a tuple (unicode, bytesread, byteorder) with byteorder
being the value in effect at the end of data.
*/
/*[clinic input]
_codecs.utf_32_ex_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_ex_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int byteorder, int final)
/*[clinic end generated code: output=ab8c70977c1992f5 input=4af3e6ccfe34a076]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
errors, &byteorder,
final ? NULL : &consumed);
if (decoded == NULL)
return NULL;
return Py_BuildValue("Nni", decoded, consumed, byteorder);
}
/*[clinic input]
_codecs.unicode_escape_decode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=d1aa63f2620c4999 input=49fd27d06813a7f5]*/
{
PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
errors);
return codec_tuple(decoded, data->len);
}
/*[clinic input]
_codecs.raw_unicode_escape_decode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_raw_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=0bf96cc182d81379 input=770903a211434ebc]*/
{
PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len,
errors);
return codec_tuple(decoded, data->len);
}
/*[clinic input]
_codecs.latin_1_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_latin_1_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=66b916f5055aaf13 input=5cad0f1759c618ec]*/
{
PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors);
return codec_tuple(decoded, data->len);
}
/*[clinic input]
_codecs.ascii_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_ascii_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=7f213a1b5cdafc65 input=ad1106f64037bd16]*/
{
PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors);
return codec_tuple(decoded, data->len);
}
/*[clinic input]
_codecs.charmap_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
mapping: object = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_charmap_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, PyObject *mapping)
/*[clinic end generated code: output=87d27f365098bbae input=19712ca35c5a80e2]*/
{
PyObject *decoded;
if (mapping == Py_None)
mapping = NULL;
decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors);
return codec_tuple(decoded, data->len);
}
#ifdef HAVE_MBCS
/*[clinic input]
_codecs.mbcs_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_mbcs_decode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=0ebaf3a5b20e53fa input=d492c1ca64f4fa8a]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len,
errors, final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
/*[clinic input]
_codecs.code_page_decode
codepage: int
data: Py_buffer
errors: str(accept={str, NoneType}) = NULL
final: int(c_default="0") = False
/
[clinic start generated code]*/
static PyObject *
_codecs_code_page_decode_impl(PyModuleDef *module, int codepage,
Py_buffer *data, const char *errors, int final)
/*[clinic end generated code: output=4318e3d9971e31ba input=4f3152a304e21d51]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage,
data->buf, data->len,
errors,
final ? NULL : &consumed);
return codec_tuple(decoded, consumed);
}
#endif /* HAVE_MBCS */
/* --- Encoder ------------------------------------------------------------ */
/*[clinic input]
_codecs.readbuffer_encode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_readbuffer_encode_impl(PyModuleDef *module, Py_buffer *data,
const char *errors)
/*[clinic end generated code: output=319cc24083299859 input=b7c322b89d4ab923]*/
{
PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len);
return codec_tuple(result, data->len);
}
/*[clinic input]
_codecs.unicode_internal_encode
obj: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_unicode_internal_encode_impl(PyModuleDef *module, PyObject *obj,
const char *errors)
/*[clinic end generated code: output=be08457068ad503b input=8628f0280cf5ba61]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"unicode_internal codec has been deprecated",
1))
return NULL;
if (PyUnicode_Check(obj)) {
Py_UNICODE *u;
Py_ssize_t len, size;
if (PyUnicode_READY(obj) < 0)
return NULL;
u = PyUnicode_AsUnicodeAndSize(obj, &len);
if (u == NULL)
return NULL;
if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
return PyErr_NoMemory();
size = len * sizeof(Py_UNICODE);
return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),
PyUnicode_GET_LENGTH(obj));
}
else {
Py_buffer view;
PyObject *result;
if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
return NULL;
result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len),
view.len);
PyBuffer_Release(&view);
return result;
}
}
/*[clinic input]
_codecs.utf_7_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_7_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=a7accc496a32b759 input=fd91a78f103b0421]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.utf_8_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ec831d80e7aedede input=2c22d40532f071f3]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/* This version provides access to the byteorder parameter of the
builtin UTF-16 codecs as optional third argument. It defaults to 0
which means: use the native byte order and prepend the data with a
BOM mark.
*/
/*[clinic input]
_codecs.utf_16_encode
str: object
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, int byteorder)
/*[clinic end generated code: output=93ac58e960a9ee4d input=3935a489b2d5385e]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.utf_16_le_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_le_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=422bedb8da34fb66 input=bc27df05d1d20dfe]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.utf_16_be_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=3aa7ee9502acdd77 input=5a69d4112763462b]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/* This version provides access to the byteorder parameter of the
builtin UTF-32 codecs as optional third argument. It defaults to 0
which means: use the native byte order and prepend the data with a
BOM mark.
*/
/*[clinic input]
_codecs.utf_32_encode
str: object
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, int byteorder)
/*[clinic end generated code: output=3e7d5a003b02baed input=434a1efa492b8d58]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.utf_32_le_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_le_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=5dda641cd33dbfc2 input=dfa2d7dc78b99422]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.utf_32_be_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_utf_32_be_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ccca8b44d91a7c7a input=4595617b18169002]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.unicode_escape_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=389f23d2b8f8d80b input=8273506f14076912]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.raw_unicode_escape_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_raw_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=fec4e39d6ec37a62 input=181755d5dfacef3c]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.latin_1_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_latin_1_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ecf00eb8e48c889c input=f03f6dcf1d84bee4]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.ascii_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_ascii_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=a9d18fc6b6b91cfb input=d87e25a10a593fee]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.charmap_encode
str: object
errors: str(accept={str, NoneType}) = NULL
mapping: object = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_charmap_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, PyObject *mapping)
/*[clinic end generated code: output=14ca42b83853c643 input=85f4172661e8dad9]*/
{
PyObject *v;
if (mapping == Py_None)
mapping = NULL;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.charmap_build
map: unicode
/
[clinic start generated code]*/
static PyObject *
_codecs_charmap_build_impl(PyModuleDef *module, PyObject *map)
/*[clinic end generated code: output=9485b58fa44afa6a input=d91a91d1717dbc6d]*/
{
return PyUnicode_BuildEncodingMap(map);
}
#ifdef HAVE_MBCS
/*[clinic input]
_codecs.mbcs_encode
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_mbcs_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=d1a013bc68798bd7 input=65c09ee1e4203263]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
/*[clinic input]
_codecs.code_page_encode
code_page: int
str: object
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_code_page_encode_impl(PyModuleDef *module, int code_page,
PyObject *str, const char *errors)
/*[clinic end generated code: output=3b406618dbfbce25 input=c8562ec460c2e309]*/
{
PyObject *v;
str = PyUnicode_FromObject(str);
if (str == NULL || PyUnicode_READY(str) < 0) {
Py_XDECREF(str);
return NULL;
}
v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
str,
errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
#endif /* HAVE_MBCS */
/* --- Error handler registry --------------------------------------------- */
/*[clinic input]
_codecs.register_error
errors: str
handler: object
/
Register the specified error handler under the name errors.
handler must be a callable object, that will be called with an exception
instance containing information about the location of the encoding/decoding
error and must return a (replacement, new position) tuple.
[clinic start generated code]*/
static PyObject *
_codecs_register_error_impl(PyModuleDef *module, const char *errors,
PyObject *handler)
/*[clinic end generated code: output=be00d3b1849ce68a input=5e6709203c2e33fe]*/
{
if (PyCodec_RegisterError(errors, handler))
return NULL;
Py_RETURN_NONE;
}
/*[clinic input]
_codecs.lookup_error
name: str
/
lookup_error(errors) -> handler
Return the error handler for the specified error handling name or raise a
LookupError, if no handler exists under this name.
[clinic start generated code]*/
static PyObject *
_codecs_lookup_error_impl(PyModuleDef *module, const char *name)
/*[clinic end generated code: output=731e6df8c83c6158 input=4775dd65e6235aba]*/
{
return PyCodec_LookupError(name);
}
/* --- Module API --------------------------------------------------------- */
static PyMethodDef _codecs_functions[] = {
_CODECS_REGISTER_METHODDEF
_CODECS_LOOKUP_METHODDEF
_CODECS_ENCODE_METHODDEF
_CODECS_DECODE_METHODDEF
_CODECS_ESCAPE_ENCODE_METHODDEF
_CODECS_ESCAPE_DECODE_METHODDEF
_CODECS_UTF_8_ENCODE_METHODDEF
_CODECS_UTF_8_DECODE_METHODDEF
_CODECS_UTF_7_ENCODE_METHODDEF
_CODECS_UTF_7_DECODE_METHODDEF
_CODECS_UTF_16_ENCODE_METHODDEF
_CODECS_UTF_16_LE_ENCODE_METHODDEF
_CODECS_UTF_16_BE_ENCODE_METHODDEF
_CODECS_UTF_16_DECODE_METHODDEF
_CODECS_UTF_16_LE_DECODE_METHODDEF
_CODECS_UTF_16_BE_DECODE_METHODDEF
_CODECS_UTF_16_EX_DECODE_METHODDEF
_CODECS_UTF_32_ENCODE_METHODDEF
_CODECS_UTF_32_LE_ENCODE_METHODDEF
_CODECS_UTF_32_BE_ENCODE_METHODDEF
_CODECS_UTF_32_DECODE_METHODDEF
_CODECS_UTF_32_LE_DECODE_METHODDEF
_CODECS_UTF_32_BE_DECODE_METHODDEF
_CODECS_UTF_32_EX_DECODE_METHODDEF
_CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF
_CODECS_UNICODE_ESCAPE_DECODE_METHODDEF
_CODECS_UNICODE_INTERNAL_ENCODE_METHODDEF
_CODECS_UNICODE_INTERNAL_DECODE_METHODDEF
_CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF
_CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF
_CODECS_LATIN_1_ENCODE_METHODDEF
_CODECS_LATIN_1_DECODE_METHODDEF
_CODECS_ASCII_ENCODE_METHODDEF
_CODECS_ASCII_DECODE_METHODDEF
_CODECS_CHARMAP_ENCODE_METHODDEF
_CODECS_CHARMAP_DECODE_METHODDEF
_CODECS_CHARMAP_BUILD_METHODDEF
_CODECS_READBUFFER_ENCODE_METHODDEF
_CODECS_MBCS_ENCODE_METHODDEF
_CODECS_MBCS_DECODE_METHODDEF
_CODECS_CODE_PAGE_ENCODE_METHODDEF
_CODECS_CODE_PAGE_DECODE_METHODDEF
_CODECS_REGISTER_ERROR_METHODDEF
_CODECS_LOOKUP_ERROR_METHODDEF
_CODECS__FORGET_CODEC_METHODDEF
{NULL, NULL} /* sentinel */
};
static struct PyModuleDef codecsmodule = {
PyModuleDef_HEAD_INIT,
"_codecs",
NULL,
-1,
_codecs_functions,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit__codecs(void)
{
return PyModule_Create(&codecsmodule);
}
|