aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Objects/exceptions.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-12-23 19:39:02 +0300
committershadchin <shadchin@yandex-team.com>2024-12-23 19:54:20 +0300
commit65a5bf9d37a3b29eb394f560b9a09318196c40e8 (patch)
treee5cd68fb0682b2388e52d9806bb87adc348e21a8 /contrib/tools/python3/Objects/exceptions.c
parenta1dd87a52878ab3e46e5fd2dba5ecbba6113d7e0 (diff)
downloadydb-65a5bf9d37a3b29eb394f560b9a09318196c40e8.tar.gz
Update Python 3 to 3.12.8
commit_hash:c20045b8a987d8720e1f3328270357491d5530f3
Diffstat (limited to 'contrib/tools/python3/Objects/exceptions.c')
-rw-r--r--contrib/tools/python3/Objects/exceptions.c111
1 files changed, 66 insertions, 45 deletions
diff --git a/contrib/tools/python3/Objects/exceptions.c b/contrib/tools/python3/Objects/exceptions.c
index 4f2153b193..c579563db7 100644
--- a/contrib/tools/python3/Objects/exceptions.c
+++ b/contrib/tools/python3/Objects/exceptions.c
@@ -2961,46 +2961,55 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject *
UnicodeEncodeError_str(PyObject *self)
{
- PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
+ PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
- if (!uself->object)
+ if (exc->object == NULL) {
/* Not properly initialized. */
return PyUnicode_FromString("");
+ }
/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
- reason_str = PyObject_Str(uself->reason);
- if (reason_str == NULL)
+ reason_str = PyObject_Str(exc->reason);
+ if (reason_str == NULL) {
goto done;
- encoding_str = PyObject_Str(uself->encoding);
- if (encoding_str == NULL)
+ }
+ encoding_str = PyObject_Str(exc->encoding);
+ if (encoding_str == NULL) {
goto done;
+ }
+
+ Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
+ Py_ssize_t start = exc->start, end = exc->end;
- if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
- Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
+ if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
+ Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
- if (badchar <= 0xff)
+ if (badchar <= 0xff) {
fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
- else if (badchar <= 0xffff)
+ }
+ else if (badchar <= 0xffff) {
fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
- else
+ }
+ else {
fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
+ }
result = PyUnicode_FromFormat(
fmt,
encoding_str,
(int)badchar,
- uself->start,
+ start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't encode characters in position %zd-%zd: %U",
encoding_str,
- uself->start,
- uself->end-1,
+ start,
+ end - 1,
reason_str);
}
done:
@@ -3074,41 +3083,46 @@ error:
static PyObject *
UnicodeDecodeError_str(PyObject *self)
{
- PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
+ PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
- if (!uself->object)
+ if (exc->object == NULL) {
/* Not properly initialized. */
return PyUnicode_FromString("");
+ }
/* Get reason and encoding as strings, which they might not be if
they've been modified after we were constructed. */
- reason_str = PyObject_Str(uself->reason);
- if (reason_str == NULL)
+ reason_str = PyObject_Str(exc->reason);
+ if (reason_str == NULL) {
goto done;
- encoding_str = PyObject_Str(uself->encoding);
- if (encoding_str == NULL)
+ }
+ encoding_str = PyObject_Str(exc->encoding);
+ if (encoding_str == NULL) {
goto done;
+ }
+
+ Py_ssize_t len = PyBytes_GET_SIZE(exc->object);
+ Py_ssize_t start = exc->start, end = exc->end;
- if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
- int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
+ if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
+ int badbyte = (int)(PyBytes_AS_STRING(exc->object)[start] & 0xff);
result = PyUnicode_FromFormat(
"'%U' codec can't decode byte 0x%02x in position %zd: %U",
encoding_str,
- byte,
- uself->start,
+ badbyte,
+ start,
reason_str);
}
else {
result = PyUnicode_FromFormat(
"'%U' codec can't decode bytes in position %zd-%zd: %U",
encoding_str,
- uself->start,
- uself->end-1,
- reason_str
- );
+ start,
+ end - 1,
+ reason_str);
}
done:
Py_XDECREF(reason_str);
@@ -3171,42 +3185,49 @@ UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
static PyObject *
UnicodeTranslateError_str(PyObject *self)
{
- PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
+ PyUnicodeErrorObject *exc = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
- if (!uself->object)
+ if (exc->object == NULL) {
/* Not properly initialized. */
return PyUnicode_FromString("");
+ }
/* Get reason as a string, which it might not be if it's been
modified after we were constructed. */
- reason_str = PyObject_Str(uself->reason);
- if (reason_str == NULL)
+ reason_str = PyObject_Str(exc->reason);
+ if (reason_str == NULL) {
goto done;
+ }
+
+ Py_ssize_t len = PyUnicode_GET_LENGTH(exc->object);
+ Py_ssize_t start = exc->start, end = exc->end;
- if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
- Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
+ if ((start >= 0 && start < len) && (end >= 0 && end <= len) && end == start + 1) {
+ Py_UCS4 badchar = PyUnicode_ReadChar(exc->object, start);
const char *fmt;
- if (badchar <= 0xff)
+ if (badchar <= 0xff) {
fmt = "can't translate character '\\x%02x' in position %zd: %U";
- else if (badchar <= 0xffff)
+ }
+ else if (badchar <= 0xffff) {
fmt = "can't translate character '\\u%04x' in position %zd: %U";
- else
+ }
+ else {
fmt = "can't translate character '\\U%08x' in position %zd: %U";
+ }
result = PyUnicode_FromFormat(
fmt,
(int)badchar,
- uself->start,
- reason_str
- );
- } else {
+ start,
+ reason_str);
+ }
+ else {
result = PyUnicode_FromFormat(
"can't translate characters in position %zd-%zd: %U",
- uself->start,
- uself->end-1,
- reason_str
- );
+ start,
+ end - 1,
+ reason_str);
}
done:
Py_XDECREF(reason_str);