diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-18 19:21:00 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-18 19:21:00 (GMT) |
commit | 3a50e7056ebfb835785c84df925d79bde6d0d209 (patch) | |
tree | 6a6f61d276043fc53dc3290cd74ed7aab3506eab /Modules/_codecsmodule.c | |
parent | 1e73a2467f312f7b5ebef515cca6d3a16cf36f97 (diff) | |
download | cpython-3a50e7056ebfb835785c84df925d79bde6d0d209.zip cpython-3a50e7056ebfb835785c84df925d79bde6d0d209.tar.gz cpython-3a50e7056ebfb835785c84df925d79bde6d0d209.tar.bz2 |
Issue #12281: Rewrite the MBCS codec to handle correctly replace and ignore
error handlers on all Windows versions. The MBCS codec is now supporting all
error handlers, instead of only replace to encode and ignore to decode.
Diffstat (limited to 'Modules/_codecsmodule.c')
-rw-r--r-- | Modules/_codecsmodule.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index 26c8788..be31fd2 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -612,6 +612,31 @@ mbcs_decode(PyObject *self, return codec_tuple(decoded, consumed); } +static PyObject * +code_page_decode(PyObject *self, + PyObject *args) +{ + Py_buffer pbuf; + const char *errors = NULL; + int final = 0; + Py_ssize_t consumed; + PyObject *decoded = NULL; + int code_page; + + if (!PyArg_ParseTuple(args, "iy*|zi:code_page_decode", + &code_page, &pbuf, &errors, &final)) + return NULL; + consumed = pbuf.len; + + decoded = PyUnicode_DecodeCodePageStateful(code_page, + pbuf.buf, pbuf.len, errors, + final ? NULL : &consumed); + PyBuffer_Release(&pbuf); + if (decoded == NULL) + return NULL; + return codec_tuple(decoded, consumed); +} + #endif /* HAVE_MBCS */ /* --- Encoder ------------------------------------------------------------ */ @@ -1011,6 +1036,29 @@ mbcs_encode(PyObject *self, return v; } +static PyObject * +code_page_encode(PyObject *self, + PyObject *args) +{ + PyObject *str, *v; + const char *errors = NULL; + int code_page; + + if (!PyArg_ParseTuple(args, "iO|z:code_page_encode", + &code_page, &str, &errors)) + return NULL; + + str = PyUnicode_FromObject(str); + if (str == NULL) + 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 --------------------------------------------- */ @@ -1101,6 +1149,8 @@ static PyMethodDef _codecs_functions[] = { #ifdef HAVE_MBCS {"mbcs_encode", mbcs_encode, METH_VARARGS}, {"mbcs_decode", mbcs_decode, METH_VARARGS}, + {"code_page_encode", code_page_encode, METH_VARARGS}, + {"code_page_decode", code_page_decode, METH_VARARGS}, #endif {"register_error", register_error, METH_VARARGS, register_error__doc__}, |