diff options
Diffstat (limited to 'Modules/cjkcodecs/multibytecodec.c')
-rw-r--r-- | Modules/cjkcodecs/multibytecodec.c | 345 |
1 files changed, 201 insertions, 144 deletions
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 435529f..c051514 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -8,6 +8,13 @@ #include "Python.h" #include "structmember.h" #include "multibytecodec.h" +#include "clinic/multibytecodec.c.h" + +/*[clinic input] +module _multibytecodec +class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "&MultibyteCodec_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6ad689546cbb5450]*/ typedef struct { PyObject *inobj; @@ -22,27 +29,7 @@ typedef struct { _PyUnicodeWriter writer; } MultibyteDecodeBuffer; -PyDoc_STRVAR(MultibyteCodec_Encode__doc__, -"I.encode(unicode[, errors]) -> (string, length consumed)\n\ -\n\ -Return an encoded string version of `unicode'. errors may be given to\n\ -set a different error handling scheme. Default is 'strict' meaning that\n\ -encoding errors raise a UnicodeEncodeError. Other possible values are\n\ -'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name\n\ -registered with codecs.register_error that can handle UnicodeEncodeErrors."); - -PyDoc_STRVAR(MultibyteCodec_Decode__doc__, -"I.decode(string[, errors]) -> (unicodeobject, length consumed)\n\ -\n\ -Decodes `string' using I, an MultibyteCodec instance. errors may be given\n\ -to set a different error handling scheme. Default is 'strict' meaning\n\ -that encoding errors raise a UnicodeDecodeError. Other possible values\n\ -are 'ignore' and 'replace' as well as any other name registered with\n\ -codecs.register_error that is able to handle UnicodeDecodeErrors."); - -static char *codeckwarglist[] = {"input", "errors", NULL}; static char *incnewkwarglist[] = {"errors", NULL}; -static char *incrementalkwarglist[] = {"input", "final", NULL}; static char *streamkwarglist[] = {"stream", "errors", NULL}; static PyObject *multibytecodec_encode(MultibyteCodec *, @@ -553,26 +540,37 @@ errorexit: return NULL; } +/*[clinic input] +_multibytecodec.MultibyteCodec.encode + + input: object + errors: str(accept={str, NoneType}) = NULL + +Return an encoded string version of `input'. + +'errors' may be given to set a different error handling scheme. Default is +'strict' meaning that encoding errors raise a UnicodeEncodeError. Other possible +values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name +registered with codecs.register_error that can handle UnicodeEncodeErrors. +[clinic start generated code]*/ + static PyObject * -MultibyteCodec_Encode(MultibyteCodecObject *self, - PyObject *args, PyObject *kwargs) +_multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self, + PyObject *input, + const char *errors) +/*[clinic end generated code: output=7b26652045ba56a9 input=05f6ced3c8dd0582]*/ { MultibyteCodec_State state; - PyObject *errorcb, *r, *arg, *ucvt; - const char *errors = NULL; + PyObject *errorcb, *r, *ucvt; Py_ssize_t datalen; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode", - codeckwarglist, &arg, &errors)) - return NULL; - - if (PyUnicode_Check(arg)) + if (PyUnicode_Check(input)) ucvt = NULL; else { - arg = ucvt = PyObject_Str(arg); - if (arg == NULL) + input = ucvt = PyObject_Str(input); + if (input == NULL) return NULL; - else if (!PyUnicode_Check(arg)) { + else if (!PyUnicode_Check(input)) { PyErr_SetString(PyExc_TypeError, "couldn't convert the object to unicode."); Py_DECREF(ucvt); @@ -580,11 +578,11 @@ MultibyteCodec_Encode(MultibyteCodecObject *self, } } - if (PyUnicode_READY(arg) < 0) { + if (PyUnicode_READY(input) < 0) { Py_XDECREF(ucvt); return NULL; } - datalen = PyUnicode_GET_LENGTH(arg); + datalen = PyUnicode_GET_LENGTH(input); errorcb = internal_error_callback(errors); if (errorcb == NULL) { @@ -596,7 +594,7 @@ MultibyteCodec_Encode(MultibyteCodecObject *self, self->codec->encinit(&state, self->codec->config) != 0) goto errorexit; r = multibytecodec_encode(self->codec, &state, - arg, NULL, errorcb, + input, NULL, errorcb, MBENC_FLUSH | MBENC_RESET); if (r == NULL) goto errorexit; @@ -611,31 +609,41 @@ errorexit: return NULL; } +/*[clinic input] +_multibytecodec.MultibyteCodec.decode + + input: Py_buffer + errors: str(accept={str, NoneType}) = NULL + +Decodes 'input'. + +'errors' may be given to set a different error handling scheme. Default is +'strict' meaning that encoding errors raise a UnicodeDecodeError. Other possible +values are 'ignore' and 'replace' as well as any other name registered with +codecs.register_error that is able to handle UnicodeDecodeErrors." +[clinic start generated code]*/ + static PyObject * -MultibyteCodec_Decode(MultibyteCodecObject *self, - PyObject *args, PyObject *kwargs) +_multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self, + Py_buffer *input, + const char *errors) +/*[clinic end generated code: output=ff419f65bad6cc77 input=a7d45f87f75e5e02]*/ { MultibyteCodec_State state; MultibyteDecodeBuffer buf; PyObject *errorcb, *res; - Py_buffer pdata; - const char *data, *errors = NULL; + const char *data; Py_ssize_t datalen; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode", - codeckwarglist, &pdata, &errors)) - return NULL; - data = pdata.buf; - datalen = pdata.len; + data = input->buf; + datalen = input->len; errorcb = internal_error_callback(errors); if (errorcb == NULL) { - PyBuffer_Release(&pdata); return NULL; } if (datalen == 0) { - PyBuffer_Release(&pdata); ERROR_DECREF(errorcb); return make_tuple(PyUnicode_New(0, 0), 0); } @@ -668,13 +676,11 @@ MultibyteCodec_Decode(MultibyteCodecObject *self, if (res == NULL) goto errorexit; - PyBuffer_Release(&pdata); Py_XDECREF(buf.excobj); ERROR_DECREF(errorcb); return make_tuple(res, datalen); errorexit: - PyBuffer_Release(&pdata); ERROR_DECREF(errorcb); Py_XDECREF(buf.excobj); _PyUnicodeWriter_Dealloc(&buf.writer); @@ -683,13 +689,9 @@ errorexit: } static struct PyMethodDef multibytecodec_methods[] = { - {"encode", (PyCFunction)MultibyteCodec_Encode, - METH_VARARGS | METH_KEYWORDS, - MultibyteCodec_Encode__doc__}, - {"decode", (PyCFunction)MultibyteCodec_Decode, - METH_VARARGS | METH_KEYWORDS, - MultibyteCodec_Decode__doc__}, - {NULL, NULL}, + _MULTIBYTECODEC_MULTIBYTECODEC_ENCODE_METHODDEF + _MULTIBYTECODEC_MULTIBYTECODEC_DECODE_METHODDEF + {NULL, NULL}, }; static void @@ -873,26 +875,34 @@ decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx, } -/** - * MultibyteIncrementalEncoder object - */ +/*[clinic input] + class _multibytecodec.MultibyteIncrementalEncoder "MultibyteIncrementalEncoderObject *" "&MultibyteIncrementalEncoder_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3be82909cd08924d]*/ -static PyObject * -mbiencoder_encode(MultibyteIncrementalEncoderObject *self, - PyObject *args, PyObject *kwargs) -{ - PyObject *data; - int final = 0; +/*[clinic input] +_multibytecodec.MultibyteIncrementalEncoder.encode - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode", - incrementalkwarglist, &data, &final)) - return NULL; + input: object + final: int(c_default="0") = False +[clinic start generated code]*/ - return encoder_encode_stateful(STATEFUL_ECTX(self), data, final); +static PyObject * +_multibytecodec_MultibyteIncrementalEncoder_encode_impl(MultibyteIncrementalEncoderObject *self, + PyObject *input, + int final) +/*[clinic end generated code: output=123361b6c505e2c1 input=a345c688fa664f92]*/ +{ + return encoder_encode_stateful(STATEFUL_ECTX(self), input, final); } +/*[clinic input] +_multibytecodec.MultibyteIncrementalEncoder.reset +[clinic start generated code]*/ + static PyObject * -mbiencoder_reset(MultibyteIncrementalEncoderObject *self) +_multibytecodec_MultibyteIncrementalEncoder_reset_impl(MultibyteIncrementalEncoderObject *self) +/*[clinic end generated code: output=b4125d8f537a253f input=930f06760707b6ea]*/ { /* Longest output: 4 bytes (b'\x0F\x1F(B') with ISO 2022 */ unsigned char buffer[4], *outbuf; @@ -909,11 +919,9 @@ mbiencoder_reset(MultibyteIncrementalEncoderObject *self) } static struct PyMethodDef mbiencoder_methods[] = { - {"encode", (PyCFunction)mbiencoder_encode, - METH_VARARGS | METH_KEYWORDS, NULL}, - {"reset", (PyCFunction)mbiencoder_reset, - METH_NOARGS, NULL}, - {NULL, NULL}, + _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_ENCODE_METHODDEF + _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_RESET_METHODDEF + {NULL, NULL}, }; static PyObject * @@ -1024,26 +1032,31 @@ static PyTypeObject MultibyteIncrementalEncoder_Type = { }; -/** - * MultibyteIncrementalDecoder object - */ +/*[clinic input] + class _multibytecodec.MultibyteIncrementalDecoder "MultibyteIncrementalDecoderObject *" "&MultibyteIncrementalDecoder_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f6003faaf2cea692]*/ + +/*[clinic input] +_multibytecodec.MultibyteIncrementalDecoder.decode + + input: Py_buffer + final: int(c_default="0") = False +[clinic start generated code]*/ static PyObject * -mbidecoder_decode(MultibyteIncrementalDecoderObject *self, - PyObject *args, PyObject *kwargs) +_multibytecodec_MultibyteIncrementalDecoder_decode_impl(MultibyteIncrementalDecoderObject *self, + Py_buffer *input, + int final) +/*[clinic end generated code: output=b9b9090e8a9ce2ba input=576631c61906d39d]*/ { MultibyteDecodeBuffer buf; char *data, *wdata = NULL; - Py_buffer pdata; Py_ssize_t wsize, size, origpending; - int final = 0; PyObject *res; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode", - incrementalkwarglist, &pdata, &final)) - return NULL; - data = pdata.buf; - size = pdata.len; + data = input->buf; + size = input->len; _PyUnicodeWriter_Init(&buf.writer); buf.excobj = NULL; @@ -1094,14 +1107,12 @@ mbidecoder_decode(MultibyteIncrementalDecoderObject *self, if (res == NULL) goto errorexit; - PyBuffer_Release(&pdata); if (wdata != data) PyMem_Del(wdata); Py_XDECREF(buf.excobj); return res; errorexit: - PyBuffer_Release(&pdata); if (wdata != NULL && wdata != data) PyMem_Del(wdata); Py_XDECREF(buf.excobj); @@ -1109,8 +1120,13 @@ errorexit: return NULL; } +/*[clinic input] +_multibytecodec.MultibyteIncrementalDecoder.reset +[clinic start generated code]*/ + static PyObject * -mbidecoder_reset(MultibyteIncrementalDecoderObject *self) +_multibytecodec_MultibyteIncrementalDecoder_reset_impl(MultibyteIncrementalDecoderObject *self) +/*[clinic end generated code: output=da423b1782c23ed1 input=3b63b3be85b2fb45]*/ { if (self->codec->decreset != NULL && self->codec->decreset(&self->state, self->codec->config) != 0) @@ -1121,11 +1137,9 @@ mbidecoder_reset(MultibyteIncrementalDecoderObject *self) } static struct PyMethodDef mbidecoder_methods[] = { - {"decode", (PyCFunction)mbidecoder_decode, - METH_VARARGS | METH_KEYWORDS, NULL}, - {"reset", (PyCFunction)mbidecoder_reset, - METH_NOARGS, NULL}, - {NULL, NULL}, + _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_DECODE_METHODDEF + _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_RESET_METHODDEF + {NULL, NULL}, }; static PyObject * @@ -1236,9 +1250,10 @@ static PyTypeObject MultibyteIncrementalDecoder_Type = { }; -/** - * MultibyteStreamReader object - */ +/*[clinic input] + class _multibytecodec.MultibyteStreamReader "MultibyteStreamReaderObject *" "MultibyteStreamReader_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d323634b74976f09]*/ static PyObject * mbstreamreader_iread(MultibyteStreamReaderObject *self, @@ -1260,10 +1275,10 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self, if (sizehint < 0) cres = PyObject_CallMethod(self->stream, - (char *)method, NULL); + method, NULL); else cres = PyObject_CallMethod(self->stream, - (char *)method, "i", sizehint); + method, "i", sizehint); if (cres == NULL) goto errorexit; @@ -1345,16 +1360,21 @@ errorexit: return NULL; } +/*[clinic input] + _multibytecodec.MultibyteStreamReader.read + + sizeobj: object = None + / +[clinic start generated code]*/ + static PyObject * -mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args) +_multibytecodec_MultibyteStreamReader_read_impl(MultibyteStreamReaderObject *self, + PyObject *sizeobj) +/*[clinic end generated code: output=35621eb75355d5b8 input=015b0d3ff2fca485]*/ { - PyObject *sizeobj = NULL; Py_ssize_t size; - if (!PyArg_UnpackTuple(args, "read", 0, 1, &sizeobj)) - return NULL; - - if (sizeobj == Py_None || sizeobj == NULL) + if (sizeobj == Py_None) size = -1; else if (PyLong_Check(sizeobj)) size = PyLong_AsSsize_t(sizeobj); @@ -1369,16 +1389,21 @@ mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args) return mbstreamreader_iread(self, "read", size); } +/*[clinic input] + _multibytecodec.MultibyteStreamReader.readline + + sizeobj: object = None + / +[clinic start generated code]*/ + static PyObject * -mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args) +_multibytecodec_MultibyteStreamReader_readline_impl(MultibyteStreamReaderObject *self, + PyObject *sizeobj) +/*[clinic end generated code: output=4fbfaae1ed457a11 input=41ccc64f9bb0cec3]*/ { - PyObject *sizeobj = NULL; Py_ssize_t size; - if (!PyArg_UnpackTuple(args, "readline", 0, 1, &sizeobj)) - return NULL; - - if (sizeobj == Py_None || sizeobj == NULL) + if (sizeobj == Py_None) size = -1; else if (PyLong_Check(sizeobj)) size = PyLong_AsSsize_t(sizeobj); @@ -1393,16 +1418,22 @@ mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args) return mbstreamreader_iread(self, "readline", size); } +/*[clinic input] + _multibytecodec.MultibyteStreamReader.readlines + + sizehintobj: object = None + / +[clinic start generated code]*/ + static PyObject * -mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args) +_multibytecodec_MultibyteStreamReader_readlines_impl(MultibyteStreamReaderObject *self, + PyObject *sizehintobj) +/*[clinic end generated code: output=e7c4310768ed2ad4 input=54932f5d4d88e880]*/ { - PyObject *sizehintobj = NULL, *r, *sr; + PyObject *r, *sr; Py_ssize_t sizehint; - if (!PyArg_UnpackTuple(args, "readlines", 0, 1, &sizehintobj)) - return NULL; - - if (sizehintobj == Py_None || sizehintobj == NULL) + if (sizehintobj == Py_None) sizehint = -1; else if (PyLong_Check(sizehintobj)) sizehint = PyLong_AsSsize_t(sizehintobj); @@ -1423,8 +1454,13 @@ mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args) return sr; } +/*[clinic input] + _multibytecodec.MultibyteStreamReader.reset +[clinic start generated code]*/ + static PyObject * -mbstreamreader_reset(MultibyteStreamReaderObject *self) +_multibytecodec_MultibyteStreamReader_reset_impl(MultibyteStreamReaderObject *self) +/*[clinic end generated code: output=138490370a680abc input=5d4140db84b5e1e2]*/ { if (self->codec->decreset != NULL && self->codec->decreset(&self->state, self->codec->config) != 0) @@ -1435,14 +1471,10 @@ mbstreamreader_reset(MultibyteStreamReaderObject *self) } static struct PyMethodDef mbstreamreader_methods[] = { - {"read", (PyCFunction)mbstreamreader_read, - METH_VARARGS, NULL}, - {"readline", (PyCFunction)mbstreamreader_readline, - METH_VARARGS, NULL}, - {"readlines", (PyCFunction)mbstreamreader_readlines, - METH_VARARGS, NULL}, - {"reset", (PyCFunction)mbstreamreader_reset, - METH_NOARGS, NULL}, + _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READ_METHODDEF + _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINE_METHODDEF + _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINES_METHODDEF + _MULTIBYTECODEC_MULTIBYTESTREAMREADER_RESET_METHODDEF {NULL, NULL}, }; @@ -1565,9 +1597,10 @@ static PyTypeObject MultibyteStreamReader_Type = { }; -/** - * MultibyteStreamWriter object - */ +/*[clinic input] + class _multibytecodec.MultibyteStreamWriter "MultibyteStreamWriterObject *" "&MultibyteStreamWriter_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cde22780a215d6ac]*/ static int mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, @@ -1588,8 +1621,17 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, return 0; } +/*[clinic input] + _multibytecodec.MultibyteStreamWriter.write + + strobj: object + / +[clinic start generated code]*/ + static PyObject * -mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *strobj) +_multibytecodec_MultibyteStreamWriter_write(MultibyteStreamWriterObject *self, + PyObject *strobj) +/*[clinic end generated code: output=e13ae841c895251e input=551dc4c018c10a2b]*/ { if (mbstreamwriter_iwrite(self, strobj)) return NULL; @@ -1597,8 +1639,17 @@ mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *strobj) Py_RETURN_NONE; } +/*[clinic input] + _multibytecodec.MultibyteStreamWriter.writelines + + lines: object + / +[clinic start generated code]*/ + static PyObject * -mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines) +_multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *self, + PyObject *lines) +/*[clinic end generated code: output=e5c4285ac8e7d522 input=57797fe7008d4e96]*/ { PyObject *strobj; int i, r; @@ -1624,8 +1675,13 @@ mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines) Py_RETURN_NONE; } +/*[clinic input] + _multibytecodec.MultibyteStreamWriter.reset +[clinic start generated code]*/ + static PyObject * -mbstreamwriter_reset(MultibyteStreamWriterObject *self) +_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self) +/*[clinic end generated code: output=8f54a4d9b03db5ff input=b56dbcbaf35cc10c]*/ { PyObject *pwrt; @@ -1724,13 +1780,10 @@ mbstreamwriter_dealloc(MultibyteStreamWriterObject *self) } static struct PyMethodDef mbstreamwriter_methods[] = { - {"write", (PyCFunction)mbstreamwriter_write, - METH_O, NULL}, - {"writelines", (PyCFunction)mbstreamwriter_writelines, - METH_O, NULL}, - {"reset", (PyCFunction)mbstreamwriter_reset, - METH_NOARGS, NULL}, - {NULL, NULL}, + _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITE_METHODDEF + _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITELINES_METHODDEF + _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_RESET_METHODDEF + {NULL, NULL}, }; static PyMemberDef mbstreamwriter_members[] = { @@ -1784,12 +1837,16 @@ static PyTypeObject MultibyteStreamWriter_Type = { }; -/** - * Exposed factory function - */ +/*[clinic input] +_multibytecodec.__create_codec + + arg: object + / +[clinic start generated code]*/ static PyObject * -__create_codec(PyObject *ignore, PyObject *arg) +_multibytecodec___create_codec(PyModuleDef *module, PyObject *arg) +/*[clinic end generated code: output=fbe74f6510640163 input=6840b2a6b183fcfa]*/ { MultibyteCodecObject *self; MultibyteCodec *codec; @@ -1812,7 +1869,7 @@ __create_codec(PyObject *ignore, PyObject *arg) } static struct PyMethodDef __methods[] = { - {"__create_codec", (PyCFunction)__create_codec, METH_O}, + _MULTIBYTECODEC___CREATE_CODEC_METHODDEF {NULL, NULL}, }; |