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

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: in response to reviews Created 5 months, 1 week ago
Right Patch Set: __len__ is back! 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 PyObject * 197 static Py_ssize_t
198 range_length_hint(rangeobject *r) 198 range_length(rangeobject *r)
199 { 199 {
200 Py_INCREF(r->length); 200 return PyLong_AsSsize_t(r->length);
201 return r->length; 201 }
202 }
203
204 PyDoc_STRVAR(range_length_hint_doc,
205 "__length_hint__() -> int\n"
206 "Return a estimate of len(list(range)).");
207 202
208 203
209 static PyObject * 204 static PyObject *
210 range_repr(rangeobject *r) 205 range_repr(rangeobject *r)
211 { 206 {
212 Py_ssize_t istep; 207 Py_ssize_t istep;
213 208
214 /* Check for special case values for printing. We don't always 209 /* Check for special case values for printing. We don't always
215 need the step value. We don't care about errors 210 need the step value. We don't care about errors
216 (it means overflow), so clear the errors. */ 211 (it means overflow), so clear the errors. */
(...skipping 15 matching lines...) Show 10 above Show 10 below
232 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY}, 227 {"step", T_OBJECT, offsetof(rangeobject, step), READONLY},
233 {0} 228 {0}
234 }; 229 };
235 230
236 static PyObject * range_iter(PyObject *seq); 231 static PyObject * range_iter(PyObject *seq);
237 static PyObject * range_reverse(PyObject *seq); 232 static PyObject * range_reverse(PyObject *seq);
238 233
239 PyDoc_STRVAR(reverse_doc, 234 PyDoc_STRVAR(reverse_doc,
240 "Returns a reverse iterator."); 235 "Returns a reverse iterator.");
241 236
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
242 static PyMethodDef range_methods[] = { 250 static PyMethodDef range_methods[] = {
243 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, 251 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS,
244 reverse_doc}, 252 reverse_doc},
245 {"__length_hint__", (PyCFunction)range_length_hint, METH_NOARGS,
246 range_length_hint_doc},
247 {NULL, NULL} /* sentinel */ 253 {NULL, NULL} /* sentinel */
248 }; 254 };
249 255
250 PyTypeObject PyRange_Type = { 256 PyTypeObject PyRange_Type = {
251 PyVarObject_HEAD_INIT(&PyType_Type, 0) 257 PyVarObject_HEAD_INIT(&PyType_Type, 0)
252 "range", /* Name of this type */ 258 "range", /* Name of this type */
253 sizeof(rangeobject), /* Basic object size */ 259 sizeof(rangeobject), /* Basic object size */
254 0, /* Item size for varobject */ 260 0, /* Item size for varobject */
255 (destructor)range_dealloc, /* tp_dealloc */ 261 (destructor)range_dealloc, /* tp_dealloc */
256 0, /* tp_print */ 262 0, /* tp_print */
257 0, /* tp_getattr */ 263 0, /* tp_getattr */
258 0, /* tp_setattr */ 264 0, /* tp_setattr */
259 0, /* tp_compare */ 265 0, /* tp_compare */
260 (reprfunc)range_repr, /* tp_repr */ 266 (reprfunc)range_repr, /* tp_repr */
261 0, /* tp_as_number */ 267 0, /* tp_as_number */
262 0, /* tp_as_sequence */ 268 &range_as_sequence, /* tp_as_sequence */
263 0, /* tp_as_mapping */ 269 0, /* tp_as_mapping */
264 0, /* tp_hash */ 270 0, /* tp_hash */
265 0, /* tp_call */ 271 0, /* tp_call */
266 0, /* tp_str */ 272 0, /* tp_str */
267 PyObject_GenericGetAttr, /* tp_getattro */ 273 PyObject_GenericGetAttr, /* tp_getattro */
268 0, /* tp_setattro */ 274 0, /* tp_setattro */
269 0, /* tp_as_buffer */ 275 0, /* tp_as_buffer */
270 Py_TPFLAGS_DEFAULT, /* tp_flags */ 276 Py_TPFLAGS_DEFAULT, /* tp_flags */
271 range_doc, /* tp_doc */ 277 range_doc, /* tp_doc */
272 0, /* tp_traverse */ 278 0, /* tp_traverse */
(...skipping 283 matching lines...) Show 10 above Show 10 below
556 562
557 /* Do all initialization here, so we can DECREF on failure. */ 563 /* Do all initialization here, so we can DECREF on failure. */
558 it->start = r->start; 564 it->start = r->start;
559 it->step = r->step; 565 it->step = r->step;
560 Py_INCREF(it->start); 566 Py_INCREF(it->start);
561 Py_INCREF(it->step); 567 Py_INCREF(it->step);
562 568
563 Py_INCREF(r->length); 569 Py_INCREF(r->length);
564 it->length = r->length; 570 it->length = r->length;
565 it->index = PyLong_FromLong(0); 571 it->index = PyLong_FromLong(0);
566 if (!it->index) 572 if (it->index == NULL)
567 goto create_failure; 573 goto create_failure;
568 574
569 return (PyObject *)it; 575 return (PyObject *)it;
570 576
571 create_failure: 577 create_failure:
572 Py_DECREF(it); 578 Py_DECREF(it);
573 return NULL; 579 return NULL;
574 } 580 }
575 581
576 static PyObject * 582 static PyObject *
(...skipping 72 matching lines...) Show 10 above Show 10 below
649 return NULL; 655 return NULL;
650 } 656 }
651 657
652 return (PyObject *)it; 658 return (PyObject *)it;
653 659
654 create_failure: 660 create_failure:
655 Py_XDECREF(len); 661 Py_XDECREF(len);
656 PyObject_Del(it); 662 PyObject_Del(it);
657 return NULL; 663 return NULL;
658 } 664 }
LEFTRIGHT

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