Skip to content

Commit b678442

Browse files
committed
Simplify implementation and don't capture all expections on error
1 parent 717ca67 commit b678442

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Objects/listobject.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int
8080
list_preallocate_exact(PyListObject *self, Py_ssize_t size)
8181
{
8282
PyObject **items;
83-
size_t allocated, num_allocated_bytes;
83+
size_t allocated;
8484

8585
allocated = (size_t)size;
8686
if (allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
@@ -91,8 +91,7 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
9191
if (size == 0) {
9292
allocated = 0;
9393
}
94-
num_allocated_bytes = allocated * sizeof(PyObject *);
95-
items = (PyObject **)PyMem_Malloc(num_allocated_bytes);
94+
items = (PyObject **)PyMem_New(PyObject*, allocated);
9695
if (items == NULL) {
9796
PyErr_NoMemory();
9897
return -1;
@@ -2675,10 +2674,14 @@ list___init___impl(PyListObject *self, PyObject *iterable)
26752674
(void)_list_clear(self);
26762675
}
26772676
if (iterable != NULL) {
2678-
if(_PyObject_HasLen(iterable)){
2679-
Py_ssize_t iter_len = PyObject_Length(iterable);
2677+
if (_PyObject_HasLen(iterable) && self->ob_item == NULL) {
2678+
Py_ssize_t iter_len = PyObject_Size(iterable);
26802679
if (iter_len == -1) {
2681-
PyErr_Clear();
2680+
if (PyErr_ExceptionMatches(PyExc_Exception)) {
2681+
PyErr_Clear();
2682+
} else {
2683+
return -1;
2684+
}
26822685
}
26832686
if (iter_len > 0 && list_preallocate_exact(self, iter_len)) {
26842687
return -1;

0 commit comments

Comments
 (0)