Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /* Range object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 5 | /* Support objects whose length is > PY_SSIZE_T_MAX. |
| 6 | |
| 7 | This could be sped up for small PyLongs if they fit in an Py_ssize_t. |
| 8 | This only matters on Win64. Though we could use PY_LONG_LONG which |
| 9 | would presumably help perf. |
| 10 | */ |
| 11 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 12 | typedef struct { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 13 | PyObject_HEAD |
| 14 | PyObject *start; |
| 15 | PyObject *stop; |
| 16 | PyObject *step; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 17 | } rangeobject; |
| 18 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 19 | /* Helper function for validating step. Always returns a new reference or |
| 20 | NULL on error. |
| 21 | */ |
| 22 | static PyObject * |
| 23 | validate_step(PyObject *step) |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 24 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 25 | /* No step specified, use a step of 1. */ |
| 26 | if (!step) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 27 | return PyLong_FromLong(1); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 28 | |
| 29 | step = PyNumber_Index(step); |
| 30 | if (step) { |
| 31 | Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL); |
| 32 | if (istep == -1 && PyErr_Occurred()) { |
| 33 | /* Ignore OverflowError, we know the value isn't 0. */ |
| 34 | PyErr_Clear(); |
| 35 | } |
| 36 | else if (istep == 0) { |
| 37 | PyErr_SetString(PyExc_ValueError, |
| 38 | "range() arg 3 must not be zero"); |
| 39 | Py_CLEAR(step); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return step; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 46 | /* XXX(nnorwitz): should we error check if the user passes any empty ranges? |
| 47 | range(-10) |
| 48 | range(0, -5) |
| 49 | range(0, 5, -1) |
| 50 | */ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 51 | static PyObject * |
| 52 | range_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 53 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 54 | rangeobject *obj = NULL; |
| 55 | PyObject *start = NULL, *stop = NULL, *step = NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 57 | if (!_PyArg_NoKeywords("range()", kw)) |
| 58 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 60 | if (PyTuple_Size(args) <= 1) { |
| 61 | if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop)) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 62 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 63 | stop = PyNumber_Index(stop); |
| 64 | if (!stop) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 65 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 66 | start = PyLong_FromLong(0); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 67 | if (!start) { |
| 68 | Py_DECREF(stop); |
| 69 | return NULL; |
| 70 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 71 | step = PyLong_FromLong(1); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 72 | if (!step) { |
| 73 | Py_DECREF(stop); |
| 74 | Py_DECREF(start); |
| 75 | return NULL; |
| 76 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 77 | } |
| 78 | else { |
| 79 | if (!PyArg_UnpackTuple(args, "range", 2, 3, |
| 80 | &start, &stop, &step)) |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 81 | return NULL; |
Tim Peters | feec453 | 2004-08-08 07:17:39 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 83 | /* Convert borrowed refs to owned refs */ |
| 84 | start = PyNumber_Index(start); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 85 | if (!start) |
| 86 | return NULL; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 87 | stop = PyNumber_Index(stop); |
Raymond Hettinger | 94f5583 | 2009-06-12 18:40:16 +0000 | [diff] [blame] | 88 | if (!stop) { |
| 89 | Py_DECREF(start); |
| 90 | return NULL; |
| 91 | } |
| 92 | step = validate_step(step); /* Caution, this can clear exceptions */ |
| 93 | if (!step) { |
| 94 | Py_DECREF(start); |
| 95 | Py_DECREF(stop); |
| 96 | return NULL; |
| 97 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | obj = PyObject_New(rangeobject, &PyRange_Type); |
| 101 | if (obj == NULL) |
| 102 | goto Fail; |
| 103 | obj->start = start; |
| 104 | obj->stop = stop; |
| 105 | obj->step = step; |
| 106 | return (PyObject *) obj; |
| 107 | |
| 108 | Fail: |
| 109 | Py_XDECREF(start); |
| 110 | Py_XDECREF(stop); |
| 111 | Py_XDECREF(step); |
| 112 | return NULL; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 115 | PyDoc_STRVAR(range_doc, |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 116 | "range([start,] stop[, step]) -> range object\n\ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 117 | \n\ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 118 | Returns an iterator that generates the numbers in the range on demand."); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 120 | static void |
| 121 | range_dealloc(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 122 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 123 | Py_DECREF(r->start); |
| 124 | Py_DECREF(r->stop); |
| 125 | Py_DECREF(r->step); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 126 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Georg Brandl | 7d1e880 | 2009-08-13 08:47:18 +0000 | [diff] [blame^] | 129 | /* Return number of items in range (lo, hi, step) as a PyLong object, |
| 130 | * when arguments are PyLong objects. Arguments MUST return 1 with |
| 131 | * PyLong_Check(). Return NULL when there is an error. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 132 | */ |
| 133 | static PyObject* |
| 134 | range_length_obj(rangeobject *r) |
| 135 | { |
| 136 | /* ------------------------------------------------------------- |
| 137 | Algorithm is equal to that of get_len_of_range(), but it operates |
Benjamin Peterson | a47af9c | 2009-06-24 21:14:38 +0000 | [diff] [blame] | 138 | on PyObjects (which are assumed to be PyLong objects). |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 139 | ---------------------------------------------------------------*/ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 140 | int cmp_result; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 141 | PyObject *lo, *hi; |
| 142 | PyObject *step = NULL; |
| 143 | PyObject *diff = NULL; |
| 144 | PyObject *one = NULL; |
| 145 | PyObject *tmp1 = NULL, *tmp2 = NULL, *result; |
| 146 | /* holds sub-expression evaluations */ |
| 147 | |
| 148 | PyObject *zero = PyLong_FromLong(0); |
| 149 | if (zero == NULL) |
| 150 | return NULL; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 151 | cmp_result = PyObject_RichCompareBool(r->step, zero, Py_GT); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 152 | Py_DECREF(zero); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 153 | if (cmp_result == -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 154 | return NULL; |
| 155 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 156 | if (cmp_result == 1) { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 157 | lo = r->start; |
| 158 | hi = r->stop; |
| 159 | step = r->step; |
| 160 | Py_INCREF(step); |
| 161 | } else { |
| 162 | lo = r->stop; |
| 163 | hi = r->start; |
| 164 | step = PyNumber_Negative(r->step); |
| 165 | if (!step) |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | /* if (lo >= hi), return length of 0. */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 170 | if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 171 | Py_XDECREF(step); |
| 172 | return PyLong_FromLong(0); |
| 173 | } |
| 174 | |
| 175 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 176 | goto Fail; |
| 177 | |
| 178 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 179 | goto Fail; |
| 180 | |
| 181 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 182 | goto Fail; |
| 183 | |
| 184 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 185 | goto Fail; |
| 186 | |
| 187 | if ((result = PyNumber_Add(tmp2, one)) == NULL) |
| 188 | goto Fail; |
| 189 | |
| 190 | Py_DECREF(tmp2); |
| 191 | Py_DECREF(diff); |
| 192 | Py_DECREF(step); |
| 193 | Py_DECREF(tmp1); |
| 194 | Py_DECREF(one); |
| 195 | return result; |
| 196 | |
| 197 | Fail: |
| 198 | Py_XDECREF(tmp2); |
| 199 | Py_XDECREF(diff); |
| 200 | Py_XDECREF(step); |
| 201 | Py_XDECREF(tmp1); |
| 202 | Py_XDECREF(one); |
| 203 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 206 | static Py_ssize_t |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 207 | range_length(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 208 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 209 | PyObject *len = range_length_obj(r); |
| 210 | Py_ssize_t result = -1; |
| 211 | if (len) { |
| 212 | result = PyLong_AsSsize_t(len); |
| 213 | Py_DECREF(len); |
| 214 | } |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | /* range(...)[x] is necessary for: seq[:] = range(...) */ |
| 219 | |
| 220 | static PyObject * |
| 221 | range_item(rangeobject *r, Py_ssize_t i) |
| 222 | { |
| 223 | Py_ssize_t len = range_length(r); |
| 224 | PyObject *rem, *incr, *result; |
| 225 | |
| 226 | /* XXX(nnorwitz): should negative indices be supported? */ |
| 227 | /* XXX(nnorwitz): should support range[x] where x > PY_SSIZE_T_MAX? */ |
| 228 | if (i < 0 || i >= len) { |
| 229 | if (!PyErr_Occurred()) |
| 230 | PyErr_SetString(PyExc_IndexError, |
Walter Dörwald | 4ad9421 | 2007-05-21 18:01:17 +0000 | [diff] [blame] | 231 | "range object index out of range"); |
Benjamin Peterson | df0a5cb | 2008-04-25 21:15:37 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 234 | |
| 235 | /* XXX(nnorwitz): optimize for short ints. */ |
Raymond Hettinger | ad3f332 | 2008-02-09 04:13:49 +0000 | [diff] [blame] | 236 | rem = PyLong_FromSsize_t(i); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 237 | if (!rem) |
| 238 | return NULL; |
| 239 | incr = PyNumber_Multiply(rem, r->step); |
| 240 | Py_DECREF(rem); |
| 241 | if (!incr) |
| 242 | return NULL; |
| 243 | result = PyNumber_Add(r->start, incr); |
| 244 | Py_DECREF(incr); |
| 245 | return result; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 249 | range_repr(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 250 | { |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 251 | Py_ssize_t istep; |
Tim Peters | d976ab7 | 2004-08-08 06:29:10 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 253 | /* Check for special case values for printing. We don't always |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 254 | need the step value. We don't care about errors |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 255 | (it means overflow), so clear the errors. */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 256 | istep = PyNumber_AsSsize_t(r->step, NULL); |
| 257 | if (istep != 1 || (istep == -1 && PyErr_Occurred())) { |
| 258 | PyErr_Clear(); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Walter Dörwald | 03b43d8 | 2007-05-21 10:43:34 +0000 | [diff] [blame] | 261 | if (istep == 1) |
Walter Dörwald | 850e516 | 2007-05-20 08:19:54 +0000 | [diff] [blame] | 262 | return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop); |
| 263 | else |
| 264 | return PyUnicode_FromFormat("range(%R, %R, %R)", |
| 265 | r->start, r->stop, r->step); |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Alexandre Vassalotti | 7505607 | 2008-06-10 04:03:04 +0000 | [diff] [blame] | 268 | /* Pickling support */ |
| 269 | static PyObject * |
| 270 | range_reduce(rangeobject *r, PyObject *args) |
| 271 | { |
| 272 | return Py_BuildValue("(O(OOO))", Py_TYPE(r), |
| 273 | r->start, r->stop, r->step); |
| 274 | } |
| 275 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | static PySequenceMethods range_as_sequence = { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 277 | (lenfunc)range_length, /* sq_length */ |
| 278 | 0, /* sq_concat */ |
| 279 | 0, /* sq_repeat */ |
| 280 | (ssizeargfunc)range_item, /* sq_item */ |
| 281 | 0, /* sq_slice */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 284 | static PyObject * range_iter(PyObject *seq); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 285 | static PyObject * range_reverse(PyObject *seq); |
| 286 | |
| 287 | PyDoc_STRVAR(reverse_doc, |
| 288 | "Returns a reverse iterator."); |
| 289 | |
| 290 | static PyMethodDef range_methods[] = { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 291 | {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, |
| 292 | reverse_doc}, |
Alexandre Vassalotti | 7505607 | 2008-06-10 04:03:04 +0000 | [diff] [blame] | 293 | {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS}, |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 294 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 295 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 297 | PyTypeObject PyRange_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 298 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 299 | "range", /* Name of this type */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 300 | sizeof(rangeobject), /* Basic object size */ |
| 301 | 0, /* Item size for varobject */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 302 | (destructor)range_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 303 | 0, /* tp_print */ |
| 304 | 0, /* tp_getattr */ |
| 305 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 306 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 307 | (reprfunc)range_repr, /* tp_repr */ |
| 308 | 0, /* tp_as_number */ |
| 309 | &range_as_sequence, /* tp_as_sequence */ |
| 310 | 0, /* tp_as_mapping */ |
| 311 | 0, /* tp_hash */ |
| 312 | 0, /* tp_call */ |
| 313 | 0, /* tp_str */ |
| 314 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 315 | 0, /* tp_setattro */ |
| 316 | 0, /* tp_as_buffer */ |
| 317 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 318 | range_doc, /* tp_doc */ |
| 319 | 0, /* tp_traverse */ |
| 320 | 0, /* tp_clear */ |
| 321 | 0, /* tp_richcompare */ |
| 322 | 0, /* tp_weaklistoffset */ |
| 323 | range_iter, /* tp_iter */ |
| 324 | 0, /* tp_iternext */ |
| 325 | range_methods, /* tp_methods */ |
| 326 | 0, /* tp_members */ |
| 327 | 0, /* tp_getset */ |
| 328 | 0, /* tp_base */ |
| 329 | 0, /* tp_dict */ |
| 330 | 0, /* tp_descr_get */ |
| 331 | 0, /* tp_descr_set */ |
| 332 | 0, /* tp_dictoffset */ |
| 333 | 0, /* tp_init */ |
| 334 | 0, /* tp_alloc */ |
| 335 | range_new, /* tp_new */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 336 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 337 | |
Walter Dörwald | 4ad9421 | 2007-05-21 18:01:17 +0000 | [diff] [blame] | 338 | /*********************** range Iterator **************************/ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 340 | /* There are 2 types of iterators, one for C longs, the other for |
| 341 | Python longs (ie, PyObjects). This should make iteration fast |
| 342 | in the normal case, but possible for any numeric value. |
| 343 | */ |
| 344 | |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 345 | typedef struct { |
| 346 | PyObject_HEAD |
| 347 | long index; |
| 348 | long start; |
| 349 | long step; |
| 350 | long len; |
| 351 | } rangeiterobject; |
| 352 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 353 | static PyObject * |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 354 | rangeiter_next(rangeiterobject *r) |
| 355 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 356 | if (r->index < r->len) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 357 | return PyLong_FromLong(r->start + (r->index++) * r->step); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 358 | return NULL; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 361 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 362 | rangeiter_len(rangeiterobject *r) |
| 363 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 364 | return PyLong_FromLong(r->len - r->index); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 367 | typedef struct { |
| 368 | PyObject_HEAD |
| 369 | PyObject *index; |
| 370 | PyObject *start; |
| 371 | PyObject *step; |
| 372 | PyObject *len; |
| 373 | } longrangeiterobject; |
| 374 | |
| 375 | static PyObject * |
| 376 | longrangeiter_len(longrangeiterobject *r, PyObject *no_args) |
| 377 | { |
| 378 | return PyNumber_Subtract(r->len, r->index); |
| 379 | } |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 381 | static PyObject *rangeiter_new(PyTypeObject *, PyObject *args, PyObject *kw); |
| 382 | |
| 383 | PyDoc_STRVAR(length_hint_doc, |
| 384 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 385 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 386 | static PyMethodDef rangeiter_methods[] = { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 387 | {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, |
| 388 | length_hint_doc}, |
| 389 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 390 | }; |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 391 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 392 | PyTypeObject PyRangeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 393 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 394 | "range_iterator", /* tp_name */ |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 395 | sizeof(rangeiterobject), /* tp_basicsize */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 396 | 0, /* tp_itemsize */ |
| 397 | /* methods */ |
| 398 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 399 | 0, /* tp_print */ |
| 400 | 0, /* tp_getattr */ |
| 401 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 402 | 0, /* tp_reserved */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 403 | 0, /* tp_repr */ |
| 404 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 405 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 406 | 0, /* tp_as_mapping */ |
| 407 | 0, /* tp_hash */ |
| 408 | 0, /* tp_call */ |
| 409 | 0, /* tp_str */ |
| 410 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 411 | 0, /* tp_setattro */ |
| 412 | 0, /* tp_as_buffer */ |
| 413 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 414 | 0, /* tp_doc */ |
| 415 | 0, /* tp_traverse */ |
| 416 | 0, /* tp_clear */ |
| 417 | 0, /* tp_richcompare */ |
| 418 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 419 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 420 | (iternextfunc)rangeiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 421 | rangeiter_methods, /* tp_methods */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 422 | 0, /* tp_members */ |
| 423 | 0, /* tp_getset */ |
| 424 | 0, /* tp_base */ |
| 425 | 0, /* tp_dict */ |
| 426 | 0, /* tp_descr_get */ |
| 427 | 0, /* tp_descr_set */ |
| 428 | 0, /* tp_dictoffset */ |
| 429 | 0, /* tp_init */ |
| 430 | 0, /* tp_alloc */ |
| 431 | rangeiter_new, /* tp_new */ |
| 432 | }; |
| 433 | |
| 434 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 435 | * required. Return a value < 0 if & only if the true value is too |
| 436 | * large to fit in a signed long. |
| 437 | */ |
| 438 | static long |
| 439 | get_len_of_range(long lo, long hi, long step) |
| 440 | { |
| 441 | /* ------------------------------------------------------------- |
| 442 | If lo >= hi, the range is empty. |
| 443 | Else if n values are in the range, the last one is |
| 444 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 445 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 446 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 447 | the RHS is non-negative and so truncation is the same as the |
| 448 | floor. Letting M be the largest positive long, the worst case |
| 449 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 450 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 451 | precision to compute the RHS exactly. |
| 452 | ---------------------------------------------------------------*/ |
| 453 | long n = 0; |
| 454 | if (lo < hi) { |
| 455 | unsigned long uhi = (unsigned long)hi; |
| 456 | unsigned long ulo = (unsigned long)lo; |
| 457 | unsigned long diff = uhi - ulo - 1; |
| 458 | n = (long)(diff / (unsigned long)step + 1); |
| 459 | } |
| 460 | return n; |
| 461 | } |
| 462 | |
| 463 | static PyObject * |
| 464 | int_range_iter(long start, long stop, long step) |
| 465 | { |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 466 | rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 467 | if (it == NULL) |
| 468 | return NULL; |
| 469 | it->start = start; |
| 470 | it->step = step; |
| 471 | if (step > 0) |
| 472 | it->len = get_len_of_range(start, stop, step); |
| 473 | else |
| 474 | it->len = get_len_of_range(stop, start, -step); |
| 475 | it->index = 0; |
| 476 | return (PyObject *)it; |
| 477 | } |
| 478 | |
| 479 | static PyObject * |
| 480 | rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 481 | { |
| 482 | long start, stop, step; |
| 483 | |
| 484 | if (!_PyArg_NoKeywords("rangeiter()", kw)) |
| 485 | return NULL; |
| 486 | |
| 487 | if (!PyArg_ParseTuple(args, "lll;rangeiter() requires 3 int arguments", |
| 488 | &start, &stop, &step)) |
| 489 | return NULL; |
| 490 | |
| 491 | return int_range_iter(start, stop, step); |
| 492 | } |
| 493 | |
| 494 | static PyMethodDef longrangeiter_methods[] = { |
| 495 | {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS, |
| 496 | length_hint_doc}, |
| 497 | {NULL, NULL} /* sentinel */ |
| 498 | }; |
| 499 | |
| 500 | static void |
| 501 | longrangeiter_dealloc(longrangeiterobject *r) |
| 502 | { |
| 503 | Py_XDECREF(r->index); |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 504 | Py_XDECREF(r->start); |
| 505 | Py_XDECREF(r->step); |
| 506 | Py_XDECREF(r->len); |
Amaury Forgeot d'Arc | b7f17e4 | 2007-11-15 20:52:21 +0000 | [diff] [blame] | 507 | PyObject_Del(r); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static PyObject * |
| 511 | longrangeiter_next(longrangeiterobject *r) |
| 512 | { |
| 513 | PyObject *one, *product, *new_index, *result; |
| 514 | if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1) |
| 515 | return NULL; |
| 516 | |
| 517 | one = PyLong_FromLong(1); |
| 518 | if (!one) |
| 519 | return NULL; |
| 520 | |
| 521 | product = PyNumber_Multiply(r->index, r->step); |
| 522 | if (!product) { |
| 523 | Py_DECREF(one); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | new_index = PyNumber_Add(r->index, one); |
| 528 | Py_DECREF(one); |
| 529 | if (!new_index) { |
| 530 | Py_DECREF(product); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | result = PyNumber_Add(r->start, product); |
| 535 | Py_DECREF(product); |
| 536 | if (result) { |
| 537 | Py_DECREF(r->index); |
| 538 | r->index = new_index; |
| 539 | } |
| 540 | |
| 541 | return result; |
| 542 | } |
| 543 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 544 | PyTypeObject PyLongRangeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 545 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 546 | "longrange_iterator", /* tp_name */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 547 | sizeof(longrangeiterobject), /* tp_basicsize */ |
| 548 | 0, /* tp_itemsize */ |
| 549 | /* methods */ |
| 550 | (destructor)longrangeiter_dealloc, /* tp_dealloc */ |
| 551 | 0, /* tp_print */ |
| 552 | 0, /* tp_getattr */ |
| 553 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 554 | 0, /* tp_reserved */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 555 | 0, /* tp_repr */ |
| 556 | 0, /* tp_as_number */ |
| 557 | 0, /* tp_as_sequence */ |
| 558 | 0, /* tp_as_mapping */ |
| 559 | 0, /* tp_hash */ |
| 560 | 0, /* tp_call */ |
| 561 | 0, /* tp_str */ |
| 562 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 563 | 0, /* tp_setattro */ |
| 564 | 0, /* tp_as_buffer */ |
| 565 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 566 | 0, /* tp_doc */ |
| 567 | 0, /* tp_traverse */ |
| 568 | 0, /* tp_clear */ |
| 569 | 0, /* tp_richcompare */ |
| 570 | 0, /* tp_weaklistoffset */ |
| 571 | PyObject_SelfIter, /* tp_iter */ |
| 572 | (iternextfunc)longrangeiter_next, /* tp_iternext */ |
| 573 | longrangeiter_methods, /* tp_methods */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 574 | 0, |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 575 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 576 | |
| 577 | static PyObject * |
| 578 | range_iter(PyObject *seq) |
| 579 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 580 | rangeobject *r = (rangeobject *)seq; |
| 581 | longrangeiterobject *it; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 582 | long lstart, lstop, lstep; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 583 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 584 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 585 | |
| 586 | /* If all three fields convert to long, use the int version */ |
| 587 | lstart = PyLong_AsLong(r->start); |
| 588 | if (lstart != -1 || !PyErr_Occurred()) { |
| 589 | lstop = PyLong_AsLong(r->stop); |
| 590 | if (lstop != -1 || !PyErr_Occurred()) { |
| 591 | lstep = PyLong_AsLong(r->step); |
| 592 | if (lstep != -1 || !PyErr_Occurred()) |
| 593 | return int_range_iter(lstart, lstop, lstep); |
| 594 | } |
| 595 | } |
| 596 | /* Some conversion failed, so there is an error set. Clear it, |
| 597 | and try again with a long range. */ |
| 598 | PyErr_Clear(); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 599 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 600 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 601 | if (it == NULL) |
| 602 | return NULL; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 603 | |
| 604 | /* Do all initialization here, so we can DECREF on failure. */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 605 | it->start = r->start; |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 606 | it->step = r->step; |
| 607 | Py_INCREF(it->start); |
| 608 | Py_INCREF(it->step); |
| 609 | |
| 610 | it->len = it->index = NULL; |
| 611 | |
Mark Dickinson | eb36d31 | 2009-06-24 18:36:46 +0000 | [diff] [blame] | 612 | it->len = range_length_obj(r); |
| 613 | if (!it->len) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 614 | goto create_failure; |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 615 | it->index = PyLong_FromLong(0); |
| 616 | if (!it->index) |
| 617 | goto create_failure; |
| 618 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 619 | return (PyObject *)it; |
| 620 | |
| 621 | create_failure: |
Guido van Rossum | 317e774 | 2007-05-08 15:18:31 +0000 | [diff] [blame] | 622 | Py_DECREF(it); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 623 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static PyObject * |
| 627 | range_reverse(PyObject *seq) |
| 628 | { |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 629 | rangeobject *range = (rangeobject*) seq; |
| 630 | longrangeiterobject *it; |
| 631 | PyObject *one, *sum, *diff, *len = NULL, *product; |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 632 | long lstart, lstop, lstep; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 634 | /* XXX(nnorwitz): do the calc for the new start/stop first, |
| 635 | then if they fit, call the proper iter()? |
| 636 | */ |
| 637 | assert(PyRange_Check(seq)); |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 638 | |
| 639 | /* If all three fields convert to long, use the int version */ |
| 640 | lstart = PyLong_AsLong(range->start); |
| 641 | if (lstart != -1 || !PyErr_Occurred()) { |
| 642 | lstop = PyLong_AsLong(range->stop); |
| 643 | if (lstop != -1 || !PyErr_Occurred()) { |
| 644 | lstep = PyLong_AsLong(range->step); |
| 645 | if (lstep != -1 || !PyErr_Occurred()) { |
| 646 | /* XXX(nnorwitz): need to check for overflow and simplify. */ |
| 647 | long len = get_len_of_range(lstart, lstop, lstep); |
| 648 | long new_start = lstart + (len - 1) * lstep; |
| 649 | long new_stop = lstart; |
| 650 | if (lstep > 0) |
| 651 | new_stop -= 1; |
| 652 | else |
| 653 | new_stop += 1; |
| 654 | return int_range_iter(new_start, new_stop, -lstep); |
| 655 | } |
| 656 | } |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 657 | } |
Martin v. Löwis | 8445104 | 2007-12-20 22:57:23 +0000 | [diff] [blame] | 658 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 659 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 660 | it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 661 | if (it == NULL) |
| 662 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 664 | /* start + (len - 1) * step */ |
| 665 | len = range_length_obj(range); |
| 666 | if (!len) |
| 667 | goto create_failure; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 668 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 669 | one = PyLong_FromLong(1); |
| 670 | if (!one) |
| 671 | goto create_failure; |
| 672 | |
| 673 | diff = PyNumber_Subtract(len, one); |
| 674 | Py_DECREF(one); |
| 675 | if (!diff) |
| 676 | goto create_failure; |
| 677 | |
| 678 | product = PyNumber_Multiply(len, range->step); |
| 679 | if (!product) |
| 680 | goto create_failure; |
| 681 | |
| 682 | sum = PyNumber_Add(range->start, product); |
| 683 | Py_DECREF(product); |
| 684 | it->start = sum; |
| 685 | if (!it->start) |
| 686 | goto create_failure; |
| 687 | it->step = PyNumber_Negative(range->step); |
| 688 | if (!it->step) { |
| 689 | Py_DECREF(it->start); |
| 690 | PyObject_Del(it); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | /* Steal reference to len. */ |
| 695 | it->len = len; |
| 696 | |
| 697 | it->index = PyLong_FromLong(0); |
| 698 | if (!it->index) { |
| 699 | Py_DECREF(it); |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | return (PyObject *)it; |
| 704 | |
| 705 | create_failure: |
| 706 | Py_XDECREF(len); |
| 707 | PyObject_Del(it); |
| 708 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 709 | } |