annotate Objects/rangeobject.c @ 31420:be56fc485e27 legacy-trunk

no reason not to use boilerplate.tex now that it names the PSF
author Fred Drake <fdrake@acm.org>
date Fri, 23 Jan 2004 15:23:49 +0000
parents 41c4e143e6e6
children 04eb2ebb46e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
3 #include "Python.h"
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
4
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
6 PyObject_HEAD
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 long len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
18391
aadd99830531 Rip out the fancy behaviors of xrange that nobody uses: repeat, slice,
Guido van Rossum <guido@python.org>
parents: 15890
diff changeset
12 PyObject *
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
13 PyRange_New(long start, long len, long step, int reps)
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
14 {
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
15 rangeobject *obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
17 if (reps != 1) {
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
18 PyErr_SetString(PyExc_ValueError,
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
19 "PyRange_New's 'repetitions' argument must be 1");
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
20 return NULL;
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
21 }
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
22
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
23 obj = PyObject_New(rangeobject, &PyRange_Type);
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
24 if (obj == NULL)
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
25 return NULL;
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
26
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
27 if (len == 0) {
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
28 start = 0;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
29 len = 0;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
30 step = 1;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
31 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
32 else {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
33 long last = start + (len - 1) * step;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
34 if ((step > 0) ?
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
35 (last > (PyInt_GetMax() - step)) :
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
36 (last < (-1 - PyInt_GetMax() - step))) {
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
37 PyErr_SetString(PyExc_OverflowError,
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
38 "integer addition");
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
39 return NULL;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
40 }
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
41 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 obj->step = step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
46 return (PyObject *) obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
49 /* Return number of items in range/xrange (lo, hi, step). step > 0
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
50 * required. Return a value < 0 if & only if the true value is too
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
51 * large to fit in a signed long.
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
52 */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
53 static long
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
54 get_len_of_range(long lo, long hi, long step)
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
55 {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
56 /* -------------------------------------------------------------
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
57 If lo >= hi, the range is empty.
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
58 Else if n values are in the range, the last one is
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
59 lo + (n-1)*step, which must be <= hi-1. Rearranging,
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
60 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
61 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
62 the RHS is non-negative and so truncation is the same as the
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
63 floor. Letting M be the largest positive long, the worst case
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
64 for the RHS numerator is hi=M, lo=-M-1, and then
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
65 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
66 precision to compute the RHS exactly.
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
67 ---------------------------------------------------------------*/
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
68 long n = 0;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
69 if (lo < hi) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
70 unsigned long uhi = (unsigned long)hi;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
71 unsigned long ulo = (unsigned long)lo;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
72 unsigned long diff = uhi - ulo - 1;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
73 n = (long)(diff / (unsigned long)step + 1);
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
74 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
75 return n;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
76 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
77
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
78 static PyObject *
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
79 range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
80 {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
81 long ilow = 0, ihigh = 0, istep = 1;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
82 long n;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
83
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
84 if (PyTuple_Size(args) <= 1) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
85 if (!PyArg_ParseTuple(args,
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
86 "l;xrange() requires 1-3 int arguments",
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
87 &ihigh))
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
88 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
89 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
90 else {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
91 if (!PyArg_ParseTuple(args,
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
92 "ll|l;xrange() requires 1-3 int arguments",
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
93 &ilow, &ihigh, &istep))
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
94 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
95 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
96 if (istep == 0) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
97 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
98 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
99 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
100 if (istep > 0)
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
101 n = get_len_of_range(ilow, ihigh, istep);
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
102 else
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
103 n = get_len_of_range(ihigh, ilow, -istep);
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
104 if (n < 0) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
105 PyErr_SetString(PyExc_OverflowError,
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
106 "xrange() result has too many items");
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
107 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
108 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
109 return PyRange_New(ilow, n, istep, 1);
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
110 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
111
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23702
diff changeset
112 PyDoc_STRVAR(range_doc,
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
113 "xrange([start,] stop[, step]) -> xrange object\n\
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
114 \n\
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
115 Like range(), but instead of returning a list, returns an object that\n\
26592
0c646baeec58 Update comments about the performance of xrange().
Raymond Hettinger <python@rcn.com>
parents: 26097
diff changeset
116 generates the numbers in the range on demand. For looping, this is \n\
0c646baeec58 Update comments about the performance of xrange().
Raymond Hettinger <python@rcn.com>
parents: 26097
diff changeset
117 slightly faster than range() and more memory efficient.");
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
118
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
119 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
120 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 {
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
122 if (i < 0 || i >= r->len) {
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
123 PyErr_SetString(PyExc_IndexError,
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
124 "xrange object index out of range");
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
125 return NULL;
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
126 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
127 return PyInt_FromLong(r->start + (i % r->len) * r->step);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
131 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
25208
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
133 #if LONG_MAX != INT_MAX
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
134 if (r->len > INT_MAX) {
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
135 PyErr_SetString(PyExc_ValueError,
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
136 "xrange object size cannot be reported");
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
137 return -1;
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
138 }
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
139 #endif
2490cf7e68a4 Untested code for 64-bit platforms. range_length() is declared as int
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
140 return (int)(r->len);
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
141 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
142
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
143 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
144 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
146 PyObject *rtn;
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
147
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
148 if (r->start == 0 && r->step == 1)
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
149 rtn = PyString_FromFormat("xrange(%ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
150 r->start + r->len * r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
151
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
152 else if (r->step == 1)
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
153 rtn = PyString_FromFormat("xrange(%ld, %ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
154 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
155 r->start + r->len * r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
156
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
157 else
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
158 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
159 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
160 r->start + r->len * r->step,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
161 r->step);
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
162 return rtn;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
163 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
164
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
165 static PySequenceMethods range_as_sequence = {
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
166 (inquiry)range_length, /* sq_length */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
167 0, /* sq_concat */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
168 0, /* sq_repeat */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
169 (intargfunc)range_item, /* sq_item */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
170 0, /* sq_slice */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
173 static PyObject * range_iter(PyObject *seq);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
174 static PyObject * range_reverse(PyObject *seq);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
175
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
176 PyDoc_STRVAR(reverse_doc,
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
177 "Returns a reverse iterator.");
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
178
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
179 static PyMethodDef range_methods[] = {
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
180 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
181 {NULL, NULL} /* sentinel */
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
182 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
183
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
184 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
185 PyObject_HEAD_INIT(&PyType_Type)
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
186 0, /* Number of items for varobject */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
187 "xrange", /* Name of this type */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
188 sizeof(rangeobject), /* Basic object size */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
189 0, /* Item size for varobject */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
190 (destructor)PyObject_Del, /* tp_dealloc */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
191 0, /* tp_print */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
192 0, /* tp_getattr */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
193 0, /* tp_setattr */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
194 0, /* tp_compare */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
195 (reprfunc)range_repr, /* tp_repr */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
196 0, /* tp_as_number */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
197 &range_as_sequence, /* tp_as_sequence */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
198 0, /* tp_as_mapping */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
199 0, /* tp_hash */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
200 0, /* tp_call */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
201 0, /* tp_str */
26097
e526365ddccd Restore attribute access so that the following work again:
Raymond Hettinger <python@rcn.com>
parents: 25208
diff changeset
202 PyObject_GenericGetAttr, /* tp_getattro */
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
203 0, /* tp_setattro */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
204 0, /* tp_as_buffer */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
205 Py_TPFLAGS_DEFAULT, /* tp_flags */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
206 range_doc, /* tp_doc */
23398
956d038b1dd2 Patch #551410: Implement tp_getiter.
Martin v. Löwis <martin@v.loewis.de>
parents: 23361
diff changeset
207 0, /* tp_traverse */
956d038b1dd2 Patch #551410: Implement tp_getiter.
Martin v. Löwis <martin@v.loewis.de>
parents: 23361
diff changeset
208 0, /* tp_clear */
956d038b1dd2 Patch #551410: Implement tp_getiter.
Martin v. Löwis <martin@v.loewis.de>
parents: 23361
diff changeset
209 0, /* tp_richcompare */
956d038b1dd2 Patch #551410: Implement tp_getiter.
Martin v. Löwis <martin@v.loewis.de>
parents: 23361
diff changeset
210 0, /* tp_weaklistoffset */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
211 (getiterfunc)range_iter, /* tp_iter */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
212 0, /* tp_iternext */
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
213 range_methods, /* tp_methods */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
214 0, /* tp_members */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
215 0, /* tp_getset */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
216 0, /* tp_base */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
217 0, /* tp_dict */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
218 0, /* tp_descr_get */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
219 0, /* tp_descr_set */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
220 0, /* tp_dictoffset */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
221 0, /* tp_init */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
222 0, /* tp_alloc */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
223 range_new, /* tp_new */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
225
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
226 /*********************** Xrange Iterator **************************/
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
227
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
228 typedef struct {
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
229 PyObject_HEAD
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
230 long index;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
231 long start;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
232 long step;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
233 long len;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
234 } rangeiterobject;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
235
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
236 static PyTypeObject Pyrangeiter_Type;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
237
23702
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
238 static PyObject *
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
239 range_iter(PyObject *seq)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
240 {
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
241 rangeiterobject *it;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
242
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
243 if (!PyRange_Check(seq)) {
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
244 PyErr_BadInternalCall();
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
245 return NULL;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
246 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
247 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
248 if (it == NULL)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
249 return NULL;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
250 it->index = 0;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
251 it->start = ((rangeobject *)seq)->start;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
252 it->step = ((rangeobject *)seq)->step;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
253 it->len = ((rangeobject *)seq)->len;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
254 return (PyObject *)it;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
255 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
256
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
257 static PyObject *
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
258 range_reverse(PyObject *seq)
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
259 {
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
260 rangeiterobject *it;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
261 long start, step, len;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
262
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
263 if (!PyRange_Check(seq)) {
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
264 PyErr_BadInternalCall();
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
265 return NULL;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
266 }
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
267 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
268 if (it == NULL)
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
269 return NULL;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
270
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
271 start = ((rangeobject *)seq)->start;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
272 step = ((rangeobject *)seq)->step;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
273 len = ((rangeobject *)seq)->len;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
274
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
275 it->index = 0;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
276 it->start = start + (len-1) * step;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
277 it->step = -step;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
278 it->len = len;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
279
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
280 return (PyObject *)it;
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
281 }
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
282
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
283 static PyObject *
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
284 rangeiter_next(rangeiterobject *r)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
285 {
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
286 if (r->index < r->len)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
287 return PyInt_FromLong(r->start + (r->index++) * r->step);
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
288 return NULL;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
289 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
290
23702
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
291 static PyTypeObject Pyrangeiter_Type = {
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
292 PyObject_HEAD_INIT(&PyType_Type)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
293 0, /* ob_size */
23702
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
294 "rangeiterator", /* tp_name */
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
295 sizeof(rangeiterobject), /* tp_basicsize */
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
296 0, /* tp_itemsize */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
297 /* methods */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
298 (destructor)PyObject_Del, /* tp_dealloc */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
299 0, /* tp_print */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
300 0, /* tp_getattr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
301 0, /* tp_setattr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
302 0, /* tp_compare */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
303 0, /* tp_repr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
304 0, /* tp_as_number */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
305 0, /* tp_as_sequence */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
306 0, /* tp_as_mapping */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
307 0, /* tp_hash */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
308 0, /* tp_call */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
309 0, /* tp_str */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
310 PyObject_GenericGetAttr, /* tp_getattro */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
311 0, /* tp_setattro */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
312 0, /* tp_as_buffer */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
313 Py_TPFLAGS_DEFAULT, /* tp_flags */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
314 0, /* tp_doc */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
315 0, /* tp_traverse */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
316 0, /* tp_clear */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
317 0, /* tp_richcompare */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
318 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
319 PyObject_SelfIter, /* tp_iter */
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
320 (iternextfunc)rangeiter_next, /* tp_iternext */
24260
6aa2d09e32c1 Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
321 0, /* tp_methods */
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
322 };