Mercurial > cpython-lru_cache-weakref
annotate Objects/rangeobject.c @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 3b484f53f91b |
| children |
| 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 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
6 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
7 long start; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
8 long step; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
9 long len; |
|
1369
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 |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
12 /* Return number of items in range (lo, hi, step). step != 0 |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
13 * required. The result always fits in an unsigned long. |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
14 */ |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
15 static unsigned long |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
16 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
|
17 { |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
18 /* ------------------------------------------------------------- |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
19 If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty. |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
20 Else for step > 0, if n values are in the range, the last one is |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
21 lo + (n-1)*step, which must be <= hi-1. Rearranging, |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
22 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
23 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
24 the RHS is non-negative and so truncation is the same as the |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
25 floor. Letting M be the largest positive long, the worst case |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
26 for the RHS numerator is hi=M, lo=-M-1, and then |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
27 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
28 precision to compute the RHS exactly. The analysis for step < 0 |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
29 is similar. |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
30 ---------------------------------------------------------------*/ |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
31 assert(step != 0); |
|
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
32 if (step > 0 && lo < hi) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
33 return 1UL + (hi - 1UL - lo) / step; |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
34 else if (step < 0 && lo > hi) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
35 return 1UL + (lo - 1UL - hi) / (0UL - step); |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
36 else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
37 return 0UL; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
38 } |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
39 |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
40 /* Return a stop value suitable for reconstructing the xrange from |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
41 * a (start, stop, step) triple. Used in range_repr and range_reduce. |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
42 * Computes start + len * step, clipped to the range [LONG_MIN, LONG_MAX]. |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
43 */ |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
44 static long |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
45 get_stop_for_range(rangeobject *r) |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
46 { |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
47 long last; |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
48 |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
49 if (r->len == 0) |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
50 return r->start; |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
51 |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
52 /* The tricky bit is avoiding overflow. We first compute the last entry in |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
53 the xrange, start + (len - 1) * step, which is guaranteed to lie within |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
54 the range of a long, and then add step to it. See the range_reverse |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
55 comments for an explanation of the casts below. |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
56 */ |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
57 last = (long)(r->start + (unsigned long)(r->len - 1) * r->step); |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
58 if (r->step > 0) |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
59 return last > LONG_MAX - r->step ? LONG_MAX : last + r->step; |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
60 else |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
61 return last < LONG_MIN - r->step ? LONG_MIN : last + r->step; |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
62 } |
|
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
63 |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
64 static PyObject * |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
65 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
|
66 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
67 rangeobject *obj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
68 long ilow = 0, ihigh = 0, istep = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
69 unsigned long n; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
70 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
71 if (!_PyArg_NoKeywords("xrange()", kw)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
72 return NULL; |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34087
diff
changeset
|
73 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
74 if (PyTuple_Size(args) <= 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
75 if (!PyArg_ParseTuple(args, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
76 "l;xrange() requires 1-3 int arguments", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
77 &ihigh)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
78 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
79 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
80 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
81 if (!PyArg_ParseTuple(args, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
82 "ll|l;xrange() requires 1-3 int arguments", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
83 &ilow, &ihigh, &istep)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
84 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
85 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
86 if (istep == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
87 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
88 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
89 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
90 n = get_len_of_range(ilow, ihigh, istep); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
91 if (n > (unsigned long)LONG_MAX || (long)n > PY_SSIZE_T_MAX) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
92 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
93 "xrange() result has too many items"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
94 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
95 } |
|
33047
c81ed079576f
Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents:
33046
diff
changeset
|
96 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
97 obj = PyObject_New(rangeobject, &PyRange_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
98 if (obj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
99 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
100 obj->start = ilow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
101 obj->len = (long)n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
102 obj->step = istep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
103 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
|
104 } |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
105 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23702
diff
changeset
|
106 PyDoc_STRVAR(range_doc, |
|
79590
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
79227
diff
changeset
|
107 "xrange(stop) -> xrange object\n\ |
|
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
79227
diff
changeset
|
108 xrange(start, stop[, step]) -> xrange object\n\ |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
109 \n\ |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
114 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
115 range_item(rangeobject *r, Py_ssize_t i) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
117 if (i < 0 || i >= r->len) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
118 PyErr_SetString(PyExc_IndexError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
119 "xrange object index out of range"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
120 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
121 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
122 /* do calculation entirely using unsigned longs, to avoid |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
123 undefined behaviour due to signed overflow. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
124 return PyInt_FromLong((long)(r->start + (unsigned long)i * r->step)); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
127 static Py_ssize_t |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
128 range_length(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
130 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
|
131 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
132 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
133 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
134 range_repr(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
136 PyObject *rtn; |
|
33046
67d25217bc7f
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
32897
diff
changeset
|
137 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
138 if (r->start == 0 && r->step == 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
139 rtn = PyString_FromFormat("xrange(%ld)", |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
140 get_stop_for_range(r)); |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
141 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
142 else if (r->step == 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
143 rtn = PyString_FromFormat("xrange(%ld, %ld)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
144 r->start, |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
145 get_stop_for_range(r)); |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
146 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
147 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
148 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
149 r->start, |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
150 get_stop_for_range(r), |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
151 r->step); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
152 return rtn; |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
153 } |
|
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
154 |
|
47534
df9b337ba311
Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47527
diff
changeset
|
155 /* Pickling support */ |
|
df9b337ba311
Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47527
diff
changeset
|
156 static PyObject * |
|
47535
3939ff89834c
Added better pickling support to xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47534
diff
changeset
|
157 range_reduce(rangeobject *r, PyObject *args) |
|
47534
df9b337ba311
Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47527
diff
changeset
|
158 { |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
159 return Py_BuildValue("(O(lll))", Py_TYPE(r), |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
160 r->start, |
|
79227
bff269ee7288
Issues #16029, #16030: Fix pickling and repr of large xranges.
Mark Dickinson <mdickinson@enthought.com>
parents:
61463
diff
changeset
|
161 get_stop_for_range(r), |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
162 r->step); |
|
47534
df9b337ba311
Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47527
diff
changeset
|
163 } |
|
df9b337ba311
Issue 2582: Fix pickling of xrange objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47527
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 = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
166 (lenfunc)range_length, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
167 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
168 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
169 (ssizeargfunc)range_item, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
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[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
180 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
181 {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
182 {NULL, NULL} /* sentinel */ |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
183 }; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
184 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
185 PyTypeObject PyRange_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
186 PyObject_HEAD_INIT(&PyType_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
187 0, /* Number of items for varobject */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
188 "xrange", /* Name of this type */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
189 sizeof(rangeobject), /* Basic object size */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
190 0, /* Item size for varobject */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
191 (destructor)PyObject_Del, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
192 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
193 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
194 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
195 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
196 (reprfunc)range_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
197 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
198 &range_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
199 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
200 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
201 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
202 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
203 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
204 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
205 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
206 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
207 range_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
208 0, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
209 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
210 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
211 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
212 range_iter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
213 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
214 range_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
215 0, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
216 0, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
217 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
218 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
219 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
220 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
221 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
222 0, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
223 0, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
224 range_new, /* tp_new */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 }; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
226 |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
227 /*********************** Xrange Iterator **************************/ |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
228 |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
229 typedef struct { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
230 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
231 long index; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
232 long start; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
233 long step; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
234 long len; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
235 } rangeiterobject; |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
236 |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
237 static PyObject * |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
238 rangeiter_next(rangeiterobject *r) |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
239 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
240 if (r->index < r->len) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
241 return PyInt_FromLong(r->start + (r->index++) * r->step); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
242 return NULL; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
243 } |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
244 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
245 static PyObject * |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
246 rangeiter_len(rangeiterobject *r) |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
247 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
248 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
|
249 } |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
250 |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
35636
diff
changeset
|
251 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
|
252 |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
253 static PyMethodDef rangeiter_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
254 {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, length_hint_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
255 {NULL, NULL} /* sentinel */ |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
256 }; |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
257 |
|
23702
d4f6e2adde48
Pyrangeiter_Type && range_iter should be static
Neal Norwitz <nnorwitz@gmail.com>
parents:
23695
diff
changeset
|
258 static PyTypeObject Pyrangeiter_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
259 PyObject_HEAD_INIT(&PyType_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
260 0, /* ob_size */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
261 "rangeiterator", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
262 sizeof(rangeiterobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
263 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
264 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
265 (destructor)PyObject_Del, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
266 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
267 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
268 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
269 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
270 0, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
271 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
272 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
273 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
274 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
275 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
276 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
277 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
278 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
279 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
280 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
281 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
282 0, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
283 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
284 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
285 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
286 PyObject_SelfIter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
287 (iternextfunc)rangeiter_next, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
288 rangeiter_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
289 0, |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
290 }; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
291 |
|
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
292 static PyObject * |
|
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
293 range_iter(PyObject *seq) |
|
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
294 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
295 rangeiterobject *it; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
296 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
297 if (!PyRange_Check(seq)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
298 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
299 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
300 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
301 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
302 if (it == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
303 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
304 it->index = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
305 it->start = ((rangeobject *)seq)->start; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
306 it->step = ((rangeobject *)seq)->step; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
307 it->len = ((rangeobject *)seq)->len; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
308 return (PyObject *)it; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
309 } |
|
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 static PyObject * |
|
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
312 range_reverse(PyObject *seq) |
|
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
313 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
314 rangeiterobject *it; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
315 long start, step, len; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
316 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
317 if (!PyRange_Check(seq)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
318 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
319 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
320 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
321 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
322 if (it == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
323 return NULL; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
324 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
325 start = ((rangeobject *)seq)->start; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
326 step = ((rangeobject *)seq)->step; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
327 len = ((rangeobject *)seq)->len; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
328 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
329 it->index = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
330 it->len = len; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
331 /* the casts below guard against signed overflow by turning it |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
332 into unsigned overflow instead. The correctness of this |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
333 code still depends on conversion from unsigned long to long |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
334 wrapping modulo ULONG_MAX+1, which isn't guaranteed (see |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
335 C99 6.3.1.3p3) but seems to hold in practice for all |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
336 platforms we're likely to meet. |
|
57265
c217e0331d4b
Merged revisions 76295 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47535
diff
changeset
|
337 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
338 If step == LONG_MIN then we still end up with LONG_MIN |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
339 after negation; but this works out, since we've still got |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
340 the correct value modulo ULONG_MAX+1, and the range_item |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
341 calculation is also done modulo ULONG_MAX+1. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
342 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
343 it->start = (long)(start + (unsigned long)(len-1) * step); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
344 it->step = (long)(0UL-step); |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
345 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57298
diff
changeset
|
346 return (PyObject *)it; |
|
37602
ca95ad5b0fbc
Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents:
37476
diff
changeset
|
347 } |
