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
|
#include "parts.h"
#include "util.h"
// === Codecs registration and un-registration ================================
static PyObject *
codec_register(PyObject *Py_UNUSED(module), PyObject *search_function)
{
if (PyCodec_Register(search_function) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
codec_unregister(PyObject *Py_UNUSED(module), PyObject *search_function)
{
if (PyCodec_Unregister(search_function) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
codec_known_encoding(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
return PyCodec_KnownEncoding(encoding) ? Py_True : Py_False;
}
// === Codecs encoding and decoding interfaces ================================
static PyObject *
codec_encode(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *input;
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
return NULL;
}
return PyCodec_Encode(input, encoding, errors);
}
static PyObject *
codec_decode(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *input;
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
return NULL;
}
return PyCodec_Decode(input, encoding, errors);
}
static PyObject *
codec_encoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
return PyCodec_Encoder(encoding);
}
static PyObject *
codec_decoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
return PyCodec_Decoder(encoding);
}
static PyObject *
codec_incremental_encoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
return NULL;
}
return PyCodec_IncrementalEncoder(encoding, errors);
}
static PyObject *
codec_incremental_decoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
return NULL;
}
return PyCodec_IncrementalDecoder(encoding, errors);
}
static PyObject *
codec_stream_reader(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
PyObject *stream;
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
return NULL;
}
return PyCodec_StreamReader(encoding, stream, errors);
}
static PyObject *
codec_stream_writer(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
PyObject *stream;
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
return NULL;
}
return PyCodec_StreamWriter(encoding, stream, errors);
}
// === Codecs errors handlers =================================================
static PyObject *
codec_register_error(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // must not be NULL
PyObject *error;
if (!PyArg_ParseTuple(args, "sO", &encoding, &error)) {
return NULL;
}
if (PyCodec_RegisterError(encoding, error) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
codec_lookup_error(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *NULL_WOULD_RAISE(encoding); // NULL case will be tested
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
return PyCodec_LookupError(encoding);
}
static PyObject *
codec_strict_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_StrictErrors(exc);
}
static PyObject *
codec_ignore_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_IgnoreErrors(exc);
}
static PyObject *
codec_replace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_ReplaceErrors(exc);
}
static PyObject *
codec_xmlcharrefreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_XMLCharRefReplaceErrors(exc);
}
static PyObject *
codec_backslashreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_BackslashReplaceErrors(exc);
}
static PyMethodDef test_methods[] = {
/* codecs registration */
{"codec_register", codec_register, METH_O},
{"codec_unregister", codec_unregister, METH_O},
{"codec_known_encoding", codec_known_encoding, METH_VARARGS},
/* encoding and decoding interface */
{"codec_encode", codec_encode, METH_VARARGS},
{"codec_decode", codec_decode, METH_VARARGS},
{"codec_encoder", codec_encoder, METH_VARARGS},
{"codec_decoder", codec_decoder, METH_VARARGS},
{"codec_incremental_encoder", codec_incremental_encoder, METH_VARARGS},
{"codec_incremental_decoder", codec_incremental_decoder, METH_VARARGS},
{"codec_stream_reader", codec_stream_reader, METH_VARARGS},
{"codec_stream_writer", codec_stream_writer, METH_VARARGS},
/* error handling */
{"codec_register_error", codec_register_error, METH_VARARGS},
{"codec_lookup_error", codec_lookup_error, METH_VARARGS},
{"codec_strict_errors", codec_strict_errors, METH_O},
{"codec_ignore_errors", codec_ignore_errors, METH_O},
{"codec_replace_errors", codec_replace_errors, METH_O},
{"codec_xmlcharrefreplace_errors", codec_xmlcharrefreplace_errors, METH_O},
{"codec_backslashreplace_errors", codec_backslashreplace_errors, METH_O},
// PyCodec_NameReplaceErrors() is tested in _testlimitedcapi/codec.c
{NULL, NULL, 0, NULL},
};
int
_PyTestCapi_Init_Codec(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}
|