| LEFT | RIGHT |
|---|---|
| 1 /* Range object implementation */ | 1 /* Range object implementation */ |
| 2 | 2 |
| 3 #include "Python.h" | 3 #include "Python.h" |
| 4 #include "structmember.h" | 4 #include "structmember.h" |
| 5 | 5 |
| 6 /* Support objects whose length is > PY_SSIZE_T_MAX. | 6 /* Support objects whose length is > PY_SSIZE_T_MAX. |
| 7 | 7 |
| 8 This could be sped up for small PyLongs if they fit in an Py_ssize_t. | 8 This could be sped up for small PyLongs if they fit in an Py_ssize_t. |
| 9 This only matters on Win64. Though we could use PY_LONG_LONG which | 9 This only matters on Win64. Though we could use PY_LONG_LONG which |
| 10 would presumably help perf. | 10 would presumably help perf. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 | 114 |
| 115 static void | 115 static void |
| 116 range_dealloc(rangeobject *r) | 116 range_dealloc(rangeobject *r) |
| 117 { | 117 { |
| 118 Py_DECREF(r->start); | 118 Py_DECREF(r->start); |
| 119 Py_DECREF(r->stop); | 119 Py_DECREF(r->stop); |
| 120 Py_DECREF(r->step); | 120 Py_DECREF(r->step); |
| 121 Py_DECREF(r->length); | 121 Py_DECREF(r->length); |
| 122 PyObject_Del(r); | 122 PyObject_Del(r); |
| 123 } | 123 } |
| 124 | 124 |
|
GvR
2008/05/03 05:04:35
Hm. I didn't mean for you to delete the entire com
| |
| 125 /*Return number of items in range (lo, hi, step), when arguments are | |
| 126 * PyLong objects. step > 0 required. Return a PyLong with the length. | |
| 127 * Return NULL when there is an error.*/ | |
| 125 static PyObject * | 128 static PyObject * |
| 126 range_compute_length(PyObject *start, PyObject *stop, PyObject *step) | 129 range_compute_length(PyObject *start, PyObject *stop, PyObject *step) |
| 127 { | 130 { |
| 128 /* ------------------------------------------------------------- | 131 /* ------------------------------------------------------------- |
| 129 Algorithm is equal to that of get_len_of_range(), but it operates | 132 Algorithm is equal to that of get_len_of_range(), but it operates |
| 130 on PyObjects (which are assumed to be PyLong or PyInt objects). | 133 on PyObjects (which are assumed to be PyLong or PyInt objects). |
| 131 ---------------------------------------------------------------*/ | 134 ---------------------------------------------------------------*/ |
| 132 int cmp_result, cmp_call; | 135 int cmp_result, cmp_call; |
| 133 PyObject *lo, *hi; | 136 PyObject *lo, *hi; |
| 134 PyObject *diff = NULL; | 137 PyObject *diff = NULL; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 | 190 |
| 188 Fail: | 191 Fail: |
| 189 Py_XDECREF(tmp2); | 192 Py_XDECREF(tmp2); |
| 190 Py_XDECREF(diff); | 193 Py_XDECREF(diff); |
| 191 Py_XDECREF(step); | 194 Py_XDECREF(step); |
| 192 Py_XDECREF(tmp1); | 195 Py_XDECREF(tmp1); |
| 193 Py_XDECREF(one); | 196 Py_XDECREF(one); |
| 194 return NULL; | 197 return NULL; |
| 195 } | 198 } |
| 196 | 199 |
| 197 static PyObject * | 200 static Py_ssize_t |
| 198 range_length_hint(rangeobject *r) | 201 range_length(rangeobject *r) |
| 199 { | 202 { |
| 200 Py_INCREF(r->length); | 203 return PyLong_AsSsize_t(r->length); |
| 201 return r->length; | 204 } |
| 202 } | |
| 203 | |
| 204 PyDoc_STRVAR(range_length_hint_doc, | |
| 205 "__length_hint__() -> int\n" | |
| 206 "Return a estimate of len(list(range))."); | |
| 207 | 205 |
| 208 | 206 |
| 209 static PyObject * | 207 static PyObject * |
| 210 range_repr(rangeobject *r) | 208 range_repr(rangeobject *r) |
| 211 { | 209 { |
| 212 Py_ssize_t istep; | 210 Py_ssize_t istep; |
| 213 | 211 |
| 214 /* Check for special case values for printing. We don't always | 212 /* Check for special case values for printing. We don't always |
| 215 need the step value. We don't care about errors | 213 need the step value. We don't care about errors |
| 216 (it means overflow), so clear the errors. */ | 214 (it means overflow), so clear the errors. */ |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 232 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY}, | 230 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY}, |
| 233 {0} | 231 {0} |
| 234 }; | 232 }; |
| 235 | 233 |
| 236 static PyObject * range_iter(PyObject *seq); | 234 static PyObject * range_iter(PyObject *seq); |
| 237 static PyObject * range_reverse(PyObject *seq); | 235 static PyObject * range_reverse(PyObject *seq); |
| 238 | 236 |
| 239 PyDoc_STRVAR(reverse_doc, | 237 PyDoc_STRVAR(reverse_doc, |
| 240 "Returns a reverse iterator."); | 238 "Returns a reverse iterator."); |
| 241 | 239 |
| 240 static PySequenceMethods range_as_sequence = { | |
| 241 (lenfunc)range_length, /* sq_length */ | |
| 242 0, /* sq_concat */ | |
| 243 0, /* sq_repeat */ | |
| 244 0, /* sq_item */ | |
| 245 0, /* sq_slice */ | |
| 246 0, /* sq_ass_item */ | |
| 247 0, /* sq_ass_slice */ | |
| 248 0, /* sq_contains */ | |
| 249 0, /* sq_inplace_concat */ | |
| 250 0, /* sq_inplace_repeat */ | |
| 251 }; | |
| 252 | |
| 242 static PyMethodDef range_methods[] = { | 253 static PyMethodDef range_methods[] = { |
| 243 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, | 254 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, |
| 244 reverse_doc}, | 255 reverse_doc}, |
| 245 {"__length_hint__", (PyCFunction)range_length_hint, METH_NOARGS, | |
| 246 range_length_hint_doc}, | |
| 247 {NULL, NULL} /* sentinel */ | 256 {NULL, NULL} /* sentinel */ |
| 248 }; | 257 }; |
| 249 | 258 |
| 250 PyTypeObject PyRange_Type = { | 259 PyTypeObject PyRange_Type = { |
| 251 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 260 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 252 "range", /* Name of this type */ | 261 "range", /* Name of this type */ |
| 253 sizeof(rangeobject), /* Basic object size */ | 262 sizeof(rangeobject), /* Basic object size */ |
| 254 0, /* Item size for varobject */ | 263 0, /* Item size for varobject */ |
| 255 (destructor)range_dealloc, /* tp_dealloc */ | 264 (destructor)range_dealloc, /* tp_dealloc */ |
| 256 0, /* tp_print */ | 265 0, /* tp_print */ |
| 257 0, /* tp_getattr */ | 266 0, /* tp_getattr */ |
| 258 0, /* tp_setattr */ | 267 0, /* tp_setattr */ |
| 259 0, /* tp_compare */ | 268 0, /* tp_compare */ |
| 260 (reprfunc)range_repr, /* tp_repr */ | 269 (reprfunc)range_repr, /* tp_repr */ |
| 261 0, /* tp_as_number */ | 270 0, /* tp_as_number */ |
| 262 » 0, /* tp_as_sequence */ | 271 » &range_as_sequence, /* tp_as_sequence */ |
| 263 0, /* tp_as_mapping */ | 272 0, /* tp_as_mapping */ |
| 264 0, /* tp_hash */ | 273 0, /* tp_hash */ |
| 265 0, /* tp_call */ | 274 0, /* tp_call */ |
| 266 0, /* tp_str */ | 275 0, /* tp_str */ |
| 267 PyObject_GenericGetAttr, /* tp_getattro */ | 276 PyObject_GenericGetAttr, /* tp_getattro */ |
| 268 0, /* tp_setattro */ | 277 0, /* tp_setattro */ |
| 269 0, /* tp_as_buffer */ | 278 0, /* tp_as_buffer */ |
| 270 Py_TPFLAGS_DEFAULT, /* tp_flags */ | 279 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 271 range_doc, /* tp_doc */ | 280 range_doc, /* tp_doc */ |
| 272 0, /* tp_traverse */ | 281 0, /* tp_traverse */ |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 | 565 |
| 557 /* Do all initialization here, so we can DECREF on failure. */ | 566 /* Do all initialization here, so we can DECREF on failure. */ |
| 558 it->start = r->start; | 567 it->start = r->start; |
| 559 it->step = r->step; | 568 it->step = r->step; |
| 560 Py_INCREF(it->start); | 569 Py_INCREF(it->start); |
| 561 Py_INCREF(it->step); | 570 Py_INCREF(it->step); |
| 562 | 571 |
| 563 Py_INCREF(r->length); | 572 Py_INCREF(r->length); |
| 564 it->length = r->length; | 573 it->length = r->length; |
| 565 it->index = PyLong_FromLong(0); | 574 it->index = PyLong_FromLong(0); |
| 566 if (!it->index) | 575 if (it->index == NULL) |
| 567 goto create_failure; | 576 goto create_failure; |
| 568 | 577 |
| 569 return (PyObject *)it; | 578 return (PyObject *)it; |
| 570 | 579 |
| 571 create_failure: | 580 create_failure: |
| 572 Py_DECREF(it); | 581 Py_DECREF(it); |
| 573 return NULL; | 582 return NULL; |
| 574 } | 583 } |
| 575 | 584 |
| 576 static PyObject * | 585 static PyObject * |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 649 return NULL; | 658 return NULL; |
| 650 } | 659 } |
| 651 | 660 |
| 652 return (PyObject *)it; | 661 return (PyObject *)it; |
| 653 | 662 |
| 654 create_failure: | 663 create_failure: |
| 655 Py_XDECREF(len); | 664 Py_XDECREF(len); |
| 656 PyObject_Del(it); | 665 PyObject_Del(it); |
| 657 return NULL; | 666 return NULL; |
| 658 } | 667 } |
| LEFT | RIGHT |