summaryrefslogtreecommitdiffstats
path: root/Modules/cjkcodecs
diff options
context:
space:
mode:
authorJohn Sloboda <sloboda@gmail.com>2024-03-17 04:58:42 (GMT)
committerGitHub <noreply@github.com>2024-03-17 04:58:42 (GMT)
commit649857a1574a02235ccfac9e2ac1c12914cf8fe0 (patch)
treecf08eb67a84db277e1a687eb7177c709d16616b6 /Modules/cjkcodecs
parentc514a975abe35fa4604cd3541e2286168ef67d10 (diff)
downloadcpython-649857a1574a02235ccfac9e2ac1c12914cf8fe0.zip
cpython-649857a1574a02235ccfac9e2ac1c12914cf8fe0.tar.gz
cpython-649857a1574a02235ccfac9e2ac1c12914cf8fe0.tar.bz2
gh-85287: Change codecs to raise precise UnicodeEncodeError and UnicodeDecodeError (#113674)
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
Diffstat (limited to 'Modules/cjkcodecs')
-rw-r--r--Modules/cjkcodecs/multibytecodec.c42
1 files changed, 37 insertions, 5 deletions
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 2125da4..e5433d7 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -825,8 +825,15 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
if (inpos < datalen) {
if (datalen - inpos > MAXENCPENDING) {
/* normal codecs can't reach here */
- PyErr_SetString(PyExc_UnicodeError,
- "pending buffer overflow");
+ PyObject *excobj = PyObject_CallFunction(PyExc_UnicodeEncodeError,
+ "sOnns",
+ ctx->codec->encoding,
+ inbuf,
+ inpos, datalen,
+ "pending buffer overflow");
+ if (excobj == NULL) goto errorexit;
+ PyErr_SetObject(PyExc_UnicodeEncodeError, excobj);
+ Py_DECREF(excobj);
goto errorexit;
}
ctx->pending = PyUnicode_Substring(inbuf, inpos, datalen);
@@ -857,7 +864,16 @@ decoder_append_pending(MultibyteStatefulDecoderContext *ctx,
npendings = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
if (npendings + ctx->pendingsize > MAXDECPENDING ||
npendings > PY_SSIZE_T_MAX - ctx->pendingsize) {
- PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow");
+ Py_ssize_t bufsize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top);
+ PyObject *excobj = PyUnicodeDecodeError_Create(ctx->codec->encoding,
+ (const char *)buf->inbuf_top,
+ bufsize,
+ 0,
+ bufsize,
+ "pending buffer overflow");
+ if (excobj == NULL) return -1;
+ PyErr_SetObject(PyExc_UnicodeDecodeError, excobj);
+ Py_DECREF(excobj);
return -1;
}
memcpy(ctx->pending + ctx->pendingsize, buf->inbuf, npendings);
@@ -938,7 +954,17 @@ _multibytecodec_MultibyteIncrementalEncoder_getstate_impl(MultibyteIncrementalEn
return NULL;
}
if (pendingsize > MAXENCPENDING*4) {
- PyErr_SetString(PyExc_UnicodeError, "pending buffer too large");
+ PyObject *excobj = PyObject_CallFunction(PyExc_UnicodeEncodeError,
+ "sOnns",
+ self->codec->encoding,
+ self->pending,
+ 0, PyUnicode_GET_LENGTH(self->pending),
+ "pending buffer too large");
+ if (excobj == NULL) {
+ return NULL;
+ }
+ PyErr_SetObject(PyExc_UnicodeEncodeError, excobj);
+ Py_DECREF(excobj);
return NULL;
}
statebytes[0] = (unsigned char)pendingsize;
@@ -1267,7 +1293,13 @@ _multibytecodec_MultibyteIncrementalDecoder_setstate_impl(MultibyteIncrementalDe
}
if (buffersize > MAXDECPENDING) {
- PyErr_SetString(PyExc_UnicodeError, "pending buffer too large");
+ PyObject *excobj = PyUnicodeDecodeError_Create(self->codec->encoding,
+ PyBytes_AS_STRING(buffer), buffersize,
+ 0, buffersize,
+ "pending buffer too large");
+ if (excobj == NULL) return NULL;
+ PyErr_SetObject(PyExc_UnicodeDecodeError, excobj);
+ Py_DECREF(excobj);
return NULL;
}