@@ -2995,52 +2995,54 @@ static PyObject *
2995
2995
UnicodeEncodeError_str (PyObject * self )
2996
2996
{
2997
2997
PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
2998
+ PyObject * result = NULL ;
2999
+ PyObject * reason_str = NULL ;
3000
+ PyObject * encoding_str = NULL ;
2998
3001
2999
- if (exc -> object == NULL ) {
3002
+ if (exc -> object == NULL )
3000
3003
/* Not properly initialized. */
3001
3004
return PyUnicode_FromString ("" );
3002
- }
3003
3005
3004
3006
/* Get reason and encoding as strings, which they might not be if
3005
3007
they've been modified after we were constructed. */
3006
- PyObject * reason = PyObject_Str (exc -> reason );
3007
- if (reason == NULL ) {
3008
- return NULL ;
3009
- }
3010
- PyObject * encoding = PyObject_Str (exc -> encoding );
3011
- if (encoding == NULL ) {
3012
- Py_DECREF (reason );
3013
- return NULL ;
3014
- }
3008
+ reason_str = PyObject_Str (exc -> reason );
3009
+ if (reason_str == NULL )
3010
+ goto done ;
3011
+ encoding_str = PyObject_Str (exc -> encoding );
3012
+ if (encoding_str == NULL )
3013
+ goto done ;
3015
3014
3016
- PyObject * res ;
3017
3015
Py_ssize_t len = PyUnicode_GET_LENGTH (exc -> object );
3018
3016
Py_ssize_t start = exc -> start , end = exc -> end ;
3019
3017
3020
3018
if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
3021
3019
Py_UCS4 badchar = PyUnicode_ReadChar (exc -> object , start );
3022
3020
const char * fmt ;
3023
- if (badchar <= 0xff ) {
3021
+ if (badchar <= 0xff )
3024
3022
fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U" ;
3025
- }
3026
- else if (badchar <= 0xffff ) {
3023
+ else if (badchar <= 0xffff )
3027
3024
fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U" ;
3028
- }
3029
- else {
3025
+ else
3030
3026
fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U" ;
3031
- }
3032
- res = PyUnicode_FromFormat (fmt , encoding , (int )badchar , start , reason );
3027
+ result = PyUnicode_FromFormat (
3028
+ fmt ,
3029
+ encoding_str ,
3030
+ (int )badchar ,
3031
+ start ,
3032
+ reason_str );
3033
3033
}
3034
3034
else {
3035
- res = PyUnicode_FromFormat (
3035
+ result = PyUnicode_FromFormat (
3036
3036
"'%U' codec can't encode characters in position %zd-%zd: %U" ,
3037
- encoding , start , end - 1 , reason
3038
- );
3037
+ encoding_str ,
3038
+ start ,
3039
+ end - 1 ,
3040
+ reason_str );
3039
3041
}
3040
-
3041
- Py_DECREF ( reason );
3042
- Py_DECREF ( encoding );
3043
- return res ;
3042
+ done :
3043
+ Py_XDECREF ( reason_str );
3044
+ Py_XDECREF ( encoding_str );
3045
+ return result ;
3044
3046
}
3045
3047
3046
3048
static PyTypeObject _PyExc_UnicodeEncodeError = {
@@ -3109,45 +3111,47 @@ static PyObject *
3109
3111
UnicodeDecodeError_str (PyObject * self )
3110
3112
{
3111
3113
PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
3114
+ PyObject * result = NULL ;
3115
+ PyObject * reason_str = NULL ;
3116
+ PyObject * encoding_str = NULL ;
3112
3117
3113
- if (exc -> object == NULL ) {
3118
+ if (exc -> object == NULL )
3114
3119
/* Not properly initialized. */
3115
3120
return PyUnicode_FromString ("" );
3116
- }
3117
3121
3118
3122
/* Get reason and encoding as strings, which they might not be if
3119
3123
they've been modified after we were constructed. */
3120
- PyObject * reason = PyObject_Str (exc -> reason );
3121
- if (reason == NULL ) {
3122
- return NULL ;
3123
- }
3124
- PyObject * encoding = PyObject_Str (exc -> encoding );
3125
- if (encoding == NULL ) {
3126
- Py_DECREF (reason );
3127
- return NULL ;
3128
- }
3124
+ reason_str = PyObject_Str (exc -> reason );
3125
+ if (reason_str == NULL )
3126
+ goto done ;
3127
+ encoding_str = PyObject_Str (exc -> encoding );
3128
+ if (encoding_str == NULL )
3129
+ goto done ;
3129
3130
3130
- PyObject * res ;
3131
3131
Py_ssize_t len = PyBytes_GET_SIZE (exc -> object );
3132
3132
Py_ssize_t start = exc -> start , end = exc -> end ;
3133
3133
3134
3134
if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
3135
3135
int badbyte = (int )(PyBytes_AS_STRING (exc -> object )[start ] & 0xff );
3136
- res = PyUnicode_FromFormat (
3136
+ result = PyUnicode_FromFormat (
3137
3137
"'%U' codec can't decode byte 0x%02x in position %zd: %U" ,
3138
- encoding , badbyte , start , reason
3139
- );
3138
+ encoding_str ,
3139
+ badbyte ,
3140
+ start ,
3141
+ reason_str );
3140
3142
}
3141
3143
else {
3142
- res = PyUnicode_FromFormat (
3144
+ result = PyUnicode_FromFormat (
3143
3145
"'%U' codec can't decode bytes in position %zd-%zd: %U" ,
3144
- encoding , start , end - 1 , reason
3145
- );
3146
+ encoding_str ,
3147
+ start ,
3148
+ end - 1 ,
3149
+ reason_str );
3146
3150
}
3147
-
3148
- Py_DECREF ( reason );
3149
- Py_DECREF ( encoding );
3150
- return res ;
3151
+ done :
3152
+ Py_XDECREF ( reason_str );
3153
+ Py_XDECREF ( encoding_str );
3154
+ return result ;
3151
3155
}
3152
3156
3153
3157
static PyTypeObject _PyExc_UnicodeDecodeError = {
@@ -3206,46 +3210,46 @@ static PyObject *
3206
3210
UnicodeTranslateError_str (PyObject * self )
3207
3211
{
3208
3212
PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
3213
+ PyObject * result = NULL ;
3214
+ PyObject * reason_str = NULL ;
3209
3215
3210
- if (exc -> object == NULL ) {
3216
+ if (exc -> object == NULL )
3211
3217
/* Not properly initialized. */
3212
3218
return PyUnicode_FromString ("" );
3213
- }
3214
3219
3215
3220
/* Get reason as a string, which it might not be if it's been
3216
3221
modified after we were constructed. */
3217
- PyObject * reason = PyObject_Str (exc -> reason );
3218
- if (reason == NULL ) {
3219
- return NULL ;
3220
- }
3222
+ reason_str = PyObject_Str (exc -> reason );
3223
+ if (reason_str == NULL )
3224
+ goto done ;
3221
3225
3222
- PyObject * res ;
3223
3226
Py_ssize_t len = PyUnicode_GET_LENGTH (exc -> object );
3224
3227
Py_ssize_t start = exc -> start , end = exc -> end ;
3225
3228
3226
3229
if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
3227
3230
Py_UCS4 badchar = PyUnicode_ReadChar (exc -> object , start );
3228
3231
const char * fmt ;
3229
- if (badchar <= 0xff ) {
3232
+ if (badchar <= 0xff )
3230
3233
fmt = "can't translate character '\\x%02x' in position %zd: %U" ;
3231
- }
3232
- else if (badchar <= 0xffff ) {
3234
+ else if (badchar <= 0xffff )
3233
3235
fmt = "can't translate character '\\u%04x' in position %zd: %U" ;
3234
- }
3235
- else {
3236
+ else
3236
3237
fmt = "can't translate character '\\U%08x' in position %zd: %U" ;
3237
- }
3238
- res = PyUnicode_FromFormat (fmt , (int )badchar , start , reason );
3239
- }
3240
- else {
3241
- res = PyUnicode_FromFormat (
3238
+ result = PyUnicode_FromFormat (
3239
+ fmt ,
3240
+ (int )badchar ,
3241
+ start ,
3242
+ reason_str );
3243
+ } else {
3244
+ result = PyUnicode_FromFormat (
3242
3245
"can't translate characters in position %zd-%zd: %U" ,
3243
- start , end - 1 , reason
3244
- );
3246
+ start ,
3247
+ end - 1 ,
3248
+ reason_str );
3245
3249
}
3246
-
3247
- Py_DECREF ( reason );
3248
- return res ;
3250
+ done :
3251
+ Py_XDECREF ( reason_str );
3252
+ return result ;
3249
3253
}
3250
3254
3251
3255
static PyTypeObject _PyExc_UnicodeTranslateError = {
0 commit comments