Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(134)

Delta Between Two Patch Sets: Objects/rangeobject.c

Issue 602: range: lean and mean (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/branches/py3k/
Left Patch Set: __len__ is back! Created 5 months, 1 week ago
Right Patch Set: in response to reviews Created 5 months, 1 week ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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...) Show 10 above Show 10 below
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 static PyObject * 125 static PyObject *
126 range_compute_length(PyObject *start, PyObject *stop, PyObject *step) 126 range_compute_length(PyObject *start, PyObject *stop, PyObject *step)
127 { 127 {
128 /* ------------------------------------------------------------- 128 /* -------------------------------------------------------------
129 Algorithm is equal to that of get_len_of_range(), but it operates 129 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). 130 on PyObjects (which are assumed to be PyLong or PyInt objects).
131 ---------------------------------------------------------------*/ 131 ---------------------------------------------------------------*/
132 int cmp_result, cmp_call; 132 int cmp_result, cmp_call;
133 PyObject *lo, *hi; 133 PyObject *lo, *hi;
134 PyObject *diff = NULL; 134 PyObject *diff = NULL;
(...skipping 52 matching lines...) Show 10 above Show 10 below
187 187
188 Fail: 188 Fail:
189 Py_XDECREF(tmp2); 189 Py_XDECREF(tmp2);
190 Py_XDECREF(diff); 190 Py_XDECREF(diff);
191 Py_XDECREF(step); 191 Py_XDECREF(step);
192 Py_XDECREF(tmp1); 192 Py_XDECREF(tmp1);
193 Py_XDECREF(one); 193 Py_XDECREF(one);
194 return NULL; 194 return NULL;
195 } 195 }
196 196
197 static Py_ssize_t 197 static PyObject *
198 range_length(rangeobject *r) 198 range_length_hint(rangeobject *r)
199 { 199 {
200 return PyLong_AsSsize_t(r->length); 200 Py_INCREF(r->length);
201 } 201 return r->length;
202 }
203
204 PyDoc_STRVAR(range_length_hint_doc,
205 "__length_hint__() -> int\n"
206 "Return a estimate of len(list(range)).");
202 207
203 208
204 static PyObject * 209 static PyObject *
205 range_repr(rangeobject *r) 210 range_repr(rangeobject *r)
206 { 211 {
207 Py_ssize_t istep; 212 Py_ssize_t istep;
208 213
209 /* Check for special case values for printing. We don't always 214 /* Check for special case values for printing. We don't always
210 need the step value. We don't care about errors 215 need the step value. We don't care about errors
211 (it means overflow), so clear the errors. */ 216 (it means overflow), so clear the errors. */
(...skipping 15 matching lines...) Show 10 above Show 10 below
227 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY}, 232 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY},
228 {0} 233 {0}
229 }; 234 };
230 235
231 static PyObject * range_iter(PyObject *seq); 236 static PyObject * range_iter(PyObject *seq);
232 static PyObject * range_reverse(PyObject *seq); 237 static PyObject * range_reverse(PyObject *seq);
233 238
234 PyDoc_STRVAR(reverse_doc, 239 PyDoc_STRVAR(reverse_doc,
235 "Returns a reverse iterator."); 240 "Returns a reverse iterator.");
236 241
237 static PySequenceMethods range_as_sequence = {
238 (lenfunc)range_length, /* sq_length */
239 0, /* sq_concat */
240 0, /* sq_repeat */
241 0, /* sq_item */
242 0, /* sq_slice */
243 0, /* sq_ass_item */
244 0, /* sq_ass_slice */
245 0, /* sq_contains */
246 0, /* sq_inplace_concat */
247 0, /* sq_inplace_repeat */
248 };
249
250 static PyMethodDef range_methods[] = { 242 static PyMethodDef range_methods[] = {
251 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, 243 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS,
252 reverse_doc}, 244 reverse_doc},
245 {"__length_hint__", (PyCFunction)range_length_hint, METH_NOARGS,
246 range_length_hint_doc},
253 {NULL, NULL} /* sentinel */ 247 {NULL, NULL} /* sentinel */
254 }; 248 };
255 249
256 PyTypeObject PyRange_Type = { 250 PyTypeObject PyRange_Type = {
257 PyVarObject_HEAD_INIT(&PyType_Type, 0) 251 PyVarObject_HEAD_INIT(&PyType_Type, 0)
258 "range", /* Name of this type */ 252 "range", /* Name of this type */
259 sizeof(rangeobject), /* Basic object size */ 253 sizeof(rangeobject), /* Basic object size */
260 0, /* Item size for varobject */ 254 0, /* Item size for varobject */
261 (destructor)range_dealloc, /* tp_dealloc */ 255 (destructor)range_dealloc, /* tp_dealloc */
262 0, /* tp_print */ 256 0, /* tp_print */
263 0, /* tp_getattr */ 257 0, /* tp_getattr */
264 0, /* tp_setattr */ 258 0, /* tp_setattr */
265 0, /* tp_compare */ 259 0, /* tp_compare */
266 (reprfunc)range_repr, /* tp_repr */ 260 (reprfunc)range_repr, /* tp_repr */
267 0, /* tp_as_number */ 261 0, /* tp_as_number */
268 &range_as_sequence, /* tp_as_sequence */ 262 0, /* tp_as_sequence */
269 0, /* tp_as_mapping */ 263 0, /* tp_as_mapping */
270 0, /* tp_hash */ 264 0, /* tp_hash */
271 0, /* tp_call */ 265 0, /* tp_call */
272 0, /* tp_str */ 266 0, /* tp_str */
273 PyObject_GenericGetAttr, /* tp_getattro */ 267 PyObject_GenericGetAttr, /* tp_getattro */
274 0, /* tp_setattro */ 268 0, /* tp_setattro */
275 0, /* tp_as_buffer */ 269 0, /* tp_as_buffer */
276 Py_TPFLAGS_DEFAULT, /* tp_flags */ 270 Py_TPFLAGS_DEFAULT, /* tp_flags */
277 range_doc, /* tp_doc */ 271 range_doc, /* tp_doc */
278 0, /* tp_traverse */ 272 0, /* tp_traverse */
(...skipping 283 matching lines...) Show 10 above Show 10 below
562 556
563 /* Do all initialization here, so we can DECREF on failure. */ 557 /* Do all initialization here, so we can DECREF on failure. */
564 it->start = r->start; 558 it->start = r->start;
565 it->step = r->step; 559 it->step = r->step;
566 Py_INCREF(it->start); 560 Py_INCREF(it->start);
567 Py_INCREF(it->step); 561 Py_INCREF(it->step);
568 562
569 Py_INCREF(r->length); 563 Py_INCREF(r->length);
570 it->length = r->length; 564 it->length = r->length;
571 it->index = PyLong_FromLong(0); 565 it->index = PyLong_FromLong(0);
572 if (it->index == NULL) 566 if (!it->index)
573 goto create_failure; 567 goto create_failure;
574 568
575 return (PyObject *)it; 569 return (PyObject *)it;
576 570
577 create_failure: 571 create_failure:
578 Py_DECREF(it); 572 Py_DECREF(it);
579 return NULL; 573 return NULL;
580 } 574 }
581 575
582 static PyObject * 576 static PyObject *
(...skipping 72 matching lines...) Show 10 above Show 10 below
655 return NULL; 649 return NULL;
656 } 650 }
657 651
658 return (PyObject *)it; 652 return (PyObject *)it;
659 653
660 create_failure: 654 create_failure:
661 Py_XDECREF(len); 655 Py_XDECREF(len);
662 PyObject_Del(it); 656 PyObject_Del(it);
663 return NULL; 657 return NULL;
664 } 658 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r338