Debian Codegraph @

Modules/_testcapi/codec.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4 // === Codecs registration and un-registration ================================
5
6 static PyObject *
7 codec_register(PyObject *Py_UNUSED(module), PyObject *search_function)
8 {
9 if (PyCodec_Register(search_function) < 0) {
10 return NULL;
11 }
12 Py_RETURN_NONE;
13 }
14
15 static PyObject *
16 codec_unregister(PyObject *Py_UNUSED(module), PyObject *search_function)
17 {
18 if (PyCodec_Unregister(search_function) < 0) {
19 return NULL;
20 }
21 Py_RETURN_NONE;
22 }
23
24 static PyObject *
25 codec_known_encoding(PyObject *Py_UNUSED(module), PyObject *args)
26 {
27 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
28 if (!PyArg_ParseTuple(args, "z", &encoding)) {
29 return NULL;
30 }
31 return PyCodec_KnownEncoding(encoding) ? Py_True : Py_False;
32 }
33
34 // === Codecs encoding and decoding interfaces ================================
35
36 static PyObject *
37 codec_encode(PyObject *Py_UNUSED(module), PyObject *args)
38 {
39 PyObject *input;
40 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
41 const char *errors; // can be NULL
42 if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
43 return NULL;
44 }
45 return PyCodec_Encode(input, encoding, errors);
46 }
47
48 static PyObject *
49 codec_decode(PyObject *Py_UNUSED(module), PyObject *args)
50 {
51 PyObject *input;
52 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
53 const char *errors; // can be NULL
54 if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
55 return NULL;
56 }
57 return PyCodec_Decode(input, encoding, errors);
58 }
59
60 static PyObject *
61 codec_encoder(PyObject *Py_UNUSED(module), PyObject *args)
62 {
63 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
64 if (!PyArg_ParseTuple(args, "z", &encoding)) {
65 return NULL;
66 }
67 return PyCodec_Encoder(encoding);
68 }
69
70 static PyObject *
71 codec_decoder(PyObject *Py_UNUSED(module), PyObject *args)
72 {
73 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
74 if (!PyArg_ParseTuple(args, "z", &encoding)) {
75 return NULL;
76 }
77 return PyCodec_Decoder(encoding);
78 }
79
80 static PyObject *
81 codec_incremental_encoder(PyObject *Py_UNUSED(module), PyObject *args)
82 {
83 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
84 const char *errors; // can be NULL
85 if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
86 return NULL;
87 }
88 return PyCodec_IncrementalEncoder(encoding, errors);
89 }
90
91 static PyObject *
92 codec_incremental_decoder(PyObject *Py_UNUSED(module), PyObject *args)
93 {
94 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
95 const char *errors; // can be NULL
96 if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
97 return NULL;
98 }
99 return PyCodec_IncrementalDecoder(encoding, errors);
100 }
101
102 static PyObject *
103 codec_stream_reader(PyObject *Py_UNUSED(module), PyObject *args)
104 {
105 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
106 PyObject *stream;
107 const char *errors; // can be NULL
108 if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
109 return NULL;
110 }
111 return PyCodec_StreamReader(encoding, stream, errors);
112 }
113
114 static PyObject *
115 codec_stream_writer(PyObject *Py_UNUSED(module), PyObject *args)
116 {
117 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
118 PyObject *stream;
119 const char *errors; // can be NULL
120 if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
121 return NULL;
122 }
123 return PyCodec_StreamWriter(encoding, stream, errors);
124 }
125
126 // === Codecs errors handlers =================================================
127
128 static PyObject *
129 codec_register_error(PyObject *Py_UNUSED(module), PyObject *args)
130 {
131 const char *encoding; // must not be NULL
132 PyObject *error;
133 if (!PyArg_ParseTuple(args, "sO", &encoding, &error)) {
134 return NULL;
135 }
136 if (PyCodec_RegisterError(encoding, error) < 0) {
137 return NULL;
138 }
139 Py_RETURN_NONE;
140 }
141
142 static PyObject *
143 codec_lookup_error(PyObject *Py_UNUSED(module), PyObject *args)
144 {
145 const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
146 if (!PyArg_ParseTuple(args, "z", &encoding)) {
147 return NULL;
148 }
149 return PyCodec_LookupError(encoding);
150 }
151
152 static PyObject *
153 codec_strict_errors(PyObject *Py_UNUSED(module), PyObject *exc)
154 {
155 assert(exc != NULL);
156 return PyCodec_StrictErrors(exc);
157 }
158
159 static PyObject *
160 codec_ignore_errors(PyObject *Py_UNUSED(module), PyObject *exc)
161 {
162 assert(exc != NULL);
163 return PyCodec_IgnoreErrors(exc);
164 }
165
166 static PyObject *
167 codec_replace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
168 {
169 assert(exc != NULL);
170 return PyCodec_ReplaceErrors(exc);
171 }
172
173 static PyObject *
174 codec_xmlcharrefreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
175 {
176 assert(exc != NULL);
177 return PyCodec_XMLCharRefReplaceErrors(exc);
178 }
179
180 static PyObject *
181 codec_backslashreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
182 {
183 assert(exc != NULL);
184 return PyCodec_BackslashReplaceErrors(exc);
185 }
186
187 static PyMethodDef test_methods[] = {
188 /* codecs registration */
189 {"codec_register", codec_register, METH_O},
190 {"codec_unregister", codec_unregister, METH_O},
191 {"codec_known_encoding", codec_known_encoding, METH_VARARGS},
192 /* encoding and decoding interface */
193 {"codec_encode", codec_encode, METH_VARARGS},
194 {"codec_decode", codec_decode, METH_VARARGS},
195 {"codec_encoder", codec_encoder, METH_VARARGS},
196 {"codec_decoder", codec_decoder, METH_VARARGS},
197 {"codec_incremental_encoder", codec_incremental_encoder, METH_VARARGS},
198 {"codec_incremental_decoder", codec_incremental_decoder, METH_VARARGS},
199 {"codec_stream_reader", codec_stream_reader, METH_VARARGS},
200 {"codec_stream_writer", codec_stream_writer, METH_VARARGS},
201 /* error handling */
202 {"codec_register_error", codec_register_error, METH_VARARGS},
203 {"codec_lookup_error", codec_lookup_error, METH_VARARGS},
204 {"codec_strict_errors", codec_strict_errors, METH_O},
205 {"codec_ignore_errors", codec_ignore_errors, METH_O},
206 {"codec_replace_errors", codec_replace_errors, METH_O},
207 {"codec_xmlcharrefreplace_errors", codec_xmlcharrefreplace_errors, METH_O},
208 {"codec_backslashreplace_errors", codec_backslashreplace_errors, METH_O},
209 // PyCodec_NameReplaceErrors() is tested in _testlimitedcapi/codec.c
210 {NULL, NULL, 0, NULL},
211 };
212
213 int
214 _PyTestCapi_Init_Codec(PyObject *m)
215 {
216 if (PyModule_AddFunctions(m, test_methods) < 0) {
217 return -1;
218 }
219
220 return 0;
221 }