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