Skip to content

Commit 109526a

Browse files
committed
make implementations of list repeat and tuple repeat similar
1 parent 7d27c22 commit 109526a

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

Objects/listobject.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,8 @@ list_concat(PyListObject *a, PyObject *bb)
542542
static PyObject *
543543
list_repeat(PyListObject *a, Py_ssize_t n)
544544
{
545-
if (n < 0)
546-
n = 0;
547545
const Py_ssize_t input_size = Py_SIZE(a);
548-
if (input_size == 0 || n==0)
546+
if (input_size == 0 || n<=0)
549547
return PyList_New(0);
550548
assert(n>0);
551549

@@ -554,7 +552,6 @@ list_repeat(PyListObject *a, Py_ssize_t n)
554552
Py_ssize_t size = input_size * n;
555553

556554
PyListObject *np = (PyListObject *) list_new_prealloc(size);
557-
558555
if (np == NULL)
559556
return NULL;
560557

@@ -737,10 +734,7 @@ PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
737734
static PyObject *
738735
list_inplace_repeat(PyListObject *self, Py_ssize_t n)
739736
{
740-
Py_ssize_t size;
741-
742-
743-
size = PyList_GET_SIZE(self);
737+
Py_ssize_t size = PyList_GET_SIZE(self);
744738
if (size == 0 || n == 1) {
745739
Py_INCREF(self);
746740
return (PyObject *)self;
@@ -767,7 +761,7 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
767761
const Py_ssize_t len_dest = n * size;
768762
while (copied < len_dest) {
769763
Py_ssize_t items_to_copy = Py_MIN(copied, len_dest - copied);
770-
memcpy(items + copied, items, sizeof(PyObject*)*items_to_copy);
764+
memcpy(items + copied, items, sizeof(PyObject*) * items_to_copy);
771765
copied += items_to_copy;
772766
}
773767

Objects/tupleobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,10 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
510510
}
511511
assert(n>0);
512512

513-
if (n > PY_SSIZE_T_MAX / input_size)
513+
if (input_size > PY_SSIZE_T_MAX / n)
514514
return PyErr_NoMemory();
515515
Py_ssize_t size = input_size * n;
516+
516517
PyTupleObject *np = tuple_alloc(size);
517518
if (np == NULL)
518519
return NULL;

0 commit comments

Comments
 (0)