@@ -321,70 +321,59 @@ static PyObject *
321321list_repr (PyListObject * v )
322322{
323323 Py_ssize_t i ;
324- PyObject * s , * temp ;
325- PyObject * pieces = NULL , * result = NULL ;
324+ PyObject * s = NULL ;
325+ _PyAccu acc ;
326+ static PyObject * sep = NULL ;
327+
328+ if (Py_SIZE (v ) == 0 ) {
329+ return PyUnicode_FromString ("[]" );
330+ }
331+
332+ if (sep == NULL ) {
333+ sep = PyUnicode_FromString (", " );
334+ if (sep == NULL )
335+ return NULL ;
336+ }
326337
327338 i = Py_ReprEnter ((PyObject * )v );
328339 if (i != 0 ) {
329340 return i > 0 ? PyUnicode_FromString ("[...]" ) : NULL ;
330341 }
331342
332- if (Py_SIZE (v ) == 0 ) {
333- result = PyUnicode_FromString ("[]" );
334- goto Done ;
335- }
343+ if (_PyAccu_Init (& acc ))
344+ goto error ;
336345
337- pieces = PyList_New (0 );
338- if (pieces == NULL )
339- goto Done ;
346+ s = PyUnicode_FromString ("[" );
347+ if (s == NULL || _PyAccu_Accumulate (& acc , s ))
348+ goto error ;
349+ Py_CLEAR (s );
340350
341351 /* Do repr() on each element. Note that this may mutate the list,
342352 so must refetch the list size on each iteration. */
343353 for (i = 0 ; i < Py_SIZE (v ); ++ i ) {
344- int status ;
345354 if (Py_EnterRecursiveCall (" while getting the repr of a list" ))
346- goto Done ;
355+ goto error ;
347356 s = PyObject_Repr (v -> ob_item [i ]);
348357 Py_LeaveRecursiveCall ();
349- if (s == NULL )
350- goto Done ;
351- status = PyList_Append (pieces , s );
352- Py_DECREF (s ); /* append created a new ref */
353- if (status < 0 )
354- goto Done ;
358+ if (i > 0 && _PyAccu_Accumulate (& acc , sep ))
359+ goto error ;
360+ if (s == NULL || _PyAccu_Accumulate (& acc , s ))
361+ goto error ;
362+ Py_CLEAR (s );
355363 }
364+ s = PyUnicode_FromString ("]" );
365+ if (s == NULL || _PyAccu_Accumulate (& acc , s ))
366+ goto error ;
367+ Py_CLEAR (s );
356368
357- /* Add "[]" decorations to the first and last items. */
358- assert (PyList_GET_SIZE (pieces ) > 0 );
359- s = PyUnicode_FromString ("[" );
360- if (s == NULL )
361- goto Done ;
362- temp = PyList_GET_ITEM (pieces , 0 );
363- PyUnicode_AppendAndDel (& s , temp );
364- PyList_SET_ITEM (pieces , 0 , s );
365- if (s == NULL )
366- goto Done ;
369+ Py_ReprLeave ((PyObject * )v );
370+ return _PyAccu_Finish (& acc );
367371
368- s = PyUnicode_FromString ("]" );
369- if (s == NULL )
370- goto Done ;
371- temp = PyList_GET_ITEM (pieces , PyList_GET_SIZE (pieces ) - 1 );
372- PyUnicode_AppendAndDel (& temp , s );
373- PyList_SET_ITEM (pieces , PyList_GET_SIZE (pieces ) - 1 , temp );
374- if (temp == NULL )
375- goto Done ;
376-
377- /* Paste them all together with ", " between. */
378- s = PyUnicode_FromString (", " );
379- if (s == NULL )
380- goto Done ;
381- result = PyUnicode_Join (s , pieces );
382- Py_DECREF (s );
383-
384- Done :
385- Py_XDECREF (pieces );
372+ error :
373+ _PyAccu_Destroy (& acc );
374+ Py_XDECREF (s );
386375 Py_ReprLeave ((PyObject * )v );
387- return result ;
376+ return NULL ;
388377}
389378
390379static Py_ssize_t
0 commit comments