@@ -1086,56 +1086,48 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
10861086 else if (format -> type == 'o' ) {
10871087 shift *= 3 ;
10881088 }
1089+ shift -- ; /* expected value in range(-2**shift, 2**shift) */
10891090
10901091 PyObject * mod = _PyLong_Lshift (PyLong_FromLong (1 ), shift );
1091- PyObject * mod2 = _PyLong_Rshift (mod , 1 );
1092- PyObject * value2 = value ;
10931092
1094- if (mod == NULL || mod2 == NULL ) {
1095- Py_XDECREF (mod );
1096- Py_XDECREF (mod2 );
1093+ if (mod == NULL ) {
10971094 goto done ;
10981095 }
10991096 if (PyLong_IsNegative (value )) {
1100- value2 = PyNumber_Negative (mod2 );
1101- if (value2 == NULL ) {
1102- Py_DECREF (mod2 );
1097+ Py_SETREF (mod , PyNumber_Negative (mod ));
1098+ if (mod == NULL ) {
11031099 goto done ;
11041100 }
1105- Py_SETREF (mod2 , value2 );
1106- if (PyObject_RichCompareBool (value , mod2 , Py_LT )) {
1107- Py_DECREF (mod2 );
1108- PyErr_Format (PyExc_ValueError ,
1109- "Expected integer in range(-2**%ld, 2**%ld)" ,
1110- shift - 1 , shift - 1 );
1111- goto done ;
1101+ if (PyObject_RichCompareBool (value , mod , Py_LT )) {
1102+ goto range ;
11121103 }
1113- Py_DECREF ( mod2 );
1114- value2 = PyNumber_Add (value , mod );
1104+ Py_SETREF ( mod , _PyLong_Lshift ( mod , 1 ) );
1105+ tmp = PyNumber_Subtract (value , mod );
11151106 Py_DECREF (mod );
1116- if (value2 == NULL ) {
1107+ if (tmp == NULL ) {
11171108 goto done ;
11181109 }
1110+ Py_SETREF (tmp , _PyLong_Format (tmp , base ));
11191111 }
11201112 else {
1121- if (PyObject_RichCompareBool (value2 , mod2 , Py_GE )) {
1113+ if (PyObject_RichCompareBool (value , mod , Py_GE )) {
1114+ range :
11221115 Py_DECREF (mod );
1123- Py_DECREF (mod2 );
11241116 PyErr_Format (PyExc_ValueError ,
11251117 "Expected integer in range(-2**%ld, 2**%ld)" ,
1126- shift - 1 , shift - 1 );
1118+ shift , shift );
11271119 goto done ;
11281120 }
11291121 Py_DECREF (mod );
1130- Py_DECREF (mod2 );
1131- Py_INCREF (value2 );
1122+ tmp = _PyLong_Format (value , base );
11321123 }
1133- tmp = _PyLong_Format (value2 , base );
1134- Py_DECREF (value2 );
11351124 }
11361125 else {
11371126 tmp = _PyLong_Format (value , base );
11381127 }
1128+ if (tmp == NULL ) {
1129+ goto done ;
1130+ }
11391131
11401132 /* Prepend enough leading zeros (after the sign) */
11411133
0 commit comments