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