bpo-34151: Improve performance of some list operations by sir-sigurd · Pull Request #8332 · python/cpython

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially, in this case deallocating the list will decref uninitialized list items

No, it won't. Either the PyMem_Malloc() fails, then ob_item is NULL and the list size is 0, which is consistent and can be cleaned up safely. Or, if PyMem_Malloc() succeeds, then ob_item points to the item array, which will be filled up right afterwards.

I think this code is ok as it is.