@@ -522,49 +522,50 @@ list_dealloc(PyObject *self)
522
522
static PyObject *
523
523
list_repr_impl (PyListObject * v )
524
524
{
525
- PyObject * s ;
526
- _PyUnicodeWriter writer ;
527
- Py_ssize_t i = Py_ReprEnter ((PyObject * )v );
528
- if (i != 0 ) {
529
- return i > 0 ? PyUnicode_FromString ("[...]" ) : NULL ;
525
+ int res = Py_ReprEnter ((PyObject * )v );
526
+ if (res != 0 ) {
527
+ return (res > 0 ? PyUnicode_FromString ("[...]" ) : NULL );
530
528
}
531
529
532
- _PyUnicodeWriter_Init (& writer );
533
- writer .overallocate = 1 ;
534
530
/* "[" + "1" + ", 2" * (len - 1) + "]" */
535
- writer .min_length = 1 + 1 + (2 + 1 ) * (Py_SIZE (v ) - 1 ) + 1 ;
531
+ Py_ssize_t prealloc = 1 + 1 + (2 + 1 ) * (Py_SIZE (v ) - 1 ) + 1 ;
532
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (prealloc );
533
+ if (writer == NULL ) {
534
+ goto error ;
535
+ }
536
536
537
- if (_PyUnicodeWriter_WriteChar ( & writer , '[' ) < 0 )
537
+ if (PyUnicodeWriter_WriteChar ( writer , '[' ) < 0 ) {
538
538
goto error ;
539
+ }
539
540
540
541
/* Do repr() on each element. Note that this may mutate the list,
541
542
so must refetch the list size on each iteration. */
542
- for (i = 0 ; i < Py_SIZE (v ); ++ i ) {
543
+ for (Py_ssize_t i = 0 ; i < Py_SIZE (v ); ++ i ) {
543
544
if (i > 0 ) {
544
- if (_PyUnicodeWriter_WriteASCIIString (& writer , ", " , 2 ) < 0 )
545
+ if (PyUnicodeWriter_WriteChar (writer , ',' ) < 0 ) {
546
+ goto error ;
547
+ }
548
+ if (PyUnicodeWriter_WriteChar (writer , ' ' ) < 0 ) {
545
549
goto error ;
550
+ }
546
551
}
547
552
548
- s = PyObject_Repr (v -> ob_item [i ]);
549
- if (s == NULL )
550
- goto error ;
551
-
552
- if (_PyUnicodeWriter_WriteStr (& writer , s ) < 0 ) {
553
- Py_DECREF (s );
553
+ if (PyUnicodeWriter_WriteRepr (writer , v -> ob_item [i ]) < 0 ) {
554
554
goto error ;
555
555
}
556
- Py_DECREF (s );
557
556
}
558
557
559
- writer .overallocate = 0 ;
560
- if (_PyUnicodeWriter_WriteChar (& writer , ']' ) < 0 )
558
+ if (PyUnicodeWriter_WriteChar (writer , ']' ) < 0 ) {
561
559
goto error ;
560
+ }
562
561
563
562
Py_ReprLeave ((PyObject * )v );
564
- return _PyUnicodeWriter_Finish ( & writer );
563
+ return PyUnicodeWriter_Finish ( writer );
565
564
566
565
error :
567
- _PyUnicodeWriter_Dealloc (& writer );
566
+ if (writer != NULL ) {
567
+ PyUnicodeWriter_Discard (writer );
568
+ }
568
569
Py_ReprLeave ((PyObject * )v );
569
570
return NULL ;
570
571
}
0 commit comments