summaryrefslogtreecommitdiffstats
path: root/Modules/_collectionsmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2015-09-10 02:39:44 (GMT)
committerRaymond Hettinger <python@rcn.com>2015-09-10 02:39:44 (GMT)
commitf5d72f35e854575772d971e35d64d643465019c6 (patch)
tree621d9974754a05a75f8ea5cbcb774959127d3537 /Modules/_collectionsmodule.c
parent87e8fe6cb3df6c89b6e3c32b1413a3901de6201f (diff)
downloadcpython-f5d72f35e854575772d971e35d64d643465019c6.zip
cpython-f5d72f35e854575772d971e35d64d643465019c6.tar.gz
cpython-f5d72f35e854575772d971e35d64d643465019c6.tar.bz2
Simply deque repeat by reusing code in in-line repeat. Avoid unnecessary division.
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r--Modules/_collectionsmodule.c48
1 files changed, 18 insertions, 30 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index ea3e88f..db27e92 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -539,32 +539,6 @@ deque_concat(dequeobject *deque, PyObject *other)
static void deque_clear(dequeobject *deque);
static PyObject *
-deque_repeat(dequeobject *deque, Py_ssize_t n)
-{
- dequeobject *new_deque;
- PyObject *result;
-
- /* XXX add a special case for when maxlen is defined */
- if (n < 0)
- n = 0;
- else if (n > 0 && Py_SIZE(deque) > MAX_DEQUE_LEN / n)
- return PyErr_NoMemory();
-
- new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
- new_deque->maxlen = deque->maxlen;
-
- for ( ; n ; n--) {
- result = deque_extend(new_deque, (PyObject *)deque);
- if (result == NULL) {
- Py_DECREF(new_deque);
- return NULL;
- }
- Py_DECREF(result);
- }
- return (PyObject *)new_deque;
-}
-
-static PyObject *
deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
{
Py_ssize_t i, size;
@@ -583,10 +557,6 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
- if (size > MAX_DEQUE_LEN / n) {
- return PyErr_NoMemory();
- }
-
if (size == 1) {
/* common case, repeating a single element */
PyObject *item = deque->leftblock->data[deque->leftindex];
@@ -594,6 +564,9 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
if (deque->maxlen != -1 && n > deque->maxlen)
n = deque->maxlen;
+ if (n > MAX_DEQUE_LEN)
+ return PyErr_NoMemory();
+
for (i = 0 ; i < n-1 ; i++) {
rv = deque_append(deque, item);
if (rv == NULL)
@@ -604,6 +577,10 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
+ if ((size_t)size > MAX_DEQUE_LEN / (size_t)n) {
+ return PyErr_NoMemory();
+ }
+
seq = PySequence_List((PyObject *)deque);
if (seq == NULL)
return seq;
@@ -621,6 +598,17 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
+static PyObject *
+deque_repeat(dequeobject *deque, Py_ssize_t n)
+{
+ dequeobject *new_deque;
+
+ new_deque = (dequeobject *)deque_copy((PyObject *) deque);
+ if (new_deque == NULL)
+ return NULL;
+ return deque_inplace_repeat(new_deque, n);
+}
+
/* The rotate() method is part of the public API and is used internally
as a primitive for other methods.