@@ -510,17 +510,14 @@ formatlong(PyObject *v, int flags, int prec, int type)
510510 iobj = _PyNumber_Index (v );
511511 else
512512 iobj = PyNumber_Long (v );
513- if (iobj == NULL ) {
514- if (!PyErr_ExceptionMatches (PyExc_TypeError ))
515- return NULL ;
516- }
517- else if (!PyLong_Check (iobj ))
518- Py_CLEAR (iobj );
519513 if (iobj != NULL ) {
514+ assert (PyLong_Check (iobj ));
520515 result = _PyUnicode_FormatLong (iobj , flags & F_ALT , prec , type );
521516 Py_DECREF (iobj );
522517 return result ;
523518 }
519+ if (!PyErr_ExceptionMatches (PyExc_TypeError ))
520+ return NULL ;
524521 }
525522 PyErr_Format (PyExc_TypeError ,
526523 "%%%c format: %s is required, not %.200s" , type ,
@@ -542,26 +539,16 @@ byte_converter(PyObject *arg, char *p)
542539 return 1 ;
543540 }
544541 else {
545- PyObject * iobj ;
546- long ival ;
547542 int overflow ;
548- /* make sure number is a type of integer */
549- if (PyLong_Check (arg )) {
550- ival = PyLong_AsLongAndOverflow (arg , & overflow );
551- }
552- else {
553- iobj = PyNumber_Index (arg );
554- if (iobj == NULL ) {
555- if (!PyErr_ExceptionMatches (PyExc_TypeError ))
556- return 0 ;
543+ long ival = PyLong_AsLongAndOverflow (arg , & overflow );
544+ if (ival == -1 && PyErr_Occurred ()) {
545+ if (PyErr_ExceptionMatches (PyExc_TypeError )) {
557546 goto onError ;
558547 }
559- ival = PyLong_AsLongAndOverflow (iobj , & overflow );
560- Py_DECREF (iobj );
548+ return 0 ;
561549 }
562- if (!overflow && ival == -1 && PyErr_Occurred ())
563- goto onError ;
564- if (overflow || !(0 <= ival && ival <= 255 )) {
550+ if (!(0 <= ival && ival <= 255 )) {
551+ /* this includes an overflow in converting to C long */
565552 PyErr_SetString (PyExc_OverflowError ,
566553 "%c arg not in range(256)" );
567554 return 0 ;
0 commit comments