annotate Objects/rangeobject.c @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents 3939ff89834c
children c217e0331d4b
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
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
12 /* 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
13 * 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
14 * 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
15 */
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
16 static long
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
17 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
18 {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
19 /* -------------------------------------------------------------
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 ---------------------------------------------------------------*/
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
31 long n = 0;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
32 if (lo < hi) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
33 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
34 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
35 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
36 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
37 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
38 return n;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
39 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
40
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
41 static PyObject *
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
42 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
43 {
33047
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
44 rangeobject *obj;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
45 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
46 long n;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
47
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
48 if (!_PyArg_NoKeywords("xrange()", kw))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
49 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
50
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
51 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
52 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
53 "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
54 &ihigh))
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
55 return NULL;
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 else {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
58 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
59 "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
60 &ilow, &ihigh, &istep))
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
61 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
62 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
63 if (istep == 0) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
64 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
65 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
66 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
67 if (istep > 0)
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
68 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
69 else
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
70 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
71 if (n < 0) {
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
72 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
73 "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
74 return NULL;
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
75 }
33047
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
76
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
77 obj = PyObject_New(rangeobject, &PyRange_Type);
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
78 if (obj == NULL)
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
79 return NULL;
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
80 obj->start = ilow;
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
81 obj->len = n;
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
82 obj->step = istep;
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
83 return (PyObject *) obj;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
84 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
85
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23702
diff changeset
86 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
87 "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
88 \n\
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
89 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
90 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
91 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
92
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
93 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
94 range_item(rangeobject *r, Py_ssize_t i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 {
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
96 if (i < 0 || i >= r->len) {
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
97 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
98 "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
99 return NULL;
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
100 }
45236
1980d7058627 Remove unnecessary modulo division.
Raymond Hettinger <python@rcn.com>
parents: 37602
diff changeset
101 return PyInt_FromSsize_t(r->start + i * r->step);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
104 static Py_ssize_t
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
105 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
107 return (Py_ssize_t)(r->len);
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
108 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
109
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
110 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
111 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
113 PyObject *rtn;
33046
67d25217bc7f Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32897
diff changeset
114
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
115 if (r->start == 0 && r->step == 1)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
116 rtn = PyString_FromFormat("xrange(%ld)",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
117 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
118
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
119 else if (r->step == 1)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
120 rtn = PyString_FromFormat("xrange(%ld, %ld)",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
121 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
122 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
123
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
124 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
125 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
126 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
127 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
128 r->step);
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
129 return rtn;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
130 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
131
47534
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
132 /* Pickling support */
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
133 static PyObject *
47535
3939ff89834c Added better pickling support to xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47534
diff changeset
134 range_reduce(rangeobject *r, PyObject *args)
47534
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
135 {
47535
3939ff89834c Added better pickling support to xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47534
diff changeset
136 return Py_BuildValue("(O(iii))", Py_TYPE(r),
47534
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
137 r->start,
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
138 r->start + r->len * r->step,
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
139 r->step);
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
140 }
df9b337ba311 Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
141
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
142 static PySequenceMethods range_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
143 (lenfunc)range_length, /* sq_length */
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
144 0, /* sq_concat */
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
145 0, /* sq_repeat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
146 (ssizeargfunc)range_item, /* sq_item */
23361
aee8f16f2774 Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents: 23348
diff changeset
147 0, /* sq_slice */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
150 static PyObject * range_iter(PyObject *seq);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
151 static PyObject * range_reverse(PyObject *seq);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
152
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
153 PyDoc_STRVAR(reverse_doc,
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
154 "Returns a reverse iterator.");
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
155
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
156 static PyMethodDef range_methods[] = {
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
157 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
47535
3939ff89834c Added better pickling support to xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47534
diff changeset
158 {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
159 {NULL, NULL} /* sentinel */
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
160 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
161
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
162 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
163 PyObject_HEAD_INIT(&PyType_Type)
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
164 0, /* Number of items for varobject */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
165 "xrange", /* Name of this type */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
166 sizeof(rangeobject), /* Basic object size */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
167 0, /* Item size for varobject */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
168 (destructor)PyObject_Del, /* tp_dealloc */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
169 0, /* tp_print */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
170 0, /* tp_getattr */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
171 0, /* tp_setattr */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
172 0, /* tp_compare */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
173 (reprfunc)range_repr, /* tp_repr */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
174 0, /* tp_as_number */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
175 &range_as_sequence, /* tp_as_sequence */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
176 0, /* tp_as_mapping */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
177 0, /* tp_hash */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
178 0, /* tp_call */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
179 0, /* tp_str */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
180 PyObject_GenericGetAttr, /* tp_getattro */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
181 0, /* tp_setattro */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
182 0, /* tp_as_buffer */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
183 Py_TPFLAGS_DEFAULT, /* tp_flags */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
184 range_doc, /* tp_doc */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
185 0, /* tp_traverse */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
186 0, /* tp_clear */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
187 0, /* tp_richcompare */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
188 0, /* tp_weaklistoffset */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
189 range_iter, /* tp_iter */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
190 0, /* tp_iternext */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
191 range_methods, /* tp_methods */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
192 0, /* tp_members */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
193 0, /* tp_getset */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
194 0, /* tp_base */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
195 0, /* tp_dict */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
196 0, /* tp_descr_get */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
197 0, /* tp_descr_set */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
198 0, /* tp_dictoffset */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
199 0, /* tp_init */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
200 0, /* tp_alloc */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36501
diff changeset
201 range_new, /* tp_new */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
203
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
204 /*********************** Xrange Iterator **************************/
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
205
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
206 typedef struct {
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
207 PyObject_HEAD
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
208 long index;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
209 long start;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
210 long step;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
211 long len;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
212 } rangeiterobject;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
213
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
214 static PyObject *
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
215 rangeiter_next(rangeiterobject *r)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
216 {
33046
67d25217bc7f Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32897
diff changeset
217 if (r->index < r->len)
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
218 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
219 return NULL;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
220 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
221
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
222 static PyObject *
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
223 rangeiter_len(rangeiterobject *r)
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
224 {
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
225 return PyInt_FromLong(r->len - r->index);
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
226 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
227
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
228 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
229
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
230 static PyMethodDef rangeiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
231 {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
232 {NULL, NULL} /* sentinel */
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
233 };
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
234
23702
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
235 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
236 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
237 0, /* ob_size */
23702
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
238 "rangeiterator", /* tp_name */
d4f6e2adde48 Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents: 23695
diff changeset
239 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
240 0, /* tp_itemsize */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
241 /* methods */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
242 (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
243 0, /* tp_print */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
244 0, /* tp_getattr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
245 0, /* tp_setattr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
246 0, /* tp_compare */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
247 0, /* tp_repr */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
248 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
249 0, /* tp_as_sequence */
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
250 0, /* tp_as_mapping */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
251 0, /* tp_hash */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
252 0, /* tp_call */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
253 0, /* tp_str */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
254 PyObject_GenericGetAttr, /* tp_getattro */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
255 0, /* tp_setattro */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
256 0, /* tp_as_buffer */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
257 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
258 0, /* tp_doc */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
259 0, /* tp_traverse */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
260 0, /* tp_clear */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
261 0, /* tp_richcompare */
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
262 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
263 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
264 (iternextfunc)rangeiter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
265 rangeiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
266 0,
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
267 };
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
268
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
269 static PyObject *
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
270 range_iter(PyObject *seq)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
271 {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
272 rangeiterobject *it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
273
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
274 if (!PyRange_Check(seq)) {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
275 PyErr_BadInternalCall();
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
276 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
277 }
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
278 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
279 if (it == NULL)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
280 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
281 it->index = 0;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
282 it->start = ((rangeobject *)seq)->start;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
283 it->step = ((rangeobject *)seq)->step;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
284 it->len = ((rangeobject *)seq)->len;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
285 return (PyObject *)it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
286 }
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
287
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
288 static PyObject *
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
289 range_reverse(PyObject *seq)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
290 {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
291 rangeiterobject *it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
292 long start, step, len;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
293
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
294 if (!PyRange_Check(seq)) {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
295 PyErr_BadInternalCall();
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
296 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
297 }
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
298 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
299 if (it == NULL)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
300 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
301
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
302 start = ((rangeobject *)seq)->start;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
303 step = ((rangeobject *)seq)->step;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
304 len = ((rangeobject *)seq)->len;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
305
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
306 it->index = 0;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
307 it->start = start + (len-1) * step;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
308 it->step = -step;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
309 it->len = len;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
310
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
311 return (PyObject *)it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37476
diff changeset
312 }