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" |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 5 | /* Support objects whose length is > PY_SSIZE_T_MAX. |
| 6 | |
| 7 | This could be sped up for small PyLongs if they fit in an Py_ssize_t. |
| 8 | This only matters on Win64. Though we could use PY_LONG_LONG which |
| 9 | would presumably help perf. |
| 10 | */ |
| 11 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 12 | typedef struct { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 13 | PyObject_HEAD |
| 14 | PyObject *start; |
| 15 | PyObject *stop; |
| 16 | PyObject *step; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 17 | } rangeobject; |
| 18 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 19 | /* Helper function for validating step. Always returns a new reference or |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | NULL on error. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 21 | */ |
| 22 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 23 | validate_step(PyObject *step) |
| 24 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 25 | /* No step specified, use a step of 1. */ |
| 26 | if (!step) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 27 | return PyLong_FromLong(1); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 28 | |
| 29 | step = PyNumber_Index(step); |
| 30 | if (step) { |
| 31 | Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL); |
| 32 | if (istep == -1 && PyErr_Occurred()) { |
| 33 | /* Ignore OverflowError, we know the value isn't 0. */ |
| 34 | PyErr_Clear(); |
| 35 | } |
| 36 | else if (istep == 0) { |
| 37 | PyErr_SetString(PyExc_ValueError, |
| 38 | "range() arg 3 must not be zero"); |
| 39 | Py_CLEAR(step); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return step; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 46 | /* XXX(nnorwitz): should we error check if the user passes any empty ranges? |
| 47 | range(-10) |
| 48 | range(0, -5) |
| 49 | range(0, 5, -1) |
| 50 | */ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 51 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 52 | range_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 53 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 54 | rangeobject *obj = NULL; |
| 55 | PyObject *start = NULL, *stop = NULL, *step = NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 57 | if (!_PyArg_NoKeywords("range()", kw)) |
| 58 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 60 | if (PyTuple_Size(args) <= 1) { |
| 61 | if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop)) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 62 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 63 | stop = PyNumber_Index(stop); |
| 64 | if (!stop) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 65 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 66 | start = PyLong_FromLong(0); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 67 | if (!start) { |
| 68 | Py_DECREF(stop); |
| 69 | return NULL; |
| 70 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 71 | step = PyLong_FromLong(1); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 72 | if (!step) { |
| 73 | Py_DECREF(stop); |
| 74 | Py_DECREF(start); |
| 75 | return NULL; |
| 76 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 77 | } |
| 78 | else { |
| 79 | if (!PyArg_UnpackTuple(args, "range", 2, 3, |
| 80 | &start, &stop, &step)) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 81 | return NULL; |
Tim Peters | feec453 | 2004-08-08 07:17:39 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 83 | /* Convert borrowed refs to owned refs */ |
| 84 | start = PyNumber_Index(start); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 85 | if (!start) |
| 86 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 87 | stop = PyNumber_Index(stop); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 88 | if (!stop) { |
| 89 | Py_DECREF(start); |
| 90 | return NULL; |
| 91 | } |
| 92 | step = validate_step(step); /* Caution, this can clear exceptions */ |
| 93 | if (!step) { |
| 94 | Py_DECREF(start); |
| 95 | Py_DECREF(stop); |
| 96 | return NULL; |
| 97 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | obj = PyObject_New(rangeobject, &PyRange_Type); |
| 101 | if (obj == NULL) |
| 102 | goto Fail; |
| 103 | obj->start = start; |
| 104 | obj->stop = stop; |
| 105 | obj->step = step; |
| 106 | return (PyObject *) obj; |
| 107 | |
| 108 | Fail: |
| 109 | Py_XDECREF(start); |
| 110 | Py_XDECREF(stop); |
| 111 | Py_XDECREF(step); |
| 112 | return NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 115 | PyDoc_STRVAR(range_doc, |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 116 | "range([start,] stop[, step]) -> range object\n\ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 117 | \n\ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 118 | Returns an iterator that generates the numbers in the range on demand."); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 120 | static void |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 121 | range_dealloc(rangeobject *r) |
| 122 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 123 | Py_DECREF(r->start); |
| 124 | Py_DECREF(r->stop); |
| 125 | Py_DECREF(r->step); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 126 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Mark Dickinson | 732166d | 2009-06-28 22:08:40 +0000 | [diff] [blame] | 129 | /* Return number of items in range (lo, hi, step) as a PyLong object, |
| 130 | * when arguments are PyLong objects. Arguments MUST return 1 with |
| 131 | * PyLong_Check(). Return NULL when there is an error. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 132 | */ |
| 133 | static PyObject* |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 134 | range_length_obj(rangeobject *r) |
| 135 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 136 | /* ------------------------------------------------------------- |
| 137 | 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] | 138 | on PyObjects (which are assumed to be PyLong objects). |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 139 | ---------------------------------------------------------------*/ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 140 | int cmp_result; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 141 | PyObject *lo, *hi; |
| 142 | PyObject *step = NULL; |
| 143 | PyObject *diff = NULL; |
| 144 | PyObject *one = NULL; |
| 145 | PyObject *tmp1 = NULL, *tmp2 = NULL, *result; |
| 146 | /* holds sub-expression evaluations */ |
| 147 | |
| 148 | PyObject *zero = PyLong_FromLong(0); |
| 149 | if (zero == NULL) |
| 150 | return NULL; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 151 | cmp_result = PyObject_RichCompareBool(r->step, zero, Py_GT); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 152 | Py_DECREF(zero); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 153 | if (cmp_result == -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 154 | return NULL; |
| 155 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 156 | if (cmp_result == 1) { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 157 | lo = r->start; |
| 158 | hi = r->stop; |
| 159 | step = r->step; |
| 160 | Py_INCREF(step); |
| 161 | } else { |
| 162 | lo = r->stop; |
| 163 | hi = r->start; |
| 164 | step = PyNumber_Negative(r->step); |
| 165 | if (!step) |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | /* if (lo >= hi), return length of 0. */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 170 | if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 171 | Py_XDECREF(step); |
| 172 | return PyLong_FromLong(0); |
| 173 | } |
| 174 | |
| 175 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 176 | goto Fail; |
| 177 | |
| 178 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 179 | goto Fail; |
| 180 | |
| 181 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 182 | goto Fail; |
| 183 | |
| 184 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 185 | goto Fail; |
| 186 | |
| 187 | if ((result = PyNumber_Add(tmp2, one)) == NULL) |
| 188 | goto Fail; |
| 189 | |
| 190 | Py_DECREF(tmp2); |
| 191 | Py_DECREF(diff); |
| 192 | Py_DECREF(step); |
| 193 | Py_DECREF(tmp1); |
| 194 | Py_DECREF(one); |
| 195 | return result; |
| 196 | |
| 197 | Fail: |
| 198 | Py_XDECREF(tmp2); |
| 199 | Py_XDECREF(diff); |
| 200 | Py_XDECREF(step); |
| 201 | Py_XDECREF(tmp1); |
| 202 | Py_XDECREF(one); |
| 203 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 206 | static Py_ssize_t |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 207 | range_length(rangeobject *r) |
| 208 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 209 | PyObject *len = range_length_obj(r); |
| 210 | Py_ssize_t result = -1; |
| 211 | if (len) { |
| 212 | result = PyLong_AsSsize_t(len); |
| 213 | Py_DECREF(len); |
| 214 | } |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | /* range(...)[x] is necessary for: seq[:] = range(...) */ |
| 219 | |
| 220 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 221 | range_item(rangeobject *r, Py_ssize_t i) |
| 222 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 223 | Py_ssize_t len = range_length(r); |
| 224 | PyObject *rem, *incr, *result; |
| 225 | |
| 226 | /* XXX(nnorwitz): should negative indices be supported? */ |
| 227 | /* XXX(nnorwitz): should support range[x] where x > PY_SSIZE_T_MAX? */ |
| 228 | if (i < 0 || i >= len) { |
| 229 | if (!PyErr_Occurred()) |
| 230 | PyErr_SetString(PyExc_IndexError, |
Walter Dörwald | 4ad9421 | 2007-05-21 18:01:17 +0000 | [diff] [blame] | 231 | "range object index out of range"); |
Benjamin Peterson | df0a5cb | 2008-04-25 21:15:37 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 234 | |
| 235 | /* XXX(nnorwitz): optimize for short ints. */ |
Raymond Hettinger | ad3f332 | 2008-02-09 04:13:49 +0000 | [diff] [blame] | 236 | rem = PyLong_FromSsize_t(i); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 237 | if (!rem) |
| 238 | return NULL; |
| 239 | incr = PyNumber_Multiply(rem, r->step); |
| 240 | Py_DECREF(rem); |
| 241 | if (!incr) |
| 242 | return NULL; |
| 243 | result = PyNumber_Add(r->start, incr); |
| 244 | Py_DECREF(incr); |
| 245 | return result; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 249 | range_repr(rangeobject *r) |
| 250 | { |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 251 | Py_ssize_t istep; |
Tim Peters | d976ab7 | 2004-08-08 06:29:10 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 253 | /* Check for special case values for printing. We don't always |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 254 | need the step value. We don't care about errors |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 255 | (it means overflow), so clear the errors. */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 256 | istep = PyNumber_AsSsize_t(r->step, NULL); |
| 257 | if (istep != 1 || (istep == -1 && PyErr_Occurred())) { |
| 258 | PyErr_Clear(); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 261 | if (istep == 1) |
Walter Dörwald | 850e516 | 2007-05-20 08:19:54 +0000 | [diff] [blame] | 262 | return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop); |
| 263 | else |
| 264 | return PyUnicode_FromFormat("range(%R, %R, %R)", |
| 265 | r->start, r->stop, r->step); |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Alexandre Vassalotti | 7505607 | 2008-06-10 04:03:04 +0000 | [diff] [blame] | 268 | /* Pickling support */ |
| 269 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 270 | range_reduce(rangeobject *r, PyObject *args) |
| 271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | return Py_BuildValue("(O(OOO))", Py_TYPE(r), |
Alexandre Vassalotti | 7505607 | 2008-06-10 04:03:04 +0000 | [diff] [blame] | 273 | r->start, r->stop, r->step); |
| 274 | } |
| 275 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 276 | /* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */ |
| 277 | static int |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 278 | range_contains_long(rangeobject *r, PyObject *ob) |
| 279 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 280 | int cmp1, cmp2, cmp3; |
| 281 | PyObject *tmp1 = NULL; |
| 282 | PyObject *tmp2 = NULL; |
| 283 | PyObject *zero = NULL; |
| 284 | int result = -1; |
| 285 | |
| 286 | zero = PyLong_FromLong(0); |
| 287 | if (zero == NULL) /* MemoryError in int(0) */ |
| 288 | goto end; |
| 289 | |
| 290 | /* Check if the value can possibly be in the range. */ |
| 291 | |
| 292 | cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT); |
| 293 | if (cmp1 == -1) |
| 294 | goto end; |
| 295 | if (cmp1 == 1) { /* positive steps: start <= ob < stop */ |
| 296 | cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE); |
| 297 | cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT); |
| 298 | } |
| 299 | else { /* negative steps: stop < ob <= start */ |
| 300 | cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE); |
| 301 | cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT); |
| 302 | } |
| 303 | |
| 304 | if (cmp2 == -1 || cmp3 == -1) /* TypeError */ |
| 305 | goto end; |
| 306 | if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */ |
| 307 | result = 0; |
| 308 | goto end; |
| 309 | } |
| 310 | |
| 311 | /* Check that the stride does not invalidate ob's membership. */ |
| 312 | tmp1 = PyNumber_Subtract(ob, r->start); |
| 313 | if (tmp1 == NULL) |
| 314 | goto end; |
| 315 | tmp2 = PyNumber_Remainder(tmp1, r->step); |
| 316 | if (tmp2 == NULL) |
| 317 | goto end; |
| 318 | /* result = (int(ob) - start % step) == 0 */ |
| 319 | result = PyObject_RichCompareBool(tmp2, zero, Py_EQ); |
| 320 | end: |
| 321 | Py_XDECREF(tmp1); |
| 322 | Py_XDECREF(tmp2); |
| 323 | Py_XDECREF(zero); |
| 324 | return result; |
| 325 | } |
| 326 | |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 327 | static int |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 328 | range_contains(rangeobject *r, PyObject *ob) |
| 329 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 330 | if (PyLong_CheckExact(ob) || PyBool_Check(ob)) |
| 331 | return range_contains_long(r, ob); |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 332 | |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 333 | return (int)_PySequence_IterSearch((PyObject*)r, ob, |
| 334 | PY_ITERSEARCH_CONTAINS); |
| 335 | } |
| 336 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 337 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 338 | range_count(rangeobject *r, PyObject *ob) |
| 339 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 340 | if (PyLong_CheckExact(ob) || PyBool_Check(ob)) { |
Georg Brandl | 7e5343b | 2010-11-20 22:40:10 +0000 | [diff] [blame] | 341 | int result = range_contains_long(r, ob); |
| 342 | if (result == -1) |
| 343 | return NULL; |
| 344 | else if (result) |
Benjamin Peterson | 0b458d5 | 2010-11-20 22:35:41 +0000 | [diff] [blame] | 345 | return PyLong_FromLong(1); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 346 | else |
Benjamin Peterson | 0b458d5 | 2010-11-20 22:35:41 +0000 | [diff] [blame] | 347 | return PyLong_FromLong(0); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 348 | } else { |
| 349 | Py_ssize_t count; |
| 350 | count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT); |
| 351 | if (count == -1) |
| 352 | return NULL; |
| 353 | return PyLong_FromSsize_t(count); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 358 | range_index(rangeobject *r, PyObject *ob) |
| 359 | { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 360 | int contains; |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 361 | |
| 362 | if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) { |
| 363 | Py_ssize_t index; |
| 364 | index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX); |
| 365 | if (index == -1) |
| 366 | return NULL; |
| 367 | return PyLong_FromSsize_t(index); |
| 368 | } |
| 369 | |
| 370 | contains = range_contains_long(r, ob); |
| 371 | if (contains == -1) |
| 372 | return NULL; |
| 373 | |
Benjamin Peterson | 155614b | 2010-11-20 22:44:32 +0000 | [diff] [blame] | 374 | if (contains) { |
| 375 | PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start); |
| 376 | if (tmp == NULL) |
| 377 | return NULL; |
| 378 | /* idx = (ob - r.start) // r.step */ |
| 379 | idx = PyNumber_FloorDivide(tmp, r->step); |
| 380 | Py_DECREF(tmp); |
| 381 | return idx; |
| 382 | } |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 383 | |
| 384 | /* object is not in the range */ |
Benjamin Peterson | 155614b | 2010-11-20 22:44:32 +0000 | [diff] [blame] | 385 | PyErr_Format(PyExc_ValueError, "%R is not in range", ob); |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
| 388 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 389 | static PySequenceMethods range_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | (lenfunc)range_length, /* sq_length */ |
| 391 | 0, /* sq_concat */ |
| 392 | 0, /* sq_repeat */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 393 | (ssizeargfunc)range_item, /* sq_item */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | 0, /* sq_slice */ |
Mark Dickinson | 3e124ae | 2009-09-22 21:47:24 +0000 | [diff] [blame] | 395 | 0, /* sq_ass_item */ |
| 396 | 0, /* sq_ass_slice */ |
| 397 | (objobjproc)range_contains, /* sq_contains */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 400 | static PyObject * range_iter(PyObject *seq); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 401 | static PyObject * range_reverse(PyObject *seq); |
| 402 | |
| 403 | PyDoc_STRVAR(reverse_doc, |
| 404 | "Returns a reverse iterator."); |
| 405 | |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 406 | PyDoc_STRVAR(count_doc, |
| 407 | "rangeobject.count(value) -> integer -- return number of occurrences of value"); |
| 408 | |
| 409 | PyDoc_STRVAR(index_doc, |
| 410 | "rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.\n" |
| 411 | "Raises ValueError if the value is not present."); |
| 412 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 413 | static PyMethodDef range_methods[] = { |
Daniel Stutzbach | 9f0cbf1 | 2010-09-13 21:16:29 +0000 | [diff] [blame] | 414 | {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc}, |
| 415 | {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS}, |
| 416 | {"count", (PyCFunction)range_count, METH_O, count_doc}, |
| 417 | {"index", (PyCFunction)range_index, METH_O, index_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 419 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | PyTypeObject PyRange_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 423 | "range", /* Name of this type */ |
| 424 | sizeof(rangeobject), /* Basic object size */ |
| 425 | 0, /* Item size for varobject */ |
| 426 | (destructor)range_dealloc, /* tp_dealloc */ |
| 427 | 0, /* tp_print */ |
| 428 | 0, /* tp_getattr */ |
| 429 | 0, /* tp_setattr */ |
| 430 | 0, /* tp_reserved */ |
| 431 | (reprfunc)range_repr, /* tp_repr */ |
| 432 | 0, /* tp_as_number */ |
| 433 | &range_as_sequence, /* tp_as_sequence */ |
| 434 | 0, /* tp_as_mapping */ |
| 435 | 0, /* tp_hash */ |
| 436 | 0, /* tp_call */ |
| 437 | 0, /* tp_str */ |
| 438 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 439 | 0, /* tp_setattro */ |
| 440 | 0, /* tp_as_buffer */ |
| 441 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 442 | range_doc, /* tp_doc */ |
| 443 | 0, /* tp_traverse */ |
| 444 | 0, /* tp_clear */ |
| 445 | 0, /* tp_richcompare */ |
| 446 | 0, /* tp_weaklistoffset */ |
| 447 | range_iter, /* tp_iter */ |
| 448 | 0, /* tp_iternext */ |
| 449 | range_methods, /* tp_methods */ |
| 450 | 0, /* tp_members */ |
| 451 | 0, /* tp_getset */ |
| 452 | 0, /* tp_base */ |
| 453 | 0, /* tp_dict */ |
| 454 | 0, /* tp_descr_get */ |
| 455 | 0, /* tp_descr_set */ |
| 456 | 0, /* tp_dictoffset */ |
| 457 | 0, /* tp_init */ |
| 458 | 0, /* tp_alloc */ |
| 459 | range_new, /* tp_new */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 460 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 461 | |
Walter Dörwald | 4ad9421 | 2007-05-21 18:01:17 +0000 | [diff] [blame] | 462 | /*********************** range Iterator **************************/ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 463 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 464 | /* There are 2 types of iterators, one for C longs, the other for |
| 465 | Python longs (ie, PyObjects). This should make iteration fast |
| 466 | in the normal case, but possible for any numeric value. |
| 467 | */ |
| 468 | |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 469 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | PyObject_HEAD |
| 471 | long index; |
| 472 | long start; |
| 473 | long step; |
| 474 | long len; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 475 | } rangeiterobject; |
| 476 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 477 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 478 | rangeiter_next(rangeiterobject *r) |
| 479 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 480 | if (r->index < r->len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | /* cast to unsigned to avoid possible signed overflow |
Mark Dickinson | b43dbc2 | 2009-11-15 12:56:08 +0000 | [diff] [blame] | 482 | in intermediate calculations. */ |
| 483 | return PyLong_FromLong((long)(r->start + |
| 484 | (unsigned long)(r->index++) * r->step)); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 485 | return NULL; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 488 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 489 | rangeiter_len(rangeiterobject *r) |
| 490 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 491 | return PyLong_FromLong(r->len - r->index); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 494 | typedef struct { |
| 495 | PyObject_HEAD |
| 496 | PyObject *index; |
| 497 | PyObject *start; |
| 498 | PyObject *step; |
| 499 | PyObject *len; |
| 500 | } longrangeiterobject; |
| 501 | |
| 502 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 503 | longrangeiter_len(longrangeiterobject *r, PyObject *no_args) |
| 504 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 505 | return PyNumber_Subtract(r->len, r->index); |
| 506 | } |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 508 | static PyObject *rangeiter_new(PyTypeObject *, PyObject *args, PyObject *kw); |
| 509 | |
| 510 | PyDoc_STRVAR(length_hint_doc, |
| 511 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 512 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 513 | static PyMethodDef rangeiter_methods[] = { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 514 | {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | length_hint_doc}, |
| 516 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 517 | }; |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 518 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 519 | PyTypeObject PyRangeIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 521 | "range_iterator", /* tp_name */ |
| 522 | sizeof(rangeiterobject), /* tp_basicsize */ |
| 523 | 0, /* tp_itemsize */ |
| 524 | /* methods */ |
| 525 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 526 | 0, /* tp_print */ |
| 527 | 0, /* tp_getattr */ |
| 528 | 0, /* tp_setattr */ |
| 529 | 0, /* tp_reserved */ |
| 530 | 0, /* tp_repr */ |
| 531 | 0, /* tp_as_number */ |
| 532 | 0, /* tp_as_sequence */ |
| 533 | 0, /* tp_as_mapping */ |
| 534 | 0, /* tp_hash */ |
| 535 | 0, /* tp_call */ |
| 536 | 0, /* tp_str */ |
| 537 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 538 | 0, /* tp_setattro */ |
| 539 | 0, /* tp_as_buffer */ |
| 540 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 541 | 0, /* tp_doc */ |
| 542 | 0, /* tp_traverse */ |
| 543 | 0, /* tp_clear */ |
| 544 | 0, /* tp_richcompare */ |
| 545 | 0, /* tp_weaklistoffset */ |
| 546 | PyObject_SelfIter, /* tp_iter */ |
| 547 | (iternextfunc)rangeiter_next, /* tp_iternext */ |
| 548 | rangeiter_methods, /* tp_methods */ |
| 549 | 0, /* tp_members */ |
| 550 | 0, /* tp_getset */ |
| 551 | 0, /* tp_base */ |
| 552 | 0, /* tp_dict */ |
| 553 | 0, /* tp_descr_get */ |
| 554 | 0, /* tp_descr_set */ |
| 555 | 0, /* tp_dictoffset */ |
| 556 | 0, /* tp_init */ |
| 557 | 0, /* tp_alloc */ |
| 558 | rangeiter_new, /* tp_new */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 559 | }; |
| 560 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 561 | /* Return number of items in range (lo, hi, step). step != 0 |
| 562 | * required. The result always fits in an unsigned long. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 563 | */ |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 564 | static unsigned long |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 565 | get_len_of_range(long lo, long hi, long step) |
| 566 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 567 | /* ------------------------------------------------------------- |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 568 | If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty. |
| 569 | 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] | 570 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 571 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 572 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 573 | the RHS is non-negative and so truncation is the same as the |
| 574 | floor. Letting M be the largest positive long, the worst case |
| 575 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 576 | 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] | 577 | precision to compute the RHS exactly. The analysis for step < 0 |
| 578 | is similar. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 579 | ---------------------------------------------------------------*/ |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 580 | assert(step != 0); |
| 581 | if (step > 0 && lo < hi) |
| 582 | return 1UL + (hi - 1UL - lo) / step; |
| 583 | else if (step < 0 && lo > hi) |
| 584 | return 1UL + (lo - 1UL - hi) / (0UL - step); |
| 585 | else |
| 586 | return 0UL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 589 | /* Initialize a rangeiter object. If the length of the rangeiter object |
| 590 | is not representable as a C long, OverflowError is raised. */ |
| 591 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 592 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 593 | int_range_iter(long start, long stop, long step) |
| 594 | { |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 595 | rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 596 | unsigned long ulen; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 597 | if (it == NULL) |
| 598 | return NULL; |
| 599 | it->start = start; |
| 600 | it->step = step; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 601 | ulen = get_len_of_range(start, stop, step); |
| 602 | if (ulen > (unsigned long)LONG_MAX) { |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 603 | Py_DECREF(it); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 604 | PyErr_SetString(PyExc_OverflowError, |
| 605 | "range too large to represent as a range_iterator"); |
| 606 | return NULL; |
| 607 | } |
| 608 | it->len = (long)ulen; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 609 | it->index = 0; |
| 610 | return (PyObject *)it; |
| 611 | } |
| 612 | |
| 613 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 614 | rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 615 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 616 | long start, stop, step; |
| 617 | |
| 618 | if (!_PyArg_NoKeywords("rangeiter()", kw)) |
| 619 | return NULL; |
| 620 | |
| 621 | if (!PyArg_ParseTuple(args, "lll;rangeiter() requires 3 int arguments", |
| 622 | &start, &stop, &step)) |
| 623 | return NULL; |
| 624 | |
| 625 | return int_range_iter(start, stop, step); |
| 626 | } |
| 627 | |
| 628 | static PyMethodDef longrangeiter_methods[] = { |
| 629 | {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | length_hint_doc}, |
| 631 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static void |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 635 | longrangeiter_dealloc(longrangeiterobject *r) |
| 636 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 637 | Py_XDECREF(r->index); |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 638 | Py_XDECREF(r->start); |
| 639 | Py_XDECREF(r->step); |
| 640 | Py_XDECREF(r->len); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 641 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 645 | longrangeiter_next(longrangeiterobject *r) |
| 646 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 647 | PyObject *one, *product, *new_index, *result; |
| 648 | if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1) |
| 649 | return NULL; |
| 650 | |
| 651 | one = PyLong_FromLong(1); |
| 652 | if (!one) |
| 653 | return NULL; |
| 654 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 655 | new_index = PyNumber_Add(r->index, one); |
| 656 | Py_DECREF(one); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 657 | if (!new_index) |
| 658 | return NULL; |
| 659 | |
| 660 | product = PyNumber_Multiply(r->index, r->step); |
| 661 | if (!product) { |
| 662 | Py_DECREF(new_index); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | result = PyNumber_Add(r->start, product); |
| 667 | Py_DECREF(product); |
| 668 | if (result) { |
| 669 | Py_DECREF(r->index); |
| 670 | r->index = new_index; |
| 671 | } |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 672 | else { |
| 673 | Py_DECREF(new_index); |
| 674 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 675 | |
| 676 | return result; |
| 677 | } |
| 678 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 679 | PyTypeObject PyLongRangeIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 681 | "longrange_iterator", /* tp_name */ |
| 682 | sizeof(longrangeiterobject), /* tp_basicsize */ |
| 683 | 0, /* tp_itemsize */ |
| 684 | /* methods */ |
| 685 | (destructor)longrangeiter_dealloc, /* tp_dealloc */ |
| 686 | 0, /* tp_print */ |
| 687 | 0, /* tp_getattr */ |
| 688 | 0, /* tp_setattr */ |
| 689 | 0, /* tp_reserved */ |
| 690 | 0, /* tp_repr */ |
| 691 | 0, /* tp_as_number */ |
| 692 | 0, /* tp_as_sequence */ |
| 693 | 0, /* tp_as_mapping */ |
| 694 | 0, /* tp_hash */ |
| 695 | 0, /* tp_call */ |
| 696 | 0, /* tp_str */ |
| 697 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 698 | 0, /* tp_setattro */ |
| 699 | 0, /* tp_as_buffer */ |
| 700 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 701 | 0, /* tp_doc */ |
| 702 | 0, /* tp_traverse */ |
| 703 | 0, /* tp_clear */ |
| 704 | 0, /* tp_richcompare */ |
| 705 | 0, /* tp_weaklistoffset */ |
| 706 | PyObject_SelfIter, /* tp_iter */ |
| 707 | (iternextfunc)longrangeiter_next, /* tp_iternext */ |
| 708 | longrangeiter_methods, /* tp_methods */ |
| 709 | 0, |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 710 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | |
| 712 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 713 | range_iter(PyObject *seq) |
| 714 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 715 | rangeobject *r = (rangeobject *)seq; |
| 716 | longrangeiterobject *it; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 717 | long lstart, lstop, lstep; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 718 | PyObject *int_it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 720 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 721 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 722 | /* If all three fields and the length convert to long, use the int |
| 723 | * version */ |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 724 | lstart = PyLong_AsLong(r->start); |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 725 | if (lstart == -1 && PyErr_Occurred()) { |
| 726 | PyErr_Clear(); |
| 727 | goto long_range; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 728 | } |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 729 | lstop = PyLong_AsLong(r->stop); |
| 730 | if (lstop == -1 && PyErr_Occurred()) { |
| 731 | PyErr_Clear(); |
| 732 | goto long_range; |
| 733 | } |
| 734 | lstep = PyLong_AsLong(r->step); |
| 735 | if (lstep == -1 && PyErr_Occurred()) { |
| 736 | PyErr_Clear(); |
| 737 | goto long_range; |
| 738 | } |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 739 | int_it = int_range_iter(lstart, lstop, lstep); |
| 740 | if (int_it == NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 741 | PyErr_Clear(); |
| 742 | goto long_range; |
| 743 | } |
| 744 | return (PyObject *)int_it; |
| 745 | |
| 746 | long_range: |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 747 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 748 | if (it == NULL) |
| 749 | return NULL; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 750 | |
| 751 | /* Do all initialization here, so we can DECREF on failure. */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 752 | it->start = r->start; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 753 | it->step = r->step; |
| 754 | Py_INCREF(it->start); |
| 755 | Py_INCREF(it->step); |
| 756 | |
| 757 | it->len = it->index = NULL; |
| 758 | |
Mark Dickinson | eb36d31 | 2009-06-24 18:36:46 +0000 | [diff] [blame] | 759 | it->len = range_length_obj(r); |
| 760 | if (!it->len) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 761 | goto create_failure; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 762 | it->index = PyLong_FromLong(0); |
| 763 | if (!it->index) |
| 764 | goto create_failure; |
| 765 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 766 | return (PyObject *)it; |
| 767 | |
| 768 | create_failure: |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 769 | Py_DECREF(it); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 770 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static PyObject * |
Benjamin Peterson | a1864f3 | 2010-11-20 23:05:39 +0000 | [diff] [blame^] | 774 | range_reverse(PyObject *seq) |
| 775 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 776 | rangeobject *range = (rangeobject*) seq; |
| 777 | longrangeiterobject *it; |
| 778 | PyObject *one, *sum, *diff, *len = NULL, *product; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 779 | long lstart, lstop, lstep, new_start, new_stop; |
| 780 | unsigned long ulen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 782 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 783 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 784 | /* reversed(range(start, stop, step)) can be expressed as |
| 785 | range(start+(n-1)*step, start-step, -step), where n is the number of |
| 786 | integers in the range. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 787 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 788 | If each of start, stop, step, -step, start-step, and the length |
| 789 | of the iterator is representable as a C long, use the int |
| 790 | version. This excludes some cases where the reversed range is |
| 791 | representable as a range_iterator, but it's good enough for |
| 792 | common cases and it makes the checks simple. */ |
| 793 | |
| 794 | lstart = PyLong_AsLong(range->start); |
| 795 | if (lstart == -1 && PyErr_Occurred()) { |
| 796 | PyErr_Clear(); |
| 797 | goto long_range; |
| 798 | } |
| 799 | lstop = PyLong_AsLong(range->stop); |
| 800 | if (lstop == -1 && PyErr_Occurred()) { |
| 801 | PyErr_Clear(); |
| 802 | goto long_range; |
| 803 | } |
| 804 | lstep = PyLong_AsLong(range->step); |
| 805 | if (lstep == -1 && PyErr_Occurred()) { |
| 806 | PyErr_Clear(); |
| 807 | goto long_range; |
| 808 | } |
| 809 | /* check for possible overflow of -lstep */ |
| 810 | if (lstep == LONG_MIN) |
| 811 | goto long_range; |
| 812 | |
| 813 | /* check for overflow of lstart - lstep: |
| 814 | |
| 815 | for lstep > 0, need only check whether lstart - lstep < LONG_MIN. |
| 816 | for lstep < 0, need only check whether lstart - lstep > LONG_MAX |
| 817 | |
| 818 | Rearrange these inequalities as: |
| 819 | |
| 820 | lstart - LONG_MIN < lstep (lstep > 0) |
| 821 | LONG_MAX - lstart < -lstep (lstep < 0) |
| 822 | |
| 823 | and compute both sides as unsigned longs, to avoid the |
| 824 | possibility of undefined behaviour due to signed overflow. */ |
| 825 | |
| 826 | if (lstep > 0) { |
| 827 | if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep) |
| 828 | goto long_range; |
| 829 | } |
| 830 | else { |
| 831 | if (LONG_MAX - (unsigned long)lstart < 0UL - lstep) |
| 832 | goto long_range; |
| 833 | } |
| 834 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 835 | ulen = get_len_of_range(lstart, lstop, lstep); |
| 836 | if (ulen > (unsigned long)LONG_MAX) |
| 837 | goto long_range; |
| 838 | |
| 839 | new_stop = lstart - lstep; |
| 840 | new_start = (long)(new_stop + ulen * lstep); |
| 841 | return int_range_iter(new_start, new_stop, -lstep); |
| 842 | |
| 843 | long_range: |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 844 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 845 | if (it == NULL) |
| 846 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 848 | /* start + (len - 1) * step */ |
| 849 | len = range_length_obj(range); |
| 850 | if (!len) |
| 851 | goto create_failure; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 852 | |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 853 | /* Steal reference to len. */ |
| 854 | it->len = len; |
| 855 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 856 | one = PyLong_FromLong(1); |
| 857 | if (!one) |
| 858 | goto create_failure; |
| 859 | |
| 860 | diff = PyNumber_Subtract(len, one); |
| 861 | Py_DECREF(one); |
| 862 | if (!diff) |
| 863 | goto create_failure; |
| 864 | |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 865 | product = PyNumber_Multiply(diff, range->step); |
| 866 | Py_DECREF(diff); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 867 | if (!product) |
| 868 | goto create_failure; |
| 869 | |
| 870 | sum = PyNumber_Add(range->start, product); |
| 871 | Py_DECREF(product); |
| 872 | it->start = sum; |
| 873 | if (!it->start) |
| 874 | goto create_failure; |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 875 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 876 | it->step = PyNumber_Negative(range->step); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 877 | if (!it->step) |
Mark Dickinson | d550c9a | 2009-11-15 09:57:26 +0000 | [diff] [blame] | 878 | goto create_failure; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 879 | |
| 880 | it->index = PyLong_FromLong(0); |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 881 | if (!it->index) |
| 882 | goto create_failure; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 883 | |
| 884 | return (PyObject *)it; |
| 885 | |
| 886 | create_failure: |
Benjamin Peterson | 36fbb73 | 2009-11-16 00:34:25 +0000 | [diff] [blame] | 887 | Py_DECREF(it); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 888 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 889 | } |