@@ -2995,52 +2995,54 @@ static PyObject *
29952995UnicodeEncodeError_str (PyObject * self )
29962996{
29972997 PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
2998+ PyObject * result = NULL ;
2999+ PyObject * reason_str = NULL ;
3000+ PyObject * encoding_str = NULL ;
29983001
2999- if (exc -> object == NULL ) {
3002+ if (exc -> object == NULL )
30003003 /* Not properly initialized. */
30013004 return PyUnicode_FromString ("" );
3002- }
30033005
30043006 /* Get reason and encoding as strings, which they might not be if
30053007 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 ;
30153014
3016- PyObject * res ;
30173015 Py_ssize_t len = PyUnicode_GET_LENGTH (exc -> object );
30183016 Py_ssize_t start = exc -> start , end = exc -> end ;
30193017
30203018 if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
30213019 Py_UCS4 badchar = PyUnicode_ReadChar (exc -> object , start );
30223020 const char * fmt ;
3023- if (badchar <= 0xff ) {
3021+ if (badchar <= 0xff )
30243022 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U" ;
3025- }
3026- else if (badchar <= 0xffff ) {
3023+ else if (badchar <= 0xffff )
30273024 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U" ;
3028- }
3029- else {
3025+ else
30303026 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 );
30333033 }
30343034 else {
3035- res = PyUnicode_FromFormat (
3035+ result = PyUnicode_FromFormat (
30363036 "'%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 );
30393041 }
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 ;
30443046}
30453047
30463048static PyTypeObject _PyExc_UnicodeEncodeError = {
@@ -3109,45 +3111,47 @@ static PyObject *
31093111UnicodeDecodeError_str (PyObject * self )
31103112{
31113113 PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
3114+ PyObject * result = NULL ;
3115+ PyObject * reason_str = NULL ;
3116+ PyObject * encoding_str = NULL ;
31123117
3113- if (exc -> object == NULL ) {
3118+ if (exc -> object == NULL )
31143119 /* Not properly initialized. */
31153120 return PyUnicode_FromString ("" );
3116- }
31173121
31183122 /* Get reason and encoding as strings, which they might not be if
31193123 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 ;
31293130
3130- PyObject * res ;
31313131 Py_ssize_t len = PyBytes_GET_SIZE (exc -> object );
31323132 Py_ssize_t start = exc -> start , end = exc -> end ;
31333133
31343134 if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
31353135 int badbyte = (int )(PyBytes_AS_STRING (exc -> object )[start ] & 0xff );
3136- res = PyUnicode_FromFormat (
3136+ result = PyUnicode_FromFormat (
31373137 "'%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 );
31403142 }
31413143 else {
3142- res = PyUnicode_FromFormat (
3144+ result = PyUnicode_FromFormat (
31433145 "'%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 );
31463150 }
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 ;
31513155}
31523156
31533157static PyTypeObject _PyExc_UnicodeDecodeError = {
@@ -3206,46 +3210,46 @@ static PyObject *
32063210UnicodeTranslateError_str (PyObject * self )
32073211{
32083212 PyUnicodeErrorObject * exc = (PyUnicodeErrorObject * )self ;
3213+ PyObject * result = NULL ;
3214+ PyObject * reason_str = NULL ;
32093215
3210- if (exc -> object == NULL ) {
3216+ if (exc -> object == NULL )
32113217 /* Not properly initialized. */
32123218 return PyUnicode_FromString ("" );
3213- }
32143219
32153220 /* Get reason as a string, which it might not be if it's been
32163221 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 ;
32213225
3222- PyObject * res ;
32233226 Py_ssize_t len = PyUnicode_GET_LENGTH (exc -> object );
32243227 Py_ssize_t start = exc -> start , end = exc -> end ;
32253228
32263229 if ((start >= 0 && start < len ) && (end >= 0 && end <= len ) && end == start + 1 ) {
32273230 Py_UCS4 badchar = PyUnicode_ReadChar (exc -> object , start );
32283231 const char * fmt ;
3229- if (badchar <= 0xff ) {
3232+ if (badchar <= 0xff )
32303233 fmt = "can't translate character '\\x%02x' in position %zd: %U" ;
3231- }
3232- else if (badchar <= 0xffff ) {
3234+ else if (badchar <= 0xffff )
32333235 fmt = "can't translate character '\\u%04x' in position %zd: %U" ;
3234- }
3235- else {
3236+ else
32363237 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 (
32423245 "can't translate characters in position %zd-%zd: %U" ,
3243- start , end - 1 , reason
3244- );
3246+ start ,
3247+ end - 1 ,
3248+ reason_str );
32453249 }
3246-
3247- Py_DECREF ( reason );
3248- return res ;
3250+ done :
3251+ Py_XDECREF ( reason_str );
3252+ return result ;
32493253}
32503254
32513255static PyTypeObject _PyExc_UnicodeTranslateError = {
0 commit comments