Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /* Range object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Benjamin Peterson | 878ce38 | 2011-11-05 15:17:52 -0400 | [diff] [blame] | 4 | #include "structmember.h" |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 5 | #include "pycore_tupleobject.h" |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 7 | /* Support objects whose length is > PY_SSIZE_T_MAX. |
| 8 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 9 | This could be sped up for small PyLongs if they fit in a Py_ssize_t. |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10 | This only matters on Win64. Though we could use long long which |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 11 | would presumably help perf. |
| 12 | */ |
| 13 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 14 | typedef struct { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 15 | PyObject_HEAD |
| 16 | PyObject *start; |
| 17 | PyObject *stop; |
| 18 | PyObject *step; |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 19 | PyObject *length; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 20 | } rangeobject; |
| 21 | |
Hai Shi | 46874c2 | 2020-01-30 17:20:25 -0600 | [diff] [blame] | 22 | _Py_IDENTIFIER(iter); |
| 23 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 24 | /* Helper function for validating step. Always returns a new reference or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | NULL on error. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 26 | */ |
| 27 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 28 | validate_step(PyObject *step) |
| 29 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 30 | /* No step specified, use a step of 1. */ |
| 31 | if (!step) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 32 | return PyLong_FromLong(1); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 33 | |
| 34 | step = PyNumber_Index(step); |
Serhiy Storchaka | 5d062d7 | 2016-06-18 09:51:55 +0300 | [diff] [blame] | 35 | if (step && _PyLong_Sign(step) == 0) { |
| 36 | PyErr_SetString(PyExc_ValueError, |
| 37 | "range() arg 3 must not be zero"); |
| 38 | Py_CLEAR(step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | return step; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 44 | static PyObject * |
| 45 | compute_range_length(PyObject *start, PyObject *stop, PyObject *step); |
| 46 | |
| 47 | static rangeobject * |
| 48 | make_range_object(PyTypeObject *type, PyObject *start, |
| 49 | PyObject *stop, PyObject *step) |
| 50 | { |
| 51 | rangeobject *obj = NULL; |
| 52 | PyObject *length; |
| 53 | length = compute_range_length(start, stop, step); |
| 54 | if (length == NULL) { |
| 55 | return NULL; |
| 56 | } |
| 57 | obj = PyObject_New(rangeobject, type); |
| 58 | if (obj == NULL) { |
| 59 | Py_DECREF(length); |
| 60 | return NULL; |
| 61 | } |
| 62 | obj->start = start; |
| 63 | obj->stop = stop; |
| 64 | obj->step = step; |
| 65 | obj->length = length; |
| 66 | return obj; |
| 67 | } |
| 68 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 69 | /* XXX(nnorwitz): should we error check if the user passes any empty ranges? |
| 70 | range(-10) |
| 71 | range(0, -5) |
| 72 | range(0, 5, -1) |
| 73 | */ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 74 | static PyObject * |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 75 | range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 76 | { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 77 | rangeobject *obj; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 78 | PyObject *start = NULL, *stop = NULL, *step = NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 79 | |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 80 | switch (num_args) { |
| 81 | case 3: |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 82 | step = args[2]; |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 83 | /* fallthrough */ |
| 84 | case 2: |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 85 | /* Convert borrowed refs to owned refs */ |
| 86 | start = PyNumber_Index(args[0]); |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 87 | if (!start) { |
| 88 | return NULL; |
| 89 | } |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 90 | stop = PyNumber_Index(args[1]); |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 91 | if (!stop) { |
| 92 | Py_DECREF(start); |
| 93 | return NULL; |
| 94 | } |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 95 | step = validate_step(step); /* Caution, this can clear exceptions */ |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 96 | if (!step) { |
| 97 | Py_DECREF(start); |
| 98 | Py_DECREF(stop); |
| 99 | return NULL; |
| 100 | } |
| 101 | break; |
| 102 | case 1: |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 103 | stop = PyNumber_Index(args[0]); |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 104 | if (!stop) { |
| 105 | return NULL; |
| 106 | } |
| 107 | Py_INCREF(_PyLong_Zero); |
| 108 | start = _PyLong_Zero; |
| 109 | Py_INCREF(_PyLong_One); |
| 110 | step = _PyLong_One; |
| 111 | break; |
| 112 | case 0: |
| 113 | PyErr_SetString(PyExc_TypeError, |
| 114 | "range expected at least 1 argument, got 0"); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 115 | return NULL; |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 116 | default: |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 117 | PyErr_Format(PyExc_TypeError, |
Pablo Galindo | 4b66fa6 | 2020-01-05 17:30:53 +0000 | [diff] [blame] | 118 | "range expected at most 3 arguments, got %zd", |
| 119 | num_args); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 120 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 121 | } |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 122 | obj = make_range_object(type, start, stop, step); |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 123 | if (obj != NULL) { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 124 | return (PyObject *) obj; |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 125 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 126 | |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 127 | /* Failed to create object, release attributes */ |
Serhiy Storchaka | cfdfbb4 | 2016-06-18 09:44:03 +0300 | [diff] [blame] | 128 | Py_DECREF(start); |
| 129 | Py_DECREF(stop); |
| 130 | Py_DECREF(step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 131 | return NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 134 | static PyObject * |
| 135 | range_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 136 | { |
| 137 | if (!_PyArg_NoKeywords("range", kw)) |
| 138 | return NULL; |
| 139 | |
| 140 | return range_from_array(type, _PyTuple_ITEMS(args), PyTuple_GET_SIZE(args)); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static PyObject * |
| 145 | range_vectorcall(PyTypeObject *type, PyObject *const *args, |
| 146 | size_t nargsf, PyObject *kwnames) |
| 147 | { |
| 148 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 149 | if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) { |
| 150 | PyErr_Format(PyExc_TypeError, "range() takes no keyword arguments"); |
| 151 | return NULL; |
| 152 | } |
| 153 | return range_from_array(type, args, nargs); |
| 154 | } |
| 155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 156 | PyDoc_STRVAR(range_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 157 | "range(stop) -> range object\n\ |
| 158 | range(start, stop[, step]) -> range object\n\ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 159 | \n\ |
Benjamin Peterson | ac22c6b | 2015-04-22 09:16:07 -0400 | [diff] [blame] | 160 | Return an object that produces a sequence of integers from start (inclusive)\n\ |
| 161 | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.\n\ |
| 162 | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.\n\ |
| 163 | These are exactly the valid indices for a list of 4 elements.\n\ |
| 164 | When step is given, it specifies the increment (or decrement)."); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 166 | static void |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 167 | range_dealloc(rangeobject *r) |
| 168 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 169 | Py_DECREF(r->start); |
| 170 | Py_DECREF(r->stop); |
| 171 | Py_DECREF(r->step); |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 172 | Py_DECREF(r->length); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 173 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Mark Dickinson | 732166d | 2009-06-28 22:08:40 +0000 | [diff] [blame] | 176 | /* Return number of items in range (lo, hi, step) as a PyLong object, |
| 177 | * when arguments are PyLong objects. Arguments MUST return 1 with |
| 178 | * PyLong_Check(). Return NULL when there is an error. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 179 | */ |
| 180 | static PyObject* |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 181 | compute_range_length(PyObject *start, PyObject *stop, PyObject *step) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 182 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 183 | /* ------------------------------------------------------------- |
| 184 | Algorithm is equal to that of get_len_of_range(), but it operates |
Benjamin Peterson | a47af9c | 2009-06-24 21:14:38 +0000 | [diff] [blame] | 185 | on PyObjects (which are assumed to be PyLong objects). |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 186 | ---------------------------------------------------------------*/ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 187 | int cmp_result; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 188 | PyObject *lo, *hi; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 189 | PyObject *diff = NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 190 | PyObject *tmp1 = NULL, *tmp2 = NULL, *result; |
| 191 | /* holds sub-expression evaluations */ |
| 192 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 193 | cmp_result = PyObject_RichCompareBool(step, _PyLong_Zero, Py_GT); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 194 | if (cmp_result == -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 195 | return NULL; |
| 196 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 197 | if (cmp_result == 1) { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 198 | lo = start; |
| 199 | hi = stop; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 200 | Py_INCREF(step); |
| 201 | } else { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 202 | lo = stop; |
| 203 | hi = start; |
| 204 | step = PyNumber_Negative(step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 205 | if (!step) |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | /* if (lo >= hi), return length of 0. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 210 | cmp_result = PyObject_RichCompareBool(lo, hi, Py_GE); |
| 211 | if (cmp_result != 0) { |
Serhiy Storchaka | cfdfbb4 | 2016-06-18 09:44:03 +0300 | [diff] [blame] | 212 | Py_DECREF(step); |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 213 | if (cmp_result < 0) |
| 214 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 215 | return PyLong_FromLong(0); |
| 216 | } |
| 217 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 218 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 219 | goto Fail; |
| 220 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 221 | if ((diff = PyNumber_Subtract(tmp1, _PyLong_One)) == NULL) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 222 | goto Fail; |
| 223 | |
| 224 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 225 | goto Fail; |
| 226 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 227 | if ((result = PyNumber_Add(tmp2, _PyLong_One)) == NULL) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 228 | goto Fail; |
| 229 | |
| 230 | Py_DECREF(tmp2); |
| 231 | Py_DECREF(diff); |
| 232 | Py_DECREF(step); |
| 233 | Py_DECREF(tmp1); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 234 | return result; |
| 235 | |
| 236 | Fail: |
Serhiy Storchaka | cfdfbb4 | 2016-06-18 09:44:03 +0300 | [diff] [blame] | 237 | Py_DECREF(step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 238 | Py_XDECREF(tmp2); |
| 239 | Py_XDECREF(diff); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 240 | Py_XDECREF(tmp1); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 241 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 244 | static Py_ssize_t |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 245 | range_length(rangeobject *r) |
| 246 | { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 247 | return PyLong_AsSsize_t(r->length); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 250 | static PyObject * |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 251 | compute_item(rangeobject *r, PyObject *i) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 252 | { |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 253 | PyObject *incr, *result; |
| 254 | /* PyLong equivalent to: |
| 255 | * return r->start + (i * r->step) |
| 256 | */ |
| 257 | incr = PyNumber_Multiply(i, r->step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 258 | if (!incr) |
| 259 | return NULL; |
| 260 | result = PyNumber_Add(r->start, incr); |
| 261 | Py_DECREF(incr); |
| 262 | return result; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 265 | static PyObject * |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 266 | compute_range_item(rangeobject *r, PyObject *arg) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 267 | { |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 268 | int cmp_result; |
| 269 | PyObject *i, *result; |
Tim Peters | d976ab7 | 2004-08-08 06:29:10 +0000 | [diff] [blame] | 270 | |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 271 | /* PyLong equivalent to: |
| 272 | * if (arg < 0) { |
| 273 | * i = r->length + arg |
| 274 | * } else { |
| 275 | * i = arg |
| 276 | * } |
| 277 | */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 278 | cmp_result = PyObject_RichCompareBool(arg, _PyLong_Zero, Py_LT); |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 279 | if (cmp_result == -1) { |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 280 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 281 | } |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 282 | if (cmp_result == 1) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 283 | i = PyNumber_Add(r->length, arg); |
| 284 | if (!i) { |
| 285 | return NULL; |
| 286 | } |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 287 | } else { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 288 | i = arg; |
| 289 | Py_INCREF(i); |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* PyLong equivalent to: |
| 293 | * if (i < 0 || i >= r->length) { |
| 294 | * <report index out of bounds> |
| 295 | * } |
| 296 | */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 297 | cmp_result = PyObject_RichCompareBool(i, _PyLong_Zero, Py_LT); |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 298 | if (cmp_result == 0) { |
| 299 | cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE); |
| 300 | } |
| 301 | if (cmp_result == -1) { |
| 302 | Py_DECREF(i); |
| 303 | return NULL; |
| 304 | } |
| 305 | if (cmp_result == 1) { |
| 306 | Py_DECREF(i); |
| 307 | PyErr_SetString(PyExc_IndexError, |
| 308 | "range object index out of range"); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | result = compute_item(r, i); |
| 313 | Py_DECREF(i); |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | static PyObject * |
| 318 | range_item(rangeobject *r, Py_ssize_t i) |
| 319 | { |
Antoine Pitrou | a103b96 | 2012-05-16 14:37:54 +0200 | [diff] [blame] | 320 | PyObject *res, *arg = PyLong_FromSsize_t(i); |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 321 | if (!arg) { |
| 322 | return NULL; |
| 323 | } |
Benjamin Peterson | 547d485 | 2011-01-13 04:22:54 +0000 | [diff] [blame] | 324 | res = compute_range_item(r, arg); |
| 325 | Py_DECREF(arg); |
| 326 | return res; |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 329 | static PyObject * |
| 330 | compute_slice(rangeobject *r, PyObject *_slice) |
| 331 | { |
| 332 | PySliceObject *slice = (PySliceObject *) _slice; |
| 333 | rangeobject *result; |
| 334 | PyObject *start = NULL, *stop = NULL, *step = NULL; |
| 335 | PyObject *substart = NULL, *substop = NULL, *substep = NULL; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 336 | int error; |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 337 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 338 | error = _PySlice_GetLongIndices(slice, r->length, &start, &stop, &step); |
| 339 | if (error == -1) |
| 340 | return NULL; |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 341 | |
| 342 | substep = PyNumber_Multiply(r->step, step); |
| 343 | if (substep == NULL) goto fail; |
| 344 | Py_CLEAR(step); |
| 345 | |
| 346 | substart = compute_item(r, start); |
| 347 | if (substart == NULL) goto fail; |
| 348 | Py_CLEAR(start); |
| 349 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 350 | substop = compute_item(r, stop); |
| 351 | if (substop == NULL) goto fail; |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 352 | Py_CLEAR(stop); |
| 353 | |
| 354 | result = make_range_object(Py_TYPE(r), substart, substop, substep); |
| 355 | if (result != NULL) { |
| 356 | return (PyObject *) result; |
| 357 | } |
| 358 | fail: |
| 359 | Py_XDECREF(start); |
| 360 | Py_XDECREF(stop); |
| 361 | Py_XDECREF(step); |
| 362 | Py_XDECREF(substart); |
| 363 | Py_XDECREF(substop); |
| 364 | Py_XDECREF(substep); |
| 365 | return NULL; |
Alexandre Vassalotti | 7505607 | 2008-06-10 04:03:04 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 368 | /* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */ |
| 369 | static int |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 370 | range_contains_long(rangeobject *r, PyObject *ob) |
| 371 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 372 | int cmp1, cmp2, cmp3; |
| 373 | PyObject *tmp1 = NULL; |
| 374 | PyObject *tmp2 = NULL; |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 375 | int result = -1; |
| 376 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 377 | /* Check if the value can possibly be in the range. */ |
| 378 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 379 | cmp1 = PyObject_RichCompareBool(r->step, _PyLong_Zero, Py_GT); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 380 | if (cmp1 == -1) |
| 381 | goto end; |
| 382 | if (cmp1 == 1) { /* positive steps: start <= ob < stop */ |
| 383 | cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE); |
| 384 | cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT); |
| 385 | } |
| 386 | else { /* negative steps: stop < ob <= start */ |
| 387 | cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE); |
| 388 | cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT); |
| 389 | } |
| 390 | |
| 391 | if (cmp2 == -1 || cmp3 == -1) /* TypeError */ |
| 392 | goto end; |
| 393 | if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */ |
| 394 | result = 0; |
| 395 | goto end; |
| 396 | } |
| 397 | |
| 398 | /* Check that the stride does not invalidate ob's membership. */ |
| 399 | tmp1 = PyNumber_Subtract(ob, r->start); |
| 400 | if (tmp1 == NULL) |
| 401 | goto end; |
| 402 | tmp2 = PyNumber_Remainder(tmp1, r->step); |
| 403 | if (tmp2 == NULL) |
| 404 | goto end; |
Berker Peksag | ed6224e | 2016-09-12 07:47:04 +0300 | [diff] [blame] | 405 | /* result = ((int(ob) - start) % step) == 0 */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 406 | result = PyObject_RichCompareBool(tmp2, _PyLong_Zero, Py_EQ); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 407 | end: |
| 408 | Py_XDECREF(tmp1); |
| 409 | Py_XDECREF(tmp2); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 410 | return result; |
| 411 | } |
| 412 | |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 413 | static int |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 414 | range_contains(rangeobject *r, PyObject *ob) |
| 415 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 416 | if (PyLong_CheckExact(ob) || PyBool_Check(ob)) |
| 417 | return range_contains_long(r, ob); |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 418 | |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 419 | return (int)_PySequence_IterSearch((PyObject*)r, ob, |
| 420 | PY_ITERSEARCH_CONTAINS); |
| 421 | } |
| 422 | |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 423 | /* Compare two range objects. Return 1 for equal, 0 for not equal |
| 424 | and -1 on error. The algorithm is roughly the C equivalent of |
| 425 | |
| 426 | if r0 is r1: |
| 427 | return True |
| 428 | if len(r0) != len(r1): |
| 429 | return False |
| 430 | if not len(r0): |
| 431 | return True |
| 432 | if r0.start != r1.start: |
| 433 | return False |
| 434 | if len(r0) == 1: |
| 435 | return True |
| 436 | return r0.step == r1.step |
| 437 | */ |
| 438 | static int |
| 439 | range_equals(rangeobject *r0, rangeobject *r1) |
| 440 | { |
| 441 | int cmp_result; |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 442 | |
| 443 | if (r0 == r1) |
| 444 | return 1; |
| 445 | cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ); |
| 446 | /* Return False or error to the caller. */ |
| 447 | if (cmp_result != 1) |
| 448 | return cmp_result; |
| 449 | cmp_result = PyObject_Not(r0->length); |
| 450 | /* Return True or error to the caller. */ |
| 451 | if (cmp_result != 0) |
| 452 | return cmp_result; |
| 453 | cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ); |
| 454 | /* Return False or error to the caller. */ |
| 455 | if (cmp_result != 1) |
| 456 | return cmp_result; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 457 | cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_One, Py_EQ); |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 458 | /* Return True or error to the caller. */ |
| 459 | if (cmp_result != 0) |
| 460 | return cmp_result; |
| 461 | return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ); |
| 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | range_richcompare(PyObject *self, PyObject *other, int op) |
| 466 | { |
| 467 | int result; |
| 468 | |
| 469 | if (!PyRange_Check(other)) |
| 470 | Py_RETURN_NOTIMPLEMENTED; |
| 471 | switch (op) { |
| 472 | case Py_NE: |
| 473 | case Py_EQ: |
| 474 | result = range_equals((rangeobject*)self, (rangeobject*)other); |
| 475 | if (result == -1) |
| 476 | return NULL; |
| 477 | if (op == Py_NE) |
| 478 | result = !result; |
| 479 | if (result) |
| 480 | Py_RETURN_TRUE; |
| 481 | else |
| 482 | Py_RETURN_FALSE; |
| 483 | case Py_LE: |
| 484 | case Py_GE: |
| 485 | case Py_LT: |
| 486 | case Py_GT: |
| 487 | Py_RETURN_NOTIMPLEMENTED; |
| 488 | default: |
| 489 | PyErr_BadArgument(); |
| 490 | return NULL; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* Hash function for range objects. Rough C equivalent of |
| 495 | |
| 496 | if not len(r): |
| 497 | return hash((len(r), None, None)) |
| 498 | if len(r) == 1: |
| 499 | return hash((len(r), r.start, None)) |
| 500 | return hash((len(r), r.start, r.step)) |
| 501 | */ |
| 502 | static Py_hash_t |
| 503 | range_hash(rangeobject *r) |
| 504 | { |
| 505 | PyObject *t; |
| 506 | Py_hash_t result = -1; |
| 507 | int cmp_result; |
| 508 | |
| 509 | t = PyTuple_New(3); |
| 510 | if (!t) |
| 511 | return -1; |
| 512 | Py_INCREF(r->length); |
| 513 | PyTuple_SET_ITEM(t, 0, r->length); |
| 514 | cmp_result = PyObject_Not(r->length); |
| 515 | if (cmp_result == -1) |
| 516 | goto end; |
| 517 | if (cmp_result == 1) { |
| 518 | Py_INCREF(Py_None); |
| 519 | Py_INCREF(Py_None); |
| 520 | PyTuple_SET_ITEM(t, 1, Py_None); |
| 521 | PyTuple_SET_ITEM(t, 2, Py_None); |
| 522 | } |
| 523 | else { |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 524 | Py_INCREF(r->start); |
| 525 | PyTuple_SET_ITEM(t, 1, r->start); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 526 | cmp_result = PyObject_RichCompareBool(r->length, _PyLong_One, Py_EQ); |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 527 | if (cmp_result == -1) |
| 528 | goto end; |
| 529 | if (cmp_result == 1) { |
| 530 | Py_INCREF(Py_None); |
| 531 | PyTuple_SET_ITEM(t, 2, Py_None); |
| 532 | } |
| 533 | else { |
| 534 | Py_INCREF(r->step); |
| 535 | PyTuple_SET_ITEM(t, 2, r->step); |
| 536 | } |
| 537 | } |
| 538 | result = PyObject_Hash(t); |
| 539 | end: |
| 540 | Py_DECREF(t); |
| 541 | return result; |
| 542 | } |
| 543 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 544 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 545 | range_count(rangeobject *r, PyObject *ob) |
| 546 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 547 | if (PyLong_CheckExact(ob) || PyBool_Check(ob)) { |
Georg Brandl | 7e5343b | 2010-11-20 22:40:10 +0000 | [diff] [blame] | 548 | int result = range_contains_long(r, ob); |
| 549 | if (result == -1) |
| 550 | return NULL; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 551 | return PyLong_FromLong(result); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 552 | } else { |
| 553 | Py_ssize_t count; |
| 554 | count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT); |
| 555 | if (count == -1) |
| 556 | return NULL; |
| 557 | return PyLong_FromSsize_t(count); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 562 | range_index(rangeobject *r, PyObject *ob) |
| 563 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 564 | int contains; |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 565 | |
| 566 | if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) { |
| 567 | Py_ssize_t index; |
| 568 | index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX); |
| 569 | if (index == -1) |
| 570 | return NULL; |
| 571 | return PyLong_FromSsize_t(index); |
| 572 | } |
| 573 | |
| 574 | contains = range_contains_long(r, ob); |
| 575 | if (contains == -1) |
| 576 | return NULL; |
| 577 | |
Benjamin Peterson | 155614b | 2010-11-20 22:44:32 +0000 | [diff] [blame] | 578 | if (contains) { |
| 579 | PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start); |
| 580 | if (tmp == NULL) |
| 581 | return NULL; |
| 582 | /* idx = (ob - r.start) // r.step */ |
| 583 | idx = PyNumber_FloorDivide(tmp, r->step); |
| 584 | Py_DECREF(tmp); |
| 585 | return idx; |
| 586 | } |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 587 | |
| 588 | /* object is not in the range */ |
Benjamin Peterson | 155614b | 2010-11-20 22:44:32 +0000 | [diff] [blame] | 589 | PyErr_Format(PyExc_ValueError, "%R is not in range", ob); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 593 | static PySequenceMethods range_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | (lenfunc)range_length, /* sq_length */ |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 595 | 0, /* sq_concat */ |
| 596 | 0, /* sq_repeat */ |
| 597 | (ssizeargfunc)range_item, /* sq_item */ |
| 598 | 0, /* sq_slice */ |
| 599 | 0, /* sq_ass_item */ |
| 600 | 0, /* sq_ass_slice */ |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 601 | (objobjproc)range_contains, /* sq_contains */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 602 | }; |
| 603 | |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 604 | static PyObject * |
| 605 | range_repr(rangeobject *r) |
| 606 | { |
| 607 | Py_ssize_t istep; |
| 608 | |
| 609 | /* Check for special case values for printing. We don't always |
Alexey Izbyshev | 7ecae3c | 2018-08-24 07:39:45 +0300 | [diff] [blame] | 610 | need the step value. We don't care about overflow. */ |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 611 | istep = PyNumber_AsSsize_t(r->step, NULL); |
Alexey Izbyshev | 7ecae3c | 2018-08-24 07:39:45 +0300 | [diff] [blame] | 612 | if (istep == -1 && PyErr_Occurred()) { |
| 613 | assert(!PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 614 | return NULL; |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | if (istep == 1) |
| 618 | return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop); |
| 619 | else |
| 620 | return PyUnicode_FromFormat("range(%R, %R, %R)", |
| 621 | r->start, r->stop, r->step); |
| 622 | } |
| 623 | |
| 624 | /* Pickling support */ |
| 625 | static PyObject * |
| 626 | range_reduce(rangeobject *r, PyObject *args) |
| 627 | { |
| 628 | return Py_BuildValue("(O(OOO))", Py_TYPE(r), |
| 629 | r->start, r->stop, r->step); |
| 630 | } |
| 631 | |
| 632 | static PyObject * |
| 633 | range_subscript(rangeobject* self, PyObject* item) |
| 634 | { |
| 635 | if (PyIndex_Check(item)) { |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 636 | PyObject *i, *result; |
| 637 | i = PyNumber_Index(item); |
| 638 | if (!i) |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 639 | return NULL; |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 640 | result = compute_range_item(self, i); |
| 641 | Py_DECREF(i); |
| 642 | return result; |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 643 | } |
| 644 | if (PySlice_Check(item)) { |
Nick Coghlan | e993b10 | 2011-01-12 03:15:52 +0000 | [diff] [blame] | 645 | return compute_slice(self, item); |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 646 | } |
| 647 | PyErr_Format(PyExc_TypeError, |
| 648 | "range indices must be integers or slices, not %.200s", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 649 | Py_TYPE(item)->tp_name); |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | static PyMappingMethods range_as_mapping = { |
| 655 | (lenfunc)range_length, /* mp_length */ |
| 656 | (binaryfunc)range_subscript, /* mp_subscript */ |
| 657 | (objobjargproc)0, /* mp_ass_subscript */ |
| 658 | }; |
| 659 | |
4kir4 | e46fb86 | 2017-03-20 09:44:46 +0300 | [diff] [blame] | 660 | static int |
| 661 | range_bool(rangeobject* self) |
| 662 | { |
| 663 | return PyObject_IsTrue(self->length); |
| 664 | } |
| 665 | |
| 666 | static PyNumberMethods range_as_number = { |
| 667 | .nb_bool = (inquiry)range_bool, |
| 668 | }; |
| 669 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 670 | static PyObject * range_iter(PyObject *seq); |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 671 | static PyObject * range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored)); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 672 | |
| 673 | PyDoc_STRVAR(reverse_doc, |
Ezio Melotti | 5792ce1 | 2013-10-06 00:36:45 +0300 | [diff] [blame] | 674 | "Return a reverse iterator."); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 675 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 676 | PyDoc_STRVAR(count_doc, |
| 677 | "rangeobject.count(value) -> integer -- return number of occurrences of value"); |
| 678 | |
| 679 | PyDoc_STRVAR(index_doc, |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | 22c5263 | 2019-05-03 17:52:12 +0530 | [diff] [blame] | 680 | "rangeobject.index(value) -> integer -- return index of value.\n" |
Ezio Melotti | 5792ce1 | 2013-10-06 00:36:45 +0300 | [diff] [blame] | 681 | "Raise ValueError if the value is not present."); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 682 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 683 | static PyMethodDef range_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 684 | {"__reversed__", range_reverse, METH_NOARGS, reverse_doc}, |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 685 | {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS}, |
| 686 | {"count", (PyCFunction)range_count, METH_O, count_doc}, |
| 687 | {"index", (PyCFunction)range_index, METH_O, index_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 689 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 690 | |
Benjamin Peterson | 878ce38 | 2011-11-05 15:17:52 -0400 | [diff] [blame] | 691 | static PyMemberDef range_members[] = { |
| 692 | {"start", T_OBJECT_EX, offsetof(rangeobject, start), READONLY}, |
| 693 | {"stop", T_OBJECT_EX, offsetof(rangeobject, stop), READONLY}, |
| 694 | {"step", T_OBJECT_EX, offsetof(rangeobject, step), READONLY}, |
| 695 | {0} |
| 696 | }; |
| 697 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 698 | PyTypeObject PyRange_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 700 | "range", /* Name of this type */ |
| 701 | sizeof(rangeobject), /* Basic object size */ |
| 702 | 0, /* Item size for varobject */ |
| 703 | (destructor)range_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 704 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | 0, /* tp_getattr */ |
| 706 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 707 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | (reprfunc)range_repr, /* tp_repr */ |
4kir4 | e46fb86 | 2017-03-20 09:44:46 +0300 | [diff] [blame] | 709 | &range_as_number, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | &range_as_sequence, /* tp_as_sequence */ |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 711 | &range_as_mapping, /* tp_as_mapping */ |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 712 | (hashfunc)range_hash, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | 0, /* tp_call */ |
| 714 | 0, /* tp_str */ |
| 715 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 716 | 0, /* tp_setattro */ |
| 717 | 0, /* tp_as_buffer */ |
| 718 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 719 | range_doc, /* tp_doc */ |
| 720 | 0, /* tp_traverse */ |
| 721 | 0, /* tp_clear */ |
Mark Dickinson | 3664568 | 2011-10-23 19:53:01 +0100 | [diff] [blame] | 722 | range_richcompare, /* tp_richcompare */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | 0, /* tp_weaklistoffset */ |
| 724 | range_iter, /* tp_iter */ |
| 725 | 0, /* tp_iternext */ |
| 726 | range_methods, /* tp_methods */ |
Benjamin Peterson | 878ce38 | 2011-11-05 15:17:52 -0400 | [diff] [blame] | 727 | range_members, /* tp_members */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | 0, /* tp_getset */ |
| 729 | 0, /* tp_base */ |
| 730 | 0, /* tp_dict */ |
| 731 | 0, /* tp_descr_get */ |
| 732 | 0, /* tp_descr_set */ |
| 733 | 0, /* tp_dictoffset */ |
| 734 | 0, /* tp_init */ |
| 735 | 0, /* tp_alloc */ |
| 736 | range_new, /* tp_new */ |
Petr Viktorin | 6e35da9 | 2020-02-18 16:13:17 +0100 | [diff] [blame] | 737 | .tp_vectorcall = (vectorcallfunc)range_vectorcall |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 738 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 739 | |
Walter Dörwald | 4ad9421 | 2007-05-21 18:01:17 +0000 | [diff] [blame] | 740 | /*********************** range Iterator **************************/ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 742 | /* There are 2 types of iterators, one for C longs, the other for |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 743 | Python ints (ie, PyObjects). This should make iteration fast |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 744 | in the normal case, but possible for any numeric value. |
| 745 | */ |
| 746 | |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 747 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | PyObject_HEAD |
| 749 | long index; |
| 750 | long start; |
| 751 | long step; |
| 752 | long len; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 753 | } rangeiterobject; |
| 754 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 755 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 756 | rangeiter_next(rangeiterobject *r) |
| 757 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 758 | if (r->index < r->len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | /* cast to unsigned to avoid possible signed overflow |
Mark Dickinson | b43dbc2 | 2009-11-15 12:56:08 +0000 | [diff] [blame] | 760 | in intermediate calculations. */ |
| 761 | return PyLong_FromLong((long)(r->start + |
| 762 | (unsigned long)(r->index++) * r->step)); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 763 | return NULL; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 766 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 767 | rangeiter_len(rangeiterobject *r, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 768 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 769 | return PyLong_FromLong(r->len - r->index); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 772 | PyDoc_STRVAR(length_hint_doc, |
| 773 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 774 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 775 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 776 | rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 777 | { |
| 778 | PyObject *start=NULL, *stop=NULL, *step=NULL; |
| 779 | PyObject *range; |
Chris Jerdonek | 042fa65 | 2012-10-07 14:56:27 -0700 | [diff] [blame] | 780 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 781 | /* create a range object for pickling */ |
| 782 | start = PyLong_FromLong(r->start); |
| 783 | if (start == NULL) |
| 784 | goto err; |
| 785 | stop = PyLong_FromLong(r->start + r->len * r->step); |
| 786 | if (stop == NULL) |
| 787 | goto err; |
| 788 | step = PyLong_FromLong(r->step); |
| 789 | if (step == NULL) |
| 790 | goto err; |
| 791 | range = (PyObject*)make_range_object(&PyRange_Type, |
| 792 | start, stop, step); |
| 793 | if (range == NULL) |
| 794 | goto err; |
| 795 | /* return the result */ |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 796 | return Py_BuildValue("N(N)i", _PyEval_GetBuiltinId(&PyId_iter), |
| 797 | range, r->index); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 798 | err: |
| 799 | Py_XDECREF(start); |
| 800 | Py_XDECREF(stop); |
| 801 | Py_XDECREF(step); |
| 802 | return NULL; |
| 803 | } |
| 804 | |
| 805 | static PyObject * |
| 806 | rangeiter_setstate(rangeiterobject *r, PyObject *state) |
| 807 | { |
| 808 | long index = PyLong_AsLong(state); |
| 809 | if (index == -1 && PyErr_Occurred()) |
| 810 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 811 | /* silently clip the index value */ |
| 812 | if (index < 0) |
| 813 | index = 0; |
| 814 | else if (index > r->len) |
| 815 | index = r->len; /* exhausted iterator */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 816 | r->index = index; |
| 817 | Py_RETURN_NONE; |
| 818 | } |
| 819 | |
| 820 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 821 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 822 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 823 | static PyMethodDef rangeiter_methods[] = { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 824 | {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 826 | {"__reduce__", (PyCFunction)rangeiter_reduce, METH_NOARGS, |
| 827 | reduce_doc}, |
| 828 | {"__setstate__", (PyCFunction)rangeiter_setstate, METH_O, |
| 829 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 831 | }; |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 832 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 833 | PyTypeObject PyRangeIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 835 | "range_iterator", /* tp_name */ |
| 836 | sizeof(rangeiterobject), /* tp_basicsize */ |
| 837 | 0, /* tp_itemsize */ |
| 838 | /* methods */ |
| 839 | (destructor)PyObject_Del, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 840 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | 0, /* tp_getattr */ |
| 842 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 843 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | 0, /* tp_repr */ |
| 845 | 0, /* tp_as_number */ |
| 846 | 0, /* tp_as_sequence */ |
| 847 | 0, /* tp_as_mapping */ |
| 848 | 0, /* tp_hash */ |
| 849 | 0, /* tp_call */ |
| 850 | 0, /* tp_str */ |
| 851 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 852 | 0, /* tp_setattro */ |
| 853 | 0, /* tp_as_buffer */ |
| 854 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 855 | 0, /* tp_doc */ |
| 856 | 0, /* tp_traverse */ |
| 857 | 0, /* tp_clear */ |
| 858 | 0, /* tp_richcompare */ |
| 859 | 0, /* tp_weaklistoffset */ |
| 860 | PyObject_SelfIter, /* tp_iter */ |
| 861 | (iternextfunc)rangeiter_next, /* tp_iternext */ |
| 862 | rangeiter_methods, /* tp_methods */ |
| 863 | 0, /* tp_members */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 864 | }; |
| 865 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 866 | /* Return number of items in range (lo, hi, step). step != 0 |
| 867 | * required. The result always fits in an unsigned long. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 868 | */ |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 869 | static unsigned long |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 870 | get_len_of_range(long lo, long hi, long step) |
| 871 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 872 | /* ------------------------------------------------------------- |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 873 | If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty. |
| 874 | Else for step > 0, if n values are in the range, the last one is |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 875 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 876 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 877 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 878 | the RHS is non-negative and so truncation is the same as the |
| 879 | floor. Letting M be the largest positive long, the worst case |
| 880 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 881 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 882 | precision to compute the RHS exactly. The analysis for step < 0 |
| 883 | is similar. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 884 | ---------------------------------------------------------------*/ |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 885 | assert(step != 0); |
| 886 | if (step > 0 && lo < hi) |
| 887 | return 1UL + (hi - 1UL - lo) / step; |
| 888 | else if (step < 0 && lo > hi) |
| 889 | return 1UL + (lo - 1UL - hi) / (0UL - step); |
| 890 | else |
| 891 | return 0UL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 894 | /* Initialize a rangeiter object. If the length of the rangeiter object |
| 895 | is not representable as a C long, OverflowError is raised. */ |
| 896 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 897 | static PyObject * |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 898 | fast_range_iter(long start, long stop, long step) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 899 | { |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 900 | rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 901 | unsigned long ulen; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 902 | if (it == NULL) |
| 903 | return NULL; |
| 904 | it->start = start; |
| 905 | it->step = step; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 906 | ulen = get_len_of_range(start, stop, step); |
| 907 | if (ulen > (unsigned long)LONG_MAX) { |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 908 | Py_DECREF(it); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 909 | PyErr_SetString(PyExc_OverflowError, |
| 910 | "range too large to represent as a range_iterator"); |
| 911 | return NULL; |
| 912 | } |
| 913 | it->len = (long)ulen; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 914 | it->index = 0; |
| 915 | return (PyObject *)it; |
| 916 | } |
| 917 | |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 918 | typedef struct { |
| 919 | PyObject_HEAD |
| 920 | PyObject *index; |
| 921 | PyObject *start; |
| 922 | PyObject *step; |
| 923 | PyObject *len; |
| 924 | } longrangeiterobject; |
| 925 | |
| 926 | static PyObject * |
| 927 | longrangeiter_len(longrangeiterobject *r, PyObject *no_args) |
| 928 | { |
| 929 | return PyNumber_Subtract(r->len, r->index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 932 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 933 | longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 934 | { |
| 935 | PyObject *product, *stop=NULL; |
| 936 | PyObject *range; |
| 937 | |
| 938 | /* create a range object for pickling. Must calculate the "stop" value */ |
| 939 | product = PyNumber_Multiply(r->len, r->step); |
| 940 | if (product == NULL) |
| 941 | return NULL; |
| 942 | stop = PyNumber_Add(r->start, product); |
| 943 | Py_DECREF(product); |
| 944 | if (stop == NULL) |
| 945 | return NULL; |
| 946 | Py_INCREF(r->start); |
| 947 | Py_INCREF(r->step); |
| 948 | range = (PyObject*)make_range_object(&PyRange_Type, |
| 949 | r->start, stop, r->step); |
| 950 | if (range == NULL) { |
| 951 | Py_DECREF(r->start); |
| 952 | Py_DECREF(stop); |
| 953 | Py_DECREF(r->step); |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | /* return the result */ |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 958 | return Py_BuildValue("N(N)O", _PyEval_GetBuiltinId(&PyId_iter), |
| 959 | range, r->index); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static PyObject * |
| 963 | longrangeiter_setstate(longrangeiterobject *r, PyObject *state) |
| 964 | { |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 965 | int cmp; |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 966 | |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 967 | /* clip the value */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 968 | cmp = PyObject_RichCompareBool(state, _PyLong_Zero, Py_LT); |
| 969 | if (cmp < 0) |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 970 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 971 | if (cmp > 0) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 972 | state = _PyLong_Zero; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 973 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 974 | else { |
| 975 | cmp = PyObject_RichCompareBool(r->len, state, Py_LT); |
| 976 | if (cmp < 0) |
| 977 | return NULL; |
| 978 | if (cmp > 0) |
| 979 | state = r->len; |
| 980 | } |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 981 | Py_INCREF(state); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 982 | Py_XSETREF(r->index, state); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 983 | Py_RETURN_NONE; |
| 984 | } |
| 985 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 986 | static PyMethodDef longrangeiter_methods[] = { |
| 987 | {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 989 | {"__reduce__", (PyCFunction)longrangeiter_reduce, METH_NOARGS, |
| 990 | reduce_doc}, |
| 991 | {"__setstate__", (PyCFunction)longrangeiter_setstate, METH_O, |
| 992 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 994 | }; |
| 995 | |
| 996 | static void |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 997 | longrangeiter_dealloc(longrangeiterobject *r) |
| 998 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 999 | Py_XDECREF(r->index); |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 1000 | Py_XDECREF(r->start); |
| 1001 | Py_XDECREF(r->step); |
| 1002 | Py_XDECREF(r->len); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 1003 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 1007 | longrangeiter_next(longrangeiterobject *r) |
| 1008 | { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1009 | PyObject *product, *new_index, *result; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1010 | if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1) |
| 1011 | return NULL; |
| 1012 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1013 | new_index = PyNumber_Add(r->index, _PyLong_One); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 1014 | if (!new_index) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | product = PyNumber_Multiply(r->index, r->step); |
| 1018 | if (!product) { |
| 1019 | Py_DECREF(new_index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1020 | return NULL; |
| 1021 | } |
| 1022 | |
| 1023 | result = PyNumber_Add(r->start, product); |
| 1024 | Py_DECREF(product); |
| 1025 | if (result) { |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1026 | Py_SETREF(r->index, new_index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1027 | } |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 1028 | else { |
| 1029 | Py_DECREF(new_index); |
| 1030 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1031 | |
| 1032 | return result; |
| 1033 | } |
| 1034 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 1035 | PyTypeObject PyLongRangeIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1037 | "longrange_iterator", /* tp_name */ |
| 1038 | sizeof(longrangeiterobject), /* tp_basicsize */ |
| 1039 | 0, /* tp_itemsize */ |
| 1040 | /* methods */ |
| 1041 | (destructor)longrangeiter_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1042 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | 0, /* tp_getattr */ |
| 1044 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1045 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | 0, /* tp_repr */ |
| 1047 | 0, /* tp_as_number */ |
| 1048 | 0, /* tp_as_sequence */ |
| 1049 | 0, /* tp_as_mapping */ |
| 1050 | 0, /* tp_hash */ |
| 1051 | 0, /* tp_call */ |
| 1052 | 0, /* tp_str */ |
| 1053 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1054 | 0, /* tp_setattro */ |
| 1055 | 0, /* tp_as_buffer */ |
| 1056 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1057 | 0, /* tp_doc */ |
| 1058 | 0, /* tp_traverse */ |
| 1059 | 0, /* tp_clear */ |
| 1060 | 0, /* tp_richcompare */ |
| 1061 | 0, /* tp_weaklistoffset */ |
| 1062 | PyObject_SelfIter, /* tp_iter */ |
| 1063 | (iternextfunc)longrangeiter_next, /* tp_iternext */ |
| 1064 | longrangeiter_methods, /* tp_methods */ |
| 1065 | 0, |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 1066 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1067 | |
| 1068 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 1069 | range_iter(PyObject *seq) |
| 1070 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1071 | rangeobject *r = (rangeobject *)seq; |
| 1072 | longrangeiterobject *it; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 1073 | long lstart, lstop, lstep; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1074 | PyObject *int_it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1075 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1076 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 1077 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1078 | /* If all three fields and the length convert to long, use the int |
| 1079 | * version */ |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 1080 | lstart = PyLong_AsLong(r->start); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1081 | if (lstart == -1 && PyErr_Occurred()) { |
| 1082 | PyErr_Clear(); |
| 1083 | goto long_range; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 1084 | } |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1085 | lstop = PyLong_AsLong(r->stop); |
| 1086 | if (lstop == -1 && PyErr_Occurred()) { |
| 1087 | PyErr_Clear(); |
| 1088 | goto long_range; |
| 1089 | } |
| 1090 | lstep = PyLong_AsLong(r->step); |
| 1091 | if (lstep == -1 && PyErr_Occurred()) { |
| 1092 | PyErr_Clear(); |
| 1093 | goto long_range; |
| 1094 | } |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 1095 | int_it = fast_range_iter(lstart, lstop, lstep); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1096 | if (int_it == NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 1097 | PyErr_Clear(); |
| 1098 | goto long_range; |
| 1099 | } |
| 1100 | return (PyObject *)int_it; |
| 1101 | |
| 1102 | long_range: |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 1103 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1104 | if (it == NULL) |
| 1105 | return NULL; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 1106 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1107 | it->start = r->start; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 1108 | it->step = r->step; |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 1109 | it->len = r->length; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1110 | it->index = _PyLong_Zero; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 1111 | Py_INCREF(it->start); |
| 1112 | Py_INCREF(it->step); |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 1113 | Py_INCREF(it->len); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1114 | Py_INCREF(it->index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1115 | return (PyObject *)it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1119 | range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame] | 1120 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1121 | rangeobject *range = (rangeobject*) seq; |
| 1122 | longrangeiterobject *it; |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1123 | PyObject *sum, *diff, *product; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1124 | long lstart, lstop, lstep, new_start, new_stop; |
| 1125 | unsigned long ulen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1127 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 1128 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1129 | /* reversed(range(start, stop, step)) can be expressed as |
| 1130 | range(start+(n-1)*step, start-step, -step), where n is the number of |
| 1131 | integers in the range. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1132 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1133 | If each of start, stop, step, -step, start-step, and the length |
| 1134 | of the iterator is representable as a C long, use the int |
| 1135 | version. This excludes some cases where the reversed range is |
| 1136 | representable as a range_iterator, but it's good enough for |
| 1137 | common cases and it makes the checks simple. */ |
| 1138 | |
| 1139 | lstart = PyLong_AsLong(range->start); |
| 1140 | if (lstart == -1 && PyErr_Occurred()) { |
| 1141 | PyErr_Clear(); |
| 1142 | goto long_range; |
| 1143 | } |
| 1144 | lstop = PyLong_AsLong(range->stop); |
| 1145 | if (lstop == -1 && PyErr_Occurred()) { |
| 1146 | PyErr_Clear(); |
| 1147 | goto long_range; |
| 1148 | } |
| 1149 | lstep = PyLong_AsLong(range->step); |
| 1150 | if (lstep == -1 && PyErr_Occurred()) { |
| 1151 | PyErr_Clear(); |
| 1152 | goto long_range; |
| 1153 | } |
| 1154 | /* check for possible overflow of -lstep */ |
| 1155 | if (lstep == LONG_MIN) |
| 1156 | goto long_range; |
| 1157 | |
| 1158 | /* check for overflow of lstart - lstep: |
| 1159 | |
| 1160 | for lstep > 0, need only check whether lstart - lstep < LONG_MIN. |
| 1161 | for lstep < 0, need only check whether lstart - lstep > LONG_MAX |
| 1162 | |
| 1163 | Rearrange these inequalities as: |
| 1164 | |
| 1165 | lstart - LONG_MIN < lstep (lstep > 0) |
| 1166 | LONG_MAX - lstart < -lstep (lstep < 0) |
| 1167 | |
| 1168 | and compute both sides as unsigned longs, to avoid the |
| 1169 | possibility of undefined behaviour due to signed overflow. */ |
| 1170 | |
| 1171 | if (lstep > 0) { |
| 1172 | if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep) |
| 1173 | goto long_range; |
| 1174 | } |
| 1175 | else { |
| 1176 | if (LONG_MAX - (unsigned long)lstart < 0UL - lstep) |
| 1177 | goto long_range; |
| 1178 | } |
| 1179 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1180 | ulen = get_len_of_range(lstart, lstop, lstep); |
| 1181 | if (ulen > (unsigned long)LONG_MAX) |
| 1182 | goto long_range; |
| 1183 | |
| 1184 | new_stop = lstart - lstep; |
| 1185 | new_start = (long)(new_stop + ulen * lstep); |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 1186 | return fast_range_iter(new_start, new_stop, -lstep); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1187 | |
| 1188 | long_range: |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 1189 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1190 | if (it == NULL) |
| 1191 | return NULL; |
Zackery Spytz | c9a6168 | 2018-10-31 03:13:16 -0600 | [diff] [blame] | 1192 | it->index = it->start = it->step = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1193 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1194 | /* start + (len - 1) * step */ |
Nick Coghlan | 37ee850 | 2010-12-03 14:26:13 +0000 | [diff] [blame] | 1195 | it->len = range->length; |
| 1196 | Py_INCREF(it->len); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 1197 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1198 | diff = PyNumber_Subtract(it->len, _PyLong_One); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1199 | if (!diff) |
| 1200 | goto create_failure; |
| 1201 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1202 | product = PyNumber_Multiply(diff, range->step); |
| 1203 | Py_DECREF(diff); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1204 | if (!product) |
| 1205 | goto create_failure; |
| 1206 | |
| 1207 | sum = PyNumber_Add(range->start, product); |
| 1208 | Py_DECREF(product); |
| 1209 | it->start = sum; |
| 1210 | if (!it->start) |
| 1211 | goto create_failure; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1212 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1213 | it->step = PyNumber_Negative(range->step); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 1214 | if (!it->step) |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 1215 | goto create_failure; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1216 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1217 | it->index = _PyLong_Zero; |
| 1218 | Py_INCREF(it->index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1219 | return (PyObject *)it; |
| 1220 | |
| 1221 | create_failure: |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 1222 | Py_DECREF(it); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1223 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1224 | } |