@@ -4449,7 +4449,7 @@ PyListWriter_Create(Py_ssize_t size)
44494449 // since the list object is not tracked by the GC, and the writer caller
44504450 // must initialize all items.
44514451
4452- #ifdef Py_DEBUG
4452+ #if _PyListWriter_INIT_ITEMS
44534453 // In debug mode, set all items to NULL to be able to check that they are
44544454 // non-NULL in PyListWriter_Finish().
44554455 if (1 <= size ) {
@@ -4498,7 +4498,7 @@ PyListWriter_Finish(PyListWriter *writer)
44984498 // it's left unchanged on memory allocation failure.
44994499 }
45004500
4501- #ifdef Py_DEBUG
4501+ #if _PyListWriter_INIT_ITEMS
45024502 // Make sure that all items have been initialized
45034503 Py_ssize_t size = Py_SIZE (list );
45044504 PyObject * * list_items = list -> ob_item ;
@@ -4513,13 +4513,21 @@ PyListWriter_Finish(PyListWriter *writer)
45134513}
45144514
45154515
4516- static int
4517- listwriter_resize (PyListWriter * writer , Py_ssize_t new_size )
4516+ int
4517+ _PyListWriter_Resize (PyListWriter * writer , Py_ssize_t new_size )
45184518{
45194519 PyListObject * list = _PyList_CAST (writer );
4520+ Py_ssize_t old_size = Py_SIZE (list );
45204521
45214522 // On Free Threading, it can call _PyObject_GC_SET_SHARED() on the list
4522- return list_resize (list , new_size );
4523+ int res = list_resize (list , new_size );
4524+ #if _PyListWriter_INIT_ITEMS
4525+ if (res == 0 && new_size > old_size ) {
4526+ memset (& list -> ob_item [old_size ], 0 ,
4527+ (new_size - old_size ) * sizeof (PyObject * ));
4528+ }
4529+ #endif
4530+ return res ;
45234531}
45244532
45254533
@@ -4529,19 +4537,19 @@ PyListWriter_AppendSteal(PyListWriter *writer, PyObject *item)
45294537 assert (listwriter_check_consistency (writer ));
45304538 PyListObject * list = _PyList_CAST (writer );
45314539
4532- Py_ssize_t len = Py_SIZE (list );
4540+ Py_ssize_t index = Py_SIZE (list );
45334541 Py_ssize_t allocated = list -> allocated ;
4534- if (len < allocated ) {
4535- list -> ob_item [ len ] = item ;
4536- Py_SET_SIZE ( list , len + 1 ) ;
4542+ if (index < allocated ) {
4543+ Py_SET_SIZE ( list , index + 1 ) ;
4544+ list -> ob_item [ index ] = item ;
45374545 return 0 ;
45384546 }
45394547
4540- if (listwriter_resize (writer , len + 1 ) < 0 ) {
4548+ if (_PyListWriter_Resize (writer , index + 1 ) < 0 ) {
45414549 Py_DECREF (item );
45424550 return -1 ;
45434551 }
4544- list -> ob_item [ len ] = item ;
4552+ _PyListWriter_SET_ITEM_STEAL ( writer , index , item ) ;
45454553 return 0 ;
45464554}
45474555
@@ -4568,7 +4576,7 @@ _PyListWriter_ResetSize(PyListWriter *writer, Py_ssize_t size)
45684576 PyObject * * items = list -> ob_item ;
45694577 for (Py_ssize_t i = size ; i < new_size ; i ++ ) {
45704578 Py_DECREF (items [i ]);
4571- #ifdef Py_DEBUG
4579+ #if _PyListWriter_INIT_ITEMS
45724580 items [i ] = NULL ;
45734581#endif
45744582 }
@@ -4652,10 +4660,9 @@ listwriter_extend_array(PyListWriter *writer,
46524660
46534661 PyListObject * list = _PyList_CAST (writer );
46544662 Py_ssize_t size = Py_SIZE (list );
4655- Py_ssize_t allocated = list -> allocated ;
46564663 Py_ssize_t new_size = size + nitem ;
4657- if (new_size > allocated ) {
4658- if (listwriter_resize (writer , new_size ) < 0 ) {
4664+ if (new_size > list -> allocated ) {
4665+ if (_PyListWriter_Resize (writer , new_size ) < 0 ) {
46594666 if (steal ) {
46604667 for (Py_ssize_t i = 0 ; i < nitem ; i ++ ) {
46614668 Py_DECREF (items [i ]);
0 commit comments