@@ -809,42 +809,80 @@ dict_print(register dictobject *mp, register FILE *fp, register int flags)
809809static PyObject *
810810dict_repr (dictobject * mp )
811811{
812- auto PyObject * v ;
813- PyObject * sepa , * colon ;
814- register int i ;
815- register int any ;
812+ int i , pos ;
813+ PyObject * s , * temp , * colon = NULL ;
814+ PyObject * pieces = NULL , * result = NULL ;
815+ PyObject * key , * value ;
816816
817- i = Py_ReprEnter ((PyObject * )mp );
817+ i = Py_ReprEnter ((PyObject * )mp );
818818 if (i != 0 ) {
819- if (i > 0 )
820- return PyString_FromString ("{...}" );
821- return NULL ;
819+ return i > 0 ? PyString_FromString ("{...}" ) : NULL ;
822820 }
823821
824- v = PyString_FromString ("{" );
825- sepa = PyString_FromString (", " );
826- colon = PyString_FromString (": " );
827- any = 0 ;
828- for (i = 0 ; i <= mp -> ma_mask && v ; i ++ ) {
829- dictentry * ep = mp -> ma_table + i ;
830- PyObject * pvalue = ep -> me_value ;
831- if (pvalue != NULL ) {
832- /* Prevent PyObject_Repr from deleting value during
833- key format */
834- Py_INCREF (pvalue );
835- if (any ++ )
836- PyString_Concat (& v , sepa );
837- PyString_ConcatAndDel (& v , PyObject_Repr (ep -> me_key ));
838- PyString_Concat (& v , colon );
839- PyString_ConcatAndDel (& v , PyObject_Repr (pvalue ));
840- Py_DECREF (pvalue );
841- }
822+ if (mp -> ma_used == 0 ) {
823+ result = PyString_FromString ("{}" );
824+ goto Done ;
842825 }
843- PyString_ConcatAndDel (& v , PyString_FromString ("}" ));
844- Py_ReprLeave ((PyObject * )mp );
845- Py_XDECREF (sepa );
826+
827+ pieces = PyList_New (0 );
828+ if (pieces == NULL )
829+ goto Done ;
830+
831+ colon = PyString_FromString (": " );
832+ if (colon == NULL )
833+ goto Done ;
834+
835+ /* Do repr() on each key+value pair, and insert ": " between them.
836+ Note that repr may mutate the dict. */
837+ pos = 0 ;
838+ while (PyDict_Next ((PyObject * )mp , & pos , & key , & value )) {
839+ int status ;
840+ /* Prevent repr from deleting value during key format. */
841+ Py_INCREF (value );
842+ s = PyObject_Repr (key );
843+ PyString_Concat (& s , colon );
844+ PyString_ConcatAndDel (& s , PyObject_Repr (value ));
845+ Py_DECREF (value );
846+ if (s == NULL )
847+ goto Done ;
848+ status = PyList_Append (pieces , s );
849+ Py_DECREF (s ); /* append created a new ref */
850+ if (status < 0 )
851+ goto Done ;
852+ }
853+
854+ /* Add "{}" decorations to the first and last items. */
855+ assert (PyList_GET_SIZE (pieces ) > 0 );
856+ s = PyString_FromString ("{" );
857+ if (s == NULL )
858+ goto Done ;
859+ temp = PyList_GET_ITEM (pieces , 0 );
860+ PyString_ConcatAndDel (& s , temp );
861+ PyList_SET_ITEM (pieces , 0 , s );
862+ if (s == NULL )
863+ goto Done ;
864+
865+ s = PyString_FromString ("}" );
866+ if (s == NULL )
867+ goto Done ;
868+ temp = PyList_GET_ITEM (pieces , PyList_GET_SIZE (pieces ) - 1 );
869+ PyString_ConcatAndDel (& temp , s );
870+ PyList_SET_ITEM (pieces , PyList_GET_SIZE (pieces ) - 1 , temp );
871+ if (temp == NULL )
872+ goto Done ;
873+
874+ /* Paste them all together with ", " between. */
875+ s = PyString_FromString (", " );
876+ if (s == NULL )
877+ goto Done ;
878+ result = _PyString_Join (s , pieces );
879+ Py_DECREF (s );
880+
881+ Done :
882+ Py_XDECREF (pieces );
846883 Py_XDECREF (colon );
847- return v ;
884+ Py_ReprLeave ((PyObject * )mp );
885+ return result ;
848886}
849887
850888static int
0 commit comments